Browse Source

cleaner exit

master
narnaud 3 years ago
parent
commit
a40e6c2de2
  1. 10
      built-in.c
  2. 6
      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); return (1);
} }
else else
{
chdir(getenv("HOME")); chdir(getenv("HOME"));
}
return (0); 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);
} }

6
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) else if (ft_strncmp(cmd->argv[0], "env", 4) == 0)
return (ft_env(datas, cmd)); return (ft_env(datas, cmd));
else if (ft_strncmp(cmd->argv[0], "exit", 5) == 0) else if (ft_strncmp(cmd->argv[0], "exit", 5) == 0)
return (close_minishell(0)); return (ft_exit(cmd));
else else
return (-1); return (-1);
} }
@ -54,8 +54,6 @@ void exe(t_datas *datas, t_command *cmd)
if (ft_strchr(cmd->argv[0], '/')) if (ft_strchr(cmd->argv[0], '/'))
execve(cmd->argv[0], cmd->argv, datas->envp); execve(cmd->argv[0], cmd->argv, datas->envp);
else
{
path_dirs = ft_split(getenv("PATH"), ':'); path_dirs = ft_split(getenv("PATH"), ':');
while (path_dirs) while (path_dirs)
{ {
@ -65,9 +63,9 @@ void exe(t_datas *datas, t_command *cmd)
ft_memcpy(path + ft_strlen(*path_dirs),"/", 1); ft_memcpy(path + ft_strlen(*path_dirs),"/", 1);
ft_memcpy(path + ft_strlen(*path_dirs) + 1, cmd->argv[0], ft_strlen(cmd->argv[0])); ft_memcpy(path + ft_strlen(*path_dirs) + 1, cmd->argv[0], ft_strlen(cmd->argv[0]));
execve(path, cmd->argv, datas->envp); execve(path, cmd->argv, datas->envp);
free(path);
path_dirs++; path_dirs++;
} }
}
} }
pid_t caller(t_datas *datas, t_command *cmd) pid_t caller(t_datas *datas, t_command *cmd)

5
minishell.c

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

4
minishell.h

@ -52,7 +52,7 @@ int is_empty(char *line);
void handle_status(t_datas *datas, int status); void handle_status(t_datas *datas, int status);
void nothing(int sig_num); void nothing(int sig_num);
void sigs_handler(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 // ----------------------------------Utils_2.c
int replace_var(t_datas *datas, char **line, \ 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); char *expend_str(t_datas *datas, char *line);
// ----------------------------------Builtins.c // ----------------------------------Builtins.c
int close_minishell(int exit_code);
int ft_echo(t_command *cmd); int ft_echo(t_command *cmd);
int ft_pwd(t_command *cmd); int ft_pwd(t_command *cmd);
int ft_cd(t_command *cmd); int ft_cd(t_command *cmd);
int ft_exit(t_datas *datas, t_command *cmd);
// ----------------------------------Env.c // ----------------------------------Env.c
int is_valid_identifier(char *ident); int is_valid_identifier(char *ident);

12
utils.c

@ -50,8 +50,18 @@ void sigs_handler(int sig_num)
return ; 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"); printf("exit\n");
exit(ret_code); exit(ret_code);
} }

Loading…
Cancel
Save