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
}
],
"preLaunchTask": "Build minishell",
"preLaunchTask": "Build minishell debug",
"miDebuggerPath": "/usr/bin/gdb",
"logging": {
"engineLogging": true

12
.vscode/tasks.json vendored
View File

@ -13,6 +13,18 @@
"problemMatcher": ["$gcc"],
"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",
"type": "shell",

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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);
char **environment_get_arr(t_minishell *minishell);
void environment_del(t_minishell *minishell, char *name);
void environment_free(void *content);
void environment_free(t_minishell *minishell, void *content);
#endif // ENVIRONMENT_H

View File

@ -14,7 +14,7 @@
# define UTILS_H
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);
t_minishell *init_minishell(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_strjoin_safe(t_minishell *minishell, const char *s1, const char *s2);
void *malloc_safe(t_minishell *minishell, size_t size);
void free_safe(t_minishell *minishell, void **ptr);
#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 \
free_token_list.c free_minishell.c free_minishell_line.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 \
free_freelist.c lexer_read_char.c lexer_token_next.c \
ft_strdup_safe.c ft_strjoin_safe.c print_banner.c free_freelist.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 \
token_parse.c environment_free.c environment_del.c \
environment_free_list.c environment_get.c environment_parse.c \

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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]);
}
free_minishell(minishell);
//free_minishell(&minishell);
exit(exit_status);
return (FAILURE);
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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]);
if (env != NULL)
{
free(env->value);
free_safe(minishell, (void **)&(env->value));
env->value = ft_strdup(arr[1]); //TODO: malloc check
}
else

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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;
cwd = getcwd(NULL, 0);
printf("%s\n", cwd);
free(cwd);
free_safe(minishell, (void **)&(cwd));
return (SUCCESS);
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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->value == NULL)
{
environment_free(new_environment);
environment_free(minishell, new_environment);
return (perror("malloc"));
}
ft_lstadd_back(environment, new_node);

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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)
{
next = current->next;
environment_free(current->content);
free(current);
environment_free(minishell, current->content);
free_safe(minishell, (void **)&(current));
if (prev == NULL)
minishell->environment = next;
else

View File

@ -6,18 +6,18 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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"
void environment_free(void *content)
void environment_free(t_minishell *minishell, void *content)
{
t_environment *env;
env = (t_environment *)content;
free(env->name);
free(env->value);
free(env);
free_safe(minishell, (void **)&(env->name));
free_safe(minishell, (void **)&(env->value));
free_safe(minishell, (void **)&(env));
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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)
{
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> +#+ */
/* +#+ */
/* 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);
return (executable);
}
free(executable);
free_safe(minishell, (void **)&executable);
i++;
}
return (ft_free_arr(path), NULL);

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* 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)
return (NULL);
env = environment_get(minishell, name);
free(name);
free_safe(minishell, (void **)&name);
return (env);
}

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* 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);
else
{
free(word);
free_safe(minishell, (void **)&word);
return (NULL);
}
return (word);

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* 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)
return (token_new(minishell, T_ERROR, &lexer->current_char, current_pos));
token = token_new(minishell, word_type, word, current_pos);
free(word);
free_safe(minishell, (void **)&word);
}
else
{

View File

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

View File

@ -6,7 +6,7 @@
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* 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
ft_strlcpy(result, cwd, len);
free(cwd);
free_safe(minishell, (void **)&cwd);
return (result);
}
@ -111,11 +111,11 @@ char *ft_prompt(t_minishell *minishell)
perror("malloc");
return (free(cwd), free(user), NULL);
}
free(user);
free(cwd);
free_safe(minishell, (void **)&user);
free_safe(minishell, (void **)&cwd);
rl_on_new_line();
line = readline(prompt);
free(prompt);
free_safe(minishell, (void **)&prompt);
if (line == NULL)
return (NULL);
history_write(line);

View File

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

View File

@ -6,17 +6,18 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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"
void free_minishell(t_minishell *minishell)
void free_minishell(t_minishell **minishell)
{
if (minishell->line)
free_minishell_line(minishell);
if (minishell->environment)
environment_free_list(minishell);
if ((*minishell)->line)
free_minishell_line(*minishell);
if ((*minishell)->environment)
environment_free_list(*minishell);
free(minishell);
*minishell = NULL;
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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)
{
if (minishell->line)
free(minishell->line);
free_safe(minishell, (void **)&(minishell->line));
if (minishell->lexer)
ft_lexer_free(minishell->lexer);
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> +#+ */
/* +#+ */