48 lines · cpp
1//===-- Implementation of fprintf -------------------------------*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/stdio/fprintf.h"10 11#include "src/__support/CPP/limits.h"12#include "src/__support/File/file.h"13#include "src/__support/arg_list.h"14#include "src/__support/macros/config.h"15#include "src/stdio/printf_core/core_structs.h"16#include "src/stdio/printf_core/error_mapper.h"17#include "src/stdio/printf_core/vfprintf_internal.h"18 19#include "hdr/types/FILE.h"20#include <stdarg.h>21 22namespace LIBC_NAMESPACE_DECL {23 24LLVM_LIBC_FUNCTION(int, fprintf,25 (::FILE *__restrict stream, const char *__restrict format,26 ...)) {27 va_list vlist;28 va_start(vlist, format);29 internal::ArgList args(vlist); // This holder class allows for easier copying30 // and pointer semantics, as well as handling31 // destruction automatically.32 va_end(vlist);33 auto ret_val = printf_core::vfprintf_internal(stream, format, args);34 if (!ret_val.has_value()) {35 libc_errno = printf_core::internal_error_to_errno(ret_val.error());36 return -1;37 }38 if (ret_val.value() > static_cast<size_t>(cpp::numeric_limits<int>::max())) {39 libc_errno =40 printf_core::internal_error_to_errno(-printf_core::OVERFLOW_ERROR);41 return -1;42 }43 44 return static_cast<int>(ret_val.value());45}46 47} // namespace LIBC_NAMESPACE_DECL48