error_msg and safe strdup strjoin and malloc
This commit is contained in:
parent
a9f356176f
commit
8da0e0a036
@ -6,7 +6,7 @@
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/02/05 16:06:35 by whaffman #+# #+# */
|
||||
/* Updated: 2025/02/05 16:23:21 by whaffman ######## odam.nl */
|
||||
/* Updated: 2025/02/20 18:07:19 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -20,5 +20,10 @@ t_minishell *init_minishell(void);
|
||||
void print_banner(void);
|
||||
void print_list(void *content);
|
||||
void simple_builtins(t_minishell *minishell);
|
||||
void error_msg(char *func, char *msg);
|
||||
void check_malloc(void *ptr);
|
||||
char *ft_strdup_safe(const char *str);
|
||||
char *ft_strjoin_safe(const char *s1, const char *s2);
|
||||
void *ft_malloc_safe(size_t size);
|
||||
|
||||
#endif // UTILS_H
|
||||
|
||||
23
src/utils/check_malloc.c
Normal file
23
src/utils/check_malloc.c
Normal file
@ -0,0 +1,23 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* check_malloc.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/02/20 18:00:10 by whaffman #+# #+# */
|
||||
/* Updated: 2025/02/20 18:01:08 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
|
||||
void check_malloc(void *ptr)
|
||||
{
|
||||
if (ptr == NULL)
|
||||
{
|
||||
error_msg("malloc", "can't allocate memory");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
34
src/utils/error_msg.c
Normal file
34
src/utils/error_msg.c
Normal file
@ -0,0 +1,34 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* error_msg.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/02/20 17:03:13 by whaffman #+# #+# */
|
||||
/* Updated: 2025/02/20 17:59:36 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
|
||||
void error_msg(char *func, char *msg)
|
||||
{
|
||||
if (errno)
|
||||
perror(RED BOLD "minishell" RESET);
|
||||
else
|
||||
{
|
||||
ft_putstr_fd(RED BOLD "minishell" RESET ": ", 2);
|
||||
if (func != NULL)
|
||||
{
|
||||
ft_putstr_fd(func, 2);
|
||||
ft_putstr_fd(": ", 2);
|
||||
}
|
||||
if (msg != NULL)
|
||||
ft_putstr_fd(msg, 2);
|
||||
else
|
||||
ft_putstr_fd("general error", 2);
|
||||
ft_putstr_fd("\n", 2);
|
||||
}
|
||||
}
|
||||
22
src/utils/ft_malloc_safe.c
Normal file
22
src/utils/ft_malloc_safe.c
Normal file
@ -0,0 +1,22 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* ft_malloc_safe.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/02/20 18:06:46 by whaffman #+# #+# */
|
||||
/* Updated: 2025/02/20 18:07:00 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
void *ft_malloc_safe(size_t size)
|
||||
{
|
||||
void *ptr;
|
||||
|
||||
ptr = malloc(size);
|
||||
check_malloc(ptr);
|
||||
return (ptr);
|
||||
}
|
||||
22
src/utils/ft_strdup_safe.c
Normal file
22
src/utils/ft_strdup_safe.c
Normal file
@ -0,0 +1,22 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* ft_strdup_safe.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/02/20 18:01:27 by whaffman #+# #+# */
|
||||
/* Updated: 2025/02/20 18:04:53 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
char *ft_strdup_safe(const char *str)
|
||||
{
|
||||
char *new_str;
|
||||
|
||||
new_str = ft_strdup(str);
|
||||
check_malloc(new_str);
|
||||
return (new_str);
|
||||
}
|
||||
23
src/utils/ft_strjoin_safe.c
Normal file
23
src/utils/ft_strjoin_safe.c
Normal file
@ -0,0 +1,23 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* :::::::: */
|
||||
/* ft_strjoin_safe.c :+: :+: */
|
||||
/* +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
||||
/* +#+ */
|
||||
/* Created: 2025/02/20 18:02:31 by whaffman #+# #+# */
|
||||
/* Updated: 2025/02/20 18:04:23 by whaffman ######## odam.nl */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
char *ft_strjoin_safe(const char *s1, const char *s2)
|
||||
{
|
||||
char *new_str;
|
||||
|
||||
new_str = ft_strjoin(s1, s2);
|
||||
check_malloc(new_str);
|
||||
return (new_str);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user