From f626f2715ab696fdb326ed67256d012d86faef9f Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Tue, 15 Sep 2020 17:54:48 +0200 Subject: Refactoring env, Removing bloat from utils, exec and env --- src/eval/cmd.c | 39 +++++++++++------------------- src/eval/eval.c | 7 ++---- src/eval/exec.c | 65 -------------------------------------------------- src/eval/operation.c | 14 +++++------ src/eval/parenthesis.c | 9 +++---- 5 files changed, 26 insertions(+), 108 deletions(-) delete mode 100644 src/eval/exec.c (limited to 'src/eval') diff --git a/src/eval/cmd.c b/src/eval/cmd.c index 04d66aa..cc22441 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/15 13:07:36 by charles ### ########.fr */ +/* Updated: 2020/09/15 17:51:05 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,25 +15,20 @@ pid_t g_child_pid = -1; int g_last_status = 0; -int wrapped_cmd(void *void_param) +int wrapped_cmd(t_fork_param_cmd *param) { - t_fork_param_cmd *param; - int status; + int status; - param = void_param; if (param->builtin != NULL) return (param->builtin->func(param->argv, param->env)); - else + status = execve(param->exec_path, param->argv, (char**)param->env->data); + if (status == -1) { - status = execve(param->exec_path, param->argv, (char**)param->env->data); - if (status == -1) - { - if (errno == ENOEXEC) - return (0); - return (errorf_ret(126, "%s: %s\n", param->exec_path, strerror(errno))); - } - return (status); + if (errno == ENOEXEC) + return (0); + return (errorf_ret(126, "%s: %s\n", param->exec_path, strerror(errno))); } + return (status); } int eval_cmd(int fds[2], t_env env, t_ast *ast, pid_t *child_pid) @@ -52,22 +47,16 @@ int eval_cmd(int fds[2], t_env env, t_ast *ast, pid_t *child_pid) if (param.builtin != NULL && !param.builtin->forked && child_pid == NULL) return (param.builtin->func(argv, env)); - if (param.builtin == NULL) + if (param.builtin == NULL + && (status = path_search(env, argv[0], param.exec_path, true)) != 0) { - - if ((status = path_search(env, argv[0], param.exec_path, true)) != 0) - { - /* errorf("%s: command not found\n", argv[0]); */ - ft_split_destroy(argv); - return (status); - } - /* if ((status = exec_path_check(param.exec_path)) != 0) */ - /* return (status); */ + ft_split_destroy(argv); + return (status); } param.argv = argv; param.env = env; - status = fork_wrap(fds, ¶m, &wrapped_cmd, child_pid); + status = fork_wrap(fds, ¶m, (t_wrapped_func)wrapped_cmd, child_pid); ft_split_destroy(argv); g_last_status = status; return (status); diff --git a/src/eval/eval.c b/src/eval/eval.c index ee5c009..9f2fc28 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/09/14 19:39:33 by charles ### ########.fr */ +/* Updated: 2020/09/15 17:45:58 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,10 +23,7 @@ */ int fork_wrap( - int fds[2], - void *passed, - int (*wrapped)(void *param), - pid_t *child_pid + int fds[2], void *passed, t_wrapped_func wrapped, pid_t *child_pid ) { int status; diff --git a/src/eval/exec.c b/src/eval/exec.c deleted file mode 100644 index f733c34..0000000 --- a/src/eval/exec.c +++ /dev/null @@ -1,65 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* exec.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: charles +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/04/01 17:06:11 by charles #+# #+# */ -/* Updated: 2020/09/14 19:48:49 by charles ### ########.fr */ -/* */ -/* ************************************************************************** */ - -/* -** \file exec.c -** \brief Executable name and path -*/ - -#include "eval.h" - -/* -** \brief Check if executable path is valid -** \param exec_path Executable path -** \return status code -*/ - -int exec_path_check(char *exec_path) -{ - struct stat statbuf; - - if (stat(exec_path, &statbuf) == -1) - return (errorf_ret(127, "%s: %s\n", exec_path, strerror(errno))); - if (S_ISDIR(statbuf.st_mode)) - return (errorf_ret(126, "%s: Is a directory\n", exec_path)); - if (!(statbuf.st_mode & 0444)) - return (errorf_ret(126, "%s: %s\n", exec_path, strerror(EACCES))); - return (0); -} - -/* -** \brief Search executable name in path -** \param path Path hash table -** \param path_var Path environment string in case we need to update path -** \param exec_name Executable name to search -** \return Executable path or NULL if not found or path update error -*/ - -/* int exec_search_path(t_path path, char *path_var, char *exec_name, char **exec_path) */ -/* { */ -/* if (ft_strchr(exec_name, '/') != NULL) // TODO test recursive link */ -/* { */ -/* *exec_path = exec_name; */ -/* return (0); */ -/* } */ -/* if (path_var == NULL) */ -/* return (127); */ -/* // TODO if PATH contain empty path, consider current directory files as cmd */ -/* if ((*exec_path = ft_htget(path, exec_name)) == NULL) */ -/* { */ -/* if (path_update(path, path_var) == NULL) */ -/* return (EVAL_FATAL); */ -/* if ((*exec_path = ft_htget(path, exec_name)) == NULL) */ -/* return (127); */ -/* } */ -/* return (0); */ -/* } */ diff --git a/src/eval/operation.c b/src/eval/operation.c index 8c80033..e2ce55b 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/09/14 19:39:16 by charles ### ########.fr */ +/* Updated: 2020/09/15 16:43:21 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,21 +33,21 @@ int eval_operation(int fds[2], t_env env, t_ast *ast) pid_t right_pid; eval(left_fds, env, ast->op.left, &left_pid); close(p[FD_WRITE]); - eval(right_fds, env, ast->op.right, &right_pid); + status = eval(right_fds, env, ast->op.right, &right_pid); close(p[FD_READ]); pid_t finished; - finished = wait(NULL); + finished = wait(&finished); if (finished == left_pid) { - waitpid(right_pid, &right_pid, 0); + waitpid(right_pid, &finished, 0); } else if (finished == right_pid) { - kill(left_pid, SIGKILL); - /* waitpid(left_pid, &left_pid, 0); */ + /* kill(left_pid, SIGTERM); // FIXME weird bug with parent on both sides */ + waitpid(left_pid, &finished, 0); } - return (0); + return (status); } if ((status = eval(left_fds, env, ast->op.left, NULL)) == EVAL_FATAL) return (EVAL_FATAL); diff --git a/src/eval/parenthesis.c b/src/eval/parenthesis.c index 610218b..249e24d 100644 --- a/src/eval/parenthesis.c +++ b/src/eval/parenthesis.c @@ -6,17 +6,14 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/09/13 20:38:29 by charles #+# #+# */ -/* Updated: 2020/09/14 19:40:12 by charles ### ########.fr */ +/* Updated: 2020/09/15 16:50:21 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "eval.h" -int wrapped_eval(void *void_param) +int wrapped_eval(t_fork_param_args *param) { - t_fork_param_args *param; - - param = void_param; return (eval(param->fds, param->env, param->ast, NULL)); } @@ -32,5 +29,5 @@ int eval_parenthesis(int fds[2], t_env env, t_ast *ast) param.fds[1] = fds[1]; param.env = env; param.ast = ast->parent_ast; - return (fork_wrap(fds, ¶m, wrapped_eval, NULL)); + return (fork_wrap(fds, ¶m, (t_wrapped_func)wrapped_eval, NULL)); } -- cgit