diff --git a/Makefile b/Makefile index dd51736..b33993a 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: ttsubo +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2025/04/03 12:55:20 by ttsubo #+# #+# # -# Updated: 2025/05/04 16:50:59 by ttsubo ### ########.fr # +# Updated: 2025/05/04 19:49:07 by ttsubo ### ########.fr # # # # **************************************************************************** # @@ -60,13 +60,13 @@ endef # 2.Add the source code when it is added ROOT_SRC = main.c minish_signal.c initialize.c error.c -COMMON_SRC = validate_key.c error_mes.c +COMMON_SRC = validate_key.c error_mes.c free_functions.c free_functions_2.c INVOKE_CMD_SRC = create_envp.c exec_pipeline.c execute_cmd.c \ execute_cmd_helper.c invoke_command.c pipeline_helper.c \ redirect.c fds.c wait_pipeline.c TOKENIZER_SRC = tokenizer.c tokenizer_error.c read_token.c \ is_quote_closed.c get_token_capa.c is_redirect_validate.c -PARSER_SRC = allocate_cmds.c parser.c parser_free.c parser_utils.c setup_cmds.c \ +PARSER_SRC = allocate_cmds.c parser.c parser_utils.c setup_cmds.c \ allocate_cmds_utils.c allocate_cmds_utils_2.c \ expand_env.c expand_tokens.c BUILTIN_SRC = cd.c exit.c pwd.c echo.c env.c unset.c export.c \ diff --git a/inc/builtin/env_utils.h b/inc/builtin/env_utils.h index 1a3fad5..86841f9 100644 --- a/inc/builtin/env_utils.h +++ b/inc/builtin/env_utils.h @@ -6,7 +6,7 @@ /* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/17 10:25:47 by dayano #+# #+# */ -/* Updated: 2025/05/04 12:14:29 by ttsubo ### ########.fr */ +/* Updated: 2025/05/04 18:49:36 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,6 @@ void free_env_content(char *value); void remove_env_node(t_env **env_lst, t_env *target); -void free_env_list(t_env *head); void cleanup_minish(t_minish *minish); void handle_error_and_exit(const char *func_name, t_minish *minish); void add_env_back(t_env **lst, t_env *new); diff --git a/inc/common/cmd.h b/inc/common/cmd.h deleted file mode 100644 index d998913..0000000 --- a/inc/common/cmd.h +++ /dev/null @@ -1,38 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* cmd.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: dayano +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/04/13 13:47:36 by ttsubo #+# #+# */ -/* Updated: 2025/04/29 16:53:33 by dayano ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef CMD_H -# define CMD_H - -# include - -typedef enum e_redir_type -{ - REDIR_NONE, - REDIR_OUT, - REDIR_APPEND, - REDIR_IN, - REDIR_HEREDOC -} t_redir_type; - -typedef struct s_cmd -{ - t_redir_type type; - int argc; - char **argv; - int capa; - int status; - pid_t pid; - struct s_cmd *next; -} t_cmd; - -#endif diff --git a/inc/common/common.h b/inc/common/common.h index a7c0d6d..247b40d 100644 --- a/inc/common/common.h +++ b/inc/common/common.h @@ -6,7 +6,7 @@ /* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/29 11:44:19 by ttsubo #+# #+# */ -/* Updated: 2025/05/04 12:18:40 by ttsubo ### ########.fr */ +/* Updated: 2025/05/04 19:17:09 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,10 +16,10 @@ # include # include # include "libft.h" -# include "cmd.h" # include "error.h" # include "initialize.h" # include "struct.h" +# include "free_functions.h" bool is_key_start(int c); bool is_key_char(int c); diff --git a/inc/parser_free.h b/inc/common/free_functions.h similarity index 57% rename from inc/parser_free.h rename to inc/common/free_functions.h index dbd2fe4..3835a29 100644 --- a/inc/parser_free.h +++ b/inc/common/free_functions.h @@ -1,20 +1,27 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* parser_free.h :+: :+: :+: */ +/* free_functions.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/05/02 21:22:56 by ttsubo #+# #+# */ -/* Updated: 2025/05/02 21:27:46 by ttsubo ### ########.fr */ +/* Created: 2025/05/04 17:50:02 by ttsubo #+# #+# */ +/* Updated: 2025/05/04 20:13:57 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef PARSER_FREE_H -# define PARSER_FREE_H +#ifndef FREE_FUNCTIONS_H +# define FREE_FUNCTIONS_H -void free_tokens(char **tokens); -void free_cmd(t_cmd *cmd); -void free_cmds(t_cmd **cmds); +# include "stdlib.h" +# include "struct.h" + +void free_str(char **pstr); +void free_strs(char ***pstrs); +void free_node(t_env **pnode); +void free_nodes(t_env **phead); +void free_cmd(t_cmd **pcmd); +void free_cmds(t_cmd ***pcmds); +void free_prompt(char ***ptokens, t_cmd ***pcmds, char **pline); #endif \ No newline at end of file diff --git a/inc/common/struct.h b/inc/common/struct.h index 33198a4..ba6a5ff 100644 --- a/inc/common/struct.h +++ b/inc/common/struct.h @@ -6,13 +6,24 @@ /* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/15 17:10:22 by dayano #+# #+# */ -/* Updated: 2025/04/29 11:57:58 by ttsubo ### ########.fr */ +/* Updated: 2025/05/04 19:16:10 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef STRUCT_H # define STRUCT_H +# include + +typedef enum e_redir_type +{ + REDIR_NONE, + REDIR_OUT, + REDIR_APPEND, + REDIR_IN, + REDIR_HEREDOC +} t_redir_type; + typedef struct s_env { char *key; @@ -27,4 +38,15 @@ typedef struct s_minish int last_status; } t_minish; +typedef struct s_cmd +{ + t_redir_type type; + int argc; + char **argv; + int capa; + int status; + pid_t pid; + struct s_cmd *next; +} t_cmd; + #endif diff --git a/inc/invoke_cmd/exec_pipeline.h b/inc/invoke_cmd/exec_pipeline.h index d72da6d..a9644fb 100644 --- a/inc/invoke_cmd/exec_pipeline.h +++ b/inc/invoke_cmd/exec_pipeline.h @@ -6,14 +6,13 @@ /* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/21 13:12:27 by dayano #+# #+# */ -/* Updated: 2025/05/04 12:33:03 by ttsubo ### ########.fr */ +/* Updated: 2025/05/04 19:17:55 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef EXEC_PIPELINE_H # define EXEC_PIPELINE_H -# include "cmd.h" # include "common.h" # include "execute_cmd.h" # include "invoke_command.h" diff --git a/inc/invoke_cmd/invoke_command.h b/inc/invoke_cmd/invoke_command.h index 85bb430..7bdd327 100644 --- a/inc/invoke_cmd/invoke_command.h +++ b/inc/invoke_cmd/invoke_command.h @@ -3,17 +3,17 @@ /* ::: :::::::: */ /* invoke_command.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dayano +#+ +:+ +#+ */ +/* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/20 21:21:13 by dayano #+# #+# */ -/* Updated: 2025/05/04 18:32:13 by dayano ### ########.fr */ +/* Updated: 2025/05/04 20:45:51 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef INVOKE_COMMAND_H # define INVOKE_COMMAND_H -# include "cmd.h" +# include "common.h" # include "create_envp.h" # include "exec_pipeline.h" # include "execute_cmd.h" diff --git a/inc/invoke_cmd/pipeline_helper.h b/inc/invoke_cmd/pipeline_helper.h index d147757..c4c7c57 100644 --- a/inc/invoke_cmd/pipeline_helper.h +++ b/inc/invoke_cmd/pipeline_helper.h @@ -3,17 +3,17 @@ /* ::: :::::::: */ /* pipeline_helper.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dayano +#+ +:+ +#+ */ +/* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/24 14:32:27 by dayano #+# #+# */ -/* Updated: 2025/04/28 14:56:23 by dayano ### ########.fr */ +/* Updated: 2025/05/04 19:18:38 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef PIPELINE_HELPER_H # define PIPELINE_HELPER_H -# include "cmd.h" +# include "common.h" # include # include diff --git a/inc/invoke_cmd/redirect.h b/inc/invoke_cmd/redirect.h index bf37425..117cacc 100644 --- a/inc/invoke_cmd/redirect.h +++ b/inc/invoke_cmd/redirect.h @@ -3,17 +3,17 @@ /* ::: :::::::: */ /* redirect.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dayano +#+ +:+ +#+ */ +/* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/28 15:20:03 by dayano #+# #+# */ -/* Updated: 2025/04/28 15:30:35 by dayano ### ########.fr */ +/* Updated: 2025/05/04 19:19:04 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef REDIRECT_H # define REDIRECT_H -# include "cmd.h" +# include "common.h" void redirect(t_cmd *cmd); void redirect_stdout(t_cmd *cmd); diff --git a/inc/parser/parser.h b/inc/parser/parser.h index 46c39aa..f339e03 100644 --- a/inc/parser/parser.h +++ b/inc/parser/parser.h @@ -6,15 +6,14 @@ /* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/21 14:14:59 by ttsubo #+# #+# */ -/* Updated: 2025/05/02 21:25:06 by ttsubo ### ########.fr */ +/* Updated: 2025/05/04 19:19:21 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef PARSER_H # define PARSER_H -# include "cmd.h" -# include "parser_free.h" +# include "common.h" # include "expand_env.h" # include "libft.h" diff --git a/src/builtin/env_utils_2.c b/src/builtin/env_utils_2.c index d93b34b..6f60e1f 100644 --- a/src/builtin/env_utils_2.c +++ b/src/builtin/env_utils_2.c @@ -6,31 +6,17 @@ /* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/17 11:17:22 by dayano #+# #+# */ -/* Updated: 2025/05/01 14:27:15 by ttsubo ### ########.fr */ +/* Updated: 2025/05/04 18:49:19 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ #include "main.h" -void free_env_list(t_env *head) -{ - t_env *next; - - while (head) - { - next = head->next; - free(head->key); - free(head->value); - free(head); - head = next; - } -} - void cleanup_minish(t_minish *minish) { if (!minish) return ; - free_env_list(minish->env); + free_nodes(&minish->env); free(minish); } diff --git a/src/builtin/split_key_value.c b/src/builtin/split_key_value.c index 7be8e20..dd3b757 100644 --- a/src/builtin/split_key_value.c +++ b/src/builtin/split_key_value.c @@ -6,7 +6,7 @@ /* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/01 14:27:33 by ttsubo #+# #+# */ -/* Updated: 2025/05/01 14:54:33 by ttsubo ### ########.fr */ +/* Updated: 2025/05/04 21:03:01 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ @@ -77,7 +77,7 @@ int split_key_value(char *str, char **key_out, char **val_out) { *val_out = _strip_quotes(str, val_start); if (!*val_out) - return (free(*key_out), *key_out = NULL, 1); + return (free_str(key_out), 1); } return (0); } diff --git a/src/common/free_functions.c b/src/common/free_functions.c new file mode 100644 index 0000000..2bae6e2 --- /dev/null +++ b/src/common/free_functions.c @@ -0,0 +1,66 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* free_functions.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ttsubo +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/05/04 17:48:51 by ttsubo #+# #+# */ +/* Updated: 2025/05/04 18:20:48 by ttsubo ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "free_functions.h" + +void free_str(char **pstr) +{ + if (!pstr || !*pstr) + return ; + free(*pstr); + *pstr = NULL; +} + +void free_strs(char ***pstrs) +{ + int i; + char **strs; + + i = 0; + if (!pstrs || !*pstrs) + return ; + strs = *pstrs; + while (strs[i]) + free_str(&strs[i++]); + free(strs); + *pstrs = NULL; +} + +void free_node(t_env **pnode) +{ + t_env *node; + + if (!pnode || !*pnode) + return ; + node = *pnode; + free(node->key); + free(node->value); + free(node); + *pnode = NULL; +} + +void free_nodes(t_env **phead) +{ + t_env *curr; + t_env *next; + + if (!phead || !*phead) + return ; + curr = *phead; + while (curr) + { + next = curr->next; + free_node(&curr); + curr = next; + } + *phead = NULL; +} diff --git a/src/parser/parser_free.c b/src/common/free_functions_2.c similarity index 56% rename from src/parser/parser_free.c rename to src/common/free_functions_2.c index e302337..63616eb 100644 --- a/src/parser/parser_free.c +++ b/src/common/free_functions_2.c @@ -1,46 +1,47 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* parser_free.c :+: :+: :+: */ +/* free_functions_2.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/05/02 20:55:39 by ttsubo #+# #+# */ -/* Updated: 2025/05/02 21:28:08 by ttsubo ### ########.fr */ +/* Created: 2025/05/04 17:48:51 by ttsubo #+# #+# */ +/* Updated: 2025/05/04 20:12:33 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ -#include "parser.h" +#include "free_functions.h" -void free_tokens(char **tokens) +void free_cmd(t_cmd **pcmd) { - size_t token_i; + t_cmd *cmd; - token_i = 0; - while (tokens && tokens[token_i]) - token_i++; - while (0 < token_i) - free(tokens[--token_i]); - free(tokens); -} - -void free_cmd(t_cmd *cmd) -{ - size_t arg_i; - - arg_i = 0; - while (cmd->argv && cmd->argv[arg_i]) - free(cmd->argv[arg_i++]); - free(cmd->argv); + if (!pcmd || !*pcmd) + return ; + cmd = *pcmd; + free_strs(&cmd->argv); free(cmd); + *pcmd = NULL; } -void free_cmds(t_cmd **cmds) +void free_cmds(t_cmd ***pcmds) { size_t cmd_i; + t_cmd **cmds; + if (!pcmds || !*pcmds) + return ; + cmds = *pcmds; cmd_i = 0; while (cmds[cmd_i]) - free_cmd(cmds[cmd_i++]); + free_cmd(&cmds[cmd_i++]); free(cmds); + *pcmds = NULL; +} + +void free_prompt(char ***ptokens, t_cmd ***pcmds, char **pline) +{ + free_strs(ptokens); + free_cmds(pcmds); + free_str(pline); } diff --git a/src/initialize.c b/src/initialize.c index 66362dd..78908b8 100644 --- a/src/initialize.c +++ b/src/initialize.c @@ -6,7 +6,7 @@ /* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/15 15:42:46 by dayano #+# #+# */ -/* Updated: 2025/05/02 14:11:51 by ttsubo ### ########.fr */ +/* Updated: 2025/05/04 18:44:19 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,25 +31,6 @@ static int _set_envp(char *env_val, t_env *node) return (0); } -static void _free_node(t_env *node) -{ - free(node->key); - free(node->value); - free(node); -} - -static void _free_nodes(t_env **head) -{ - t_env *tmp; - - while (*head) - { - tmp = (*head)->next; - _free_node(*head); - *head = tmp; - } -} - /** * @brief Create a envp list object * @@ -72,7 +53,7 @@ static t_env *create_envp_list(char **envp) if (!node) return (NULL); if (_set_envp(envp[i], node)) - return (_free_nodes(&head), _free_node(node), NULL); + return (free_nodes(&head), free_node(&node), NULL); node->next = NULL; if (!head) head = node; diff --git a/src/invoke_cmd/create_envp.c b/src/invoke_cmd/create_envp.c index d3dfa20..85d4cc4 100644 --- a/src/invoke_cmd/create_envp.c +++ b/src/invoke_cmd/create_envp.c @@ -3,31 +3,15 @@ /* ::: :::::::: */ /* create_envp.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dayano +#+ +:+ +#+ */ +/* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/24 13:56:59 by dayano #+# #+# */ -/* Updated: 2025/04/25 21:22:44 by dayano ### ########.fr */ +/* Updated: 2025/05/04 22:09:48 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ #include "main.h" -static void free_envp(char **envp) -{ - int i; - - i = 0; - if (!envp) - return ; - while (envp[i]) - { - if (envp[i]) - free(envp[i]); - i++; - } - free(envp); -} - static int _count_value(t_minish *minish) { int count; @@ -77,7 +61,7 @@ char **create_envp(t_minish *minish) int count; count = _count_value(minish); - envp = (char **)ft_calloc(count, sizeof(char *)); + envp = (char **)ft_calloc(count + 1, sizeof(char *)); if (!envp) return (NULL); env = minish->env; @@ -88,7 +72,7 @@ char **create_envp(t_minish *minish) { envp[i] = _get_envp_key_value(env); if (!envp[i]) - return (free_envp(envp), NULL); + return (free_strs(&envp), NULL); } env = env->next; i++; diff --git a/src/main.c b/src/main.c index 95e9423..de38e17 100644 --- a/src/main.c +++ b/src/main.c @@ -6,26 +6,12 @@ /* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/03 12:50:11 by ttsubo #+# #+# */ -/* Updated: 2025/05/02 21:25:49 by ttsubo ### ########.fr */ +/* Updated: 2025/05/04 21:05:24 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ #include "main.h" -static void _free_tokens(char **tokens) -{ - int i; - - i = 0; - while (tokens[i]) - { - free(tokens[i]); - tokens[i++] = NULL; - } - free(tokens); - tokens = NULL; -} - static void destroy_minish(t_minish *minish) { cleanup_minish(minish); @@ -45,9 +31,11 @@ static bool prompt(char *program_name, t_minish *minish) char **tokens; t_cmd **cmds; + tokens = NULL; + cmds = NULL; line = readline("$ "); if (!line) - return (false); + return (free_prompt(&tokens, &cmds, &line), false); if (line[0] != '\0') add_history(line); tokens = tokenizer(line); @@ -55,14 +43,13 @@ static bool prompt(char *program_name, t_minish *minish) if (!cmds) { error_mes(program_name, ": syntax error\n"); + free_prompt(&tokens, &cmds, &line); cleanup_minish(minish); return (false); } if (cmds[0]->argc > 0) minish->last_status = invoke_commands(cmds[0], minish); - _free_tokens(tokens); - free_cmds(cmds); - free(line); + free_prompt(&tokens, &cmds, &line); return (true); } diff --git a/src/parser/allocate_cmds.c b/src/parser/allocate_cmds.c index 36dab42..2bb7121 100644 --- a/src/parser/allocate_cmds.c +++ b/src/parser/allocate_cmds.c @@ -6,7 +6,7 @@ /* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/24 12:19:28 by ttsubo #+# #+# */ -/* Updated: 2025/05/02 21:26:21 by ttsubo ### ########.fr */ +/* Updated: 2025/05/04 20:10:54 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,12 +26,7 @@ static int _allocate_argv(t_cmd *cmd, char **token_ptr) { cmd->argv[arg_i] = calloc_arg(token_ptr[arg_i]); if (!cmd->argv[arg_i]) - { - while (0 < arg_i) - free(cmd->argv[--arg_i]); - free(cmd->argv); - return (1); - } + return (free_cmd(&cmd), 1); arg_i++; } return (0); @@ -45,7 +40,7 @@ static t_cmd *_allocate_cmd(char **token_ptr) if (!cmd) return (NULL); if (_allocate_argv(cmd, token_ptr)) - return (free(cmd), NULL); + return (free_cmd(&cmd), NULL); return (cmd); } @@ -66,7 +61,7 @@ t_cmd **allocate_cmds(char **tokens) { cmds[cmd_i] = _allocate_cmd(token_ptr); if (!cmds[cmd_i]) - return (free_cmds(cmds), NULL); + return (free_cmds(&cmds), NULL); if (cmd_i > 0) cmds[cmd_i - 1]->next = cmds[cmd_i]; token_ptr = next_cmd_start(token_ptr); diff --git a/src/parser/allocate_cmds_utils.c b/src/parser/allocate_cmds_utils.c index 94cdf87..2625ed6 100644 --- a/src/parser/allocate_cmds_utils.c +++ b/src/parser/allocate_cmds_utils.c @@ -6,7 +6,7 @@ /* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/24 12:19:28 by ttsubo #+# #+# */ -/* Updated: 2025/05/02 18:12:06 by ttsubo ### ########.fr */ +/* Updated: 2025/05/04 22:01:51 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,5 +59,5 @@ char **calloc_argv(char **tokens) char *calloc_arg(char *token) { - return (ft_strdup(token)); + return (ft_calloc(ft_strlen(token) + 1, sizeof(char))); } diff --git a/src/parser/parser.c b/src/parser/parser.c index 6657ade..87ad077 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/21 14:14:32 by ttsubo #+# #+# */ -/* Updated: 2025/05/02 21:27:58 by ttsubo ### ########.fr */ +/* Updated: 2025/05/04 19:20:38 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,10 +28,10 @@ t_cmd **parser(char **tokens, t_minish *minish) return (NULL); cmds = allocate_cmds(expanded_tokens); if (!cmds) - return (free_tokens(expanded_tokens), NULL); + return (free_strs(&expanded_tokens), NULL); cmds = setup_cmds(cmds, expanded_tokens); if (!cmds) - return (free_tokens(expanded_tokens), NULL); - free_tokens(expanded_tokens); + return (free_strs(&expanded_tokens), NULL); + free_strs(&expanded_tokens); return (cmds); } diff --git a/src/parser/setup_cmds.c b/src/parser/setup_cmds.c index c781b34..1d10bd4 100644 --- a/src/parser/setup_cmds.c +++ b/src/parser/setup_cmds.c @@ -6,7 +6,7 @@ /* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/24 13:38:36 by ttsubo #+# #+# */ -/* Updated: 2025/05/02 21:26:54 by ttsubo ### ########.fr */ +/* Updated: 2025/05/04 22:06:35 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,9 +37,10 @@ t_cmd **setup_cmds(t_cmd **cmds, char **tokens) _next_cmd(cmds, &cmd_i, &arg_i, tokens[token_i]); else { - cmds[cmd_i]->argv[arg_i] = ft_strdup(tokens[token_i]); + ft_strlcpy(cmds[cmd_i]->argv[arg_i], tokens[token_i], + ft_strlen(tokens[token_i]) + 1); if (!cmds[cmd_i]->argv[arg_i]) - return (free_cmds(cmds), NULL); + return (free_cmds(&cmds), NULL); arg_i++; } token_i++;