42_minishell/utils/ft_get_executable.c
Etienne Rey-bethbeder 0b56e95868 _
2023-04-14 16:17:35 +02:00

116 lines
2.8 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_get_executable.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/27 13:39:48 by cchauvet #+# #+# */
/* Updated: 2023/04/05 15:09:29 by alouis-j ### ########.fr */
/* */
/* ************************************************************************** */
#include "./utils.h"
#include <unistd.h>
char *ft_get_executable_with_path(t_data *data, const char *name)
{
char *path;
if (access(name, F_OK) != 0)
{
*data->exit_code = 127;
ft_eprintf("bozoshell: %s: No such file or directery\n", name);
return (NULL);
}
if (access(name, X_OK) != 0)
{
*data->exit_code = 126;
ft_eprintf("bozoshell: %s: permission denied\n", name);
return (NULL);
}
path = ft_strdup(name);
if (path == NULL)
{
ft_eprintf("bozoshell: malloc failed\n");
return (NULL);
}
return (path);
}
static char **ft_get_paths(t_data *data, const char *name)
{
char *paths;
char **tab;
paths = get_value_by_key("PATH", data->env);
if (paths == NULL)
{
*data->exit_code = 127;
ft_eprintf("bozoshell: %s: command not found\n", name);
return (NULL);
}
tab = ft_split(paths, ':');
if (tab == NULL)
{
ft_eprintf("bozoshell: malloc failed\n");
return (NULL);
}
return (tab);
}
static char *ft_file_is_executable(const char *path, const char *name)
{
char *out;
out = ft_strmerger(3, path, "/", name);
if (out == NULL)
{
ft_eprintf("bozoshell: malloc failed\n");
free(out);
return (NULL);
}
if (access(out, X_OK) == 0)
return (out);
free(out);
return (NULL);
}
static char *ft_get_executable_without_path(t_data *data, const char *name)
{
char **paths;
char *path;
size_t i;
paths = ft_get_paths(data, name);
if (paths == NULL)
return (NULL);
path = NULL;
i = 0;
while (paths[i] != NULL)
{
path = ft_file_is_executable(paths[i], name);
if (path != NULL)
break ;
i++;
}
ft_freer_tab_ultimate(1, paths);
if (path == NULL)
{
*data->exit_code = 127;
ft_eprintf("bozoshell: %s: command not found\n", name);
}
return (path);
}
char *ft_get_executable(t_data *data, const char *name)
{
char *path;
if (name[0] == '.' || name[0] == '/')
path = ft_get_executable_with_path(data, name);
else
path = ft_get_executable_without_path(data, name);
return (path);
}