From 0e0f22cde8fa9a8c9e0ff509a90727092e0af58b Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Sat, 17 Dec 2022 17:27:57 +0100 Subject: add errno and file support --- smash/main.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'smash/main.c') 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); } -- cgit v1.2.3-70-g09d2