95 lines
2.3 KiB
C
95 lines
2.3 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* pipex_parsing.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2023/01/19 17:03:01 by erey-bet #+# #+# */
|
|
/* Updated: 2023/01/19 17:05:16 by erey-bet ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "../pipex.h"
|
|
|
|
int parsing(char *argv[], t_data *data)
|
|
{
|
|
data->fd2 = open(argv[4], O_WRONLY | O_TRUNC | O_CREAT);
|
|
if (data->fd2 < 0)
|
|
return (1);
|
|
data->fd1 = open(argv[1], O_RDONLY);
|
|
if (data->fd1 < 0)
|
|
print_error(0, argv[1]);
|
|
data->cmd1 = get_command(argv[2]);
|
|
data->cmd2 = get_command(argv[3]);
|
|
data->flg1 = ft_split(argv[2], ' ');
|
|
data->flg2 = ft_split(argv[3], ' ');
|
|
data->fl1 = argv[1];
|
|
data->fl2 = argv[4];
|
|
return (0);
|
|
}
|
|
|
|
char *test_acces(char **s, char **cmd, char **path, int *y)
|
|
{
|
|
*s = ft_strjoin_free(*s, "/", 1);
|
|
if (*s == NULL)
|
|
return (NULL);
|
|
*path = ft_strjoin_free(*s, *cmd, 1);
|
|
if (*path == NULL)
|
|
return (NULL);
|
|
if (access(*path, X_OK) == 0)
|
|
{
|
|
free(*cmd);
|
|
return (*path);
|
|
}
|
|
free(*path);
|
|
*path = NULL;
|
|
*y = 0;
|
|
return (NULL);
|
|
}
|
|
|
|
char *cmd_exist(char **env, char **cmd, int y, char **s)
|
|
{
|
|
char *path;
|
|
int i;
|
|
|
|
i = 5;
|
|
while ((*env)[i])
|
|
{
|
|
if ((*env)[i] != ':')
|
|
(*s)[y++] = (*env)[i];
|
|
else
|
|
{
|
|
test_acces(s, cmd, &path, &y);
|
|
if (path)
|
|
return (path);
|
|
*s = ft_calloc(ft_strlen(*env) + 1, 1);
|
|
if (*s == NULL)
|
|
return (NULL);
|
|
}
|
|
i++;
|
|
}
|
|
free(*s);
|
|
free(*cmd);
|
|
return (NULL);
|
|
}
|
|
|
|
char *find_command(char **env, char *cmd)
|
|
{
|
|
char *s;
|
|
int y;
|
|
|
|
while (*env != NULL)
|
|
{
|
|
if (ft_strnstr(*env, "PATH=/", ft_strlen(*env))
|
|
&& ft_strchr(*env, ':') > 0)
|
|
break ;
|
|
env++;
|
|
}
|
|
y = 0;
|
|
s = ft_calloc(ft_strlen(*env) + 1, 1);
|
|
if (s == NULL)
|
|
return (NULL);
|
|
return (cmd_exist(env, &cmd, y, &s));
|
|
}
|