diff --git a/libc/config/linux/api.td b/libc/config/linux/api.td --- a/libc/config/linux/api.td +++ b/libc/config/linux/api.td @@ -149,6 +149,7 @@ def StdIOAPI : PublicAPI<"stdio.h"> { let Macros = [ SimpleMacroDef<"stderr", "stderr">, + SimpleMacroDef<"stdin", "stdin">, SimpleMacroDef<"stdout", "stdout">, SimpleMacroDef<"_IOFBF", "0">, SimpleMacroDef<"_IOLBF", "1">, 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 @@ -387,6 +387,7 @@ libc.src.stdio.putchar libc.src.stdio.puts libc.src.stdio.stderr + libc.src.stdio.stdin libc.src.stdio.stdout # stdlib.h entrypoints diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -491,6 +491,7 @@ HeaderSpec StdIO = HeaderSpec< "stdio.h", [ + Macro<"stdin">, Macro<"stderr">, Macro<"stdout">, Macro<"_IOFBF">, @@ -621,6 +622,10 @@ >, ], [ + ObjectSpec< + "stdin", + "FILE *" + >, ObjectSpec< "stdout", "FILE *" diff --git a/libc/src/__support/File/file.h b/libc/src/__support/File/file.h --- a/libc/src/__support/File/file.h +++ b/libc/src/__support/File/file.h @@ -233,6 +233,7 @@ // library. File *openfile(const char *path, const char *mode); +extern File *stdin; extern File *stdout; extern File *stderr; diff --git a/libc/src/__support/File/linux_file.cpp b/libc/src/__support/File/linux_file.cpp --- a/libc/src/__support/File/linux_file.cpp +++ b/libc/src/__support/File/linux_file.cpp @@ -164,6 +164,12 @@ return file; } +constexpr size_t STDIN_BUFFER_SIZE = 512; +char stdin_buffer[STDIN_BUFFER_SIZE]; +static LinuxFile StdIn(0, stdin_buffer, STDIN_BUFFER_SIZE, _IOFBF, false, + File::ModeFlags(File::OpenMode::READ)); +File *stdin = &StdIn; + constexpr size_t STDOUT_BUFFER_SIZE = 1024; char stdout_buffer[STDOUT_BUFFER_SIZE]; static LinuxFile StdOut(1, stdout_buffer, STDOUT_BUFFER_SIZE, _IOLBF, false, 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 @@ -268,6 +268,18 @@ libc.src.__support.File.file ) +add_entrypoint_object( + stdin + SRCS + stdin.cpp + HDRS + stdin.h + DEPENDS + libc.include.stdio + libc.src.__support.File.file + libc.src.__support.File.platform_file +) + add_entrypoint_object( stdout SRCS diff --git a/libc/src/stdio/stdin.h b/libc/src/stdio/stdin.h new file mode 100644 --- /dev/null +++ b/libc/src/stdio/stdin.h @@ -0,0 +1,9 @@ +//===------------------------------------------------------------*- 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 +// +//===----------------------------------------------------------------------===// + +#error "Do not include this file. Instead include __support/File/file.h." diff --git a/libc/src/stdio/stdin.cpp b/libc/src/stdio/stdin.cpp new file mode 100644 --- /dev/null +++ b/libc/src/stdio/stdin.cpp @@ -0,0 +1,13 @@ +//===-- Definition of the global stdin object -----------------------------===// +// +// 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/__support/File/file.h" + +#include + +extern "C" FILE *stdin = reinterpret_cast(__llvm_libc::stdin);