diff --git a/src/array.c b/src/array.c index 1cd8eb1..5e41e1c 100644 --- a/src/array.c +++ b/src/array.c @@ -35,20 +35,20 @@ void insert_string_array(StringArray *string_array, char *string) { if (string_array->size == 0) { string_array->array = malloc(sizeof(char *)); if (string_array->array == NULL) { - fprintf(stderr, "malloc failed"); + perror("malloc"); exit(EXIT_FAILURE); } } else { string_array->array = realloc(string_array->array, (string_array->size + 1) * sizeof(char *)); if (string_array->array == NULL) { - fprintf(stderr, "realloc failed"); + perror("realloc"); exit(EXIT_FAILURE); } } string_array->array[string_array->size] = malloc(sizeof(string)); if (string_array->array == NULL) { - fprintf(stderr, "malloc failed"); + perror("malloc"); exit(EXIT_FAILURE); } strcpy(string_array->array[string_array->size], string); @@ -61,13 +61,13 @@ void insert_string_array(StringArray *string_array, char *string) { * @param index The index in the String Array to delete. */ void delete_string_array(StringArray *string_array, int index) { - if (string_array->size > 0 && string_array->size > index) { + if (string_array->array != NULL && string_array->size > 0 && string_array->size > index) { for (int i = index; i < string_array->size - 1; i++) { free(string_array->array[i]); string_array->array[i] = NULL; string_array->array[i] = malloc(sizeof(string_array->array[i + 1])); if (string_array->array[i] == NULL) { - fprintf(stderr, "malloc failed"); + perror("malloc"); exit(EXIT_FAILURE); } strcpy(string_array->array[i], string_array->array[i + 1]); @@ -76,11 +76,16 @@ void delete_string_array(StringArray *string_array, int index) { string_array->array[string_array->size - 1] = NULL; string_array->array = realloc(string_array->array, (string_array->size - 1) * sizeof(char *)); if (string_array->array == NULL) { - fprintf(stderr, "realloc failed"); + perror("realloc"); exit(EXIT_FAILURE); } string_array->size--; } + else { + fprintf(stderr, "StringArray index doesn't exist!\n"); + free_string_array(string_array); + exit(EXIT_FAILURE); + } } /** @@ -88,15 +93,22 @@ void delete_string_array(StringArray *string_array, int index) { * @param string_array The String Array to free. */ void free_string_array(StringArray *string_array) { - for (int i = 0; i < string_array->size; i++) { - if (string_array->array[i] != NULL) { - free(string_array->array[i]); - string_array->array[i] = NULL; + if (string_array->array == NULL) { + fprintf(stderr, "StringArray is not freeable!\n"); + free_string_array(string_array); + exit(EXIT_FAILURE); + } + else { + for (int i = 0; i < string_array->size; i++) { + if (string_array->array[i] != NULL) { + free(string_array->array[i]); + string_array->array[i] = NULL; + } } + if (string_array->array != NULL) { + free(string_array->array); + string_array->array = NULL; + } + string_array->size = 0; } - if (string_array->array != NULL) { - free(string_array->array); - string_array->array = NULL; - } - string_array->size = 0; } diff --git a/src/builtins.c b/src/builtins.c index a677774..94045c6 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -14,6 +14,7 @@ */ #include +#include #include #include "array.h" #include "builtins.h" @@ -43,6 +44,11 @@ void run_builtin(StringArray *args) { if (strcmp(args->array[0], "exit") == 0) { exit_shell(args); } + else { + fprintf(stderr, "Builtin %s does not exist!\n", args->array[0]); + free_string_array(args); + exit(EXIT_FAILURE); + } } /** diff --git a/src/console_line.c b/src/console_line.c index 3c1413c..3904f47 100644 --- a/src/console_line.c +++ b/src/console_line.c @@ -39,6 +39,10 @@ void remove_new_line(char* line) { char *get_username() { struct passwd *pass; pass = getpwuid(getuid()); + if (pass == NULL) { + perror("getpwuid"); + exit(EXIT_FAILURE); + } return pass->pw_name; } @@ -51,7 +55,7 @@ char *get_hostname() { gethostname(hostname, HOST_NAME_MAX + 1); char *result = malloc((HOST_NAME_MAX + 1) * sizeof(char)); if (result == NULL) { - fprintf(stderr, "malloc failed"); + perror("malloc"); exit(EXIT_FAILURE); } strcpy(result, hostname); @@ -65,13 +69,11 @@ char *get_hostname() { char *get_working_directory() { char *cwd = NULL; cwd = getcwd(NULL, PATH_MAX); - if (cwd != NULL) { - return cwd; - } - else { - perror("getcwd() error: "); + if (cwd == NULL) { + perror("getcwd"); exit(EXIT_FAILURE); } + return cwd; } /** @@ -105,7 +107,7 @@ char *get_console_input() { exit(EXIT_SUCCESS); } else { - perror("getline() error: "); + perror("getline"); if (line != NULL) { free(line); line = NULL;