From da54110a375b9570cf12dc40d2319b1d3225617a Mon Sep 17 00:00:00 2001 From: narnaud Date: Fri, 6 May 2022 22:22:04 +0200 Subject: [PATCH] leaks blocked --- README.md | 18 +++++++++--------- built-in.c | 12 +++++++----- lexer.c | 2 +- minishell.c | 3 +++ parser.c | 2 +- utils_2.c | 27 +++++++++++++++------------ 6 files changed, 36 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 62423c1..2aa2986 100755 --- a/README.md +++ b/README.md @@ -2,17 +2,17 @@ --- ## TODO : -- free stack, -- protect mallocs, -- Norminette -- Finish check_builtin(); +- Protect mallocs, +- [WIP]Norminette +- [WIP]Free heap -- export : edit datas->env -- unset : idem, -- exit: handle arguments +- export : edit datas->env +- unset : idem, +- exit: handle arguments -- setup correct ext_codes - https://wiki.bash-hackers.org/scripting/basics?s[]=variables#exit_codes +- setup correct ext_codes + https://wiki.bash-hackers.org/scripting/basics?s[]=variables#exit_codes ## Issues : +- error using unknow command with valgrind diff --git a/built-in.c b/built-in.c index 931266d..e22b423 100755 --- a/built-in.c +++ b/built-in.c @@ -16,20 +16,22 @@ int ft_echo(t_command *cmd) { int no_nl; int fd_out; + int i; if (cmd->fd[1]) fd_out = cmd->fd[1]; else fd_out = 1; no_nl = 0; - if (*(++cmd->argv)) + i = 1; + if (cmd->argv[1]) { - if (ft_strncmp(*(cmd->argv), "-n", 3) == 0 && cmd->argv++) + if (ft_strncmp(cmd->argv[1], "-n", 3) == 0 && i++) no_nl = 1; - while (*(cmd->argv)) + while (cmd->argv[i]) { - ft_putstr_fd(*(cmd->argv), fd_out); - if (*(++cmd->argv)) + ft_putstr_fd(cmd->argv[i], fd_out); + if (cmd->argv[++i]) ft_putstr_fd(" ", fd_out); } } diff --git a/lexer.c b/lexer.c index 5a130f9..474a6c8 100755 --- a/lexer.c +++ b/lexer.c @@ -133,7 +133,7 @@ t_token *lexer(t_datas *datas, char *line) continue; if (lex->state != S_QUOTE_ST && *line == '$') tmp_i = replace_var(datas, &line, tmp, tmp_i); - if (check_register(lex, &line, tmp)) + else if (check_register(lex, &line, tmp)) tmp_i = (ft_bzero(tmp, 1024), 0); else tmp[tmp_i++] = *(line++); diff --git a/minishell.c b/minishell.c index a613934..76dea37 100755 --- a/minishell.c +++ b/minishell.c @@ -40,6 +40,8 @@ void free_cmd(t_command *cmd) { int i; + if (!cmd) + return ; i = 0; while (cmd->argv[i]) { @@ -47,6 +49,7 @@ void free_cmd(t_command *cmd) i++; } free(cmd->argv); + free_cmd(cmd->next); free(cmd); } diff --git a/parser.c b/parser.c index 373de7f..9856118 100755 --- a/parser.c +++ b/parser.c @@ -100,7 +100,7 @@ t_command *parser(t_datas *datas, t_token *tok, t_command *prev) cmd->argc = 0; while(tok && tok->type != PIPE) tok = parse_cmd(datas, tok, cmd); - if (tok && tok->type == PIPE) + if (tok) { cmd->next = parser(datas, tok->next, cmd); free(tok->value); diff --git a/utils_2.c b/utils_2.c index 13e19eb..91282d0 100644 --- a/utils_2.c +++ b/utils_2.c @@ -24,32 +24,29 @@ int file_error(char *cmd, char *file, char *msg) return (0); } -char *get_var_value(t_datas *datas, char **line, int *name_len) +char *get_var_value(t_datas *datas, char **line, int name_len) { int i; char **env; char *name; char *value; - i = 1; - while (ft_isalpha((*line)[i]) || ft_isdigit((*line)[i]) || (*line)[i] == '_') - i++; - name = ft_substr(*line, 1, i - 1); + name = ft_substr(*line, 1, name_len - 1); i = 0; - *name_len = ft_strlen(name); value = NULL; while (datas->envp[i] && !value) { env = ft_split(datas->envp[i], '='); - if (!ft_strncmp(name, env[0], *name_len + 1)) + if (!ft_strncmp(name, env[0], name_len + 1)) value = (free(env[0]), env[1]); else if (++i) ft_free_split(env); } - if (!datas->envp[i]) - value = strdup(""); - else + free(name); + if (value) free(env); + else + value = strdup(""); return (value); } @@ -65,12 +62,18 @@ int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i) name_len = 2; } else - value = get_var_value(datas, line, &name_len); + { + name_len = 1; + while (ft_isalpha((*line)[name_len]) \ + || ft_isdigit((*line)[name_len]) || (*line)[name_len] == '_') + name_len++; + value = get_var_value(datas, line, name_len); + } i = 0; while (value[i]) tmp[tmp_i++] = value[i++]; tmp[tmp_i] = 0; - *line += name_len + 1; + *line += name_len; free(value); return (tmp_i); }