improve error handling #8

Merged
cromer merged 1 commits from errorhandling into master 2021-06-27 00:18:41 -04:00
3 changed files with 42 additions and 22 deletions
Showing only changes of commit 14b2c79c92 - Show all commits

View File

@ -35,20 +35,20 @@ void insert_string_array(StringArray *string_array, char *string) {
if (string_array->size == 0) { if (string_array->size == 0) {
string_array->array = malloc(sizeof(char *)); string_array->array = malloc(sizeof(char *));
if (string_array->array == NULL) { if (string_array->array == NULL) {
fprintf(stderr, "malloc failed"); perror("malloc");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
else { else {
string_array->array = realloc(string_array->array, (string_array->size + 1) * sizeof(char *)); string_array->array = realloc(string_array->array, (string_array->size + 1) * sizeof(char *));
if (string_array->array == NULL) { if (string_array->array == NULL) {
fprintf(stderr, "realloc failed"); perror("realloc");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
string_array->array[string_array->size] = malloc(sizeof(string)); string_array->array[string_array->size] = malloc(sizeof(string));
if (string_array->array == NULL) { if (string_array->array == NULL) {
fprintf(stderr, "malloc failed"); perror("malloc");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
strcpy(string_array->array[string_array->size], string); 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. * @param index The index in the String Array to delete.
*/ */
void delete_string_array(StringArray *string_array, int index) { 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++) { for (int i = index; i < string_array->size - 1; i++) {
free(string_array->array[i]); free(string_array->array[i]);
string_array->array[i] = NULL; string_array->array[i] = NULL;
string_array->array[i] = malloc(sizeof(string_array->array[i + 1])); string_array->array[i] = malloc(sizeof(string_array->array[i + 1]));
if (string_array->array[i] == NULL) { if (string_array->array[i] == NULL) {
fprintf(stderr, "malloc failed"); perror("malloc");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
strcpy(string_array->array[i], string_array->array[i + 1]); 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[string_array->size - 1] = NULL;
string_array->array = realloc(string_array->array, (string_array->size - 1) * sizeof(char *)); string_array->array = realloc(string_array->array, (string_array->size - 1) * sizeof(char *));
if (string_array->array == NULL) { if (string_array->array == NULL) {
fprintf(stderr, "realloc failed"); perror("realloc");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
string_array->size--; 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. * @param string_array The String Array to free.
*/ */
void free_string_array(StringArray *string_array) { void free_string_array(StringArray *string_array) {
for (int i = 0; i < string_array->size; i++) { if (string_array->array == NULL) {
if (string_array->array[i] != NULL) { fprintf(stderr, "StringArray is not freeable!\n");
free(string_array->array[i]); free_string_array(string_array);
string_array->array[i] = NULL; 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;
} }

View File

@ -14,6 +14,7 @@
*/ */
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#include "array.h" #include "array.h"
#include "builtins.h" #include "builtins.h"
@ -43,6 +44,11 @@ void run_builtin(StringArray *args) {
if (strcmp(args->array[0], "exit") == 0) { if (strcmp(args->array[0], "exit") == 0) {
exit_shell(args); exit_shell(args);
} }
else {
fprintf(stderr, "Builtin %s does not exist!\n", args->array[0]);
free_string_array(args);
exit(EXIT_FAILURE);
}
} }
/** /**

View File

@ -39,6 +39,10 @@ void remove_new_line(char* line) {
char *get_username() { char *get_username() {
struct passwd *pass; struct passwd *pass;
pass = getpwuid(getuid()); pass = getpwuid(getuid());
if (pass == NULL) {
perror("getpwuid");
exit(EXIT_FAILURE);
}
return pass->pw_name; return pass->pw_name;
} }
@ -51,7 +55,7 @@ char *get_hostname() {
gethostname(hostname, HOST_NAME_MAX + 1); gethostname(hostname, HOST_NAME_MAX + 1);
char *result = malloc((HOST_NAME_MAX + 1) * sizeof(char)); char *result = malloc((HOST_NAME_MAX + 1) * sizeof(char));
if (result == NULL) { if (result == NULL) {
fprintf(stderr, "malloc failed"); perror("malloc");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
strcpy(result, hostname); strcpy(result, hostname);
@ -65,13 +69,11 @@ char *get_hostname() {
char *get_working_directory() { char *get_working_directory() {
char *cwd = NULL; char *cwd = NULL;
cwd = getcwd(NULL, PATH_MAX); cwd = getcwd(NULL, PATH_MAX);
if (cwd != NULL) { if (cwd == NULL) {
return cwd; perror("getcwd");
}
else {
perror("getcwd() error: ");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
return cwd;
} }
/** /**
@ -105,7 +107,7 @@ char *get_console_input() {
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
else { else {
perror("getline() error: "); perror("getline");
if (line != NULL) { if (line != NULL) {
free(line); free(line);
line = NULL; line = NULL;