From c3b2482da0428b479fae48a32ff9d96cbac4e739 Mon Sep 17 00:00:00 2001 From: czjstmax Date: Thu, 19 Mar 2026 00:55:54 +0100 Subject: ver 1.1 - now uses `execvp()` for PATH-awareness fixed a small bug in `cd` built-in Signed-off-by: czjstmax --- nosh.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/nosh.c b/nosh.c index b4ed1aa..0db35df 100644 --- a/nosh.c +++ b/nosh.c @@ -1,5 +1,5 @@ /* nosh - noshell - * precisely 100 lines of pure POSIX and standard C code for thy eyes! + * ~100+ lines of pure POSIX and standard C code for thy eyes! * * von czjstmax, */ @@ -36,8 +36,10 @@ int main(void) // printf("argv[%zu] = %s\n", i, p.we_wordv[i]); // } - if (strlen(line) == 0) - continue; //otherwise.. SIGSEGV! + if (strlen(line) == 0) { + wordfree(&p); + continue; //otherwise.. SIGSEGV + } if (strcmp(p.we_wordv[0], ":q") == 0 || strcmp(p.we_wordv[0], "exit") == 0) { wordfree(&p); free(line); @@ -53,6 +55,11 @@ int main(void) } } else if (strcmp(p.we_wordv[0], "cd") == 0) { + if (p.we_wordc <= 1) { + printf("nsh! cd: no arguments were given.\n"); + wordfree(&p); + continue; + } if (chdir(p.we_wordv[1])) perror("nsh! ERROR: failed to change directory"); } @@ -75,10 +82,8 @@ int main(void) pid_t pid = fork(); char **argv = p.we_wordv; // pray with me now if (pid == 0) { - char path[strlen("/usr/bin/") + strlen(p.we_wordv[0]) + 1]; - sprintf(path, "/usr/bin/%s", p.we_wordv[0]); - execv(path, argv); - perror("nsh! ERROR: failed to execv()"); + execvp(p.we_wordv[0], argv); + perror("nsh! ERROR: failed to execvp()"); _exit(1); } else if (pid > 0) { -- cgit v1.3.1