2024-05-19 05:49:22 +02:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* get_path.c :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2024/05/19 01:42:17 by mmoussou #+# #+# */
|
2024-06-13 14:21:45 +02:00
|
|
|
/* Updated: 2024/06/13 14:19:20 by mmoussou ### ########.fr */
|
2024-05-19 05:49:22 +02:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
#include "minishell.h"
|
|
|
|
|
2024-06-13 14:21:45 +02:00
|
|
|
char *check_path_access(char *path, char *cmd)
|
2024-06-05 01:23:18 +02:00
|
|
|
{
|
2024-06-13 14:21:45 +02:00
|
|
|
char *abs_path;
|
2024-06-05 01:23:18 +02:00
|
|
|
|
2024-06-13 14:21:45 +02:00
|
|
|
abs_path = ft_strjoin(path, "/");
|
2024-06-05 01:23:18 +02:00
|
|
|
if (!abs_path)
|
2024-06-13 14:21:45 +02:00
|
|
|
return (NULL);
|
|
|
|
abs_path = ft_strjoin_free_s1(abs_path, cmd);
|
|
|
|
if (!abs_path)
|
|
|
|
return (NULL);
|
|
|
|
if (access(abs_path, X_OK) == 0)
|
|
|
|
return (abs_path);
|
|
|
|
free(abs_path);
|
|
|
|
return (NULL);
|
2024-06-05 01:23:18 +02:00
|
|
|
}
|
|
|
|
|
2024-06-13 14:21:45 +02:00
|
|
|
char *get_path(char *path, char *cmd)
|
2024-05-19 05:49:22 +02:00
|
|
|
{
|
|
|
|
char **path_dir;
|
|
|
|
int i;
|
|
|
|
|
2024-06-04 22:28:13 +02:00
|
|
|
if (!path)
|
|
|
|
return (NULL);
|
2024-05-19 05:49:22 +02:00
|
|
|
path_dir = ft_split(path, ':');
|
|
|
|
if (!path_dir)
|
2024-06-04 21:40:14 +02:00
|
|
|
return (NULL);
|
2024-05-19 05:49:22 +02:00
|
|
|
i = 0;
|
|
|
|
while (path_dir[i])
|
|
|
|
{
|
2024-06-13 14:21:45 +02:00
|
|
|
path = check_path_access(path_dir[i], cmd);
|
|
|
|
if (path)
|
|
|
|
{
|
|
|
|
ft_free("ac", &path_dir, &cmd);
|
|
|
|
return (path);
|
|
|
|
}
|
2024-06-02 21:17:02 +02:00
|
|
|
i++;
|
|
|
|
}
|
2024-06-13 14:21:45 +02:00
|
|
|
ft_free("ac", &path_dir, &cmd);
|
|
|
|
return (path);
|
2024-05-19 05:49:22 +02:00
|
|
|
}
|