fix: exit code works

This commit is contained in:
Camille Chauvet 2023-03-20 15:31:39 +01:00
parent d56aac4b25
commit a58f9a0c76
8 changed files with 37 additions and 18 deletions

0
bozo
View File

View File

@ -1,3 +0,0 @@
hi >> ./bozoman
hi >> ./bozoman
hi

1
env/env_fill.c vendored
View File

@ -86,6 +86,7 @@ char *ft_env_filler(t_data *data, const char *str)
char *temp; char *temp;
char *value; char *value;
ft_gen_exit_code(data);
out = ft_strdup(str); out = ft_strdup(str);
if (out == NULL) if (out == NULL)
{ {

View File

@ -92,8 +92,6 @@ static int ft_cmd_executor(t_data *data, t_cmd *cmd, int fd)
if (exit_code == 1) if (exit_code == 1)
return (1); return (1);
} }
if (ft_gen_exit_code(data))
return (1);
return (0); return (0);
} }

7
main.c
View File

@ -10,6 +10,7 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "data/data.h"
#include "minishell.h" #include "minishell.h"
static char *ft_get_user_input() static char *ft_get_user_input()
@ -59,7 +60,12 @@ static int ft_minishell(t_data *data, char *line)
{ {
content = current->content; content = current->content;
if (content->own_cmd == 0 && content->pid != -1) if (content->own_cmd == 0 && content->pid != -1)
{
waitpid(content->pid, &data->exit_code, 0); waitpid(content->pid, &data->exit_code, 0);
data->exit_code = data->exit_code / 256;
}
else
data->exit_code = 0;
current = current->next; current = current->next;
} }
ft_lstclear(data->cmds, ft_cmddel); ft_lstclear(data->cmds, ft_cmddel);
@ -103,7 +109,6 @@ int main(int ac, char **av, char **env)
signal(SIGQUIT, ft_quit); signal(SIGQUIT, ft_quit);
data.exit_code = 0; data.exit_code = 0;
data.exit_code_str = NULL; data.exit_code_str = NULL;
ft_gen_exit_code(&data);
data.cmds = malloc(sizeof(t_cmd *)); data.cmds = malloc(sizeof(t_cmd *));
if (data.cmds == NULL) if (data.cmds == NULL)
return (1); return (1);

View File

@ -45,10 +45,14 @@ static int ft_executable_parse(t_data *data, t_cmd *cmd)
else if (ft_strcmp(cmd->args[0], "cd") == 0) else if (ft_strcmp(cmd->args[0], "cd") == 0)
own = 1; own = 1;
else else
path = ft_get_executable(data->env, cmd->args[0]); {
path = ft_get_executable(data, cmd->args[0]);
if (path == NULL)
return (1);
}
cmd->own_cmd = own; cmd->own_cmd = own;
cmd->executable = path; cmd->executable = path;
return (own); return (0);
} }
int ft_cmd_parser(t_data *data, char *cmd_str) int ft_cmd_parser(t_data *data, char *cmd_str)
@ -65,14 +69,18 @@ int ft_cmd_parser(t_data *data, char *cmd_str)
if (ft_redirection(data, cmd, cmd_str)) if (ft_redirection(data, cmd, cmd_str))
{ {
ft_cmddel(cmd); ft_cmddel(cmd);
return (1); return (0);
} }
if (ft_args_parse(cmd_str, cmd)) if (ft_args_parse(cmd_str, cmd))
{ {
ft_cmddel(cmd); ft_cmddel(cmd);
return (1); return (1);
} }
ft_executable_parse(data, cmd); if (ft_executable_parse(data, cmd))
{
ft_cmddel(cmd);
return (1);
}
element = ft_lstnew(cmd); element = ft_lstnew(cmd);
if (element == NULL) if (element == NULL)
{ {
@ -98,7 +106,11 @@ int ft_cmds_parser(t_data *data, const char *line)
i = 0; i = 0;
while (tab[i] != NULL) while (tab[i] != NULL)
{ {
ft_cmd_parser(data, tab[i]); if (ft_cmd_parser(data, tab[i]))
{
ft_freer_tab_ultimate(1, tab);
return (1);
}
i++; i++;
} }
if (*data->cmds != NULL) if (*data->cmds != NULL)

View File

@ -1,17 +1,19 @@
#include "./utils.h" #include "./utils.h"
#include <unistd.h> #include <unistd.h>
char *ft_get_executable_with_path(const char *name) char *ft_get_executable_with_path(t_data *data, const char *name)
{ {
char *path; char *path;
if (access(name, F_OK) != 0) if (access(name, F_OK) != 0)
{ {
data->exit_code = 127;
ft_eprintf("minishell: %s bash: No such file or directery\n"); ft_eprintf("minishell: %s bash: No such file or directery\n");
return (NULL); return (NULL);
} }
if (access(name, X_OK) != 0) if (access(name, X_OK) != 0)
{ {
data->exit_code = 126;
ft_eprintf("minishell: %s: permission denied\n", name); ft_eprintf("minishell: %s: permission denied\n", name);
return (NULL); return (NULL);
} }
@ -24,7 +26,7 @@ char *ft_get_executable_with_path(const char *name)
return (path); return (path);
} }
char *ft_get_executable_without_path(t_list **env, const char *name) char *ft_get_executable_without_path(t_data *data, const char *name)
{ {
char **tab; char **tab;
char *paths; char *paths;
@ -32,9 +34,10 @@ char *ft_get_executable_without_path(t_list **env, const char *name)
size_t i; size_t i;
path = NULL; path = NULL;
paths = get_value_by_key("PATH", env); paths = get_value_by_key("PATH", data->env);
if (paths == NULL) if (paths == NULL)
{ {
data->exit_code = 127;
ft_eprintf("minishell: %s: command not found\n", name); ft_eprintf("minishell: %s: command not found\n", name);
return (NULL); return (NULL);
} }
@ -61,17 +64,20 @@ char *ft_get_executable_without_path(t_list **env, const char *name)
} }
ft_freer_tab_ultimate(1, tab); ft_freer_tab_ultimate(1, tab);
if (path == NULL) if (path == NULL)
{
data->exit_code = 127;
ft_eprintf("minishell: %s: command not found\n", name); ft_eprintf("minishell: %s: command not found\n", name);
}
return (path); return (path);
} }
char *ft_get_executable(t_list **env, const char *name) char *ft_get_executable(t_data *data, const char *name)
{ {
char *path; char *path;
if (name[0] == '.' || name[0] == '/') if (name[0] == '.' || name[0] == '/')
path = ft_get_executable_with_path(name); path = ft_get_executable_with_path(data, name);
else else
path = ft_get_executable_without_path(env, name); path = ft_get_executable_without_path(data, name);
return (path); return (path);
} }

View File

@ -28,7 +28,7 @@ char **ft_split_quoted(const char *s, char c);
void ft_strshift(char *str, int shift); void ft_strshift(char *str, int shift);
char *ft_quote_remover(char *str); char *ft_quote_remover(char *str);
int ft_atoi_check(const char *nptr); int ft_atoi_check(const char *nptr);
char *ft_get_executable(t_list **env, const char *name); char *ft_get_executable(t_data *data, const char *name);
void ft_closer(int fds[2]); void ft_closer(int fds[2]);
void ft_add_fd(int fds[2], int fd); void ft_add_fd(int fds[2], int fd);