coreutils

custom mini coreutils
git clone git://git.evenfri.duckdns.org/coreutils.git
Log | Files | Refs | README

commit ce14fd6525d157cccb7e5a0578c9391ea3007cc6
parent b8a3686be2bf0ffa7c79315af97fd7a30d6be74b
Author: evenfri <evenfri256@gmail.com>
Date:   Sun,  9 Aug 2026 02:41:32 +0800

sh/ to sh.c

Diffstat:
Mmkfile | 2+-
Rsh/main.c -> sh.c | 0
Dsh/mkfile | 11-----------
Dsh/sh.c | 70----------------------------------------------------------------------
4 files changed, 1 insertion(+), 82 deletions(-)

diff --git a/mkfile b/mkfile @@ -12,8 +12,8 @@ targ = init \ pwd \ ls \ free \ + sh \ -dirs = sh \ <$PLAN9/src/mkmany <$PLAN9/src/mkdirs diff --git a/sh/main.c b/sh.c diff --git a/sh/mkfile b/sh/mkfile @@ -1,11 +0,0 @@ -<$PLAN9/src/mkhdr - -ld = ld -cflags = -I../libc -lib = ../libc/libc.a - -ofiles = main.$o \ - -targ = sh - -<$PLAN9/src/mkone diff --git a/sh/sh.c b/sh/sh.c @@ -1,70 +0,0 @@ -#include <libc.h> - -#define msg_clear "\033[2J\033[H" - -#define max_line 256 -#define max_args 16 -#define max_cwd 256 - -static char prompt[] = "; "; - -void execcd(const char *path) { - chdir(path); -} - -int main(void) { - char line[max_line]; - char *argv[max_args]; - - while (1) { - write(stdout, prompt, 2); - - ssize_t n = read(stdin, line, max_line - 1); - if (n <= 0) continue; - - line[n] = '\0'; - if (line[n - 1] == '\n') line[n - 1] = '\0'; - - if (strlen(line) == 0) continue; - - if (strcmp(line, "clear") == 0) { - write(stdout, msg_clear, strlen(msg_clear)); - continue; - } - - if (strcmp(line, "exit") == 0) { - break; - } - - int argc = 0; - char *token = strtok(line, " "); - while (token != NULL && argc < max_args - 1) { - argv[argc++] = token; - token = strtok(NULL, " "); - } - argv[argc] = NULL; - - if (argc == 0) continue; - - if (strcmp(line, "cd") == 0) { - execcd(argv[1]); - continue; - } - - int pid = fork(); - if (pid == 0) { - /* child */ - execvp(argv[0], argv); - - write(stderr, "sh: command not found: ", 23); - write(stderr, argv[0], strlen(argv[0])); - write(stderr, "\n", 1); - exit(127); - } else if (pid > 0) { - /* parent */ - int status; - waitpid(pid, &status, 0); - } - } - return 0; -}