109 lines · c
1//===-- Internal implementation header of vfprintf --------------*- 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#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_VFPRINTF_INTERNAL_H10#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_VFPRINTF_INTERNAL_H11 12#include "src/__support/File/file.h"13#include "src/__support/arg_list.h"14#include "src/__support/error_or.h"15#include "src/__support/macros/attributes.h" // For LIBC_INLINE16#include "src/__support/macros/config.h"17#include "src/stdio/printf_core/core_structs.h"18#include "src/stdio/printf_core/printf_main.h"19#include "src/stdio/printf_core/writer.h"20 21#include "hdr/types/FILE.h"22 23namespace LIBC_NAMESPACE_DECL {24 25namespace internal {26#ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE27LIBC_INLINE int ferror_unlocked(FILE *f) {28 return reinterpret_cast<LIBC_NAMESPACE::File *>(f)->error_unlocked();29}30 31LIBC_INLINE void flockfile(FILE *f) {32 reinterpret_cast<LIBC_NAMESPACE::File *>(f)->lock();33}34 35LIBC_INLINE void funlockfile(FILE *f) {36 reinterpret_cast<LIBC_NAMESPACE::File *>(f)->unlock();37}38 39LIBC_INLINE FileIOResult fwrite_unlocked(const void *ptr, size_t size,40 size_t nmemb, FILE *f) {41 return reinterpret_cast<LIBC_NAMESPACE::File *>(f)->write_unlocked(42 ptr, size * nmemb);43}44#else // defined(LIBC_COPT_STDIO_USE_SYSTEM_FILE)45LIBC_INLINE int ferror_unlocked(::FILE *f) { return ::ferror_unlocked(f); }46 47LIBC_INLINE void flockfile(::FILE *f) { ::flockfile(f); }48 49LIBC_INLINE void funlockfile(::FILE *f) { ::funlockfile(f); }50 51LIBC_INLINE FileIOResult fwrite_unlocked(const void *ptr, size_t size,52 size_t nmemb, ::FILE *f) {53 // Need to use system errno in this case, as system write will set this errno54 // which we need to propagate back into our code. fwrite only modifies errno55 // if there was an error, and errno may have previously been nonzero. Only56 // return errno if there was an error.57 size_t members_written = ::fwrite_unlocked(ptr, size, nmemb, f);58 return {members_written, members_written == nmemb ? 0 : errno};59}60#endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE61} // namespace internal62 63namespace printf_core {64 65LIBC_INLINE int file_write_hook(cpp::string_view new_str, void *fp) {66 ::FILE *target_file = reinterpret_cast<::FILE *>(fp);67 // Write new_str to the target file. The logic preventing a zero-length write68 // is in the writer, so we don't check here.69 auto write_result = internal::fwrite_unlocked(new_str.data(), sizeof(char),70 new_str.size(), target_file);71 // Propagate actual system error in FileIOResult.72 if (write_result.has_error())73 return -write_result.error;74 75 // In case short write occured or error was not set on FileIOResult for some76 // reason.77 if (write_result.value != new_str.size() ||78 internal::ferror_unlocked(target_file))79 return FILE_WRITE_ERROR;80 81 return WRITE_OK;82}83 84LIBC_INLINE ErrorOr<size_t> vfprintf_internal(::FILE *__restrict stream,85 const char *__restrict format,86 internal::ArgList &args) {87 constexpr size_t BUFF_SIZE = 1024;88 char buffer[BUFF_SIZE];89 printf_core::WriteBuffer<Mode<WriteMode::FLUSH_TO_STREAM>::value> wb(90 buffer, BUFF_SIZE, &file_write_hook, reinterpret_cast<void *>(stream));91 Writer writer(wb);92 internal::flockfile(stream);93 auto retval = printf_main(&writer, format, args);94 if (!retval.has_value()) {95 internal::funlockfile(stream);96 return retval;97 }98 int flushval = wb.overflow_write("");99 if (flushval != WRITE_OK)100 retval = Error(-flushval);101 internal::funlockfile(stream);102 return retval;103}104 105} // namespace printf_core106} // namespace LIBC_NAMESPACE_DECL107 108#endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_VFPRINTF_INTERNAL_H109