diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2022-12-17 17:27:57 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2022-12-17 17:27:57 +0100 |
| commit | 0e0f22cde8fa9a8c9e0ff509a90727092e0af58b (patch) | |
| tree | ce32216649fa8651d3ae29a4429427ed033f9c37 /smash | |
| parent | 48ab221a6d6e4d71e00e0258dd26d63f35bb6f38 (diff) | |
add errno and file support
Diffstat (limited to 'smash')
| -rw-r--r-- | smash/main.c | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/smash/main.c b/smash/main.c index 656eeea..57de496 100644 --- a/smash/main.c +++ b/smash/main.c @@ -4,6 +4,9 @@ #include "../lib/cstr/cstr.h" #include "../lib/sys/dup2.h" +#include "../lib/io/io.h" +#include "../lib/sys/errno.h" + #define BUFSIZ 1024 void clear_buf(char *buf) @@ -17,20 +20,35 @@ int main(int argc, char *argv[], char *envp[]) char buf[BUFSIZ] = {0}; char prompt[] = "$ "; u64 line_length; + int fd = STDIN_FD; + + if (argc == 2) { + fd = open(argv[1], FILE_READ_ONLY, 0); + + if (fd < 0) + wstdf("%s\n", errstr[-fd]); + } while (1) { - write(STDOUT_FD, prompt, cstr_length(prompt)); - read(STDIN_FD, buf, 1024); + if (fd == STDIN_FD) + write(STDOUT_FD, prompt, cstr_length(prompt)); + int p = read(fd, buf, BUFSIZ); line_length = cstr_length(buf); + if (line_length == 0) - return 0; + break; + + if (buf[line_length - 1] == '\n') + buf[--line_length] = 0; - if (line_length > 1) { - buf[line_length - 1] = 0; + if (line_length > 0) { exec(buf); } clear_buf(buf); } + + if (fd != STDOUT_FD) + close(fd); } |