Browse Source

leaks blocked

master
narnaud 3 years ago
parent
commit
da54110a37
  1. 8
      README.md
  2. 12
      built-in.c
  3. 2
      lexer.c
  4. 3
      minishell.c
  5. 2
      parser.c
  6. 27
      utils_2.c

8
README.md

@ -2,10 +2,9 @@
--- ---
## TODO : ## TODO :
- free stack, - Protect mallocs,
- protect mallocs, - [WIP]Norminette
- Norminette - [WIP]Free heap
- Finish check_builtin();
- export : edit datas->env - export : edit datas->env
- unset : idem, - unset : idem,
@ -16,3 +15,4 @@
## Issues : ## Issues :
- error using unknow command with valgrind

12
built-in.c

@ -16,20 +16,22 @@ int ft_echo(t_command *cmd)
{ {
int no_nl; int no_nl;
int fd_out; int fd_out;
int i;
if (cmd->fd[1]) if (cmd->fd[1])
fd_out = cmd->fd[1]; fd_out = cmd->fd[1];
else else
fd_out = 1; fd_out = 1;
no_nl = 0; 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; no_nl = 1;
while (*(cmd->argv)) while (cmd->argv[i])
{ {
ft_putstr_fd(*(cmd->argv), fd_out); ft_putstr_fd(cmd->argv[i], fd_out);
if (*(++cmd->argv)) if (cmd->argv[++i])
ft_putstr_fd(" ", fd_out); ft_putstr_fd(" ", fd_out);
} }
} }

2
lexer.c

@ -133,7 +133,7 @@ t_token *lexer(t_datas *datas, char *line)
continue; continue;
if (lex->state != S_QUOTE_ST && *line == '$') if (lex->state != S_QUOTE_ST && *line == '$')
tmp_i = replace_var(datas, &line, tmp, tmp_i); 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); tmp_i = (ft_bzero(tmp, 1024), 0);
else else
tmp[tmp_i++] = *(line++); tmp[tmp_i++] = *(line++);

3
minishell.c

@ -40,6 +40,8 @@ void free_cmd(t_command *cmd)
{ {
int i; int i;
if (!cmd)
return ;
i = 0; i = 0;
while (cmd->argv[i]) while (cmd->argv[i])
{ {
@ -47,6 +49,7 @@ void free_cmd(t_command *cmd)
i++; i++;
} }
free(cmd->argv); free(cmd->argv);
free_cmd(cmd->next);
free(cmd); free(cmd);
} }

2
parser.c

@ -100,7 +100,7 @@ t_command *parser(t_datas *datas, t_token *tok, t_command *prev)
cmd->argc = 0; cmd->argc = 0;
while(tok && tok->type != PIPE) while(tok && tok->type != PIPE)
tok = parse_cmd(datas, tok, cmd); tok = parse_cmd(datas, tok, cmd);
if (tok && tok->type == PIPE) if (tok)
{ {
cmd->next = parser(datas, tok->next, cmd); cmd->next = parser(datas, tok->next, cmd);
free(tok->value); free(tok->value);

27
utils_2.c

@ -24,32 +24,29 @@ int file_error(char *cmd, char *file, char *msg)
return (0); 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; int i;
char **env; char **env;
char *name; char *name;
char *value; char *value;
i = 1; name = ft_substr(*line, 1, name_len - 1);
while (ft_isalpha((*line)[i]) || ft_isdigit((*line)[i]) || (*line)[i] == '_')
i++;
name = ft_substr(*line, 1, i - 1);
i = 0; i = 0;
*name_len = ft_strlen(name);
value = NULL; value = NULL;
while (datas->envp[i] && !value) while (datas->envp[i] && !value)
{ {
env = ft_split(datas->envp[i], '='); 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]); value = (free(env[0]), env[1]);
else if (++i) else if (++i)
ft_free_split(env); ft_free_split(env);
} }
if (!datas->envp[i]) free(name);
value = strdup(""); if (value)
else
free(env); free(env);
else
value = strdup("");
return (value); return (value);
} }
@ -65,12 +62,18 @@ int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i)
name_len = 2; name_len = 2;
} }
else 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; i = 0;
while (value[i]) while (value[i])
tmp[tmp_i++] = value[i++]; tmp[tmp_i++] = value[i++];
tmp[tmp_i] = 0; tmp[tmp_i] = 0;
*line += name_len + 1; *line += name_len;
free(value); free(value);
return (tmp_i); return (tmp_i);
} }

Loading…
Cancel
Save