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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/stdio/fprintf.h"
10 
11 #include "src/__support/arg_list.h"
12 #include "src/stdio/ferror.h"
13 #include "src/stdio/printf_core/file_writer.h"
14 #include "src/stdio/printf_core/printf_main.h"
15 #include "src/stdio/printf_core/writer.h"
16 
17 #include <stdarg.h>
18 #include <stdio.h>
19 
20 namespace __llvm_libc {
21 
22 LLVM_LIBC_FUNCTION(int, fprintf,
23                    (::FILE *__restrict stream, const char *__restrict format,
24                     ...)) {
25   va_list vlist;
26   va_start(vlist, format);
27   internal::ArgList args(vlist); // This holder class allows for easier copying
28                                  // and pointer semantics, as well as handling
29                                  // destruction automatically.
30   va_end(vlist);
31   printf_core::Writer writer(reinterpret_cast<void *>(stream),
32                              printf_core::write_to_file);
33 
34   int ret_val = printf_core::printf_main(&writer, format, args);
35   if (__llvm_libc::ferror(stream))
36     return -1;
37   return ret_val;
38 }
39 
40 } // namespace __llvm_libc
41