From f573db414f297c2d439b118949bc3cf55165b514 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Luk=C3=A1=C5=A1=20Ji=C5=99i=C5=A1t=C4=9B?= Date: Wed, 28 Aug 2024 09:59:34 +0200 Subject: [PATCH] Make subshell execute in a new process Because the OS keeps track of CWDs of its processes, subshell is made to run in its own process not to change the CWD of main shell. Also make exit the last executed command in a pipeline and in a program. --- inc/minishell_structs.h | 3 ++- src/env.c | 3 ++- src/execution.c | 48 +++++++++++++++++------------------------ src/main.c | 5 +++-- 4 files changed, 27 insertions(+), 32 deletions(-) diff --git a/inc/minishell_structs.h b/inc/minishell_structs.h index 5c9f5ba..e3894c7 100644 --- a/inc/minishell_structs.h +++ b/inc/minishell_structs.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/26 09:08:46 by ljiriste #+# #+# */ -/* Updated: 2024/08/27 16:59:59 by ljiriste ### ########.fr */ +/* Updated: 2024/08/28 09:36:55 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,6 +32,7 @@ typedef struct s_execution_env int stdout_fd; int ret_val; int exit; + int subshell; int last_was_builtin; int last_builtin_ret_val; t_vars *vars; diff --git a/src/env.c b/src/env.c index 44d2a30..7f6dac8 100644 --- a/src/env.c +++ b/src/env.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/21 21:24:52 by ljiriste #+# #+# */ -/* Updated: 2024/08/27 16:37:47 by ljiriste ### ########.fr */ +/* Updated: 2024/08/28 09:36:37 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,7 @@ int init_env(t_execution_env *env, char **envp) env->ret_val = 0; env->exit = 0; env->last_was_builtin = 0; + env->subshell = 0; env->vars = malloc(sizeof(t_vars)); if (!env->vars) return (1); diff --git a/src/execution.c b/src/execution.c index 9a07f82..a3ba582 100644 --- a/src/execution.c +++ b/src/execution.c @@ -6,7 +6,7 @@ /* By: lnikolov vars = malloc(sizeof(t_vars)); - if (!copy->vars) - return (1); - ft_vec_init(©->vars->exported, sizeof(char *)); - ft_vec_init(©->vars->other, sizeof(char *)); - ft_vec_copy(©->vars->exported, &env->vars->exported, v_strcopy, free); - ft_vec_copy(©->vars->other, &env->vars->other, v_strcopy, free); - return (0); -} - -void free_env(t_execution_env *env) -{ - clean_vars(env->vars); - free(env->vars); - return ; -} - int ex_program(t_parse_tree_node *program, t_execution_env *env); int subshell(t_parse_tree_node *program, t_execution_env *env) { - int res; - t_execution_env env_copy; + int res; + int status; + pid_t pid; - if (copy_env(&env_copy, env)) + pid = fork(); + if (pid == 0) + { + env->subshell = 1; + res = ex_program(program, env); + env->exit = 1; + return (res); + } + else if (pid < 0) return (1); - res = ex_program(program, &env_copy); - env->ret_val = env_copy.ret_val; - free_env(&env_copy); - return (res); + waitpid(pid, &status, 0); + if (!WIFEXITED(status)) + return (1); + env->ret_val = WEXITSTATUS(status); + return (0); } int ex_command(t_parse_tree_node *command, t_execution_env *env) { t_parse_tree_node *comp_command; + if (env->exit) + return (0); if (is_token_type(ft_vec_caccess(&command->children, 0), "simple_command")) return (ex_simple_command(ft_vec_access(&command->children, 0), env)); else diff --git a/src/main.c b/src/main.c index 7231928..337843e 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/26 13:11:47 by ljiriste #+# #+# */ -/* Updated: 2024/08/27 15:08:06 by ljiriste ### ########.fr */ +/* Updated: 2024/08/28 09:38:51 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -56,10 +56,11 @@ int main(int argc, __attribute__((unused)) char **argv, char **envp) handle_input(&line, &env); free(line); } + if (!env.subshell) + ft_printf("exit\n"); res = env.ret_val; clean_env(&env); parse(NULL, NULL); terminate_input(); - ft_printf("exit\n"); return (res); } -- 2.30.2