Skip to content

Commit

Permalink
tester passe presque
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptiste Renaudon authored and Baptiste Renaudon committed Jan 6, 2023
1 parent e5edfb6 commit 6ee1f67
Show file tree
Hide file tree
Showing 15 changed files with 79 additions and 29 deletions.
2 changes: 1 addition & 1 deletion add_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void add_cmd(t_token *token, t_list **cmds, bool *pb)
if (node->str)
free(node->str);
node->str = ft_strdup(token->arg);
node->fd_in = 0;
node->fd_in = -3;
node->fd_out = 1;
}
else
Expand Down
6 changes: 4 additions & 2 deletions builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ int builtin(char *cmd)
{"echo", "cd", "export", "env", "unset", "exit", "pwd", NULL};

i = 0;
cmd_split = ft_split_sep(cmd, " \t");
while (cmd_split[0] && builtins[i])
cmd_split = ft_split_quotes(cmd, " \t");
while (cmd_split && cmd_split[0] && builtins[i])
{
if (!ft_strncmp(builtins[i], cmd_split[0], ft_strlen(builtins[i]) + 1))
{
Expand Down Expand Up @@ -68,4 +68,6 @@ void call_builtin(int built_in, t_cmd *cmd, int *pipes, int *children_pid)
free(children_pid);
if (cmd->fd_in != -1 && cmd->fd_out != -1)
tab[built_in](cmd->str, fd_out);
else
exit(1);
}
4 changes: 2 additions & 2 deletions cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ bool cd_parent(void)

if (ft_lstsize(*g_glob->cmds) == 1)
{
cmd_split = ft_split_sep(((t_cmd *)(*g_glob->cmds)->content)->str, \
cmd_split = ft_split_quotes(((t_cmd *)(*g_glob->cmds)->content)->str, \
" \t");
if (cmd_split[0] && !ft_strncmp(cmd_split[0], "cd", 3))
if (cmd_split && cmd_split[0] && !ft_strncmp(cmd_split[0], "cd", 3))
{
if (cmd_split[1])
{
Expand Down
4 changes: 2 additions & 2 deletions dollar.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ void expand(t_token *var)
static bool str_is_op(char *needle)
{
int i;
static const char *op_tab[11] = {"<<", ">>", "|", \
">", "<", "$", " ", "\"", "'", "/", NULL};
static const char *op_tab[13] = {"<<", ">>", "|", \
">", "<", "$", " ", "\"", "'", "/", "\t", NULL};

i = 0;
while (op_tab[i])
Expand Down
8 changes: 4 additions & 4 deletions exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ bool exit_parent(void)

if (ft_lstsize(*g_glob->cmds) == 1)
{
cmd_split = ft_split_sep(((t_cmd *)(*g_glob->cmds)->content)->str, \
cmd_split = ft_split_quotes(((t_cmd *)(*g_glob->cmds)->content)->str, \
" \t");
if (cmd_split[0] && !ft_strncmp(cmd_split[0], "exit", 5))
if (cmd_split && cmd_split[0] && !ft_strncmp(cmd_split[0], "exit", 5))
{
ft_putstr_fd("exit\n", 2);
ft_putstr_fd("exit\n", 1);
if (exit_parent_arg(cmd_split))
{
free_tab(cmd_split);
Expand All @@ -67,7 +67,7 @@ void exit_child(char *cmd, int fd_out)
char **cmd_split;

close(fd_out);
cmd_split = ft_split_sep(cmd, " \t");
cmd_split = ft_split_quotes(cmd, " \t");
if (cmd_split[1])
{
if (check_exit_error(cmd_split))
Expand Down
8 changes: 5 additions & 3 deletions export.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ static void export_no_arg(int fd_out)
free_tab(envp_entry_split);
}
free_tab(envp);
g_glob->exit_ret = 0;
}

void export(char *cmd, int fd_out)
Expand Down Expand Up @@ -96,9 +97,11 @@ static void handle_export_add(char *var)
else if (var_split[1][0])
edit_var(plus_char, var, var_split, env_cpy);
free_and_set_exit_ret(var_split, 0);
g_glob->exit_ret = 0;
return ;
}
free_and_set_exit_ret(var_split, 1);
g_glob->exit_ret = 1;
}

bool export_parent(void)
Expand All @@ -108,9 +111,9 @@ bool export_parent(void)

if (ft_lstsize(*g_glob->cmds) == 1)
{
cmd_split = ft_split_sep(((t_cmd *)(*g_glob->cmds)->content)->str, \
cmd_split = ft_split_quotes(((t_cmd *)(*g_glob->cmds)->content)->str, \
" \t");
if (cmd_split[0] && !ft_strncmp(cmd_split[0], "export", 7))
if (cmd_split && cmd_split[0] && !ft_strncmp(cmd_split[0], "export", 7))
{
if (!cmd_split[1])
export_no_arg(((t_cmd *)(*g_glob->cmds)->content)->fd_out);
Expand All @@ -120,7 +123,6 @@ bool export_parent(void)
while (cmd_split[++i])
handle_export_add(cmd_split[i]);
}
g_glob->exit_ret = 0;
free_tab(cmd_split);
return (true);
}
Expand Down
3 changes: 2 additions & 1 deletion find_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ void find_cmd_infile(t_token *token, t_cmd *node, bool *pb)
free(file);
if (!g_glob->sig_int)
*pb = ft_open_in(node, token);
*pb = false;
else
*pb = false;
}

void find_cmd_outfile(t_token *token, t_cmd *node, bool *pb)
Expand Down
2 changes: 1 addition & 1 deletion find_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void get_ops(const char *input, t_list **head)
single_q = !single_q;
else if (input[i] == '$' && !ft_isprint_nospace_nodollar(input[i + 1]))
i++;
if ((!single_q && !double_q) || (input[i] == '$' && double_q))
if (input[i] && (!single_q && !double_q) || (input[i] == '$' && double_q))
i += add_ops(input, head, i);
i++;
}
Expand Down
14 changes: 13 additions & 1 deletion get_sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ void get_sum(char *cmd, char **ret, int *pipes)
close(pipefd[0]);
}

bool is_dir(char *cmd)
{
DIR *dir;

dir = opendir(cmd);
if (dir == NULL)
return (false);
closedir(dir);
return (true);
}

static void sha1sum_child(char *cmd, int *pipefd, int *pipes)
{
int i;
Expand All @@ -52,7 +63,8 @@ static void sha1sum_child(char *cmd, int *pipefd, int *pipes)
close(pipefd[1]);
envp = envp_list_to_tab();
clean_exit(NULL);
if (!access(cmd, X_OK) && execve(exec_args[0], exec_args, envp) == -1)
if (!is_dir(cmd) && !access(cmd, X_OK) \
&& execve(exec_args[0], exec_args, envp) == -1)
perror("minishell");
exit(1);
}
2 changes: 1 addition & 1 deletion minishell.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void event_loop(void)
input = readline("minishell> ");
if (input == NULL)
{
ft_putstr_fd("exit\n", 2);
ft_putstr_fd("exit\n", 1);
break ;
}
if (*input == '\0')
Expand Down
5 changes: 4 additions & 1 deletion minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ void eof_limiter_not_found(char *here_doc_entry, char *limiter);
void exit_child(char *cmd, int fd_out);
void exit_on_bad_cmd(char **cmd_split, \
int *pipes, char *cmd, int *children_pid);
void exit_on_not_existing_file(char *cmd, char **cmd_split, \
int *pipes, int *children_pid);
void exit_on_permission(char **cmd_split, \
int *pipes, int *children_pid);
bool exit_parent(void);
Expand Down Expand Up @@ -136,7 +138,7 @@ char *get_first(void);
char *get_limiter(t_token *token);
void get_sum(char *cmd, char **ret, int *pipes);
void get_ops(const char *input, t_list **head);
char *get_path(char *cmd);
char *get_path(char *cmd, bool shaone);
void handle_sigint(void);
void handle_sigquit(void);
void ignore_sig(int sig);
Expand All @@ -146,6 +148,7 @@ void init_g_glob(void);
void init_new_var(bool plus_char, char *var, char **var_split);
void init_sig_callbacks(int process);
void insert_node(t_list **head, t_list *node, int idx);
bool is_dir(char *cmd);
bool is_valid_exit_arg(char *cmd);
char *longest_str(char *s1, char *s2);
void lst_dellast(t_list **lst, void (*del)(void *));
Expand Down
18 changes: 15 additions & 3 deletions pipex_children.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,21 @@ void child(t_cmd *cmd, int *pipes, int *children_pid)
char **envp;
char **cmd_split;

g_glob->exit_ret = 1;
if (cmd->fd_in != -1 && cmd->fd_out != -1 && cmd->str[0] != '\0')
{
cmd_split = ft_split_quotes(cmd->str, " \t");
cmd_split[0] = remove_quotes(cmd_split[0]);
cmd_split[0] = get_path(cmd_split[0]);
cmd_split[0] = get_path(cmd_split[0], false);
exit_on_not_existing_file(cmd->str, cmd_split, pipes, children_pid);
exit_on_bad_cmd(cmd_split, pipes, cmd->str, children_pid);
exit_on_permission(cmd_split, pipes, children_pid);
dup_and_close(cmd, pipes);
envp = envp_list_to_tab();
clean_exit(children_pid);
execve(cmd_split[0], cmd_split, envp);
close_pipes(pipes);
exit(1);
exit(g_glob->exit_ret);
}
else
clean_exit(children_pid);
Expand Down Expand Up @@ -84,7 +86,7 @@ static char *get_path_loop(char *cmd, t_list *envp_entry)
return (NULL);
}

char *get_path(char *cmd)
char *get_path(char *cmd, bool shaone)
{
t_list *envp_entry;

Expand All @@ -93,7 +95,17 @@ char *get_path(char *cmd)
ft_strncmp((char *)envp_entry->content, "PATH=", 5))
envp_entry = envp_entry->next;
if (cmd && access(cmd, F_OK) == 0 && ft_strchr(cmd, '/'))
{
if (is_dir(cmd) && !shaone)
{
ft_putstr_fd("minishell: ", 2);
ft_putstr_fd(cmd, 2);
ft_putstr_fd(": Is a directory\n", 2);
g_glob->exit_ret = 126;
}
return (cmd);
}

return (get_path_loop(cmd, envp_entry));
}

Expand Down
2 changes: 1 addition & 1 deletion signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void change_sig_handling(char *cmd, int *pipes)
if (cmd[0])
{
cmd_split = ft_split_sep(cmd, " \t");
cmd_split[0] = get_path(cmd_split[0]);
cmd_split[0] = get_path(cmd_split[0], true);
if (cmd_split[0] != NULL)
{
get_sum(cmd_split[0], &sum, pipes);
Expand Down
4 changes: 2 additions & 2 deletions unset.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ bool unset_parent(void)

if (ft_lstsize(*g_glob->cmds) == 1)
{
cmd_split = ft_split_sep(((t_cmd *)(*g_glob->cmds)->content)->str, \
cmd_split = ft_split_quotes(((t_cmd *)(*g_glob->cmds)->content)->str, \
" \t");
if (cmd_split[0] && !ft_strncmp(cmd_split[0], "unset", 6))
if (cmd_split && cmd_split[0] && !ft_strncmp(cmd_split[0], "unset", 6))
{
i = 0;
while (cmd_split[++i])
Expand Down
26 changes: 22 additions & 4 deletions utils_pipex.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@

#include "minishell.h"

void exit_on_not_existing_file(char *cmd, char **cmd_split, \
int *pipes, int *children_pid)
{
char **cmd_split_bis;

cmd_split_bis = ft_split_quotes(cmd, " \t");
if (cmd_split[0] == NULL && ft_strchr(cmd, '/') && \
access(cmd_split_bis[0], F_OK) != 0)
{
ft_putstr_fd(cmd_split[0], 2);
ft_putstr_fd(": No such file or directory\n", 2);
free_tab(cmd_split);
free_tab(cmd_split_bis);
close_pipes(pipes);
clean_exit(children_pid);
exit(127);
}
free_tab(cmd_split_bis);
}

void exit_on_bad_cmd(char **cmd_split, \
int *pipes, char *cmd, int *children_pid)
{
Expand All @@ -30,9 +50,8 @@ void exit_on_permission(char **cmd_split, \
{
if (access(cmd_split[0], X_OK) != 0)
{
ft_putstr_fd("Permission denied: ", 2);
ft_putstr_fd(cmd_split[0], 2);
ft_putchar_fd('\n', 2);
ft_putstr_fd(": Permission denied\n", 2);
free_tab(cmd_split);
close_pipes(pipes);
clean_exit(children_pid);
Expand All @@ -42,9 +61,8 @@ void exit_on_permission(char **cmd_split, \

void print_cmd_not_found(char *str)
{
ft_putstr_fd("Command not found: ", 2);
ft_putstr_fd(str, 2);
ft_putstr_fd("\n", 2);
ft_putstr_fd(": command not found\n", 2);
}

void close_pipes(int *pipes)
Expand Down

0 comments on commit 6ee1f67

Please sign in to comment.