94 lines
2.1 KiB
C
94 lines
2.1 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* pipex_utils2_bonus.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2023/02/14 15:46:08 by erey-bet #+# #+# */
|
|
/* Updated: 2023/03/22 16:24:46 by erey-bet ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "pipex_bonus.h"
|
|
|
|
int open_close_pipe(t_data *data, int boolean, int file)
|
|
{
|
|
if (!boolean)
|
|
{
|
|
if (!file)
|
|
if (pipe(data->fd[0]) < 0)
|
|
return (1);
|
|
if (file)
|
|
if (pipe(data->fd[1]) < 0)
|
|
return (1);
|
|
}
|
|
else
|
|
{
|
|
if (!file)
|
|
if (close(data->fd[0][0]) < 0 || close(data->fd[0][1]) < 0)
|
|
return (1);
|
|
if (file)
|
|
if (close(data->fd[1][0]) < 0 || close(data->fd[1][1]) < 0)
|
|
return (1);
|
|
}
|
|
return (0);
|
|
}
|
|
|
|
int verification_command(char *argv[], t_data data)
|
|
{
|
|
int i;
|
|
|
|
i = -1;
|
|
while (++i < data.argc - 3)
|
|
{
|
|
if (data.cmds[i])
|
|
{
|
|
data.cmds[i] = find_command(data.env, data.cmds[i]);
|
|
if (data.cmds[i] == NULL)
|
|
{
|
|
print_error(1, argv[2 + i]);
|
|
if (i == data.argc - 4)
|
|
return (1);
|
|
}
|
|
}
|
|
}
|
|
return (0);
|
|
}
|
|
|
|
void free_flgs(t_data *data)
|
|
{
|
|
int y;
|
|
int i;
|
|
|
|
y = -1;
|
|
while (++y < data->argc - 3)
|
|
{
|
|
i = -1;
|
|
while (data->flgs[y][++i])
|
|
free(data->flgs[y][i]);
|
|
free(data->flgs[y]);
|
|
}
|
|
free(data->flgs);
|
|
}
|
|
|
|
int free_all(t_data *data)
|
|
{
|
|
int i;
|
|
|
|
if (data->fd1 > 0)
|
|
close(data->fd1);
|
|
if (data->fd2 > 0)
|
|
close(data->fd2);
|
|
if (data->flgs)
|
|
free_flgs(data);
|
|
if (data->cmds)
|
|
{
|
|
i = -1;
|
|
while (++i < data->argc - 3)
|
|
free(data->cmds[i]);
|
|
free(data->cmds);
|
|
}
|
|
return (1);
|
|
}
|