#include "exec.h" #include "../lib/sys/io.h" #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) { for (int i = 0; i < BUFSIZ; ++i) buf[i] = 0; } 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], OPEN_READ_ONLY, 0); if (fd < 0) wstdf("%s\n", errstr[-fd]); } while (1) { 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) break; if (buf[line_length - 1] == '\n') buf[--line_length] = 0; if (line_length > 0) { exec(buf); } clear_buf(buf); } if (fd != STDOUT_FD) close(fd); }