42_minishell/path.c
joris schaft 17770aa6ae a
2024-04-23 16:04:24 +02:00

61 lines
1.5 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* path.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 12:07:26 by jschaft #+# #+# */
/* Updated: 2024/04/23 15:08:11 by jschaft ### ########.fr */
/* */
/* ************************************************************************** */
#include "minish.h"
char **get_path(char **envp)
{
int i;
char **re;
char *path;
i = 0;
while (ft_strncmp(envp[i], "PATH=", 5) != 0)
i++;
path = ft_substr(envp[i], 5, ft_strlen(envp[i]) - 5);
re = ft_split(path, ':');
free(path);
return (re);
}
int search_path(char **path)
{
int i;
i = 0;
while (path[i] != NULL)
{
if (access(path[i], X_OK) >= 0)
return (i);
i++;
}
return (-1);
}
char **cp_path(char **path)
{
char **re;
int i;
i = 0;
while (path[i] != NULL)
i++;
re = malloc(sizeof(char *) * i + 1);
i = 0;
while (path[i] != NULL)
{
re[i] = ft_strdup(path[i]);
i++;
}
re[i] == NULL;
return (re);
}