• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Reader definition for scanf -----------------------------*- 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/scanf_core/reader.h"
10 #include <stddef.h>
11 
12 namespace LIBC_NAMESPACE {
13 namespace scanf_core {
14 
ungetc(char c)15 void Reader::ungetc(char c) {
16   --cur_chars_read;
17   if (rb != nullptr && rb->buff_cur > 0) {
18     // While technically c should be written back to the buffer, in scanf we
19     // always write the character that was already there. Additionally, the
20     // buffer is most likely to contain a string that isn't part of a file,
21     // which may not be writable.
22     --(rb->buff_cur);
23     return;
24   }
25   stream_ungetc(static_cast<int>(c), input_stream);
26 }
27 } // namespace scanf_core
28 } // namespace LIBC_NAMESPACE
29