Index: libc/config/linux/riscv64/entrypoints.txt =================================================================== --- libc/config/linux/riscv64/entrypoints.txt +++ libc/config/linux/riscv64/entrypoints.txt @@ -408,6 +408,8 @@ libc.src.stdio.fprintf libc.src.stdio.getc libc.src.stdio.getc_unlocked + libc.src.stdio.getchar + libc.src.stdio.getchar_unlocked libc.src.stdio.printf libc.src.stdio.sscanf libc.src.stdio.scanf Index: libc/config/linux/x86_64/entrypoints.txt =================================================================== --- libc/config/linux/x86_64/entrypoints.txt +++ libc/config/linux/x86_64/entrypoints.txt @@ -422,6 +422,8 @@ libc.src.stdio.fwrite_unlocked libc.src.stdio.getc libc.src.stdio.getc_unlocked + libc.src.stdio.getchar + libc.src.stdio.getchar_unlocked libc.src.stdio.sscanf libc.src.stdio.scanf libc.src.stdio.fscanf Index: libc/docs/stdio.rst =================================================================== --- libc/docs/stdio.rst +++ libc/docs/stdio.rst @@ -83,7 +83,7 @@ ============= ========= (f)getc |check| fgets |check| -getchar +getchar |check| fread |check| (f)putc |check| (f)puts |check| Index: libc/spec/posix.td =================================================================== --- libc/spec/posix.td +++ libc/spec/posix.td @@ -1063,6 +1063,11 @@ RetValSpec, [ArgSpec] >, + FunctionSpec< + "getchar_unlocked", + RetValSpec, + [ArgSpec<>] + >, ] >; Index: libc/spec/stdc.td =================================================================== --- libc/spec/stdc.td +++ libc/spec/stdc.td @@ -580,6 +580,11 @@ RetValSpec, [ArgSpec] >, + FunctionSpec< + "getchar", + RetValSpec, + [ArgSpec<>] + >, FunctionSpec< "putc", RetValSpec, Index: libc/src/stdio/CMakeLists.txt =================================================================== --- libc/src/stdio/CMakeLists.txt +++ libc/src/stdio/CMakeLists.txt @@ -154,6 +154,32 @@ libc.src.__support.File.platform_file ) +add_entrypoint_object( + getchar + SRCS + getchar.cpp + HDRS + getchar.h + DEPENDS + libc.src.errno.errno + libc.include.stdio + libc.src.__support.File.file + libc.src.__support.File.platform_file +) + +add_entrypoint_object( + getchar_unlocked + SRCS + getc_unlocked.cpp + HDRS + getc_unlocked.h + DEPENDS + libc.src.errno.errno + libc.include.stdio + libc.src.__support.File.file + libc.src.__support.File.platform_file +) + add_entrypoint_object( fgets SRCS Index: libc/src/stdio/getchar.h =================================================================== --- /dev/null +++ libc/src/stdio/getchar.h @@ -0,0 +1,18 @@ +//===-- Implementation header of getchar ------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC_STDIO_GETCHAR_H +#define LLVM_LIBC_SRC_STDIO_GETCHAR_H + +namespace __llvm_libc { + +int getchar(); + +} // namespace __llvm_libc + +#endif // LLVM_LIBC_SRC_STDIO_GETCHAR_H Index: libc/src/stdio/getchar.cpp =================================================================== --- /dev/null +++ libc/src/stdio/getchar.cpp @@ -0,0 +1,29 @@ +//===-- Implementation of getchar -----------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/stdio/getchar.h" +#include "src/__support/File/file.h" + +#include "src/errno/libc_errno.h" +#include + +namespace __llvm_libc { + +LLVM_LIBC_FUNCTION(int, getchar, ()) { + unsigned char c; + auto result = stdin->read(&c, 1); + size_t r = result.value; + if (result.has_error()) + libc_errno = result.error; + + if (r != 1) + return EOF; + return c; +} + +} // namespace __llvm_libc Index: libc/src/stdio/getchar_unlocked.h =================================================================== --- /dev/null +++ libc/src/stdio/getchar_unlocked.h @@ -0,0 +1,18 @@ +//===-- Implementation header of getchar_unlocked ---------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC_STDIO_GETCHAR_UNLOCKED_H +#define LLVM_LIBC_SRC_STDIO_GETCHAR_UNLOCKED_H + +namespace __llvm_libc { + +int getchar_unlocked(); + +} // namespace __llvm_libc + +#endif // LLVM_LIBC_SRC_STDIO_GETCHAR_UNLOCKED_H Index: libc/src/stdio/getchar_unlocked.cpp =================================================================== --- /dev/null +++ libc/src/stdio/getchar_unlocked.cpp @@ -0,0 +1,29 @@ +//===-- Implementation of getchar_unlocked --------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/stdio/getchar_unlocked.h" +#include "src/__support/File/file.h" + +#include "src/errno/libc_errno.h" +#include + +namespace __llvm_libc { + +LLVM_LIBC_FUNCTION(int, getchar_unlocked, ()) { + unsigned char c; + auto result = stdin->read_unlocked(&c, 1); + size_t r = result.value; + if (result.has_error()) + libc_errno = result.error; + + if (r != 1) + return EOF; + return c; +} + +} // namespace __llvm_libc