diff options
Diffstat (limited to 'src/eval/op.c')
| -rw-r--r-- | src/eval/op.c | 38 |
1 files changed, 32 insertions, 6 deletions
diff --git a/src/eval/op.c b/src/eval/op.c index 9a37736..a39e380 100644 --- a/src/eval/op.c +++ b/src/eval/op.c @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/17 15:27:22 by charles #+# #+# */ -/* Updated: 2020/09/10 20:28:57 by charles ### ########.fr */ +/* Updated: 2020/09/12 17:00:11 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,6 +28,26 @@ int eval_op(int fds[2], t_env env, t_path path, t_ast *ast) pipe(p); left_fds[FD_WRITE] = p[FD_WRITE]; right_fds[FD_READ] = p[FD_READ]; + + pid_t left_pid; + pid_t right_pid; + eval_forked(left_fds, env, path, ast->op.left, &left_pid); + eval_forked(right_fds, env, path, ast->op.right, &right_pid); + + pid_t finished; + finished = wait(NULL); + close(p[FD_READ]); + close(p[FD_WRITE]); + if (finished == left_pid) + { + /* waitpid(right_pid, &right_pid, 0); */ + } + else if (finished == right_pid) + { + kill(left_pid, SIGKILL); + /* waitpid(left_pid, &left_pid, 0); */ + } + return (0); } if ((status = eval(left_fds, env, path, ast->op.left)) == EVAL_FATAL) return (EVAL_FATAL); @@ -41,7 +61,7 @@ int eval_op(int fds[2], t_env env, t_path path, t_ast *ast) int wrapped_eval(void *void_param) { - t_fork_param_parent *param; + t_fork_param_args *param; param = void_param; return (eval(param->fds, param->env, param->path, param->ast)); @@ -49,18 +69,24 @@ int wrapped_eval(void *void_param) int eval_parent(int fds[2], t_env env, t_path path, t_ast *ast) { - t_fork_param_parent param; int status; if ((status = redir_extract(&ast->redirs, env, fds)) != 0) return (status); + ast->tag ^= AST_PARENT; + return (eval_forked(fds, env, path, ast->parent_ast, NULL)); +} + +int eval_forked(int fds[2], t_env env, t_path path, t_ast *ast, pid_t *child_pid) +{ + t_fork_param_args param; + param.fds[0] = fds[0]; param.fds[1] = fds[1]; param.env = env; param.path = path; - ast->tag ^= AST_PARENT; - param.ast = ast->parent_ast; - return (fork_wrap(fds, ¶m, wrapped_eval)); + param.ast = ast; + return (fork_wrap(fds, ¶m, wrapped_eval, child_pid)); } int eval(int fds[2], t_env env, t_path path, t_ast *ast) |
