xref: /llvm-project/libc/src/stdio/scanf.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
136991d83SMichael Jones //===-- Implementation of scanf ---------------------------------*- C++ -*-===//
236991d83SMichael Jones //
336991d83SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
436991d83SMichael Jones // See https://llvm.org/LICENSE.txt for license information.
536991d83SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
636991d83SMichael Jones //
736991d83SMichael Jones //===----------------------------------------------------------------------===//
836991d83SMichael Jones 
936991d83SMichael Jones #include "src/stdio/scanf.h"
1036991d83SMichael Jones 
1136991d83SMichael Jones #include "src/__support/File/file.h"
1236991d83SMichael Jones #include "src/__support/arg_list.h"
13*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
1436991d83SMichael Jones #include "src/stdio/scanf_core/vfscanf_internal.h"
1536991d83SMichael Jones 
165aed6d67SMichael Jones #include "hdr/types/FILE.h"
1736991d83SMichael Jones #include <stdarg.h>
1836991d83SMichael Jones 
19a5a008ffSmichaelrj-google #ifndef LIBC_COPT_STDIO_USE_SYSTEM_FILE
20b6bc9d72SGuillaume Chatelet #define SCANF_STDIN LIBC_NAMESPACE::stdin
21a5a008ffSmichaelrj-google #else // LIBC_COPT_STDIO_USE_SYSTEM_FILE
22a5a008ffSmichaelrj-google #define SCANF_STDIN ::stdin
23a5a008ffSmichaelrj-google #endif // LIBC_COPT_STDIO_USE_SYSTEM_FILE
24a5a008ffSmichaelrj-google 
25*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
2636991d83SMichael Jones 
2736991d83SMichael Jones LLVM_LIBC_FUNCTION(int, scanf, (const char *__restrict format, ...)) {
2836991d83SMichael Jones   va_list vlist;
2936991d83SMichael Jones   va_start(vlist, format);
3036991d83SMichael Jones   internal::ArgList args(vlist); // This holder class allows for easier copying
3136991d83SMichael Jones                                  // and pointer semantics, as well as handling
3236991d83SMichael Jones                                  // destruction automatically.
3336991d83SMichael Jones   va_end(vlist);
3436991d83SMichael Jones   int ret_val = scanf_core::vfscanf_internal(
35a5a008ffSmichaelrj-google       reinterpret_cast<::FILE *>(SCANF_STDIN), format, args);
3636991d83SMichael Jones   // This is done to avoid including stdio.h in the internals. On most systems
3736991d83SMichael Jones   // EOF is -1, so this will be transformed into just "return ret_val".
3836991d83SMichael Jones   return (ret_val == -1) ? EOF : ret_val;
3936991d83SMichael Jones }
4036991d83SMichael Jones 
41*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
42