xref: /llvm-project/libc/src/stdio/vscanf.cpp (revision b8f134faba3a41f47d2d05125118ea1acf512cb3)
1*b8f134faSJoseph Huber //===-- Implementation of vscanf --------------------------------*- C++ -*-===//
2*b8f134faSJoseph Huber //
3*b8f134faSJoseph Huber // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*b8f134faSJoseph Huber // See https://llvm.org/LICENSE.txt for license information.
5*b8f134faSJoseph Huber // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*b8f134faSJoseph Huber //
7*b8f134faSJoseph Huber //===----------------------------------------------------------------------===//
8*b8f134faSJoseph Huber 
9*b8f134faSJoseph Huber #include "src/stdio/vscanf.h"
10*b8f134faSJoseph Huber 
11*b8f134faSJoseph Huber #include "src/__support/File/file.h"
12*b8f134faSJoseph Huber #include "src/__support/arg_list.h"
13*b8f134faSJoseph Huber #include "src/__support/macros/config.h"
14*b8f134faSJoseph Huber #include "src/stdio/scanf_core/vfscanf_internal.h"
15*b8f134faSJoseph Huber 
16*b8f134faSJoseph Huber #include "hdr/types/FILE.h"
17*b8f134faSJoseph Huber #include <stdarg.h>
18*b8f134faSJoseph Huber 
19*b8f134faSJoseph Huber #ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
20*b8f134faSJoseph Huber #define SCANF_STDIN LIBC_NAMESPACE::stdin
21*b8f134faSJoseph Huber #else // LIBC_COPT_STDIO_USE_SYSTEM_FILE
22*b8f134faSJoseph Huber #define SCANF_STDIN ::stdin
23*b8f134faSJoseph Huber #endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
24*b8f134faSJoseph Huber 
25*b8f134faSJoseph Huber namespace LIBC_NAMESPACE_DECL {
26*b8f134faSJoseph Huber 
27*b8f134faSJoseph Huber LLVM_LIBC_FUNCTION(int, vscanf,
28*b8f134faSJoseph Huber                    (const char *__restrict format, va_list vlist)) {
29*b8f134faSJoseph Huber   internal::ArgList args(vlist); // This holder class allows for easier copying
30*b8f134faSJoseph Huber                                  // and pointer semantics, as well as handling
31*b8f134faSJoseph Huber                                  // destruction automatically.
32*b8f134faSJoseph Huber   va_end(vlist);
33*b8f134faSJoseph Huber   int ret_val = scanf_core::vfscanf_internal(
34*b8f134faSJoseph Huber       reinterpret_cast<::FILE *>(SCANF_STDIN), format, args);
35*b8f134faSJoseph Huber   // This is done to avoid including stdio.h in the internals. On most systems
36*b8f134faSJoseph Huber   // EOF is -1, so this will be transformed into just "return ret_val".
37*b8f134faSJoseph Huber   return (ret_val == -1) ? EOF : ret_val;
38*b8f134faSJoseph Huber }
39*b8f134faSJoseph Huber 
40*b8f134faSJoseph Huber } // namespace LIBC_NAMESPACE_DECL
41