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

View File

@ -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,6 +93,12 @@ void delete_string_array(StringArray *string_array, int index) {
* @param string_array The String Array to free.
*/
void free_string_array(StringArray *string_array) {
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]);
@ -99,4 +110,5 @@ void free_string_array(StringArray *string_array) {
string_array->array = NULL;
}
string_array->size = 0;
}
}

View File

@ -14,6 +14,7 @@
*/
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#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);
}
}
/**

View File

@ -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;