diff --git a/Makefile b/Makefile index 00b1cf2..8e6f6c3 100644 --- a/Makefile +++ b/Makefile @@ -13,11 +13,11 @@ NAME = pipex all: ${NAME} ${NAME}: ${OBJS} - make -C libft && print + make -C libft ${CC} ${CFLAGS} -o ${NAME} ${OBJS} ${LIBS} bonus: ${OBJS_BONUS} - make -C libft && print + make -C libft ${CC} ${CFLAGS} -o ${NAME} ${OBJS_BONUS} ${LIBS} %.o:%.c diff --git a/get_next_line/get_next_line.o b/get_next_line/get_next_line.o index 3f937bc..4882671 100644 Binary files a/get_next_line/get_next_line.o and b/get_next_line/get_next_line.o differ diff --git a/get_next_line/get_next_line_utils.o b/get_next_line/get_next_line_utils.o index f0ec4a7..8ab93b1 100644 Binary files a/get_next_line/get_next_line_utils.o and b/get_next_line/get_next_line_utils.o differ diff --git a/input b/input index 9daeafb..e69de29 100644 --- a/input +++ b/input @@ -1 +0,0 @@ -test diff --git a/libft/ft_atoi.o b/libft/ft_atoi.o index 17303a8..600e718 100644 Binary files a/libft/ft_atoi.o and b/libft/ft_atoi.o differ diff --git a/libft/ft_atoi_check.o b/libft/ft_atoi_check.o index 843ec1e..426a42f 100644 Binary files a/libft/ft_atoi_check.o and b/libft/ft_atoi_check.o differ diff --git a/libft/ft_bzero.o b/libft/ft_bzero.o index 8f185a2..192a08e 100644 Binary files a/libft/ft_bzero.o and b/libft/ft_bzero.o differ diff --git a/libft/ft_calloc.o b/libft/ft_calloc.o index 13201ce..e49c7d7 100644 Binary files a/libft/ft_calloc.o and b/libft/ft_calloc.o differ diff --git a/libft/ft_get_size.o b/libft/ft_get_size.o index 9d09ecc..b4af639 100644 Binary files a/libft/ft_get_size.o and b/libft/ft_get_size.o differ diff --git a/libft/ft_isalnum.o b/libft/ft_isalnum.o index c08b5fa..0c75fb9 100644 Binary files a/libft/ft_isalnum.o and b/libft/ft_isalnum.o differ diff --git a/libft/ft_isalpha.o b/libft/ft_isalpha.o index b703773..5de1912 100644 Binary files a/libft/ft_isalpha.o and b/libft/ft_isalpha.o differ diff --git a/libft/ft_isascii.o b/libft/ft_isascii.o index 926ca43..7c2d1d1 100644 Binary files a/libft/ft_isascii.o and b/libft/ft_isascii.o differ diff --git a/libft/ft_isdigit.o b/libft/ft_isdigit.o index 4a1c779..36a4cc5 100644 Binary files a/libft/ft_isdigit.o and b/libft/ft_isdigit.o differ diff --git a/libft/ft_isprint.o b/libft/ft_isprint.o index 3a498c8..a8ca869 100644 Binary files a/libft/ft_isprint.o and b/libft/ft_isprint.o differ diff --git a/libft/ft_itoa.o b/libft/ft_itoa.o index 1decb7c..bef8678 100644 Binary files a/libft/ft_itoa.o and b/libft/ft_itoa.o differ diff --git a/libft/ft_memchr.o b/libft/ft_memchr.o index c515b83..281f08d 100644 Binary files a/libft/ft_memchr.o and b/libft/ft_memchr.o differ diff --git a/libft/ft_memcmp.o b/libft/ft_memcmp.o index e45e49d..d5c245c 100644 Binary files a/libft/ft_memcmp.o and b/libft/ft_memcmp.o differ diff --git a/libft/ft_memcpy.o b/libft/ft_memcpy.o index ba83c2a..a8c1c90 100644 Binary files a/libft/ft_memcpy.o and b/libft/ft_memcpy.o differ diff --git a/libft/ft_memmove.o b/libft/ft_memmove.o index 2974700..f847ccc 100644 Binary files a/libft/ft_memmove.o and b/libft/ft_memmove.o differ diff --git a/libft/ft_memset.o b/libft/ft_memset.o index 8b36b44..8aad44b 100644 Binary files a/libft/ft_memset.o and b/libft/ft_memset.o differ diff --git a/libft/ft_power.o b/libft/ft_power.o index 9e4459c..252758b 100644 Binary files a/libft/ft_power.o and b/libft/ft_power.o differ diff --git a/libft/ft_putchar_fd.o b/libft/ft_putchar_fd.o index bd2f6b6..356fe67 100644 Binary files a/libft/ft_putchar_fd.o and b/libft/ft_putchar_fd.o differ diff --git a/libft/ft_putendl_fd.o b/libft/ft_putendl_fd.o index 364faed..ce3088a 100644 Binary files a/libft/ft_putendl_fd.o and b/libft/ft_putendl_fd.o differ diff --git a/libft/ft_putnbr_fd.o b/libft/ft_putnbr_fd.o index 3bcbc7e..d70f8af 100644 Binary files a/libft/ft_putnbr_fd.o and b/libft/ft_putnbr_fd.o differ diff --git a/libft/ft_putstr_fd.o b/libft/ft_putstr_fd.o index accddc2..73ecb9a 100644 Binary files a/libft/ft_putstr_fd.o and b/libft/ft_putstr_fd.o differ diff --git a/libft/ft_split.o b/libft/ft_split.o index d6931e4..ff40892 100644 Binary files a/libft/ft_split.o and b/libft/ft_split.o differ diff --git a/libft/ft_strchr.o b/libft/ft_strchr.o index 7afcea5..2b37ecb 100644 Binary files a/libft/ft_strchr.o and b/libft/ft_strchr.o differ diff --git a/libft/ft_strdup.o b/libft/ft_strdup.o index 9e2087b..145c148 100644 Binary files a/libft/ft_strdup.o and b/libft/ft_strdup.o differ diff --git a/libft/ft_strdup_free.o b/libft/ft_strdup_free.o index 085b5f2..db4ccc0 100644 Binary files a/libft/ft_strdup_free.o and b/libft/ft_strdup_free.o differ diff --git a/libft/ft_strdups.o b/libft/ft_strdups.o index b438dae..40faa82 100644 Binary files a/libft/ft_strdups.o and b/libft/ft_strdups.o differ diff --git a/libft/ft_striteri.o b/libft/ft_striteri.o index 9c22927..7557153 100644 Binary files a/libft/ft_striteri.o and b/libft/ft_striteri.o differ diff --git a/libft/ft_strjoin.o b/libft/ft_strjoin.o index 6c66b4a..de70217 100644 Binary files a/libft/ft_strjoin.o and b/libft/ft_strjoin.o differ diff --git a/libft/ft_strjoin_free.o b/libft/ft_strjoin_free.o index 4614d90..069349b 100644 Binary files a/libft/ft_strjoin_free.o and b/libft/ft_strjoin_free.o differ diff --git a/libft/ft_strlcat.o b/libft/ft_strlcat.o index b9b8ba3..99ea065 100644 Binary files a/libft/ft_strlcat.o and b/libft/ft_strlcat.o differ diff --git a/libft/ft_strlcpy.o b/libft/ft_strlcpy.o index 36935b9..68ef1f1 100644 Binary files a/libft/ft_strlcpy.o and b/libft/ft_strlcpy.o differ diff --git a/libft/ft_strlen.o b/libft/ft_strlen.o index 44a92ad..8ec0fa6 100644 Binary files a/libft/ft_strlen.o and b/libft/ft_strlen.o differ diff --git a/libft/ft_strlen_double.o b/libft/ft_strlen_double.o index 0c68076..ac1b352 100644 Binary files a/libft/ft_strlen_double.o and b/libft/ft_strlen_double.o differ diff --git a/libft/ft_strmapi.o b/libft/ft_strmapi.o index a6ed151..99e2140 100644 Binary files a/libft/ft_strmapi.o and b/libft/ft_strmapi.o differ diff --git a/libft/ft_strncmp.o b/libft/ft_strncmp.o index 6a8cdb1..5689389 100644 Binary files a/libft/ft_strncmp.o and b/libft/ft_strncmp.o differ diff --git a/libft/ft_strnstr.o b/libft/ft_strnstr.o index 6892ee6..93952f3 100644 Binary files a/libft/ft_strnstr.o and b/libft/ft_strnstr.o differ diff --git a/libft/ft_strrchr.o b/libft/ft_strrchr.o index cd68480..4cccd4f 100644 Binary files a/libft/ft_strrchr.o and b/libft/ft_strrchr.o differ diff --git a/libft/ft_strslen.o b/libft/ft_strslen.o index 45ef044..ffdaef8 100644 Binary files a/libft/ft_strslen.o and b/libft/ft_strslen.o differ diff --git a/libft/ft_strtrim.o b/libft/ft_strtrim.o index 7df8f10..c939d95 100644 Binary files a/libft/ft_strtrim.o and b/libft/ft_strtrim.o differ diff --git a/libft/ft_substr.o b/libft/ft_substr.o index 153f309..0f3455e 100644 Binary files a/libft/ft_substr.o and b/libft/ft_substr.o differ diff --git a/libft/ft_tolower.o b/libft/ft_tolower.o index e83e769..cd26d05 100644 Binary files a/libft/ft_tolower.o and b/libft/ft_tolower.o differ diff --git a/libft/ft_toupper.o b/libft/ft_toupper.o index ff59a8b..9686687 100644 Binary files a/libft/ft_toupper.o and b/libft/ft_toupper.o differ diff --git a/libft/libft.a b/libft/libft.a index 6dd6e56..d513a6e 100644 Binary files a/libft/libft.a and b/libft/libft.a differ diff --git a/pipex b/pipex index 25d0828..76011ba 100755 Binary files a/pipex and b/pipex differ diff --git a/pipex.c b/pipex.c index 36c82bd..d0b8340 100644 --- a/pipex.c +++ b/pipex.c @@ -19,7 +19,7 @@ char *read_file(int fd) return (str); } -int get_next(char *cmd, char c) +int get_next(char *cmd, char c) { int i; @@ -36,7 +36,7 @@ char *get_flags(char *cmd) char *new_str; 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; while (cmd[i]) new_str[y++] = cmd[i++]; @@ -49,7 +49,7 @@ char *get_command(char *cmd) int i; char *new_str; - new_str = ft_calloc(get_next(cmd, ' ') + 1); + new_str = ft_calloc(get_next(cmd, ' ') + 1, 1); i = -1; while (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); if (fd2 < 0) return (-1); - fd1 = open(argv[1], O_RDONLY); + fd1 = open(argv[1], O_RDONLY | O_TRUNC | O_CREAT); if (fd1 < 0) { close(fd2); @@ -78,63 +78,110 @@ int parsing(char *argv[], t_data *data) data->fl1 = argv[1]; data->fl2 = argv[4]; data->ct_fl1 = read_file(fd1); - close(fd1); + data->fd1 = fd1; + data->fd2 = fd2; return (0); } -int find_command(char **env) +char *find_command(char **env, char *cmd) { + char *path; char *s; + int fd; + int i; + int y; while (*env != NULL) { if (ft_strnstr(*env, "PATH=/", ft_strlen(*env))) - s = *env; + break; 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]); - 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 fd[2] - int id; + int fd[2]; + int cmd1; + int cmd2; if (pipe(fd) < -1) return (1); - id = fork(); - if (id == 0) - 1_cmd(fd, data); - else - 2_cmd(fd, data); + cmd1 = fork(); + if (cmd1 == 0) + first_cmd(fd, data); + cmd2 = fork(); + if (cmd2 == 0) + second_cmd(fd, data); + close(fd[0]); + close(fd[1]); + waitpid(cmd1, NULL, 0); + waitpid(cmd2, NULL, 0); + return (0); } #include int main(int argc, char *argv[], char **env) { - //t_data data; + t_data data; - (void)argc; - (void)argv; - /*if (argc < 5) + //(void)argc; + //(void)argv; + if (argc < 5) return (1); if (parsing(argv, &data)) return (2); - find_command(env); - if (pipex(data)) + data.cmd1 = find_command(env, data.cmd1); + if (data.cmd1 == NULL) 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); } - diff --git a/pipex.h b/pipex.h index 69c4ff0..e57f4db 100644 --- a/pipex.h +++ b/pipex.h @@ -17,6 +17,8 @@ # include "get_next_line/get_next_line.h" # include # include +# include +# include typedef struct s_data { @@ -27,6 +29,8 @@ typedef struct s_data char *fl1; char *fl2; char *ct_fl1; + int fd1; + int fd2; } t_data; #endif diff --git a/pipex.o b/pipex.o index 7e82803..a8feddf 100644 Binary files a/pipex.o and b/pipex.o differ