free_safe working state WIP

This commit is contained in:
whaffman 2025-02-25 15:43:56 +01:00
parent ab72bd5bbb
commit 07f48cfc62
23 changed files with 117 additions and 48 deletions

2
.vscode/launch.json vendored
View File

@ -19,7 +19,7 @@
"ignoreFailures": true "ignoreFailures": true
} }
], ],
"preLaunchTask": "Build minishell", "preLaunchTask": "Build minishell debug",
"miDebuggerPath": "/usr/bin/gdb", "miDebuggerPath": "/usr/bin/gdb",
"logging": { "logging": {
"engineLogging": true "engineLogging": true

12
.vscode/tasks.json vendored
View File

@ -13,6 +13,18 @@
"problemMatcher": ["$gcc"], "problemMatcher": ["$gcc"],
"detail": "Generated task for building minishell" "detail": "Generated task for building minishell"
}, },
{
"label": "Build minishell debug",
"type": "shell",
"command": "make debug",
"args": [],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": ["$gcc"],
"detail": "Generated task for building minishell in debug mode"
},
{ {
"label": "Run minishell", "label": "Run minishell",
"type": "shell", "type": "shell",

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/04 16:26:35 by whaffman #+# #+# */ /* Created: 2025/02/04 16:26:35 by whaffman #+# #+# */
/* Updated: 2025/02/25 13:31:55 by whaffman ######## odam.nl */ /* Updated: 2025/02/25 15:19:01 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,6 +23,6 @@ void environment_free_list(t_minishell *minishell);
int environment_parse(t_minishell *minishell, char **envp); int environment_parse(t_minishell *minishell, char **envp);
char **environment_get_arr(t_minishell *minishell); char **environment_get_arr(t_minishell *minishell);
void environment_del(t_minishell *minishell, char *name); void environment_del(t_minishell *minishell, char *name);
void environment_free(void *content); void environment_free(t_minishell *minishell, void *content);
#endif // ENVIRONMENT_H #endif // ENVIRONMENT_H

View File

@ -14,7 +14,7 @@
# define UTILS_H # define UTILS_H
void free_minishell_line(t_minishell *minishell); void free_minishell_line(t_minishell *minishell);
void free_minishell(t_minishell *minishell); void free_minishell(t_minishell **minishell);
void free_command_list(void *content); void free_command_list(void *content);
t_minishell *init_minishell(void); t_minishell *init_minishell(void);
void print_banner(void); void print_banner(void);
@ -25,5 +25,6 @@ void check_malloc(t_minishell *minishell, void *ptr);
char *ft_strdup_safe(t_minishell *minishell, const char *str); char *ft_strdup_safe(t_minishell *minishell, const char *str);
char *ft_strjoin_safe(t_minishell *minishell, const char *s1, const char *s2); char *ft_strjoin_safe(t_minishell *minishell, const char *s1, const char *s2);
void *malloc_safe(t_minishell *minishell, size_t size); void *malloc_safe(t_minishell *minishell, size_t size);
void free_safe(t_minishell *minishell, void **ptr);
#endif // UTILS_H #endif // UTILS_H

View File

@ -2,8 +2,8 @@ VPATH = src:src/prompt:src/utils:src/lexer:src/token:src/environment:src/executo
SOURCES = history_load.c history_write.c prompt.c free_lexer.c free_token.c \ SOURCES = history_load.c history_write.c prompt.c free_lexer.c free_token.c \
free_token_list.c free_minishell.c free_minishell_line.c \ free_token_list.c free_minishell.c free_minishell_line.c \
init_minishell.c check_malloc.c error_msg.c free_command_list.c \ init_minishell.c check_malloc.c error_msg.c free_command_list.c \
ft_malloc_safe.c ft_strdup_safe.c ft_strjoin_safe.c print_banner.c \ ft_strdup_safe.c ft_strjoin_safe.c print_banner.c free_freelist.c \
free_freelist.c lexer_read_char.c lexer_token_next.c \ malloc_safe.c free_safe.c lexer_read_char.c lexer_token_next.c \
lexer_parse_input.c lexer_new.c lexer_read_word.c token_new.c \ lexer_parse_input.c lexer_new.c lexer_read_word.c token_new.c \
token_parse.c environment_free.c environment_del.c \ token_parse.c environment_free.c environment_del.c \
environment_free_list.c environment_get.c environment_parse.c \ environment_free_list.c environment_get.c environment_parse.c \

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/20 11:32:59 by whaffman #+# #+# */ /* Created: 2025/02/20 11:32:59 by whaffman #+# #+# */
/* Updated: 2025/02/20 15:54:11 by whaffman ######## odam.nl */ /* Updated: 2025/02/25 15:31:16 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -36,7 +36,7 @@ int builtin_exit(t_minishell *minishell, t_command *cmd)
} }
exit_status = ft_atoi(cmd->args[1]); exit_status = ft_atoi(cmd->args[1]);
} }
free_minishell(minishell); //free_minishell(&minishell);
exit(exit_status); exit(exit_status);
return (FAILURE); return (FAILURE);
} }

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/20 11:32:53 by whaffman #+# #+# */ /* Created: 2025/02/20 11:32:53 by whaffman #+# #+# */
/* Updated: 2025/02/25 13:57:08 by whaffman ######## odam.nl */ /* Updated: 2025/02/25 15:17:21 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -31,7 +31,7 @@ int builtin_export(t_minishell *minishell, t_command *cmd)
env = environment_get(minishell, arr[0]); env = environment_get(minishell, arr[0]);
if (env != NULL) if (env != NULL)
{ {
free(env->value); free_safe(minishell, (void **)&(env->value));
env->value = ft_strdup(arr[1]); //TODO: malloc check env->value = ft_strdup(arr[1]); //TODO: malloc check
} }
else else

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/20 11:32:28 by whaffman #+# #+# */ /* Created: 2025/02/20 11:32:28 by whaffman #+# #+# */
/* Updated: 2025/02/20 13:57:01 by whaffman ######## odam.nl */ /* Updated: 2025/02/25 15:17:51 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,6 +20,6 @@ int builtin_pwd(t_minishell *minishell, t_command *cmd)
(void)cmd; (void)cmd;
cwd = getcwd(NULL, 0); cwd = getcwd(NULL, 0);
printf("%s\n", cwd); printf("%s\n", cwd);
free(cwd); free_safe(minishell, (void **)&(cwd));
return (SUCCESS); return (SUCCESS);
} }

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/04 16:13:33 by whaffman #+# #+# */ /* Created: 2025/02/04 16:13:33 by whaffman #+# #+# */
/* Updated: 2025/02/25 14:28:43 by whaffman ######## odam.nl */ /* Updated: 2025/02/25 15:24:34 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -31,7 +31,7 @@ void environment_add(t_minishell *minishell, char *name, char *value)
|| new_environment->name == NULL || new_environment->name == NULL
|| new_environment->value == NULL) || new_environment->value == NULL)
{ {
environment_free(new_environment); environment_free(minishell, new_environment);
return (perror("malloc")); return (perror("malloc"));
} }
ft_lstadd_back(environment, new_node); ft_lstadd_back(environment, new_node);

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/11 14:17:53 by whaffman #+# #+# */ /* Created: 2025/02/11 14:17:53 by whaffman #+# #+# */
/* Updated: 2025/02/25 13:41:06 by whaffman ######## odam.nl */ /* Updated: 2025/02/25 15:19:14 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -28,8 +28,8 @@ void environment_del(t_minishell *minishell, char *name)
if (ft_strncmp(env->name, name, ft_strlen(name) + 1) == 0) if (ft_strncmp(env->name, name, ft_strlen(name) + 1) == 0)
{ {
next = current->next; next = current->next;
environment_free(current->content); environment_free(minishell, current->content);
free(current); free_safe(minishell, (void **)&(current));
if (prev == NULL) if (prev == NULL)
minishell->environment = next; minishell->environment = next;
else else

View File

@ -6,18 +6,18 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/11 15:38:43 by whaffman #+# #+# */ /* Created: 2025/02/11 15:38:43 by whaffman #+# #+# */
/* Updated: 2025/02/11 15:38:48 by whaffman ######## odam.nl */ /* Updated: 2025/02/25 15:18:45 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void environment_free(void *content) void environment_free(t_minishell *minishell, void *content)
{ {
t_environment *env; t_environment *env;
env = (t_environment *)content; env = (t_environment *)content;
free(env->name); free_safe(minishell, (void **)&(env->name));
free(env->value); free_safe(minishell, (void **)&(env->value));
free(env); free_safe(minishell, (void **)&(env));
} }

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/04 16:13:59 by whaffman #+# #+# */ /* Created: 2025/02/04 16:13:59 by whaffman #+# #+# */
/* Updated: 2025/02/25 13:37:21 by whaffman ######## odam.nl */ /* Updated: 2025/02/25 15:24:08 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,5 +14,20 @@
void environment_free_list(t_minishell *minishell) void environment_free_list(t_minishell *minishell)
{ {
ft_lstclear(&(minishell->environment), environment_free); t_list **lst;
t_list *next;
lst = &minishell->environment;
if (!lst)
return ;
while (*lst)
{
if ((*lst)->next)
next = (*lst)->next;
else
next = NULL;
environment_free(minishell, (*lst)->content);
free(*lst);
*lst = next;
}
} }

View File

@ -6,7 +6,7 @@
/* By: willem <willem@student.codam.nl> +#+ */ /* By: willem <willem@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/08 17:00:24 by willem #+# #+# */ /* Created: 2025/02/08 17:00:24 by willem #+# #+# */
/* Updated: 2025/02/25 14:30:18 by whaffman ######## odam.nl */ /* Updated: 2025/02/25 15:26:05 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -46,7 +46,7 @@ char *executor_absolute_path(t_minishell *minishell, char *cmd)
ft_free_arr(path); ft_free_arr(path);
return (executable); return (executable);
} }
free(executable); free_safe(minishell, (void **)&executable);
i++; i++;
} }
return (ft_free_arr(path), NULL); return (ft_free_arr(path), NULL);

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/19 13:59:03 by qmennen #+# #+# */ /* Created: 2025/02/19 13:59:03 by qmennen #+# #+# */
/* Updated: 2025/02/25 13:47:55 by whaffman ######## odam.nl */ /* Updated: 2025/02/25 15:26:36 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,6 +25,6 @@ t_environment *expander_get_var(const char *s, int idx, t_minishell *minishell)
if (!name || !*name) if (!name || !*name)
return (NULL); return (NULL);
env = environment_get(minishell, name); env = environment_get(minishell, name);
free(name); free_safe(minishell, (void **)&name);
return (env); return (env);
} }

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/05 19:03:47 by qmennen #+# #+# */ /* Created: 2025/02/05 19:03:47 by qmennen #+# #+# */
/* Updated: 2025/02/25 14:40:33 by whaffman ######## odam.nl */ /* Updated: 2025/02/25 15:27:01 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -32,7 +32,7 @@ static char *parse_quotes(t_minishell *minishell, t_lexer *lexer)
lexer_readchar(lexer); lexer_readchar(lexer);
else else
{ {
free(word); free_safe(minishell, (void **)&word);
return (NULL); return (NULL);
} }
return (word); return (word);

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/04 16:07:58 by qmennen #+# #+# */ /* Created: 2025/02/04 16:07:58 by qmennen #+# #+# */
/* Updated: 2025/02/25 14:45:28 by whaffman ######## odam.nl */ /* Updated: 2025/02/25 15:27:11 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -65,7 +65,7 @@ t_token *ft_token_next(t_minishell *minishell, t_lexer *lexer)
if (!word) if (!word)
return (token_new(minishell, T_ERROR, &lexer->current_char, current_pos)); return (token_new(minishell, T_ERROR, &lexer->current_char, current_pos));
token = token_new(minishell, word_type, word, current_pos); token = token_new(minishell, word_type, word, current_pos);
free(word); free_safe(minishell, (void **)&word);
} }
else else
{ {

View File

@ -38,6 +38,6 @@ int main(int argc, char **argv, char **envp)
free_minishell_line(minishell); free_minishell_line(minishell);
} }
ft_lstclear(&minishell->commands, free_command_list); ft_lstclear(&minishell->commands, free_command_list);
free_minishell(minishell); free_minishell(&minishell);
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/04 16:13:08 by whaffman #+# #+# */ /* Created: 2025/02/04 16:13:08 by whaffman #+# #+# */
/* Updated: 2025/02/25 14:43:34 by whaffman ######## odam.nl */ /* Updated: 2025/02/25 15:27:33 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -65,7 +65,7 @@ char *get_path_with_home(t_minishell *minishell)
} }
else else
ft_strlcpy(result, cwd, len); ft_strlcpy(result, cwd, len);
free(cwd); free_safe(minishell, (void **)&cwd);
return (result); return (result);
} }
@ -111,11 +111,11 @@ char *ft_prompt(t_minishell *minishell)
perror("malloc"); perror("malloc");
return (free(cwd), free(user), NULL); return (free(cwd), free(user), NULL);
} }
free(user); free_safe(minishell, (void **)&user);
free(cwd); free_safe(minishell, (void **)&cwd);
rl_on_new_line(); rl_on_new_line();
line = readline(prompt); line = readline(prompt);
free(prompt); free_safe(minishell, (void **)&prompt);
if (line == NULL) if (line == NULL)
return (NULL); return (NULL);
history_write(line); history_write(line);

View File

@ -18,7 +18,6 @@ void check_malloc(t_minishell *minishell, void *ptr)
if (ptr == NULL) if (ptr == NULL)
{ {
error_msg("malloc", "can't allocate memory"); error_msg("malloc", "can't allocate memory");
ft_lstclear(&(minishell->freelist), free); ft_lstclear(&(minishell->freelist), free);
exit(1); exit(1);
} }

View File

@ -6,17 +6,18 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/05 16:03:57 by whaffman #+# #+# */ /* Created: 2025/02/05 16:03:57 by whaffman #+# #+# */
/* Updated: 2025/02/25 13:47:23 by whaffman ######## odam.nl */ /* Updated: 2025/02/25 15:30:19 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void free_minishell(t_minishell *minishell) void free_minishell(t_minishell **minishell)
{ {
if (minishell->line) if ((*minishell)->line)
free_minishell_line(minishell); free_minishell_line(*minishell);
if (minishell->environment) if ((*minishell)->environment)
environment_free_list(minishell); environment_free_list(*minishell);
free(minishell); free(minishell);
*minishell = NULL;
} }

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/05 16:01:44 by whaffman #+# #+# */ /* Created: 2025/02/05 16:01:44 by whaffman #+# #+# */
/* Updated: 2025/02/05 16:02:07 by whaffman ######## odam.nl */ /* Updated: 2025/02/25 15:28:18 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,7 +15,7 @@
void free_minishell_line(t_minishell *minishell) void free_minishell_line(t_minishell *minishell)
{ {
if (minishell->line) if (minishell->line)
free(minishell->line); free_safe(minishell, (void **)&(minishell->line));
if (minishell->lexer) if (minishell->lexer)
ft_lexer_free(minishell->lexer); ft_lexer_free(minishell->lexer);
if (minishell->tokens) if (minishell->tokens)

41
src/utils/free_safe.c Normal file
View File

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* free_safe.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/25 15:09:34 by whaffman #+# #+# */
/* Updated: 2025/02/25 15:41:05 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
void free_safe(t_minishell *minishell, void **ptr)
{
t_list *prev;
t_list *current;
prev = NULL;
current = minishell->freelist;
if (*ptr)
{
while (current)
{
if (current->content == *ptr)
{
if (prev)
prev->next = current->next;
else
minishell->freelist = current->next;
free(*ptr);
free(current);
break ;
}
prev = current;
current = current->next;
}
}
*ptr = NULL;
}

View File

@ -1,7 +1,7 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* :::::::: */
/* ft_malloc_safe.c :+: :+: */ /* malloc_safe.c :+: :+: */
/* +:+ */ /* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */