diff options
author | dzwdz | 2022-08-03 15:20:19 +0200 |
---|---|---|
committer | dzwdz | 2022-08-03 15:20:19 +0200 |
commit | c510603cedcd3e39681ae38560f62adc9d70a53b (patch) | |
tree | bd8f61274f0e85d6cd1b0814058b6a7eeca07f3b /src | |
parent | 4c8caaec8e856686581feb0a84089cdad07a0018 (diff) |
user/shell: make the builtins handle multiple args; misc fixes
Diffstat (limited to 'src')
-rw-r--r-- | src/user/app/shell/builtins.c | 176 | ||||
-rw-r--r-- | src/user/app/shell/builtins.h | 2 | ||||
-rw-r--r-- | src/user/app/testelf/main.c | 2 |
3 files changed, 94 insertions, 86 deletions
diff --git a/src/user/app/shell/builtins.c b/src/user/app/shell/builtins.c index d0a2e3a..1a46003 100644 --- a/src/user/app/shell/builtins.c +++ b/src/user/app/shell/builtins.c @@ -1,34 +1,38 @@ #include "builtins.h" #include "shell.h" #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <unistd.h> -static void cmd_cat(int argc, const char **argv) { - // TODO loop over argv - - FILE *file; - static char buf[512]; - - if (argv[1]) - file = fopen(argv[1], "r"); - else - file = file_clone(stdin, "r"); - - if (!file) { - eprintf("couldn't open"); - return; +#define DEFAULT_ARGV(...) \ + char *_argv_default[] = {argv[0], __VA_ARGS__}; \ + if (argc <= 1) { \ + argc = sizeof(_argv_default) / sizeof(*_argv_default); \ + argv = _argv_default; \ } - for (;;) { - int len = fread(buf, 1, sizeof buf, file); - if (len <= 0) break; - fwrite(buf, 1, len, stdout); +static void cmd_cat(int argc, char **argv) { + const size_t buflen = 4096; + char *buf = malloc(buflen); + + DEFAULT_ARGV("!stdin"); + for (int i = 1; i < argc; i++) { + FILE *file = fopen(argv[i], "r"); + if (!file) { + eprintf("couldn't open %s", argv[i]); + return; + } + for (;;) { + int len = fread(buf, 1, sizeof buf, file); + if (len <= 0) break; + fwrite(buf, 1, len, stdout); + } + fclose(file); } - fclose(file); } -static void cmd_echo(int argc, const char **argv) { +static void cmd_echo(int argc, char **argv) { bool newline = true; int i = 1; @@ -38,88 +42,92 @@ static void cmd_echo(int argc, const char **argv) { } printf("%s", argv[i++]); - for (; argv[i]; i++) + for (; i < argc; i++) printf(" %s", argv[i]); if (newline) printf("\n"); } -void cmd_hexdump(int argc, const char **argv) { - // TODO loop over argv - // TODO use fopen/fread - static uint8_t buf[512]; - int fd, len; - - fd = _syscall_open(argv[1], strlen(argv[1]), 0); - if (fd < 0) { - eprintf("couldn't open %s", argv[1]); - return; - } - - len = _syscall_read(fd, buf, sizeof buf, 0); - for (int i = 0; i < len; i += 16) { - printf("%08x ", i); - - for (int j = i; j < i + 8 && j < len; j++) - printf("%02x ", buf[j]); - printf(" "); - for (int j = i + 8; j < i + 16 && j < len; j++) - printf("%02x ", buf[j]); - printf(" |"); - - for (int j = i; j < i + 16 && j < len; j++) { - char c = '.'; - if (0x20 <= buf[j] && buf[j] < 0x7f) c = buf[j]; - printf("%c", c); +void cmd_hexdump(int argc, char **argv) { + const size_t buflen = 512; + uint8_t *buf = malloc(buflen); + FILE *file; + int len; + + DEFAULT_ARGV("!stdin"); + for (int i = 1; i < argc; i++) { + file = fopen(argv[i], "r"); + if (!file) { + eprintf("couldn't open %s", argv[i]); + return; + } + len = fread(buf, 1, buflen, file); + fclose(file); + + for (int i = 0; i < len; i += 16) { + printf("%08x ", i); + + for (int j = i; j < i + 8 && j < len; j++) + printf("%02x ", buf[j]); + printf(" "); + for (int j = i + 8; j < i + 16 && j < len; j++) + printf("%02x ", buf[j]); + printf(" |"); + + for (int j = i; j < i + 16 && j < len; j++) { + char c = '.'; + if (0x20 <= buf[j] && buf[j] < 0x7f) c = buf[j]; + printf("%c", c); + } + printf("|\n"); } - printf("|\n"); } - - close(fd); } -static void cmd_ls(int argc, const char **argv) { - // TODO loop over argv +static void cmd_ls(int argc, char **argv) { FILE *file; - static char buf[512]; - - if (argv[1]) { - int len = strlen(argv[1]); - memcpy(buf, argv[1], len + 1); // TODO no overflow check - - if (buf[len-1] != '/') { - buf[len] = '/'; - buf[len+1] = '\0'; + const size_t buflen = 4096; + char *buf = malloc(buflen); + + DEFAULT_ARGV("!stdin"); + for (int i = 1; i < argc; i++) { + char *path = (void*)argv[i]; + int pathlen = strlen(path); + + if (!pathlen || path[pathlen - 1] != '/') { + memcpy(buf, path, pathlen); + buf[pathlen] = '/'; + buf[pathlen+1] = '\0'; + path = buf; } - file = fopen(buf, "r"); - } else { - file = fopen("/", "r"); + file = fopen(path, "r"); + if (!file) { + eprintf("couldn't open %s", argv[i]); + return; + } + for (;;) { + int len = fread(buf, 1, sizeof buf, file); + if (len <= 0) break; + for (int i = 0; i < len; i++) + if (buf[i] == '\0') buf[i] = '\n'; + fwrite(buf, 1, len, stdout); + } + fclose(file); } +} - if (!file) { - eprintf("couldn't open"); +static void cmd_touch(int argc, char **argv) { + if (argc <= 1) { + eprintf("no arguments"); return; } - for (;;) { - int len = fread(buf, 1, sizeof buf, file); - if (len <= 0) break; - for (int i = 0; i < len; i++) - if (buf[i] == '\0') buf[i] = '\n'; - fwrite(buf, 1, len, stdout); - } - fclose(file); -} - -static void cmd_touch(int argc, const char **argv) { - // TODO loop over argv - int fd = _syscall_open(argv[1], strlen(argv[1]), OPEN_CREATE); - if (fd < 0) { - eprintf("couldn't touch %s\n", argv[1]); - return; + for (int i = 1; i < argc; i++) { + FILE *f = fopen(argv[i], "a"); + if (!f) eprintf("couldn't touch %s\n", argv[i]); + fclose(f); } - close(fd); } struct builtin builtins[] = { diff --git a/src/user/app/shell/builtins.h b/src/user/app/shell/builtins.h index aa2cd8c..1e422bb 100644 --- a/src/user/app/shell/builtins.h +++ b/src/user/app/shell/builtins.h @@ -4,7 +4,7 @@ struct builtin { const char *name; - void (*fn)(int argc, const char **argv); + void (*fn)(int argc, char **argv); }; extern struct builtin builtins[]; diff --git a/src/user/app/testelf/main.c b/src/user/app/testelf/main.c index aa1f596..ee7465b 100644 --- a/src/user/app/testelf/main.c +++ b/src/user/app/testelf/main.c @@ -2,7 +2,7 @@ const char *str = "Hello!", *str2 = "World."; -int main(int argc, char **argv, char **envp) { +int main(int argc, char **argv) { printf("elftest's &main == 0x%x\n", &main); printf("%s %s\n", str, str2); printf("argc == %u\n", argc); |