From 3aa64599696a80641b089d6b1fa24527948035d3 Mon Sep 17 00:00:00 2001 From: whaffman Date: Sat, 8 Feb 2025 19:42:34 +0100 Subject: [PATCH] executor_absolute_path --- inc/executor.h | 20 ++++++++++ src/executor/executor_absolute_path.c | 53 +++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 inc/executor.h create mode 100644 src/executor/executor_absolute_path.c diff --git a/inc/executor.h b/inc/executor.h new file mode 100644 index 0000000..1415487 --- /dev/null +++ b/inc/executor.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* executor.h :+: :+: */ +/* +:+ */ +/* By: willem +#+ */ +/* +#+ */ +/* Created: 2025/02/08 17:06:07 by willem #+# #+# */ +/* Updated: 2025/02/08 17:15:24 by willem ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#ifndef EXECUTER_H +# define EXECUTER_H + +# include "minishell.h" + +char *executor_absolute_path(t_list *env, char *cmd); + +#endif // EXECUTER_H \ No newline at end of file diff --git a/src/executor/executor_absolute_path.c b/src/executor/executor_absolute_path.c new file mode 100644 index 0000000..db09808 --- /dev/null +++ b/src/executor/executor_absolute_path.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* executor_absolute_path.c :+: :+: */ +/* +:+ */ +/* By: willem +#+ */ +/* +#+ */ +/* Created: 2025/02/08 17:00:24 by willem #+# #+# */ +/* Updated: 2025/02/08 19:27:05 by willem ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +char *executor_absolute_path(t_list *env, char *cmd) +{ + char **path; + char *executable; + int i; + + if (cmd[0] == '/' || cmd[0] == '.') + { + if (access(cmd, F_OK) == 0) + { + executable = ft_strdup(cmd); + if (!executable) + return (NULL); + return (executable); + } + return (NULL); + } + path = ft_split(environment_get(env, "PATH")->value, ':'); + if (!path) + return (NULL); + i = 0; + while (path[i] != NULL) + { + executable = malloc(ft_strlen(path[i]) + ft_strlen(cmd) + 2); + if (!executable) + return (ft_free_arr(path), NULL); + ft_strlcpy(executable, path[i], ft_strlen(path[i]) + 1); + ft_strlcat(executable, "/", ft_strlen(path[i]) + 2); + ft_strlcat(executable, cmd, ft_strlen(path[i]) + ft_strlen(cmd) + 2); + if (access(executable, F_OK) == 0) + { + ft_free_arr(path); + return (executable); + } + free(executable); + i++; + } + return (ft_free_arr(path), NULL); +}