From 719711ee1974b0cabc3129877a8a96fc79363b36 Mon Sep 17 00:00:00 2001 From: Alexandre POMMIER Date: Fri, 21 Jan 2022 04:00:43 +0100 Subject: [PATCH] maybe leaks + no check error --- libft/Makefile | 1 + libft/ft_strjoin.c | 41 ++++++++++ libft/get_next_line.c | 2 +- libft/get_next_line_utils.c | 4 +- libft/libft.h | 3 +- main.c | 147 +++++++++++++++++------------------- pipex.h | 10 ++- 7 files changed, 123 insertions(+), 85 deletions(-) create mode 100644 libft/ft_strjoin.c diff --git a/libft/Makefile b/libft/Makefile index 314810d..f402b07 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -26,6 +26,7 @@ SRCS = ft_memset.c \ ft_substr.c \ ft_strtrim.c \ ft_split.c \ + ft_strjoin.c \ ft_itoa.c \ ft_strmapi.c \ ft_putchar_fd.c \ diff --git a/libft/ft_strjoin.c b/libft/ft_strjoin.c new file mode 100644 index 0000000..4287fd2 --- /dev/null +++ b/libft/ft_strjoin.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/20 21:44:01 by apommier #+# #+# */ +/* Updated: 2022/01/20 21:57:45 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strjoin(char *save, char *s2) +{ + char *dest; + int i; + int j; + + i = 0; + j = 0; + if (!save && !s2) + return (0); + dest = malloc(ft_strlen(save) + ft_strlen(s2) + 1); + while (save && save[i]) + { + dest[j] = save[i]; + j++; + i++; + } + i = 0; + while (s2 && s2[i]) + { + dest[j] = s2[i]; + j++; + i++; + } + dest[j] = 0; + return (dest); +} \ No newline at end of file diff --git a/libft/get_next_line.c b/libft/get_next_line.c index c56d27e..b918e13 100644 --- a/libft/get_next_line.c +++ b/libft/get_next_line.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "../Utils/push_swap.h" +#include "../pipex.h" char *ft_free(char *save, int *end) { diff --git a/libft/get_next_line_utils.c b/libft/get_next_line_utils.c index ccd419d..cbe0bab 100644 --- a/libft/get_next_line_utils.c +++ b/libft/get_next_line_utils.c @@ -6,11 +6,11 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/14 06:05:54 by apommier #+# #+# */ -/* Updated: 2022/01/17 21:42:00 by apommier ### ########.fr */ +/* Updated: 2022/01/20 21:58:39 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ -#include "../Utils/push_swap.h" +#include "../pipex.h" char *ft_strjoin(char *save, char *s2) { diff --git a/libft/libft.h b/libft/libft.h index 9a8f26b..9976227 100644 --- a/libft/libft.h +++ b/libft/libft.h @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/11 03:21:27 by apommier #+# #+# */ -/* Updated: 2022/01/18 01:32:40 by apommier ### ########.fr */ +/* Updated: 2022/01/20 21:46:04 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,6 +45,7 @@ char *ft_strrchr(const char *s, int c); int ft_strncmp(const char *s1, const char *s2, size_t n); size_t ft_strlcpy(char *dst, const char *src, size_t size); size_t ft_strlcat(char *dst, const char *src, size_t size); +char *ft_strjoin(char *save, char *s2); char *ft_strnstr(const char *big, const char *little, size_t len); long ft_atoi(const char *nptr); void *ft_calloc(size_t nmenb, size_t size); diff --git a/main.c b/main.c index 5e3a79f..e4530f2 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/19 12:45:03 by apommier #+# #+# */ -/* Updated: 2022/01/21 01:16:22 by apommier ### ########.fr */ +/* Updated: 2022/01/21 03:56:28 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -47,101 +47,90 @@ char *get_command(char *exec, char **envp) i = 0; swap = 0; - cmd = ft_strjoin("/", exec); + cmd = 0; path = get_path(envp); - swap = ft_strjoin(path[i], cmd); - while (access(swap, F_OK) == -1 && path[i++]) + swap = ft_strjoin(path[i], "/"); + while (access(swap, F_OK) && path[i++]) { if (swap) free(swap); - swap = ft_strjoin(path[i], cmd); + swap = ft_strjoin(path[i], "/"); + cmd = swap; + swap = ft_strjoin(swap, exec); + free(cmd); } - printf("cmd = %s path = %s\n", cmd, swap); - free(cmd); + if (!path[i]) + return (0); return (swap); } -void pipex(int fd1, int fd2, char **envp, char **argv) +void child_process(char **envp, char **argv, int *end) { - int pipe_tab[2]; + int fd1; + char **exec; + char *cmd1; + + fd1 = open(argv[1], O_RDONLY); + exec = ft_split(argv[2], ' '); + cmd1 = get_command(exec[0], envp); + if (dup2(end[1], 1) == -1) + { + perror("Error"); + exit(1); + } + if (dup2(fd1, 0) == -1) + { + perror("Error"); + exit(1); + } + close(end[0]); + close(fd1); + if (execve(cmd1, exec, envp) == -1) + perror("Error"); + free(cmd1); +} + + +void parent_process(char **env, char **argv, int *end) +{ + char **exec; + char *cmd2; + int status; + int fd2; + + + waitpid(-1, &status, 0); + exec = ft_split(argv[3], ' '); + fd2 = open(argv[4], O_RDWR | O_CREAT | O_TRUNC, 0666); + cmd2 = get_command(exec[0], env); + if (dup2(end[0], 0) == -1) + exit(1); + if (dup2(fd2, 1) == -1) + exit(1); + close(end[1]); + close(fd2); + if (execve(cmd2, exec, env) == -1) + perror("Error"); +} + +int main(int argc, char **argv, char **envp) +{ + int end[2]; + char *cmd1; + char *cmd2; pid_t parent; + pipe(end); parent = fork(); if (parent < 0) { printf("here\n"); perror("Error"); - return ; + return (0); } if (!parent) - child_process(fd1, envp, argv, pipe_tab); + child_process(envp, argv, end); else - parent_process(fd2, envp, argv, pipe_tab); - printf("pipex end\n"); -} - -void child_process(int fd1, char **envp, char **argv, int *pipe_tab) -{ - int i; - char *cmd1; - - cmd1 = 0; - i = 0; - if (dup2(fd1, 0) == -1) - { - perror("Error"); - return ; - } - if (dup2(pipe_tab[1], 1) == -1) - { - perror("Error"); - return ; - } - close(pipe_tab[0]); - close(fd1); - cmd1 = get_command(argv[2], envp); - if (cmd1) - { - execve(cmd1, argv, envp); - free(cmd1); - } -} - -void parent_process(int fd2, char **envp, char **argv, int *pipe_tab) -{ - int i; - int status; - char *cmd2; - - waitpid(-1, &status, 0); - cmd2 = 0; - i = 0; - if (!dup2(fd2, 1)) - return ; - if (!dup2(pipe_tab[0], 0)) - return ; - close(pipe_tab[1]); - close(fd2); - cmd2 = get_command(argv[3], envp); - if (cmd2) - { - execve(cmd2, argv, envp); - free(cmd2); - } -} -int main(int argc, char **argv, char **envp) -{ - int fd1; - int fd2; - char *path; - - //if (check_error) - // return (0); - fd1 = open(argv[1], O_RDONLY); - fd2 = open(argv[4], O_RDWR | O_CREAT | O_TRUNC, 0644); - if (!fd1 || !fd2) - return (0); - pipex(fd1, fd2 , envp, argv); - printf("--end--\n"); - return (1); + parent_process(envp, argv, end); + return (0); } diff --git a/pipex.h b/pipex.h index aada620..870edb8 100644 --- a/pipex.h +++ b/pipex.h @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/19 12:45:13 by apommier #+# #+# */ -/* Updated: 2022/01/20 19:38:53 by apommier ### ########.fr */ +/* Updated: 2022/01/21 03:00:34 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,4 +15,10 @@ #include #include #include -#include "./libft/libft.h" \ No newline at end of file +#include "./libft/libft.h" + +char **get_path(char **envp); +char *get_command(char *exec, char **envp); +void pipex(char **envp, char **argv); +void child_process(char **envp, char **argv, int *pipe_tab); +void parent_process(char **envp, char **argv, int *pipe_tab);