Files
minishell/srcs/path.c
2024-05-06 14:29:15 +02:00

105 lines
2.3 KiB
C
Executable File

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* path.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/21 21:47:15 by marde-vr #+# #+# */
/* Updated: 2024/05/06 14:25:40 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include <unistd.h>
void find_cmd_path(t_msh *msh, char **paths, int *found)
{
char *tmp;
char *path;
int i;
i = 0;
while (paths[i])
{
path = paths[i];
tmp = ft_strjoin(path, "/");
if (!tmp)
ft_exit(msh, 1);
path = ft_strjoin(tmp, msh->tokens->value);
if (!path)
ft_exit(msh, 1);
free(tmp);
if (access(path, X_OK) != -1)
{
*found = 1;
free(msh->tokens->value);
msh->tokens->value = path;
break ;
}
free(path);
i++;
}
}
void free_paths(char **paths)
{
int i;
i = 0;
while (paths && paths[i])
{
free(paths[i]);
i++;
}
free(paths);
}
void get_path(t_msh *msh, int *found)
{
char **paths;
paths = split_paths_from_env(msh->env);
if (!paths || !*(msh->tokens->value)
|| ft_str_is_only_char(msh->tokens->value, '.'))
{
free_paths(paths);
return ;
}
find_cmd_path(msh, paths, found);
free_paths(paths);
}
char *remove_path(char *token)
{
while (ft_strchr(token, '/'))
token++;
return (token);
}
void get_cmd_path(t_msh *msh)
{
int found;
found = 0;
if (ft_strchr(msh->tokens->value, '/'))
{
if (!file_access(msh, &found))
{
free(msh->tokens->value);
msh->tokens->value = 0;
return ;
}
}
else
get_path(msh, &found);
if (!found)
{
if (!*(msh->tokens->value))
ft_printf_fd(2, "'': command not found\n");
else
ft_printf_fd(2, "%s: command not found\n", msh->tokens->value);
g_return_code = 127;
}
}