Mise au clair, problème de norme

This commit is contained in:
Etienne Rey-bethbeder 2023-03-08 14:47:02 +01:00
parent 5006061fa6
commit 5b3f57421a
11 changed files with 89 additions and 84 deletions

Binary file not shown.

Binary file not shown.

View file

@ -1 +0,0 @@
a

View file

@ -5,7 +5,7 @@ OBJS = ${SRCS:.c=.o}
OBJS_BONUS = ${SRCS_BONUS:.c=.o}
LIBS = libft/libft.a
CC = clang
CFLAGS = -Wall -Wextra -Werror
CFLAGS = -g -Wall -Wextra -Werror
NAME = pipex
all: ${NAME}

Binary file not shown.

View file

@ -6,36 +6,12 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/18 18:04:58 by erey-bet #+# #+# */
/* Updated: 2023/03/07 18:16:17 by erey-bet ### ########.fr */
/* Updated: 2023/03/08 14:34:26 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
int free_all(t_data *data)
{
int i;
int y;
if (data->fd1 > 0)
close(data->fd1);
close(data->fd2);
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);
i = -1;
while (++i < data->argc - 3)
free(data->cmds[i]);
free(data->cmds);
return (1);
}
void make_cmd(t_data data, int i, int fl1, int fl2)
{
if (fl1 > 0)
@ -55,33 +31,57 @@ void make_cmd(t_data data, int i, int fl1, int fl2)
execve(data.cmds[i], data.flgs[i], data.env);
}
void cmd(t_data data, int i, int t)
void choice_cmd(t_data data, int i)
{
if (t == 0)
if (i == 0)
make_cmd(data, 0, data.fd1, data.fd[0][1]);
else if (t == 1)
else if (i + 1 < data.argc - 3)
make_cmd(data, i, data.fd[(i + 1) % 2][0], data.fd[i % 2][1]);
else if (t == 2)
else
make_cmd(data, i, data.fd[(i + 1) % 2][0], data.fd2);
write(2, "Error execve\n", 13);
free_all(&data);
exit(1);
}
int pipex(int argc, t_data data)
int pipex_loop(int argc, int **fk, t_data data)
{
int *fk;
int i;
fk = ft_calloc(argc - 3, sizeof(int));
if (!fk)
i = -1;
while (++i < argc - 3)
{
if (open_close_pipe(&data, 0, i % 2))
return (1);
if ((i > 0 || (i == 0 && data.fd1 > 0)) && data.cmds[i])
{
(*fk)[i] = fork();
if ((*fk)[i] < 0)
return (1);
if ((*fk)[i] == 0)
choice_cmd(data, i);
}
if (i > 0)
if (open_close_pipe(&data, 1, (i + 1) % 2))
return (1);
}
return (open_close_pipe(&data, 1, (i + 1) % 2));
}
int pipex(int argc, t_data data)
{
int *forks;
int i;
forks = ft_calloc(argc - 3, sizeof(int));
if (!forks)
return (1);
if (pipex_loop(argc, &fk, data))
if (pipex_loop(argc, &forks, data))
return (1);
i = -1;
while (++i < argc - 3)
waitpid(fk[i], NULL, 0);
free(fk);
waitpid(forks[i], NULL, 0);
free(forks);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/11 15:33:34 by erey-bet #+# #+# */
/* Updated: 2023/03/05 23:38:16 by erey-bet ### ########.fr */
/* Updated: 2023/03/08 14:33:18 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -44,8 +44,9 @@ char *cmd_exist(char **env, char **cmd, int y, char **s);
char *find_command(char **env, char *cmd);
int here_doc(int argc, char *argv[], char **env, t_data *data);
int pipex_loop(int argc, int **fk, t_data data);
void cmd(t_data data, int i, int t);
void choice_cmd(t_data data, int i);
int verification_command(char *argv[], t_data data);
int free_all(t_data *data);
int open_close_pipe(t_data *data, int boolean, int file);
#endif

View file

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/14 11:22:13 by erey-bet #+# #+# */
/* Updated: 2023/03/07 15:46:36 by erey-bet ### ########.fr */
/* Updated: 2023/03/08 14:38:34 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -33,6 +33,20 @@ int parsing_here_doc(int argc, char *argv[], t_data *data)
return (0);
}
static int ft_strcmp(char *s1, char *s2)
{
if (!s1 || !s2)
return (-1);
while (*s1 && *s2)
{
if (*s1 != *s2)
return (*s1 - *s2);
s1++;
s2++;
}
return (*s1 - *s2);
}
int here_doc(int argc, char *argv[], char **env, t_data *data)
{
char *cur;
@ -43,8 +57,10 @@ int here_doc(int argc, char *argv[], char **env, t_data *data)
while (1)
{
cur = get_next_line(0);
if (ft_strncmp(cur, argv[2], ft_strlen(argv[2])) == 0)
cur[ft_strlen(cur) - 1] = '\0';
if (ft_strcmp(cur, argv[2]) == 0)
break ;
cur[ft_strlen(cur)] = '\n';
ft_putstr_fd(cur, fd[1]);
free(cur);
}

View file

@ -6,13 +6,13 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/14 15:46:08 by erey-bet #+# #+# */
/* Updated: 2023/03/07 18:56:52 by erey-bet ### ########.fr */
/* Updated: 2023/03/08 14:39:39 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
static int open_close_pipe(t_data *data, int boolean, int file)
int open_close_pipe(t_data *data, int boolean, int file)
{
if (!boolean)
{
@ -35,40 +35,6 @@ static int open_close_pipe(t_data *data, int boolean, int file)
return (0);
}
static void choice_command(int argc, t_data data, int i)
{
if (i == 0)
cmd(data, i, 0);
else if (i + 1 < argc - 3)
cmd(data, i, 1);
else
cmd(data, i, 2);
}
int pipex_loop(int argc, int **fk, t_data data)
{
int i;
i = -1;
while (++i < argc - 3)
{
if (open_close_pipe(&data, 0, i % 2))
return (1);
if ((i > 0 || (i == 0 && data.fd1 > 0)) && data.cmds[i])
{
(*fk)[i] = fork();
if ((*fk)[i] < 0)
return (1);
if ((*fk)[i] == 0)
choice_command(argc, data, i);
}
if (i > 0)
if (open_close_pipe(&data, 1, (i + 1) % 2))
return (1);
}
return (open_close_pipe(&data, 1, (i + 1) % 2));
}
int verification_command(char *argv[], t_data data)
{
int i;
@ -86,3 +52,27 @@ int verification_command(char *argv[], t_data data)
}
return (0);
}
int free_all(t_data *data)
{
int i;
int y;
if (data->fd1 > 0)
close(data->fd1);
close(data->fd2);
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);
i = -1;
while (++i < data->argc - 3)
free(data->cmds[i]);
free(data->cmds);
return (1);
}

View file

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/11 15:33:34 by erey-bet #+# #+# */
/* Updated: 2023/02/28 12:59:46 by erey-bet ### ########.fr */
/* Updated: 2023/03/08 11:33:24 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,13 +22,12 @@
typedef struct s_data
{
char *fl1;
char *cmd1;
char **flg1;
char *fl2;
char *cmd2;
char **flg2;
char *fl1;
char *fl2;
char *ct_fl1;
int fd1;
int fd2;
char **env;

View file

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/19 17:03:01 by erey-bet #+# #+# */
/* Updated: 2023/03/07 09:24:12 by erey-bet ### ########.fr */
/* Updated: 2023/03/08 11:29:33 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,7 +16,7 @@ int parsing(char *argv[], t_data *data)
{
data->fd2 = open(argv[4], O_WRONLY | O_TRUNC | O_CREAT, 0644);
if (data->fd2 < 0)
return (print_error(1, argv[4]));
return (print_error(0, argv[4]));
data->fd1 = open(argv[1], O_RDONLY);
if (data->fd1 < 0)
print_error(0, argv[1]);