From 5b313a1f5cd4fba2d9435e9d1d9b1e875783582c Mon Sep 17 00:00:00 2001 From: joris schaft Date: Mon, 1 Jul 2024 12:36:35 +0200 Subject: [PATCH] [ADD] helo --- src/builtin.c | 18 +++++++++--------- src/builtin2.c | 14 ++++++++++---- src/exec_command.c | 18 +++++++++--------- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/builtin.c b/src/builtin.c index 8d45b7a..21f51fb 100644 --- a/src/builtin.c +++ b/src/builtin.c @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/03 12:02:45 by jschaft #+# #+# */ -/* Updated: 2024/06/28 15:30:51 by mcolonna ### ########.fr */ +/* Updated: 2024/07/01 12:35:09 by jschaft ### ########.fr */ /* */ /* ************************************************************************** */ @@ -82,18 +82,18 @@ static int env_builtin(t_env *env, t_call call) int exec_builtin(t_env *env, t_call call) { if (ft_strncmp(call.program, "cd", 3) == 0) - return (cd_builtin(env, call)); + exit (cd_builtin(env, call)); if (ft_strncmp(call.program, "exit", 5) == 0) - return (exit_builtin(env, call)); + exit (exit_builtin(env, call)); if (ft_strncmp(call.program, "export", 7) == 0) - return (export_builtin(env, call)); + exit (export_builtin(env, call)); if (ft_strncmp(call.program, "unset", 6) == 0) - return (unset_builtin(env, call)); + exit (unset_builtin(env, call)); if (ft_strncmp(call.program, "pwd", 4) == 0) - return (pwd_builtin(env, call)); + exit (pwd_builtin(env, call)); if (ft_strncmp(call.program, "echo", 5) == 0) - return (echo_builtin(env, call)); + exit (echo_builtin(env, call)); if (ft_strncmp(call.program, "env", 4) == 0) - return (env_builtin(env, call)); - return (1); + exit (env_builtin(env, call)); + exit (1); } diff --git a/src/builtin2.c b/src/builtin2.c index 02f9373..b28b98d 100644 --- a/src/builtin2.c +++ b/src/builtin2.c @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/25 13:28:29 by jschaft #+# #+# */ -/* Updated: 2024/06/28 15:30:55 by mcolonna ### ########.fr */ +/* Updated: 2024/07/01 12:35:28 by jschaft ### ########.fr */ /* */ /* ************************************************************************** */ @@ -88,9 +88,13 @@ int echo_builtin(t_env *env, t_call call) return (0); } -static int go_home(t_env *env) +static int go_home(t_env *env, const char dest) { - return (chdir(variables_get(env->variables, "HOME"))); + if (dest == 'n') + return (chdir(variables_get(env->variables, "HOME")), dest); + if (dest == '-') + return (chdir(variables_get(env->variables, "OLDPWD"))); + return (0); } // Execute the builtin cd for the specified path. @@ -106,7 +110,9 @@ int cd_builtin(t_env *env, t_call call) if (argc == 2) r = chdir(call.argv[1]); else if (argc == 1) - r = go_home(env); + r = go_home(env, 'n'); + else if (argc == 2 && ft_strncmp(call.argv[1], "-", 2)) + r = go_home(env, '-'); else { print_line(fatal_error, 2, "cd: too many arguments"); diff --git a/src/exec_command.c b/src/exec_command.c index 37e2d66..d050a18 100644 --- a/src/exec_command.c +++ b/src/exec_command.c @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/24 13:48:00 by jschaft #+# #+# */ -/* Updated: 2024/06/28 16:08:02 by mcolonna ### ########.fr */ +/* Updated: 2024/07/01 12:36:03 by jschaft ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,8 +40,8 @@ static void close_all_pipes(t_exec_command_global *global) // If the program wasn't to found in $PATH, or the fork didn't work, // write the error and return the error status. static int execute_call( - t_exec_command_global *global, t_call call, const int inout[2], - char *const envp[]) + t_exec_command_global *global, t_call call, + const int inout[2], char *const envp[]) { pid_t pid; const char *program_path; @@ -60,7 +60,10 @@ static int execute_call( if (dup2(inout[0], 0) < 0 || dup2(inout[1], 1) < 0) return (minishell_error("errno"), errno); close_all_pipes(global); - execve(program_path, call.argv, envp); + if (is_builtin(call.program)) + exec_builtin(global->env, call); + else + execve(program_path, call.argv, envp); return (minishell_error("errno"), errno); } return (0); @@ -97,11 +100,8 @@ static int exec_each_call(t_exec_command_global *global, mc = mem_subclass(fatal_error, global->mc); inout[0] = global->pipes[i].pipe[0]; inout[1] = global->pipes[i + 1].pipe[1]; - if (is_builtin(command.calls[i].program)) - r = exec_builtin(env, command.calls[i]); - else - r = execute_call(global, command.calls[i], inout, - variables_envp(env->variables, mc)); + r = execute_call(global, command.calls[i], inout, + variables_envp(env->variables, mc)); mem_freeall(mc); if (r) return (r);