From 89bd998bcf0839a83daf16ab9b1e8ae568a8a9f2 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Tue, 6 Oct 2020 17:23:23 +0200 Subject: Removing previous pipe bloat --- src/eval/cmd.c | 8 ++++---- src/eval/eval.c | 37 ++++++++++++++----------------------- src/eval/operation.c | 10 +++++----- src/eval/parenthesis.c | 8 ++++---- 4 files changed, 27 insertions(+), 36 deletions(-) (limited to 'src/eval') diff --git a/src/eval/cmd.c b/src/eval/cmd.c index 5cabbba..8c08b78 100644 --- a/src/eval/cmd.c +++ b/src/eval/cmd.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/14 10:41:31 by charles #+# #+# */ -/* Updated: 2020/09/16 19:39:42 by charles ### ########.fr */ +/* Updated: 2020/10/06 17:18:20 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,7 +30,7 @@ int wrapped_cmd(t_fork_param_cmd *param) return (status); } -int eval_cmd(int fds[2], t_env env, t_ast *ast, pid_t *child_pid, int fd_to_close) +int eval_cmd(int fds[2], t_env env, t_ast *ast) { t_fork_param_cmd param; char **argv; @@ -44,7 +44,7 @@ int eval_cmd(int fds[2], t_env env, t_ast *ast, pid_t *child_pid, int fd_to_cl if (argv[0] == NULL) return (0); param.builtin = builtin_search_func(argv[0]); - if (param.builtin != NULL && !param.builtin->forked && child_pid == NULL) + if (param.builtin != NULL && !param.builtin->forked) return (param.builtin->func(argv, env)); if (param.builtin == NULL @@ -56,7 +56,7 @@ int eval_cmd(int fds[2], t_env env, t_ast *ast, pid_t *child_pid, int fd_to_cl param.argv = argv; param.env = env; - status = fork_wrap(fds, ¶m, (t_wrapped_func)wrapped_cmd, child_pid, fd_to_close); + status = fork_wrap(fds, ¶m, (t_wrapped_func)wrapped_cmd); ft_split_destroy(argv); g_state.last_status = status; return (status); diff --git a/src/eval/eval.c b/src/eval/eval.c index d2eb822..2aed026 100644 --- a/src/eval/eval.c +++ b/src/eval/eval.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/09/13 20:38:06 by charles #+# #+# */ -/* Updated: 2020/10/06 12:57:33 by cacharle ### ########.fr */ +/* Updated: 2020/10/06 17:21:54 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,39 +32,30 @@ int st_replace(int oldfd, int newfd) ** \return The child status code or EVAL_FATAL on error */ -int fork_wrap( - int fds[2], void *passed, t_wrapped_func wrapped, pid_t *child_pid, int fd_to_close -) +int fork_wrap(int fds[2], void *passed, t_wrapped_func wrapped) { int status; - bool waiting; + /* bool waiting; */ pid_t pid; - waiting = child_pid == NULL; - if (waiting) - child_pid = &pid; - if ((*child_pid = fork()) == -1) + if ((pid = fork()) == -1) return (EVAL_FATAL); - if (*child_pid == 0) + if (pid == 0) { if (st_replace(fds[FD_READ], STDIN_FILENO) != 0) exit(EXIT_FAILURE); if (st_replace(fds[FD_WRITE], STDOUT_FILENO) != 0) exit(EXIT_FAILURE); - if (fd_to_close != FD_NONE) - close(fd_to_close); + /* if (fd_to_close != FD_NONE) */ + /* close(fd_to_close); */ if ((status = wrapped(passed)) == EVAL_FATAL) exit(EXIT_FAILURE); exit(status); } - g_child_pid = *child_pid; - if (waiting) - { - waitpid(*child_pid, child_pid, 0); - close(fds[FD_WRITE]); - return (WEXITSTATUS(*child_pid)); - } - return (0); + g_child_pid = pid; + waitpid(pid, &pid, 0); + /* close(fds[FD_WRITE]); */ + return (WEXITSTATUS(pid)); } /* @@ -72,15 +63,15 @@ int fork_wrap( ** \return The last command status or EVAL_FATAL on error */ -int eval(int fds[2], t_env env, t_ast *ast, pid_t *child_pid, int fd_to_close) +int eval(int fds[2], t_env env, t_ast *ast) { if (ast->tag == AST_OP) return (eval_operation(fds, env, ast)); if (ast->tag == AST_CMD) - return (eval_cmd(fds, env, ast, child_pid, fd_to_close)); + return (eval_cmd(fds, env, ast)); if (ast->tag == AST_PIPELINE) return (eval_pipeline(fds, env, ast)); if (ast->tag == AST_PARENT) - return (eval_parenthesis(fds, env, ast, child_pid, fd_to_close)); + return (eval_parenthesis(fds, env, ast)); return (EVAL_FATAL); } diff --git a/src/eval/operation.c b/src/eval/operation.c index d8206b6..df54346 100644 --- a/src/eval/operation.c +++ b/src/eval/operation.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/17 15:27:22 by charles #+# #+# */ -/* Updated: 2020/10/06 16:11:54 by cacharle ### ########.fr */ +/* Updated: 2020/10/06 17:21:38 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,13 +22,13 @@ int eval_operation(int fds[2], t_env env, t_ast *ast) left_fds[FD_WRITE] = FD_NONE; right_fds[FD_READ] = FD_NONE; right_fds[FD_WRITE] = fds[FD_WRITE]; - if ((status = eval(left_fds, env, ast->op.left, NULL, FD_NONE)) == EVAL_FATAL) + if ((status = eval(left_fds, env, ast->op.left)) == EVAL_FATAL) return (EVAL_FATAL); g_state.last_status = status; if ((ast->op.sep == TAG_AND && status != 0) || (ast->op.sep == TAG_OR && status == 0)) return (status); - return (eval(right_fds, env, ast->op.right, NULL, FD_NONE)); + return (eval(right_fds, env, ast->op.right)); } /* pid_t run_piped_child(t_env env, t_ast *ast, int copied, int closed) */ @@ -76,7 +76,7 @@ int eval_pipeline(int fds[2], t_env env, t_ast *ast) close(p[FD_READ]); fds[0] = FD_NONE; fds[1] = FD_NONE; - exit(eval(fds, env, curr->data, NULL, FD_NONE)); + exit(eval(fds, env, curr->data)); } close(p[FD_WRITE]); if (prev_output != STDIN_FILENO) @@ -97,7 +97,7 @@ int eval_pipeline(int fds[2], t_env env, t_ast *ast) close(p[FD_WRITE]); fds[0] = FD_NONE; fds[1] = FD_NONE; - exit(eval(fds, env, curr->data, NULL, FD_NONE)); + exit(eval(fds, env, curr->data)); } close(p[FD_READ]); diff --git a/src/eval/parenthesis.c b/src/eval/parenthesis.c index 83edf40..5a1fafa 100644 --- a/src/eval/parenthesis.c +++ b/src/eval/parenthesis.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/09/13 20:38:29 by charles #+# #+# */ -/* Updated: 2020/09/15 20:09:13 by charles ### ########.fr */ +/* Updated: 2020/10/06 17:22:29 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,10 +14,10 @@ int wrapped_eval(t_fork_param_args *param) { - return (eval(param->fds, param->env, param->ast, NULL, FD_NONE)); + return (eval(param->fds, param->env, param->ast)); } -int eval_parenthesis(int fds[2], t_env env, t_ast *ast, pid_t *child_pid, int fd_to_close) +int eval_parenthesis(int fds[2], t_env env, t_ast *ast) { int status; t_fork_param_args param; @@ -29,5 +29,5 @@ int eval_parenthesis(int fds[2], t_env env, t_ast *ast, pid_t *child_pid, int param.fds[1] = fds[1]; param.env = env; param.ast = ast->parent_ast; - return (fork_wrap(fds, ¶m, (t_wrapped_func)wrapped_eval, child_pid, fd_to_close)); + return (fork_wrap(fds, ¶m, (t_wrapped_func)wrapped_eval)); } -- cgit