1
1
mirror of https://github.com/theoludwig/libcproject.git synced 2024-11-08 22:31:31 +01:00

fix: handle EXIT_FAILURE by printing error message with perror

This commit is contained in:
Théo LUDWIG 2023-08-07 00:11:07 +02:00
parent 72645da4b2
commit b922fd9cd3
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
16 changed files with 71 additions and 5 deletions

View File

@ -1,6 +1,7 @@
#ifndef __LIBCPROJECT_ARRAY_LIST__
#define __LIBCPROJECT_ARRAY_LIST__
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>

View File

@ -1,6 +1,7 @@
#ifndef __LIBCPROJECT_CHARACTER__
#define __LIBCPROJECT_CHARACTER__
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>

View File

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

View File

@ -1,6 +1,7 @@
#ifndef __LIBCPROJECT_CONVERT__
#define __LIBCPROJECT_CONVERT__
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

View File

@ -1,6 +1,7 @@
#ifndef __LIBCPROJECT_HASH_MAP__
#define __LIBCPROJECT_HASH_MAP__
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

View File

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

View File

@ -1,6 +1,7 @@
#ifndef __LIBCPROJECT_LINKED_LIST__
#define __LIBCPROJECT_LINKED_LIST__
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>

View File

@ -3,6 +3,7 @@
#define MATHEMATICS_FLOAT_PRECISION 0.00000001
#include <errno.h>
#include <stdbool.h>
#include "types.h"

View File

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

View File

@ -1,6 +1,7 @@
#ifndef __LIBCPROJECT_QUEUE__
#define __LIBCPROJECT_QUEUE__
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

View File

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

View File

@ -1,6 +1,7 @@
#ifndef __LIBCPROJECT_STACK__
#define __LIBCPROJECT_STACK__
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

View File

@ -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;
@ -150,6 +151,7 @@ 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) {
@ -157,6 +159,7 @@ string_t* string_split(const string_t string, char separator, size_t* result_siz
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);
@ -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);
@ -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;
@ -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;
@ -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;
@ -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;

View File

@ -1,6 +1,7 @@
#ifndef __LIBCPROJECT_STRING__
#define __LIBCPROJECT_STRING__
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

View File

@ -5,6 +5,7 @@ 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';
@ -12,6 +13,7 @@ string_t terminal_input() {
length++;
string = realloc(string, length * sizeof(char));
if (string == NULL) {
perror("Error (terminal_input)");
exit(EXIT_FAILURE);
}
character_append(string, character);
@ -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;
@ -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;
@ -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;
@ -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");

View File

@ -1,6 +1,7 @@
#ifndef __LIBCPROJECT_TERMINAL__
#define __LIBCPROJECT_TERMINAL__
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>