Skip to content

Commit

Permalink
fix: handle EXIT_FAILURE by printing error message with perror
Browse files Browse the repository at this point in the history
  • Loading branch information
theoludwig committed Aug 6, 2023
1 parent 72645da commit 8d984e0
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/array_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <stdbool.h>
#include <stdlib.h>
#include <errno.h>

#include "types.h"

Expand Down
1 change: 1 addition & 0 deletions lib/character.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __LIBCPROJECT_CHARACTER__
#define __LIBCPROJECT_CHARACTER__

#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>

Expand Down
2 changes: 2 additions & 0 deletions lib/convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
string_t convert_character_to_string(const char character) {
string_t string = malloc(sizeof(char) * 2);
if (string == NULL) {
perror("Error (convert_character_to_string)");
exit(EXIT_FAILURE);
}
string[0] = character;
Expand Down Expand Up @@ -44,6 +45,7 @@ string_t convert_number_to_string(const long long integer) {
}
string_t string = malloc(sizeof(char) * length);
if (string == NULL) {
perror("Error (convert_number_to_string)");
exit(EXIT_FAILURE);
}
current = mathematics_absolute_value(integer);
Expand Down
1 change: 1 addition & 0 deletions lib/convert.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __LIBCPROJECT_CONVERT__
#define __LIBCPROJECT_CONVERT__

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

Expand Down
1 change: 1 addition & 0 deletions lib/hash_map.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __LIBCPROJECT_HASH_MAP__
#define __LIBCPROJECT_HASH_MAP__

#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
Expand Down
19 changes: 17 additions & 2 deletions lib/linked_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
struct linked_list *linked_list_initialization() {
struct linked_list *list = malloc(sizeof(*list));
if (list == NULL) {
perror("Error (linked_list_initialization)");
exit(EXIT_FAILURE);
}
list->head = NULL;
Expand All @@ -11,8 +12,14 @@ struct linked_list *linked_list_initialization() {
}

struct linked_list_node *linked_list_add_in_head(struct linked_list *list, void *new_data) {
if (list == NULL) {
errno = EINVAL;
perror("Error (linked_list_add_in_head)");
exit(EXIT_FAILURE);
}
struct linked_list_node *node_new = malloc(sizeof(*node_new));
if (list == NULL || node_new == NULL) {
if (node_new == NULL) {
perror("Error (linked_list_add_in_head)");
exit(EXIT_FAILURE);
}
node_new->data = new_data;
Expand All @@ -24,6 +31,8 @@ struct linked_list_node *linked_list_add_in_head(struct linked_list *list, void

void linked_list_delete_in_head(struct linked_list *list) {
if (list == NULL) {
errno = EINVAL;
perror("Error (linked_list_delete_in_head)");
exit(EXIT_FAILURE);
}
if (list->head != NULL) {
Expand All @@ -35,11 +44,17 @@ void linked_list_delete_in_head(struct linked_list *list) {
}

struct linked_list_node *linked_list_add_after_last(struct linked_list *list, void *new_data) {
if (list == NULL) {
errno = EINVAL;
perror("Error (linked_list_add_after_last)");
exit(EXIT_FAILURE);
}
if (list->head == NULL) {
return linked_list_add_in_head(list, new_data);
}
struct linked_list_node *node_new = malloc(sizeof(*node_new));
if (list == NULL || node_new == NULL) {
if (node_new == NULL) {
perror("Error (linked_list_add_after_last)");
exit(EXIT_FAILURE);
}
node_new->data = new_data;
Expand Down
1 change: 1 addition & 0 deletions lib/linked_list.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __LIBCPROJECT_LINKED_LIST__
#define __LIBCPROJECT_LINKED_LIST__

#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>

Expand Down
1 change: 1 addition & 0 deletions lib/mathematics.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#define MATHEMATICS_FLOAT_PRECISION 0.00000001

#include <errno.h>
#include <stdbool.h>

#include "types.h"
Expand Down
13 changes: 12 additions & 1 deletion lib/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
struct queue *queue_initialization() {
struct queue *queue = malloc(sizeof(*queue));
if (queue == NULL) {
perror("Error (queue_initialization)");
exit(EXIT_FAILURE);
}
queue->first = NULL;
Expand All @@ -11,8 +12,14 @@ struct queue *queue_initialization() {
}

void queue_push(struct queue *queue, void *data) {
if (queue == NULL) {
errno = EINVAL;
perror("Error (queue_push)");
exit(EXIT_FAILURE);
}
struct queue_node *node_new = malloc(sizeof(*node_new));
if (queue == NULL || node_new == NULL) {
if (node_new == NULL) {
perror("Error (queue_push)");
exit(EXIT_FAILURE);
}
node_new->data = data;
Expand All @@ -31,6 +38,8 @@ void queue_push(struct queue *queue, void *data) {

void *queue_pop(struct queue *queue) {
if (queue == NULL) {
errno = EINVAL;
perror("Error (queue_pop)");
exit(EXIT_FAILURE);
}
struct queue_node *node = queue->first;
Expand All @@ -46,6 +55,8 @@ void *queue_pop(struct queue *queue) {

void queue_free(struct queue *queue) {
if (queue == NULL) {
errno = EINVAL;
perror("Error (queue_free)");
exit(EXIT_FAILURE);
}
struct queue_node *node = queue->first;
Expand Down
1 change: 1 addition & 0 deletions lib/queue.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __LIBCPROJECT_QUEUE__
#define __LIBCPROJECT_QUEUE__

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

Expand Down
13 changes: 12 additions & 1 deletion lib/stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
struct stack *stack_initialization() {
struct stack *stack = malloc(sizeof(*stack));
if (stack == NULL) {
perror("Error (stack_initialization)");
exit(EXIT_FAILURE);
}
stack->first = NULL;
Expand All @@ -11,8 +12,14 @@ struct stack *stack_initialization() {
}

void stack_push(struct stack *stack, void *data) {
if (stack == NULL) {
errno = EINVAL;
perror("Error (stack_push)");
exit(EXIT_FAILURE);
}
struct stack_node *node_new = malloc(sizeof(*node_new));
if (stack == NULL || data == NULL) {
if (data == NULL) {
perror("Error (stack_push)");
exit(EXIT_FAILURE);
}
node_new->data = data;
Expand All @@ -23,6 +30,8 @@ void stack_push(struct stack *stack, void *data) {

void *stack_pop(struct stack *stack) {
if (stack == NULL) {
errno = EINVAL;
perror("Error (stack_pop)");
exit(EXIT_FAILURE);
}
struct stack_node *node = stack->first;
Expand All @@ -38,6 +47,8 @@ void *stack_pop(struct stack *stack) {

void stack_free(struct stack *stack) {
if (stack == NULL) {
errno = EINVAL;
perror("Error (stack_free)");
exit(EXIT_FAILURE);
}
struct stack_node *node = stack->first;
Expand Down
1 change: 1 addition & 0 deletions lib/stack.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __LIBCPROJECT_STACK__
#define __LIBCPROJECT_STACK__

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

Expand Down
9 changes: 8 additions & 1 deletion lib/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ string_t string_copy(const string_t string) {
size_t source_length = string_get_length(string);
string_t copy = malloc(sizeof(char) * (source_length + 1));
if (copy == NULL) {
perror("Error (string_copy)");
exit(EXIT_FAILURE);
}
size_t index;
Expand Down Expand Up @@ -150,13 +151,15 @@ string_t* string_split(const string_t string, char separator, size_t* result_siz
string_t current = malloc(sizeof(char) * (string_length + 1));
string_t* result = NULL;
if (current == NULL) {
perror("Error (string_split)");
exit(EXIT_FAILURE);
}
while (index_string < string_length) {
if (string[index_string] == separator) {
current[index_current] = '\0';
result = realloc(result, sizeof(string_t) * (index_result + 1));
if (result == NULL) {
perror("Error (string_split)");
exit(EXIT_FAILURE);
}
result[index_result] = string_copy(current);
Expand All @@ -168,10 +171,10 @@ string_t* string_split(const string_t string, char separator, size_t* result_siz
}
index_string++;
}

current[index_current] = '\0';
result = realloc(result, sizeof(string_t) * (index_result + 1));
if (result == NULL) {
perror("Error (string_split)");
exit(EXIT_FAILURE);
}
result[index_result] = string_copy(current);
Expand All @@ -188,6 +191,7 @@ string_t string_join(string_t* array, const char separator, size_t array_length)
size_t string_length = total_length + (array_length - 1);
string_t string = malloc(sizeof(char) * (string_length + 1));
if (string == NULL) {
perror("Error (string_join)");
exit(EXIT_FAILURE);
}
size_t current_index = 0;
Expand All @@ -214,6 +218,7 @@ string_t string_concatenate(string_t string1, string_t string2) {
size_t result_length = string1_length + string2_length;
string_t result = malloc(sizeof(char) * (result_length + 1));
if (result == NULL) {
perror("Error (string_concatenate)");
exit(EXIT_FAILURE);
}
size_t index_string1 = 0;
Expand Down Expand Up @@ -293,6 +298,7 @@ string_t string_get_formatted_number(const long long number, string_t separator)
size_t formatted_length = number_string_length + (number_string_length - 1) / 3;
string_t result = malloc(sizeof(char) * (formatted_length + 1));
if (result == NULL) {
perror("Error (string_get_formatted_number)");
exit(EXIT_FAILURE);
}
size_t count = 0;
Expand Down Expand Up @@ -336,6 +342,7 @@ string_t string_get_last_occurence_of_character(const string_t string, char char
}
string_t result = malloc(sizeof(char) * (string_length - index_last_occurrence + 1));
if (result == NULL) {
perror("Error (string_get_last_occurence_of_character)");
exit(EXIT_FAILURE);
}
size_t index_result = 0;
Expand Down
1 change: 1 addition & 0 deletions lib/string.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __LIBCPROJECT_STRING__
#define __LIBCPROJECT_STRING__

#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down
10 changes: 10 additions & 0 deletions lib/terminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ string_t terminal_input() {
size_t length = 1;
string_t string = malloc(length * sizeof(char));
if (string == NULL) {
perror("Error (terminal_input)");
exit(EXIT_FAILURE);
}
*string = '\0';
while ((character = getchar()) != '\n' && character != EOF) {
length++;
string = realloc(string, length * sizeof(char));
if (string == NULL) {
perror("Error (terminal_input)");
exit(EXIT_FAILURE);
}
character_append(string, character);
Expand Down Expand Up @@ -54,6 +56,8 @@ void terminal_print_string(void* value) {

void terminal_print_stack(struct stack* stack, void (*print_element)(void*)) {
if (stack == NULL) {
errno = EINVAL;
perror("Error (terminal_print_stack)");
exit(EXIT_FAILURE);
}
struct stack_node* node_current = stack->first;
Expand All @@ -68,6 +72,8 @@ void terminal_print_stack(struct stack* stack, void (*print_element)(void*)) {

void terminal_print_queue(struct queue* queue, void (*print_element)(void*)) {
if (queue == NULL) {
errno = EINVAL;
perror("Error (terminal_print_queue)");
exit(EXIT_FAILURE);
}
struct queue_node* node_current = queue->first;
Expand All @@ -82,6 +88,8 @@ void terminal_print_queue(struct queue* queue, void (*print_element)(void*)) {

void terminal_print_linked_list(struct linked_list* linked_list, void (*print_element)(void*)) {
if (linked_list == NULL) {
errno = EINVAL;
perror("Error (terminal_print_linked_list)");
exit(EXIT_FAILURE);
}
struct linked_list_node* node_current = linked_list->head;
Expand All @@ -96,6 +104,8 @@ void terminal_print_linked_list(struct linked_list* linked_list, void (*print_el

void terminal_print_hash_map(struct hash_map* hash_map, void (*print_element)(void*)) {
if (hash_map == NULL) {
errno = EINVAL;
perror("Error (terminal_print_hash_map)");
exit(EXIT_FAILURE);
}
printf("{\n");
Expand Down
1 change: 1 addition & 0 deletions lib/terminal.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef __LIBCPROJECT_TERMINAL__
#define __LIBCPROJECT_TERMINAL__

#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
Expand Down

0 comments on commit 8d984e0

Please sign in to comment.