Browse Source

cleaner exit

master
narnaud 3 years ago
parent
commit
a40e6c2de2
  1. 10
      built-in.c
  2. 24
      caller.c
  3. 5
      minishell.c
  4. 4
      minishell.h
  5. 12
      utils.c

10
built-in.c

@ -69,13 +69,15 @@ int ft_cd(t_command *cmd)
return (1);
}
else
{
chdir(getenv("HOME"));
}
return (0);
}
int close_minishell(int exit_code)
int ft_exit(t_datas *datas, t_command *cmd)
{
exit(exit_code);
if (cmd->argc > 1)
halt(datas, EXIT_SUCCESS);
else
halt(datas, ft_atoi(cmd->argv[1]));
return (1);
}

24
caller.c

@ -27,7 +27,7 @@ int builtin_call(t_datas *datas, t_command *cmd)
else if (ft_strncmp(cmd->argv[0], "env", 4) == 0)
return (ft_env(datas, cmd));
else if (ft_strncmp(cmd->argv[0], "exit", 5) == 0)
return (close_minishell(0));
return (ft_exit(cmd));
else
return (-1);
}
@ -54,19 +54,17 @@ void exe(t_datas *datas, t_command *cmd)
if (ft_strchr(cmd->argv[0], '/'))
execve(cmd->argv[0], cmd->argv, datas->envp);
else
path_dirs = ft_split(getenv("PATH"), ':');
while (path_dirs)
{
path_dirs = ft_split(getenv("PATH"), ':');
while (path_dirs)
{
path_length = ft_strlen(*path_dirs) + ft_strlen(cmd->argv[0]) + 2;
path = ft_calloc(path_length, sizeof(char));
ft_memcpy(path, *path_dirs, ft_strlen(*path_dirs));
ft_memcpy(path + ft_strlen(*path_dirs),"/", 1);
ft_memcpy(path + ft_strlen(*path_dirs) + 1, cmd->argv[0], ft_strlen(cmd->argv[0]));
execve(path, cmd->argv, datas->envp);
path_dirs++;
}
path_length = ft_strlen(*path_dirs) + ft_strlen(cmd->argv[0]) + 2;
path = ft_calloc(path_length, sizeof(char));
ft_memcpy(path, *path_dirs, ft_strlen(*path_dirs));
ft_memcpy(path + ft_strlen(*path_dirs),"/", 1);
ft_memcpy(path + ft_strlen(*path_dirs) + 1, cmd->argv[0], ft_strlen(cmd->argv[0]));
execve(path, cmd->argv, datas->envp);
free(path);
path_dirs++;
}
}

5
minishell.c

@ -62,7 +62,7 @@ void prompt(t_datas *datas)
line = readline("$ ");
termios(1);
if (line == NULL)
halt(EXIT_FAILURE);
halt(datas, EXIT_FAILURE);
if (is_empty(line))
return ;
cmd = parser(datas, lexer(datas, line), NULL);
@ -88,6 +88,5 @@ int main(int argc, char **argv, char **envp)
(void)argv;
while (1)
prompt(&datas);
clear_history();
return (EXIT_SUCCESS);
halt(&datas, EXIT_SUCCESS);
}

4
minishell.h

@ -52,7 +52,7 @@ int is_empty(char *line);
void handle_status(t_datas *datas, int status);
void nothing(int sig_num);
void sigs_handler(int sig_num);
void halt(int ret_code);
void halt(t_datas *datas, int ret_code);
// ----------------------------------Utils_2.c
int replace_var(t_datas *datas, char **line, \
@ -61,10 +61,10 @@ int file_error(char *cmd, char *file, char *msg);
char *expend_str(t_datas *datas, char *line);
// ----------------------------------Builtins.c
int close_minishell(int exit_code);
int ft_echo(t_command *cmd);
int ft_pwd(t_command *cmd);
int ft_cd(t_command *cmd);
int ft_exit(t_datas *datas, t_command *cmd);
// ----------------------------------Env.c
int is_valid_identifier(char *ident);

12
utils.c

@ -50,8 +50,18 @@ void sigs_handler(int sig_num)
return ;
}
void halt(int ret_code)
void halt(t_datas *datas, int ret_code)
{
int i;
i = 0;
while (datas->envp[i])
{
free(datas->envp[i]);
i++;
}
free(datas->envp);
clear_history();
printf("exit\n");
exit(ret_code);
}

Loading…
Cancel
Save