/* ************************************************************************** */ /* */ /* :::::::: */ /* executor_absolute_path.c :+: :+: */ /* +:+ */ /* By: willem +#+ */ /* +#+ */ /* Created: 2025/02/08 17:00:24 by willem #+# #+# */ /* Updated: 2025/02/25 14:30:18 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" char *executor_absolute_path(t_minishell *minishell, char *cmd) { char **path; t_environment *path_env; 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_env = environment_get(minishell, "PATH"); if (!path_env) return (NULL); path = ft_split(path_env->value, ':'); i = 0; while (path[i] != NULL) { executable = malloc_safe(minishell, ft_strlen(path[i]) + ft_strlen(cmd) + 2); 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); }