xref: /llvm-project/libc/src/stdio/fscanf.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
136991d83SMichael Jones //===-- Implementation of fscanf --------------------------------*- 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/fscanf.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 
19*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
2036991d83SMichael Jones 
2136991d83SMichael Jones LLVM_LIBC_FUNCTION(int, fscanf,
2236991d83SMichael Jones                    (::FILE *__restrict stream, const char *__restrict format,
2336991d83SMichael Jones                     ...)) {
2436991d83SMichael Jones   va_list vlist;
2536991d83SMichael Jones   va_start(vlist, format);
2636991d83SMichael Jones   internal::ArgList args(vlist); // This holder class allows for easier copying
2736991d83SMichael Jones                                  // and pointer semantics, as well as handling
2836991d83SMichael Jones                                  // destruction automatically.
2936991d83SMichael Jones   va_end(vlist);
3036991d83SMichael Jones   int ret_val = scanf_core::vfscanf_internal(stream, format, args);
3136991d83SMichael Jones   // This is done to avoid including stdio.h in the internals. On most systems
3236991d83SMichael Jones   // EOF is -1, so this will be transformed into just "return ret_val".
3336991d83SMichael Jones   return (ret_val == -1) ? EOF : ret_val;
3436991d83SMichael Jones }
3536991d83SMichael Jones 
36*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
37