diff --git a/libft/ft_split.c b/libft/ft_split.c index 1fd6a7f..4434dbb 100644 --- a/libft/ft_split.c +++ b/libft/ft_split.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/12/07 00:54:12 by apommier #+# #+# */ -/* Updated: 2022/01/17 11:34:08 by apommier ### ########.fr */ +/* Updated: 2022/01/21 08:09:38 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -71,7 +71,7 @@ char **ft_split(char const *s, char c) while (s[i] == c && s[i]) i++; } - dest = (char **)malloc(sizeof(char *) * (i + j)); + dest = (char **)calloc(sizeof(char *), (i + j)); if (!dest) return (0); dest[j] = 0; diff --git a/libft/ft_strjoin.c b/libft/ft_strjoin.c index 4287fd2..da55361 100644 --- a/libft/ft_strjoin.c +++ b/libft/ft_strjoin.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/20 21:44:01 by apommier #+# #+# */ -/* Updated: 2022/01/20 21:57:45 by apommier ### ########.fr */ +/* Updated: 2022/01/21 08:07:04 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,4 +38,4 @@ char *ft_strjoin(char *save, char *s2) } dest[j] = 0; return (dest); -} \ No newline at end of file +} diff --git a/main.c b/main.c index e4530f2..8831850 100644 --- a/main.c +++ b/main.c @@ -6,14 +6,13 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/19 12:45:03 by apommier #+# #+# */ -/* Updated: 2022/01/21 03:56:28 by apommier ### ########.fr */ +/* Updated: 2022/01/21 08:02:42 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ #include "pipex.h" -#include -char **get_path(char **envp) +char **get_path(char **env) { int i; int j; @@ -24,12 +23,16 @@ char **get_path(char **envp) j = 0; line = 0; swap = 0; - while (envp[i++] && envp[i]) + while (env[i++] && env[i]) { - if (!ft_strncmp(envp[i], "PATH=", 5)) + if (!ft_strncmp(env[i], "PATH=", 5)) { - swap = ft_substr(envp[i], 5, ft_strlen(envp[i])); + swap = ft_substr(env[i], 5, ft_strlen(env[i])); + if (!swap) + exit(1); line = ft_split(swap, ':'); + if (!line) + exit(1); free(swap); return (line); } @@ -38,99 +41,105 @@ char **get_path(char **envp) return (0); } -char *get_command(char *exec, char **envp) +char *get_command(char **exec, char **env) { char **path; char *cmd; - char *swap; + char *swap; int i; i = 0; - swap = 0; cmd = 0; - path = get_path(envp); - swap = ft_strjoin(path[i], "/"); + swap = 0; + path = get_path(env); + if (exec[0][0] != '/') + swap = ft_strjoin(path[i], "/"); + else + swap = ft_strdup(path[i]); while (access(swap, F_OK) && path[i++]) { - if (swap) - free(swap); + free(swap); swap = ft_strjoin(path[i], "/"); cmd = swap; - swap = ft_strjoin(swap, exec); + swap = ft_strjoin(swap, exec[0]); free(cmd); } - if (!path[i]) - return (0); + exit_command(path, exec, swap, i); return (swap); } -void child_process(char **envp, char **argv, int *end) +void child_process(int fd1, char **env, char **argv, int *end) { - int fd1; char **exec; - char *cmd1; - - fd1 = open(argv[1], O_RDONLY); - exec = ft_split(argv[2], ' '); - cmd1 = get_command(exec[0], envp); + char *cmd; + int i; + + i = 0; if (dup2(end[1], 1) == -1) - { - perror("Error"); - exit(1); - } + error("dup2 fail"); if (dup2(fd1, 0) == -1) - { - perror("Error"); - exit(1); - } + error("dup2 fail"); close(end[0]); close(fd1); - if (execve(cmd1, exec, envp) == -1) - perror("Error"); - free(cmd1); + exec = ft_split(argv[2], ' '); + cmd = get_command(exec, env); + if (execve(cmd, exec, env) == -1) + { + free_double(exec); + free(cmd); + error("Execve went wrong"); + } + free_double(exec); + free(cmd); } - -void parent_process(char **env, char **argv, int *end) +void parent_process(int fd2, char **env, char **argv, int *end) { char **exec; - char *cmd2; + 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); + waitpid(-1, &status, 0); if (dup2(end[0], 0) == -1) - exit(1); + error("dup2 fail"); if (dup2(fd2, 1) == -1) - exit(1); + error("dup2 fail"); close(end[1]); close(fd2); + exec = ft_split(argv[3], ' '); + cmd2 = get_command(exec, env); if (execve(cmd2, exec, env) == -1) - perror("Error"); + { + free_double(exec); + free(cmd2); + error("Execve went wrong"); + } + free_double(exec); + free(cmd2); } -int main(int argc, char **argv, char **envp) +int main(int argc, char **argv, char **env) { int end[2]; - char *cmd1; - char *cmd2; pid_t parent; + int fd1; + int fd2; + if (argc != 5) + error("Bad number of argument"); + fd1 = open(argv[1], O_RDONLY); + fd2 = open(argv[4], O_RDWR | O_CREAT | O_TRUNC, 0666); + if (fd2 == -1 || fd1 == -1) + { + error("Bad file descriptor"); + } pipe(end); parent = fork(); if (parent < 0) - { - printf("here\n"); - perror("Error"); - return (0); - } + error("Fork Error"); if (!parent) - child_process(envp, argv, end); + child_process(fd1, env, argv, end); else - parent_process(envp, argv, end); - return (0); + parent_process(fd2, env, argv, end); + exit(1); } diff --git a/pipex.h b/pipex.h index 870edb8..e6b30cb 100644 --- a/pipex.h +++ b/pipex.h @@ -6,19 +6,30 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/19 12:45:13 by apommier #+# #+# */ -/* Updated: 2022/01/21 03:00:34 by apommier ### ########.fr */ +/* Updated: 2022/01/21 08:07:31 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ -#include -#include -#include -#include -#include -#include "./libft/libft.h" +#ifndef PIPEX_H +# define PIPEX_H + +# include +# include +# include +# include +# include +# include +# include +# include "./libft/libft.h" char **get_path(char **envp); -char *get_command(char *exec, 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); +void child_process(int fd1, char **envp, char **argv, int *pipe_tab); +void parent_process(int fd2, char **envp, char **argv, int *pipe_tab); + +void error(char *error); +void exit_command(char **path, char **exec, char *swap, int i); +void free_double(char **tab); + +#endif \ No newline at end of file diff --git a/utils.c b/utils.c new file mode 100644 index 0000000..6a7b4e8 --- /dev/null +++ b/utils.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/01/21 08:03:23 by apommier #+# #+# */ +/* Updated: 2022/01/21 08:04:01 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "pipex.h" + +void error(char *error) +{ + perror(error); + exit(1); +} + +void exit_command(char **path, char **exec, char *swap, int i) +{ + if (!path[i]) + { + i = 0; + while (path[i]) + free(path[i++]); + free(path); + i = 0; + while (exec[i]) + free(exec[i++]); + free(exec); + free(swap); + error("command not found"); + } + i = 0; + while (path[i++]) + free(path[i]); + free(path); +} + +void free_double(char **tab) +{ + int i; + + i = 0; + while (tab[i++]) + free(tab[i]); + free(tab); +}