This commit is contained in:
xamora 2023-01-16 11:55:27 +01:00
parent ce946aeed1
commit 76d8f65ca2
51 changed files with 79 additions and 29 deletions

View file

@ -13,11 +13,11 @@ NAME = pipex
all: ${NAME} all: ${NAME}
${NAME}: ${OBJS} ${NAME}: ${OBJS}
make -C libft && print make -C libft
${CC} ${CFLAGS} -o ${NAME} ${OBJS} ${LIBS} ${CC} ${CFLAGS} -o ${NAME} ${OBJS} ${LIBS}
bonus: ${OBJS_BONUS} bonus: ${OBJS_BONUS}
make -C libft && print make -C libft
${CC} ${CFLAGS} -o ${NAME} ${OBJS_BONUS} ${LIBS} ${CC} ${CFLAGS} -o ${NAME} ${OBJS_BONUS} ${LIBS}
%.o:%.c %.o:%.c

Binary file not shown.

Binary file not shown.

1
input
View file

@ -1 +0,0 @@
test

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
pipex

Binary file not shown.

97
pipex.c
View file

@ -36,7 +36,7 @@ char *get_flags(char *cmd)
char *new_str; char *new_str;
i = get_next(cmd, ' '); i = get_next(cmd, ' ');
new_str = ft_calloc(ft_strlen(cmd) - i + 1); new_str = ft_calloc(ft_strlen(cmd) - i + 1, 1);
y = i; y = i;
while (cmd[i]) while (cmd[i])
new_str[y++] = cmd[i++]; new_str[y++] = cmd[i++];
@ -49,7 +49,7 @@ char *get_command(char *cmd)
int i; int i;
char *new_str; char *new_str;
new_str = ft_calloc(get_next(cmd, ' ') + 1); new_str = ft_calloc(get_next(cmd, ' ') + 1, 1);
i = -1; i = -1;
while (cmd[++i] != ' ') while (cmd[++i] != ' ')
new_str[i] = cmd[i]; new_str[i] = cmd[i];
@ -65,7 +65,7 @@ int parsing(char *argv[], t_data *data)
fd2 = open(argv[4], O_WRONLY | O_TRUNC | O_CREAT); fd2 = open(argv[4], O_WRONLY | O_TRUNC | O_CREAT);
if (fd2 < 0) if (fd2 < 0)
return (-1); return (-1);
fd1 = open(argv[1], O_RDONLY); fd1 = open(argv[1], O_RDONLY | O_TRUNC | O_CREAT);
if (fd1 < 0) if (fd1 < 0)
{ {
close(fd2); close(fd2);
@ -78,63 +78,110 @@ int parsing(char *argv[], t_data *data)
data->fl1 = argv[1]; data->fl1 = argv[1];
data->fl2 = argv[4]; data->fl2 = argv[4];
data->ct_fl1 = read_file(fd1); data->ct_fl1 = read_file(fd1);
close(fd1); data->fd1 = fd1;
data->fd2 = fd2;
return (0); return (0);
} }
int find_command(char **env) char *find_command(char **env, char *cmd)
{ {
char *path;
char *s; char *s;
int fd;
int i;
int y;
while (*env != NULL) while (*env != NULL)
{ {
if (ft_strnstr(*env, "PATH=/", ft_strlen(*env))) if (ft_strnstr(*env, "PATH=/", ft_strlen(*env)))
s = *env; break;
env++; env++;
} }
i = 4;
y = 4;
s = malloc(ft_strlen(*env) + 1);
while (*env[i])
{
if (*env[i] != ':')
s[y++] = *env[i];
else
{
path = ft_strjoin(s, cmd);
fd = open(path, O_RDONLY);
free(path);
if (fd != -1)
{
free(s);
return (path);
}
y = 0;
}
i++;
}
return (NULL);
} }
int first_cmd(int fd[2], t_data data) void first_cmd(int fd[2], t_data data)
{ {
dup2(data.fd1, 0);
dup2(fd[1], 1);
close(fd[0]); close(fd[0]);
execve(data->cmd1) close(fd[1]);
execve(data.cmd1, &data.flg1, NULL);
} }
int second_cmd(int fd[2], t_data data) void second_cmd(int fd[2], t_data data)
{ {
dup2(fd[0], 0);
dup2(data.fd2, 1);
close(fd[0]);
close(fd[1]);
execve(data.cmd2, &data.flg2, NULL);
} }
int pipex(t_data data) int pipex(t_data data)
{ {
int fd[2] int fd[2];
int id; int cmd1;
int cmd2;
if (pipe(fd) < -1) if (pipe(fd) < -1)
return (1); return (1);
id = fork(); cmd1 = fork();
if (id == 0) if (cmd1 == 0)
1_cmd(fd, data); first_cmd(fd, data);
else cmd2 = fork();
2_cmd(fd, data); if (cmd2 == 0)
second_cmd(fd, data);
close(fd[0]);
close(fd[1]);
waitpid(cmd1, NULL, 0);
waitpid(cmd2, NULL, 0);
return (0);
} }
#include <stdio.h> #include <stdio.h>
int main(int argc, char *argv[], char **env) int main(int argc, char *argv[], char **env)
{ {
//t_data data; t_data data;
(void)argc; //(void)argc;
(void)argv; //(void)argv;
/*if (argc < 5) if (argc < 5)
return (1); return (1);
if (parsing(argv, &data)) if (parsing(argv, &data))
return (2); return (2);
find_command(env); data.cmd1 = find_command(env, data.cmd1);
if (pipex(data)) if (data.cmd1 == NULL)
return (3); return (3);
free(data.fl1);*/ data.cmd2 = find_command(env, data.cmd2);
if (data.cmd2 == NULL)
return (4);
if (pipex(data))
return (5);
free(data.fl1);
close(data.fd1);
close(data.fd2);
return (0); return (0);
} }

View file

@ -17,6 +17,8 @@
# include "get_next_line/get_next_line.h" # include "get_next_line/get_next_line.h"
# include <fcntl.h> # include <fcntl.h>
# include <unistd.h> # include <unistd.h>
# include <sys/types.h>
# include <sys/wait.h>
typedef struct s_data typedef struct s_data
{ {
@ -27,6 +29,8 @@ typedef struct s_data
char *fl1; char *fl1;
char *fl2; char *fl2;
char *ct_fl1; char *ct_fl1;
int fd1;
int fd2;
} t_data; } t_data;
#endif #endif

BIN
pipex.o

Binary file not shown.