From 442e001dc6a54465cb028af6b15e598fe71ca32e Mon Sep 17 00:00:00 2001 From: Gitea Date: Tue, 1 Dec 2020 20:33:33 -0600 Subject: [PATCH] Fix for GNU project, replace Single Line Comments --- sh.c | 59 +++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/sh.c b/sh.c index 0791282..d56a652 100755 --- a/sh.c +++ b/sh.c @@ -3,11 +3,25 @@ #include #include #include -#include #include #include +#include -#define ARG_SEP " " //Argument separator for commands +/* + Gods damn it, of course we have to have special shit + just for the fucking GNU Project. Fuckers can't let + anything be done to standard unless they fucking want + to or you beg them to let you, can they? + + -Kat +*/ +#ifdef __GLIBC__ +#define __USE_POSIX +#define __USE_POSIX2 +#endif +#include + +#define ARG_SEP " " /* Argument separator for commands */ #define PROMPT " $ " char** create_argv(char* command); @@ -16,12 +30,12 @@ char* prompt(); int cd(char* path); int main() { - char** argv; //The command with arguments + char** argv; /* The command with arguments */ char* user_input; pid_t child_pid; - int stat_loc; //??? (Needed for waitpid) + int stat_loc; /* ??? (Needed for waitpid) */ - signal(SIGINT, SIG_IGN); //Tell vsh to ignore SIGINT (^C) + signal(SIGINT, SIG_IGN); /* Tell vsh to ignore SIGINT (^C) */ while(1) { user_input = prompt(); @@ -31,7 +45,7 @@ int main() { if(cd(&user_input[3]) < 0) { perror(argv[1]); } - //causes the fork to get skipped + /* causes the fork to get skipped */ continue; } else if(strcmp(argv[0], "exit") == 0 || @@ -41,12 +55,23 @@ int main() { child_pid = fork(); if(child_pid < 0) { - //If the child couldn't run, assume no memory - perror("Out of memory"); - exit(1); + /* Could not start child process. Check why and error out. */ + if(errno == EAGAIN) { + perror("Could not execute command: insufficient resources"); + exit(1); + } + else if(errno == ENOMEM) { + /* Is this out of RAM or out of storage? */ + perror("Could not execute command: insufficient storage space"); + exit(1); + } + else { + perror("Could not execute command: unknown failure"); + exit(1); + } } else if(child_pid == 0) { - //If we are the child, allow ^C + /* We are the child, so allow ^C */ signal(SIGINT, SIG_DFL); if(execvp(argv[0], argv) < 0) { perror(argv[0]); @@ -54,6 +79,7 @@ int main() { } } else { + /* Wait for the child to finish */ waitpid(child_pid, &stat_loc, WUNTRACED); } @@ -71,8 +97,7 @@ char** create_argv(char* command) { char** argv = calloc(argc, sizeof(*argv)); if(argv == NULL) { - //If couldn't allocate argv, assume no memory - perror("Out of memory"); + perror("Unable to parse command: could not allocate memory"); exit(1); } @@ -82,7 +107,7 @@ char** create_argv(char* command) { i++; } - //Set last item in argv to NULL, since execvp requires it + /* Set last item in argv to NULL, since execvp requires it */ argv[i] = NULL; return argv; } @@ -94,15 +119,13 @@ char* prompt() { char prompt[PATH_MAX + 5] = ""; strcat(prompt, bname); strcat(prompt, PROMPT); - char* input = calloc(512, sizeof(*input)); + char* input = calloc(LINE_MAX, sizeof(*input)); if(input == NULL) { - //If input allocate failed, assume no memory - perror("Out of memory"); + perror("Unable to read command: could not allocate memory"); exit(1); } printf("%s", prompt); - fgets(input, 512, stdin); - //Is 512 enough? What about really long commands? + fgets(input, LINE_MAX, stdin); input[strcspn(input, "\r\n")] = '\0'; return input; }