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
12namespace LIBC_NAMESPACE {
13namespace scanf_core {
14
15void 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

source code of libc/src/stdio/scanf_core/reader.cpp