diff --git a/libc/config/linux/riscv64/entrypoints.txt b/libc/config/linux/riscv64/entrypoints.txt --- a/libc/config/linux/riscv64/entrypoints.txt +++ b/libc/config/linux/riscv64/entrypoints.txt @@ -405,6 +405,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 diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -419,6 +419,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 diff --git a/libc/docs/stdio.rst b/libc/docs/stdio.rst --- a/libc/docs/stdio.rst +++ b/libc/docs/stdio.rst @@ -83,7 +83,7 @@ ============= ========= (f)getc |check| fgets |check| -getchar +getchar |check| fread |check| (f)putc |check| (f)puts |check| diff --git a/libc/spec/posix.td b/libc/spec/posix.td --- a/libc/spec/posix.td +++ b/libc/spec/posix.td @@ -1058,6 +1058,11 @@ RetValSpec, [ArgSpec] >, + FunctionSpec< + "getchar_unlocked", + RetValSpec, + [ArgSpec<>] + >, ] >; diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -580,6 +580,11 @@ RetValSpec, [ArgSpec] >, + FunctionSpec< + "getchar", + RetValSpec, + [ArgSpec<>] + >, FunctionSpec< "putc", RetValSpec, diff --git a/libc/src/stdio/CMakeLists.txt b/libc/src/stdio/CMakeLists.txt --- a/libc/src/stdio/CMakeLists.txt +++ b/libc/src/stdio/CMakeLists.txt @@ -154,6 +154,30 @@ libc.src.__support.File.platform_file ) +add_entrypoint_object( + getchar + SRCS + getchar.cpp + HDRS + getchar.h + DEPENDS + libc.src.getc + 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.getc_unlocked + libc.src.__support.File.file + libc.src.__support.File.platform_file +) + add_entrypoint_object( fgets SRCS diff --git a/libc/src/stdio/getchar.h b/libc/src/stdio/getchar.h new file mode 100644 --- /dev/null +++ b/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 diff --git a/libc/src/stdio/getchar.cpp b/libc/src/stdio/getchar.cpp new file mode 100644 --- /dev/null +++ b/libc/src/stdio/getchar.cpp @@ -0,0 +1,20 @@ +//===-- 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/stdio/getc.h" + +namespace __llvm_libc { + +LIBC_INLINE LLVM_LIBC_FUNCTION(int, getchar, ()) { + return __llvm_libc::getc(reinterpret_cast<::FILE *>(stdin)); +} + +} // namespace __llvm_libc diff --git a/libc/src/stdio/getchar_unlocked.h b/libc/src/stdio/getchar_unlocked.h new file mode 100644 --- /dev/null +++ b/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 diff --git a/libc/src/stdio/getchar_unlocked.cpp b/libc/src/stdio/getchar_unlocked.cpp new file mode 100644 --- /dev/null +++ b/libc/src/stdio/getchar_unlocked.cpp @@ -0,0 +1,20 @@ +//===-- 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/stdio/getc_unlocked.h" + +namespace __llvm_libc { + +LIBC_INLINE LLVM_LIBC_FUNCTION(int, getchar_unlocked, ()) { + return __llvm_libc::getc_unlocked(reinterpret_cast<::FILE *>(stdin)); +} + +} // namespace __llvm_libc