libc: Add stdin

This commit is contained in:
apio 2023-03-19 19:19:20 +01:00
parent d33fccc66f
commit 31ef96ebfc
Signed by: apio
GPG Key ID: B8A7D06E42258954
5 changed files with 12 additions and 6 deletions

View File

@ -24,18 +24,19 @@ int main()
{ {
if (getpid() != 1) if (getpid() != 1)
{ {
printf("error: init not running as PID 1.\n"); fprintf(stderr, "error: init not running as PID 1.\n");
return 1; return 1;
} }
populate_devfs(); 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 // Before this point, we don't even have an stdin, stdout and stderr. Set it up now so that child processes (and us)
// print stuff. // can print stuff.
stdin = fopen("/dev/console", "r");
stdout = fopen("/dev/console", "w"); stdout = fopen("/dev/console", "w");
stderr = 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(); pid_t ret = fork();

View File

@ -16,8 +16,10 @@ typedef struct
#define EOF -1 #define EOF -1
extern FILE* stdin;
extern FILE* stdout; extern FILE* stdout;
extern FILE* stderr; extern FILE* stderr;
#define stdin stdin
#define stdout stdout #define stdout stdout
#define stderr stderr #define stderr stderr

View File

@ -10,8 +10,9 @@
#include <stdint.h> #include <stdint.h>
#include <sys/types.h> #include <sys/types.h>
#define STDOUT_FILENO 0 #define STDIN_FILENO 0
#define STDERR_FILENO 1 #define STDOUT_FILENO 1
#define STDERR_FILENO 2
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"

View File

@ -5,6 +5,7 @@ extern "C"
{ {
void libc_init() void libc_init()
{ {
stdin = fdopen(STDIN_FILENO, "r");
stdout = fdopen(STDOUT_FILENO, "w"); stdout = fdopen(STDOUT_FILENO, "w");
stderr = fdopen(STDERR_FILENO, "w"); stderr = fdopen(STDERR_FILENO, "w");
} }

View File

@ -8,6 +8,7 @@
#include <sys/syscall.h> #include <sys/syscall.h>
#include <unistd.h> #include <unistd.h>
FILE* stdin = nullptr;
FILE* stderr = nullptr; FILE* stderr = nullptr;
FILE* stdout = nullptr; FILE* stdout = nullptr;