diff options
author | dzwdz | 2022-07-30 14:52:22 +0200 |
---|---|---|
committer | dzwdz | 2022-07-30 14:52:22 +0200 |
commit | b26399ad055aae9ef4b01694967515235568cd85 (patch) | |
tree | f8eec54655b5461e00596d7aa1f2a27aa5fede25 /src | |
parent | 9efd7f96b028a69b1da0cf53f6d7be71048c73d7 (diff) |
user: stderr
Diffstat (limited to 'src')
-rw-r--r-- | src/user/app/init/init.c | 17 | ||||
-rw-r--r-- | src/user/app/shell/builtins.c | 7 | ||||
-rw-r--r-- | src/user/app/shell/shell.c | 12 | ||||
-rw-r--r-- | src/user/app/shell/shell.h | 2 | ||||
-rw-r--r-- | src/user/lib/file.c | 7 | ||||
-rw-r--r-- | src/user/lib/include/stdio.h | 2 |
6 files changed, 27 insertions, 20 deletions
diff --git a/src/user/app/init/init.c b/src/user/app/init/init.c index d7dc34d..bb92cce 100644 --- a/src/user/app/init/init.c +++ b/src/user/app/init/init.c @@ -6,20 +6,21 @@ #include <unistd.h> #include <user/lib/fs/misc.h> +#define die(fmt, ...) do { fprintf(stderr, "init: " fmt, __VA_ARGS__); exit(1); } while (0) + void redirect(const char *exe, const char *out, const char *in) { if (!fork()) { - if (!freopen(out, "a+", stdout)) { - printf("init: couldn't open %s\n", out); // TODO borked - exit(1); - } - if (!freopen(in, "r", stdin)) { - printf("init: couldn't open %s\n", in); + if (!freopen(out, "a+", stderr)) { + fprintf(stdout, "couldn't open %s\n", out); exit(1); } + if (!freopen(out, "a+", stdout)) + die("couldn't open %s\n", out); + if (!freopen(in, "r", stdin)) + die(" couldn't open %s\n", in); termcook(); execv(exe, NULL); - printf("couldn't start %s\n", exe); - exit(1); + die("couldn't start %s\n", exe); } } diff --git a/src/user/app/shell/builtins.c b/src/user/app/shell/builtins.c index fef46a5..3a19a2f 100644 --- a/src/user/app/shell/builtins.c +++ b/src/user/app/shell/builtins.c @@ -1,4 +1,5 @@ #include "builtins.h" +#include "shell.h" #include <stdio.h> #include <string.h> #include <unistd.h> @@ -27,7 +28,7 @@ void cmd_cat_ls(const char *args, bool ls) { } if (!file) { - printf("couldn't open.\n"); + eprintf("couldn't open"); return; } @@ -50,7 +51,7 @@ void cmd_hexdump(const char *args) { fd = _syscall_open(args, strlen(args), 0); if (fd < 0) { - printf("couldn't open.\n"); + eprintf("couldn't open %s", args); return; } @@ -79,7 +80,7 @@ void cmd_hexdump(const char *args) { void cmd_touch(const char *args) { int fd = _syscall_open(args, strlen(args), OPEN_CREATE); if (fd < 0) { - printf("couldn't create file.\n"); + eprintf("couldn't touch %s\n", args); return; } close(fd); diff --git a/src/user/app/shell/shell.c b/src/user/app/shell/shell.c index 7f892c0..8bfe105 100644 --- a/src/user/app/shell/shell.c +++ b/src/user/app/shell/shell.c @@ -20,7 +20,7 @@ static void execp(char **argv) { size_t cmdlen = strlen(argv[0]); char *s = malloc(cmdlen); if (!s) { - printf("sh: out of memory.\n"); + eprintf("out of memory."); exit(1); } memcpy(s, "/bin/", 5); @@ -38,7 +38,7 @@ static void run(char *cmd) { int ret = parse(cmd, argv, ARGV_MAX, &redir); if (ret < 0) { - printf("sh: error parsing command\n"); + eprintf("error parsing command"); return; } @@ -59,7 +59,7 @@ static void run(char *cmd) { } if (redir.stdout && !freopen(redir.stdout, redir.append ? "a" : "w", stdout)) { - // TODO stderr + eprintf("couldn't open %s for redirection", redir.stdout); exit(0); } @@ -92,9 +92,9 @@ static void run(char *cmd) { } else { execp(argv); if (errno == EINVAL) { - printf("%s isn't a valid executable\n", argv[0]); + eprintf("%s isn't a valid executable\n", argv[0]); } else { - printf("unknown command: %s\n", argv[0]); + eprintf("unknown command: %s\n", argv[0]); } } exit(0); /* kills the subprocess */ @@ -108,7 +108,7 @@ int main(int argc, char **argv) { if (argc > 1) { f = fopen(argv[1], "r"); if (!f) { - printf("sh: couldn't open %s\n", argv[1]); + eprintf("couldn't open %s\n", argv[1]); return 1; } } diff --git a/src/user/app/shell/shell.h b/src/user/app/shell/shell.h index 4c7eeb3..5097e00 100644 --- a/src/user/app/shell/shell.h +++ b/src/user/app/shell/shell.h @@ -2,6 +2,8 @@ #include <stdbool.h> #include <stddef.h> +#define eprintf(fmt, ...) fprintf(stderr, "sh: "fmt"\n" __VA_OPT__(,) __VA_ARGS__) + struct redir { const char *stdout; bool append; diff --git a/src/user/lib/file.c b/src/user/lib/file.c index bd8cb76..05ae2e8 100644 --- a/src/user/lib/file.c +++ b/src/user/lib/file.c @@ -8,7 +8,10 @@ static FILE _stdin_null = { .fd = 0 }; static FILE _stdout_null = { .fd = 1 }; -FILE *const stdin = &_stdin_null, *const stdout = &_stdout_null; +static FILE _stderr_null = { .fd = 2 }; +FILE *const stdin = &_stdin_null; +FILE *const stdout = &_stdout_null; +FILE *const stderr = &_stderr_null; FILE *fopen(const char *path, const char *mode) { @@ -190,7 +193,7 @@ int fseek(FILE *f, long offset, int whence) { int fclose(FILE *f) { fflush(f); if (f->fd > 0) close(f->fd); - if (f != &_stdin_null && f != &_stdout_null) + if (f != &_stdin_null && f != &_stdout_null && f != &_stderr_null) free(f); return 0; } diff --git a/src/user/lib/include/stdio.h b/src/user/lib/include/stdio.h index fe754da..fd4a676 100644 --- a/src/user/lib/include/stdio.h +++ b/src/user/lib/include/stdio.h @@ -20,7 +20,7 @@ int vsnprintf(char *restrict str, size_t len, const char *restrict fmt, va_list int _klogf(const char *fmt, ...); // for kernel debugging only -extern FILE *const stdin, *const stdout; +extern FILE *const stdin, *const stdout, *const stderr; FILE *fopen(const char *path, const char *mode); FILE *freopen(const char *path, const char *mode, FILE *); |