diff options
Diffstat (limited to 'src/user/app')
-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 |
4 files changed, 21 insertions, 17 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; |