From f01bc708f202c8ffa968457cb98d296c4be32e30 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Tue, 15 Feb 2022 12:24:09 -0500 Subject: [PATCH] Added helpful hints --- cmd/shell.c | 21 +++++++++++++++++++++ src/cmd_utils.c | 18 ++++++++++-------- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/cmd/shell.c b/cmd/shell.c index 4616160..43c5675 100644 --- a/cmd/shell.c +++ b/cmd/shell.c @@ -8,6 +8,8 @@ int main() { int status_return = 0; char *cmd; char **args; + int numFail = 0; + int numSuccess = 0; init_shell(); @@ -18,6 +20,25 @@ int main() { free(cmd); free(args); + + // Check exit status for feedback + if ( status_return > 0 ) { + numFail++; + // Check for fail help messages + if ( numFail == 2 ) { + printf("Have you read the output of the `author` command?\n"); + } else if ( numFail == 10 ) { + printf("Maybe we try typing backwards?\n"); + } + } else if ( status_return == 0 ) { + numSuccess++; + // Check for success help messages + if ( numSuccess == 4 ) { + printf("Might be worth trying to figure out why this is your shell?\n"); + } else if ( numSuccess == 10 ) { + printf("Random trivia: Shells are defined in /etc/passwd\n"); + } + } // else command was NULL and skip } while(status_return != 255); return EXIT_SUCCESS; } diff --git a/src/cmd_utils.c b/src/cmd_utils.c index 734e5c1..03396fd 100644 --- a/src/cmd_utils.c +++ b/src/cmd_utils.c @@ -42,6 +42,7 @@ void init_shell() { "= For more information run help. =\n" "= For information on the author: =\n" "= run author. =\n" + "= READ ME!! =\n" "======================================\n"; clear(); @@ -51,7 +52,6 @@ void init_shell() { } int reverse_exit(char **args) { - printf("Exiting...\n"); return 255; } @@ -62,9 +62,9 @@ int reverse_help(char **args) { char *read_input() { int c, input_len = 0; - char *user_input = malloc((input_len+1) * sizeof(char)); + char *user_input = calloc(0, (input_len+1) * sizeof(char)); - printf(">> "); + printf("$ "); while((c=getchar()) !='\n' && c != EOF) { input_len++; @@ -89,22 +89,24 @@ int reverse_external_execute(char **args) { if (pid == 0) { if(execvp(args[0], args) == -1) { printf("ERROR\n"); + //return 1; } exit(EXIT_FAILURE); } else if(pid < 0) { - printf("ERROR\n"); + printf("PID ERROR\n"); + return 1; } else { do { waitpid(pid, &status, WUNTRACED); } while(!WIFEXITED(status) && !WIFSIGNALED(status)); } - return 1; + return status; } void reverse_command(char *args) { char *temp_string; - temp_string = malloc(strlen(args)); + temp_string = calloc(0, strlen(args)); strcpy(temp_string, args); for(int i = 0; i < strlen(args); i++) { temp_string[i] = args[strlen(args)-(i+1)]; @@ -116,7 +118,7 @@ void reverse_command(char *args) { int reverse_execute(char **args) { if (args[0] == NULL) { - return 1; + return -1; } for(int i = 0; i < builtin_func_num(); i++) { @@ -130,7 +132,7 @@ int reverse_execute(char **args) { char **split_cmd(char *line) { size_t size = 8; int pos = 0; - char **args = malloc(size * sizeof(char*)); + char **args = calloc(0, size * sizeof(char*)); char *arg = NULL; if(line == NULL) {