From 31ef96ebfc35dab79865ea8f7f8c2f7e2f1b8bd2 Mon Sep 17 00:00:00 2001 From: apio Date: Sun, 19 Mar 2023 19:19:20 +0100 Subject: [PATCH] libc: Add stdin --- apps/init.c | 9 +++++---- libc/include/stdio.h | 2 ++ libc/include/unistd.h | 5 +++-- libc/src/init.cpp | 1 + libc/src/stdio.cpp | 1 + 5 files changed, 12 insertions(+), 6 deletions(-) diff --git a/apps/init.c b/apps/init.c index b08eac44..fb1d9b67 100644 --- a/apps/init.c +++ b/apps/init.c @@ -24,18 +24,19 @@ int main() { if (getpid() != 1) { - printf("error: init not running as PID 1.\n"); + fprintf(stderr, "error: init not running as PID 1.\n"); return 1; } populate_devfs(); - // Before this point, we don't even have an stdout and stderr. Set it up now so that child processes (and us) can - // print stuff. + // Before this point, we don't even have an stdin, stdout and stderr. Set it up now so that child processes (and us) + // can print stuff. + stdin = fopen("/dev/console", "r"); stdout = fopen("/dev/console", "w"); stderr = fopen("/dev/console", "w"); - fprintf(stderr, "init is running as PID %d\n", getpid()); + printf("init is running as PID %d\n", getpid()); pid_t ret = fork(); diff --git a/libc/include/stdio.h b/libc/include/stdio.h index f72f1961..acf3643c 100644 --- a/libc/include/stdio.h +++ b/libc/include/stdio.h @@ -16,8 +16,10 @@ typedef struct #define EOF -1 +extern FILE* stdin; extern FILE* stdout; extern FILE* stderr; +#define stdin stdin #define stdout stdout #define stderr stderr diff --git a/libc/include/unistd.h b/libc/include/unistd.h index de69d2e5..ccadde52 100644 --- a/libc/include/unistd.h +++ b/libc/include/unistd.h @@ -10,8 +10,9 @@ #include #include -#define STDOUT_FILENO 0 -#define STDERR_FILENO 1 +#define STDIN_FILENO 0 +#define STDOUT_FILENO 1 +#define STDERR_FILENO 2 #ifdef __cplusplus extern "C" diff --git a/libc/src/init.cpp b/libc/src/init.cpp index 1582945f..3c98b054 100644 --- a/libc/src/init.cpp +++ b/libc/src/init.cpp @@ -5,6 +5,7 @@ extern "C" { void libc_init() { + stdin = fdopen(STDIN_FILENO, "r"); stdout = fdopen(STDOUT_FILENO, "w"); stderr = fdopen(STDERR_FILENO, "w"); } diff --git a/libc/src/stdio.cpp b/libc/src/stdio.cpp index 5ac9cfec..d18bb112 100644 --- a/libc/src/stdio.cpp +++ b/libc/src/stdio.cpp @@ -8,6 +8,7 @@ #include #include +FILE* stdin = nullptr; FILE* stderr = nullptr; FILE* stdout = nullptr;