Skip to content

Commit

Permalink
c fini
Browse files Browse the repository at this point in the history
  • Loading branch information
zakissimo committed Jan 13, 2023
1 parent d44eca2 commit 0142237
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
3 changes: 2 additions & 1 deletion minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/27 20:30:45 by zhabri #+# #+# */
/* Updated: 2023/01/12 14:28:56 by zhabri ### ########.fr */
/* Updated: 2023/01/13 11:52:07 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -164,6 +164,7 @@ char *op_error_trimmed(t_list *curr);
bool overflows_llong(char *str);
int pipe_error(void);
void pipex(void);
void pipex_handle_exit_ret(int children_pid, int exit_ret);
bool plus_end_str(char *str);
void print_cmds(void);
void print_cmd_not_found(char *str);
Expand Down
23 changes: 14 additions & 9 deletions pipex.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: brenaudo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/21 12:02:33 by brenaudo #+# #+# */
/* Updated: 2023/01/12 14:13:02 by zhabri ### ########.fr */
/* Updated: 2023/01/13 11:52:31 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,7 +17,8 @@ static void close_pipe_and_recreate(int *pipes, t_cmd *cmd);

static void pipex_loop_core(int *children_pid, t_list *curr, int *pipes)
{
int built_in;
int built_in;
int ret;

built_in = builtin(((t_cmd *)curr->content)->str);
children_pid[((t_cmd *)curr->content)->cmd_idx] = fork();
Expand All @@ -31,6 +32,14 @@ static void pipex_loop_core(int *children_pid, t_list *curr, int *pipes)
}
if (built_in == -1)
change_sig_handling(((t_cmd *)curr->content)->str, pipes, children_pid);
else
{
waitpid(children_pid[((t_cmd *)curr->content)->cmd_idx], &ret, 0);
if (ret >> 8 == 0)
children_pid[((t_cmd *)curr->content)->cmd_idx] = -256;
else
children_pid[((t_cmd *)curr->content)->cmd_idx] = -1 * (ret >> 8);
}
g_glob->in_child = true;
close_pipe_and_recreate(pipes, ((t_cmd *)curr->content));
}
Expand Down Expand Up @@ -66,13 +75,9 @@ void pipex(void)
children_pid = pipex_loop();
while (children_pid[i] != 0)
{
waitpid(children_pid[i], &exit_ret, 0);
if (g_glob->sig_int)
g_glob->exit_ret = 130;
else if (g_glob->sig_quit)
g_glob->exit_ret = 131;
else
g_glob->exit_ret = exit_ret >> 8;
if (children_pid[i] > 0)
waitpid(children_pid[i], &exit_ret, 0);
pipex_handle_exit_ret(children_pid[i], exit_ret);
i++;
g_glob->sig_quit = false;
g_glob->sig_int = false;
Expand Down
19 changes: 18 additions & 1 deletion utils_pipex_bis.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/09 13:24:09 by zhabri #+# #+# */
/* Updated: 2023/01/10 10:59:53 by zhabri ### ########.fr */
/* Updated: 2023/01/13 11:51:51 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -26,3 +26,20 @@ void init_children_pid(int **children_pid, int size)
while (!*children_pid)
*children_pid = ft_calloc(size, sizeof(int));
}

void pipex_handle_exit_ret(int children_pid, int exit_ret)
{
if (g_glob->sig_int)
g_glob->exit_ret = 130;
else if (g_glob->sig_quit)
g_glob->exit_ret = 131;
else
{
if (children_pid < 0 && children_pid != -256)
g_glob->exit_ret = children_pid * (-1);
else if (children_pid < 0 && children_pid == -256)
g_glob->exit_ret = 0;
else if (children_pid > 0)
g_glob->exit_ret = exit_ret >> 8;
}
}

0 comments on commit 0142237

Please sign in to comment.