From e3c98f2d228135d9af3e521273e51d2ae5fbb949 Mon Sep 17 00:00:00 2001 From: mcolonna Date: Thu, 25 Apr 2024 17:25:37 +0200 Subject: [PATCH] [FIX] several things* fix: - free_tab(): useless line - fork_exec(): comp error change: - search_path() works differently (doesn't modify path) - execute_command() is changed accordingly - get_path() now returns a non-const --- include/include.h | 4 ++-- src/exec_command.c | 29 ++++++++++++----------------- src/path.c | 18 +++++++++++++----- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/include/include.h b/include/include.h index f4e37a8..26e31f0 100644 --- a/include/include.h +++ b/include/include.h @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */ -/* Updated: 2024/04/24 13:19:28 by mcolonna ### ########.fr */ +/* Updated: 2024/04/25 17:30:22 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -70,6 +70,6 @@ void minishell_error(const char *msg); // Get the PATH values. // Return a list of strings ended by NULL. -const char **get_path(const char **envp); +char **get_path(const char **envp); #endif diff --git a/src/exec_command.c b/src/exec_command.c index 470e905..f7d609d 100644 --- a/src/exec_command.c +++ b/src/exec_command.c @@ -6,12 +6,13 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/24 13:48:00 by jschaft #+# #+# */ -/* Updated: 2024/04/24 14:22:13 by jschaft ### ########.fr */ +/* Updated: 2024/04/25 17:23:34 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ #include "include.h" +// Free every element of tab, then tab itself. static void free_tab(char **tab) { int i; @@ -22,41 +23,35 @@ static void free_tab(char **tab) free(tab[i]); i++; } - free(tab[i]); free(tab); } +// TODO works with only one call (no pipe). +// TODO is t_call.argv ended by NULL? static void fork_exec(const char *path, t_command command, const char **envp) { if (command.input_fd != 0) dup2(command.input_fd, 0); if (command.output_fd != 1) dup2(command.output_fd, 1); - execve(path, command.calls->argv, envp); + execve(path, command.calls[0]->argv, envp); } +// TODO works with only one call (no pipe). int execute_command(t_command command, const char **envp) { - char **path; - pid_t pid; - int i; + const char **path = get_path(envp); + pid_t pid; + int program_path; - i = 0; - path = get_path(envp); - while (path[i] != NULL) - { - path[i] = ft_strjoin(path[i], "/"); - path[i] = ft_strjoin(path[i], command.calls->program); - i++; - } - i = search_path(path); - if (i == -1) + program_path = search_path(path); + if (!program_path) return (127); pid = fork(); if (pid < 0) return (1); if (pid == 0) - fork_exec((const char *)path[i], command, envp); + fork_exec(program_path, command, envp); free_tab(path); return (0); } diff --git a/src/path.c b/src/path.c index 14b2e08..37ea53e 100644 --- a/src/path.c +++ b/src/path.c @@ -6,12 +6,13 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/16 12:07:26 by jschaft #+# #+# */ -/* Updated: 2024/04/24 14:08:11 by jschaft ### ########.fr */ +/* Updated: 2024/04/25 17:30:05 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ #include "include.h" +// Get the path values from envp. char **get_path(const char **envp) { int i; @@ -27,16 +28,23 @@ char **get_path(const char **envp) return (char **re); } -int search_path(char **path) +// Search the program in PATH. +// Returns the path of the program to use, or NULL if there is none. +// TODO does it return the good path if there is several possibilities? +const char *search_path( + const t_memclass mc, const char **path, const char *prog) { - int i; + const t_memclass mc_in = mem_newclass(fatal_error); + const char *prog2 = str_join(fatal_error, mc_in, "/", prog); + int i; i = 0; while (path[i] != NULL) { - if (access(path[i], X_OK) >= 0) + if (access(str_join(fatal_error, mc_in, path[i], prog2), X_OK) >= 0) return (i); i++; } - return (-1); + mem_freeall(mc_in); + return (NULL); }