This commit is contained in:
whaffman 2025-02-11 16:45:30 +01:00
parent 21b3245ebf
commit ed7a23eeb3

View File

@ -6,48 +6,50 @@
/* By: willem <willem@student.codam.nl> +#+ */ /* By: willem <willem@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/08 17:00:24 by willem #+# #+# */ /* Created: 2025/02/08 17:00:24 by willem #+# #+# */
/* Updated: 2025/02/08 19:27:05 by willem ######## odam.nl */ /* Updated: 2025/02/11 16:45:17 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
char *executor_absolute_path(t_list *env, char *cmd) char *executor_absolute_path(t_list *env, char *cmd)
{ {
char **path; char **path;
char *executable; t_environment *path_env;
int i; char *executable;
int i;
if (cmd[0] == '/' || cmd[0] == '.') if (cmd[0] == '/' || cmd[0] == '.')
{ {
if (access(cmd, F_OK) == 0) if (access(cmd, F_OK) == 0)
{ {
executable = ft_strdup(cmd); executable = ft_strdup(cmd);
if (!executable) if (!executable)
return (NULL); return (NULL);
return (executable); return (executable);
} }
return (NULL); return (NULL);
} }
path = ft_split(environment_get(env, "PATH")->value, ':'); path_env = environment_get(env, "PATH");
if (!path) if (!path_env)
return (NULL); return (NULL);
i = 0; path = ft_split(path_env->value, ':');
while (path[i] != NULL) i = 0;
{ while (path[i] != NULL)
executable = malloc(ft_strlen(path[i]) + ft_strlen(cmd) + 2); {
if (!executable) executable = malloc(ft_strlen(path[i]) + ft_strlen(cmd) + 2);
return (ft_free_arr(path), NULL); if (!executable)
ft_strlcpy(executable, path[i], ft_strlen(path[i]) + 1); return (ft_free_arr(path), NULL);
ft_strlcat(executable, "/", ft_strlen(path[i]) + 2); ft_strlcpy(executable, path[i], ft_strlen(path[i]) + 1);
ft_strlcat(executable, cmd, ft_strlen(path[i]) + ft_strlen(cmd) + 2); ft_strlcat(executable, "/", ft_strlen(path[i]) + 2);
if (access(executable, F_OK) == 0) ft_strlcat(executable, cmd, ft_strlen(path[i]) + ft_strlen(cmd) + 2);
{ if (access(executable, F_OK) == 0)
ft_free_arr(path); {
return (executable); ft_free_arr(path);
} return (executable);
free(executable); }
i++; free(executable);
} i++;
return (ft_free_arr(path), NULL); }
return (ft_free_arr(path), NULL);
} }