From ef11eb0c8ca6c868c0c62e18e29477e9000bac3f Mon Sep 17 00:00:00 2001 From: mcolonna Date: Tue, 25 Jun 2024 13:10:01 +0200 Subject: [PATCH] fix: $PATH checked from variables * also fix variables assignment --- dev/TODO | 1 + include/parse_command.h | 5 ++++- include/path.h | 8 ++------ src/exec_command.c | 8 +++++--- src/main.c | 2 +- src/parse_command.c | 8 ++++---- src/path.c | 16 +++++++--------- 7 files changed, 24 insertions(+), 24 deletions(-) diff --git a/dev/TODO b/dev/TODO index d5619bb..add83c7 100644 --- a/dev/TODO +++ b/dev/TODO @@ -1,5 +1,6 @@ - finding commands - built-in commands + - find with given path only if there is a '/' - variables - uses $PATH in env.variables instead of envp - builtins: diff --git a/include/parse_command.h b/include/parse_command.h index 7e85986..44d1d0d 100644 --- a/include/parse_command.h +++ b/include/parse_command.h @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */ -/* Updated: 2024/06/13 14:01:41 by mcolonna ### ########.fr */ +/* Updated: 2024/06/21 16:22:20 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -50,4 +50,7 @@ typedef struct s_pipes // If error, return a t_command wth the value .error = true. t_command parse_command(t_env *env, const char *command); +// TODO reorganize +const char *variables_get(t_list *variables, const char *name); + #endif diff --git a/include/path.h b/include/path.h index d09bb86..c2972f1 100644 --- a/include/path.h +++ b/include/path.h @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */ -/* Updated: 2024/06/13 13:00:10 by mcolonna ### ########.fr */ +/* Updated: 2024/06/21 16:24:15 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,15 +17,11 @@ ///// PATH ///// -// Get the PATH values. -// Return a list of strings ended by NULL. -const char **get_path(const t_memclass mc, char *const envp[]); - // Search the program in $PATH. // Returns the path of the program to use, or NULL if there is none. // If there is several possibilities, it returns the one from the first path // given in $PATH. const char *search_path( - const t_memclass mc, const char **path, const char *prog); + const t_memclass mc, t_list *variables, const char *prog); #endif diff --git a/src/exec_command.c b/src/exec_command.c index 92a2205..4d3d1d8 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/21 15:57:28 by mcolonna ### ########.fr */ +/* Updated: 2024/06/24 16:16:31 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,7 @@ typedef struct s_exec_command_global t_memclass mc; int nb_calls; t_pipes *pipes; + t_env *env; } t_exec_command_global; static void close_all_pipes(t_exec_command_global *global) @@ -50,11 +51,11 @@ static int execute_call( t_exec_command_global *global, t_call call, const int inout[2], char *const envp[]) { - const char **path = get_path(global->mc, envp); pid_t pid; const char *program_path; - program_path = search_path(global->mc, path, call.program); + program_path = search_path(global->mc, global->env->variables, + call.program); if (!program_path) return (minishell_error(str_join(fatal_error, global->mc, "command not found: ", str_join(fatal_error, global->mc, @@ -121,6 +122,7 @@ int execute_command(t_env *env, t_command command) r = 0; global.mc = env->mc_command; global.nb_calls = 0; + global.env = env; while (command.calls[global.nb_calls].program != NULL) global.nb_calls++; if (global.nb_calls == 0) diff --git a/src/main.c b/src/main.c index 4040605..7109804 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 14:33:45 by mcolonna #+# #+# */ -/* Updated: 2024/06/21 15:48:48 by mcolonna ### ########.fr */ +/* Updated: 2024/06/24 17:30:19 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/parse_command.c b/src/parse_command.c index d552ca3..f305f1b 100644 --- a/src/parse_command.c +++ b/src/parse_command.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/24 13:47:40 by mcolonna #+# #+# */ -/* Updated: 2024/06/21 15:44:51 by mcolonna ### ########.fr */ +/* Updated: 2024/06/25 13:06:57 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,7 +31,7 @@ static t_variable *variables_find(t_list *variables, const char *name) el = el->next; } r = mem_alloc(fatal_error, variables->mc, sizeof(t_variable)); - r->name = name; + r->name = str_dup(fatal_error, variables->mc, name); r->value = ""; list_add(fatal_error, variables, r); return (r); @@ -40,7 +40,8 @@ static t_variable *variables_find(t_list *variables, const char *name) // Set a variable to a new value. void variables_set(t_list *variables, const t_variable var) { - variables_find(variables, var.name)->value = var.value; + variables_find(variables, var.name)->value = + str_dup(fatal_error, variables->mc, var.value); } // Get the value of a variable from its name. @@ -152,7 +153,6 @@ static void read_variable_value(t_parsing_args *args, const char **dest) const char *tmp; name = NULL; - tmp = NULL; if (stream_read(&args->stream) == '?') { stream_pop(&args->stream); diff --git a/src/path.c b/src/path.c index e24978e..f1f41f4 100644 --- a/src/path.c +++ b/src/path.c @@ -6,29 +6,26 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/16 12:07:26 by jschaft #+# #+# */ -/* Updated: 2024/06/07 11:40:04 by jschaft ### ########.fr */ +/* Updated: 2024/06/21 16:23:54 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ #include "include.h" -const char **get_path(const t_memclass mc, char *const envp[]) +static const char **get_path(const t_memclass mc, t_list *variables) { - int i; const char **re; - char *path; + const char *path; - i = 0; - while (ft_strncmp(envp[i], "PATH=", 5) != 0) - i++; - path = envp[i] + 5; + path = variables_get(variables, "PATH"); re = (const char **)str_split(fatal_error, mc, path, ":"); return (re); } const char *search_path( - const t_memclass mc, const char **path, const char *prog) + const t_memclass mc, t_list *variables, const char *prog) { + const char **path = get_path(mc, variables); const char *prog2 = str_join(fatal_error, mc, "/", prog); int i; const char *r; @@ -48,5 +45,6 @@ const char *search_path( i++; } mem_free((void *)prog2); + mem_free(path); return (NULL); }