From: Lukas Jiriste Date: Thu, 1 Aug 2024 11:05:33 +0000 (+0200) Subject: Add the possibility to specify path to executable X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=7dc40d4cb4960093e498ab2549ec19020cdb5fef;p=42%2Fminishell.git Add the possibility to specify path to executable Before only the PATH was examined, now the field itself may contain the path and minishell will recognize it. --- diff --git a/src/execution.c b/src/execution.c index bf4d325..ee541e2 100644 --- a/src/execution.c +++ b/src/execution.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/21 08:57:54 by ljiriste #+# #+# */ -/* Updated: 2024/08/01 12:07:31 by ljiriste ### ########.fr */ +/* Updated: 2024/08/01 13:03:51 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -603,10 +603,24 @@ void free_split(char **fields) return ; } -int is_contained_in_dir(const char *exe_name, DIR *dir) +int contains_path(const char *str) { + return (ft_strchr(str, '/') != NULL); +} + +int file_exists(const char *path) +{ + return (!access(path, F_OK)); +} + +int is_contained_in_dir(const char *exe_name, const char *dir_name) +{ + DIR *dir; struct dirent *dir_entry; + dir = opendir(dir_name); + if (!dir) + return (0); dir_entry = readdir(dir); while (dir_entry) { @@ -614,6 +628,7 @@ int is_contained_in_dir(const char *exe_name, DIR *dir) return (1); dir_entry = readdir(dir); } + closedir(dir); return (0); } @@ -622,7 +637,6 @@ char *find_exe(const char *exe_name, const t_execution_env *env) const char *path; char **paths; size_t i; - DIR *dir; char *res; path = get_env_var_value(env, "PATH"); @@ -631,26 +645,15 @@ char *find_exe(const char *exe_name, const t_execution_env *env) return (NULL); res = NULL; i = 0; - while (paths[i]) + while (paths[i] && !res) { - dir = opendir(paths[i]); - if (!dir) - { - ++i; - continue ; - } - if (is_contained_in_dir(exe_name, dir)) - { + if (is_contained_in_dir(exe_name, paths[i])) if (ft_strcat_alloc(&res, paths[i])) if (ft_strcat_alloc(&res, "/")) ft_strcat_alloc(&res, exe_name); - break ; - } - closedir(dir); ++i; } free_split(paths); - closedir(dir); return (res); } @@ -675,7 +678,11 @@ int ex_fields(char **fields, t_vec *assignments, const t_vec *redirections, t_ex int status; char *path; - path = find_exe(fields[0], env); + path = NULL; + if (contains_path(fields[0]) && file_exists(fields[0])) + path = ft_strdup(fields[0]); + else if (!contains_path(fields[0])) + path = find_exe(fields[0], env); if (!path) return (1); pid = -1;