X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/b3b42765712afed5a35c9be5c832f4a06bd85e7a..61a1daa59589212608039e2734009870818bacd3:/lunaix-os/kernel/demos/simple_sh.c diff --git a/lunaix-os/kernel/demos/simple_sh.c b/lunaix-os/kernel/demos/simple_sh.c index 036550a..693bb75 100644 --- a/lunaix-os/kernel/demos/simple_sh.c +++ b/lunaix-os/kernel/demos/simple_sh.c @@ -8,6 +8,7 @@ #include char pwd[512]; +char cat_buf[1024]; /* Simple shell - (actually this is not even a shell) @@ -55,6 +56,9 @@ sh_printerr() case ENOMEM: printf("Error: Out of memory\n"); break; + case EISDIR: + printf("Error: This is a directory\n"); + break; default: printf("Error: Fail to open (%d)\n", errno); break; @@ -67,9 +71,11 @@ sh_main() char buf[512]; char *cmd, *argpart; + printf("\n Simple shell. Use or to scroll.\n\n"); + while (1) { getcwd(pwd, 512); - printf("%s> ", pwd); + printf("[\033[2m%s\033[39;49m]$ ", pwd); size_t sz = read(stdin, buf, 512); if (sz < 0) { printf("fail to read user input (%d)\n", geterrno()); @@ -77,6 +83,9 @@ sh_main() } buf[sz - 1] = '\0'; parse_cmdline(buf, &cmd, &argpart); + if (cmd[0] == 0) { + goto cont; + } if (streq(cmd, "cd")) { if (chdir(argpart) < 0) { sh_printerr(); @@ -89,7 +98,11 @@ sh_main() struct dirent ent = { .d_offset = 0 }; int status; while ((status = readdir(fd, &ent)) == 1) { - printf(" %s\n", ent.d_name); + if (ent.d_type == DT_DIR) { + printf(" \033[3m%s\033[39;49m\n", ent.d_name); + } else { + printf(" %s\n", ent.d_name); + } } if (status < 0) @@ -97,9 +110,25 @@ sh_main() close(fd); } + } else if (streq(cmd, "cat")) { + int fd = open(argpart, 0); + if (fd < 0) { + sh_printerr(); + } else { + int sz; + while ((sz = read(fd, cat_buf, 1024)) > 0) { + write(stdout, cat_buf, sz); + } + if (sz < 0) { + sh_printerr(); + } + close(fd); + printf("\n"); + } } else { printf("unknow command"); } + cont: printf("\n"); } } \ No newline at end of file