From dac4fe971775e3c265ba4f9a30fef1b0a9f8ff72 Mon Sep 17 00:00:00 2001 From: Etienne Rey-bethbeder Date: Fri, 20 Jan 2023 18:51:53 +0100 Subject: [PATCH] 0.5 --- Makefile | 4 +- bonus/pipex.c | 136 ++++++++++++++++++++--------------- bonus/pipex.h | 45 ++++++++++++ bonus/pipex_parsing.c | 20 ++++-- bonus/pipex_utils.c | 4 +- mandatory/pipex.c | 4 +- pipex.h => mandatory/pipex.h | 6 +- mandatory/pipex_parsing.c | 4 +- mandatory/pipex_utils.c | 4 +- output | 4 -- pipex_parsing.c | 83 --------------------- pipex_utils.c | 106 --------------------------- 12 files changed, 148 insertions(+), 272 deletions(-) create mode 100644 bonus/pipex.h rename pipex.h => mandatory/pipex.h (91%) delete mode 100644 pipex_parsing.c delete mode 100644 pipex_utils.c diff --git a/Makefile b/Makefile index 2a9327a..2b9ffca 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ GNL = get_next_line/get_next_line.c get_next_line/get_next_line_utils.c SRCS = mandatory/pipex.c mandatory/pipex_utils.c mandatory/pipex_parsing.c ${GNL} -SRCS_BONUS = +SRCS_BONUS = bonus/pipex.c bonus/pipex_utils.c bonus/pipex_parsing.c ${GNL} OBJS = ${SRCS:.c=.o} OBJS_BONUS = ${SRCS_BONUS:.c=.o} LIBS = libft/libft.a @@ -15,7 +15,7 @@ all: ${NAME} ${NAME}: ${OBJS} make -C libft ${CC} ${CFLAGS} -o ${NAME} ${OBJS} ${LIBS} - + bonus: ${OBJS_BONUS} make -C libft ${CC} ${CFLAGS} -o ${NAME} ${OBJS_BONUS} ${LIBS} diff --git a/bonus/pipex.c b/bonus/pipex.c index b7ea8a7..db4b4b5 100644 --- a/bonus/pipex.c +++ b/bonus/pipex.c @@ -6,102 +6,120 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/01/18 18:04:58 by erey-bet #+# #+# */ -/* Updated: 2023/01/19 16:57:40 by erey-bet ### ########.fr */ +/* Updated: 2023/01/20 17:58:22 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ -#include "../pipex.h" +#include "pipex.h" int free_all(t_data *data) { int i; + int y; - if (data->fd1 > 0 && data->cmd1) + if (data->fd1 > 0 && data->cmds[0]) close(data->fd1); close(data->fd2); - i = 0; - while (data->flg1[i]) - free(data->flg1[i++]); - free(data->flg1); - i = 0; - while (data->flg2[i]) - free(data->flg2[i++]); - free(data->flg2); - free(data->cmd1); - free(data->cmd2); + 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]); return (1); } -void first_cmd(int fd[2], t_data data) +void make_cmd(int fd[2][2], t_data data, int fl1, int fl2) { - dup2(data.fd1, 0); - dup2(fd[1], 1); - close(fd[0]); - close(fd[1]); - execve(data.cmd1, data.flg1, data.env); + dup2(fl1, 0); + dup2(fl2, 1); + close(fd[0][0]); + close(fd[0][1]); + close(fd[1][0]); + close(fd[1][1]); + execve(data.cmds[0], data.flgs[0], data.env); +} + +void cmd(int fd[2][2], t_data data, int i, int t) +{ + if (t == 0) + make_cmd(fd, data, data.fd1, 0); + else if (t == 1) + make_cmd(fd, data, (i + 1) % 2, i % 2); + else if (t == 2) + make_cmd(fd, data, (i + 1) % 2, data.fd2); free_all(&data); exit(1); } -void second_cmd(int fd[2], t_data data) +int pipex(int argc, t_data data) { - dup2(fd[0], 0); - dup2(data.fd2, 1); - close(fd[0]); - close(fd[1]); - execve(data.cmd2, data.flg2, data.env); - free_all(&data); - exit(1); -} + int fd[2][2]; + int *fk; + int i; -int pipex(t_data data) -{ - int fd[2]; - int cmd1; - int cmd2; - - if (pipe(fd) < -1) + if (pipe(fd[0]) < -1) return (1); - if (data.fd1 > 0 && data.cmd1) + if (pipe(fd[1]) < -1) + return (1); + fk = ft_calloc(argc - 3, sizeof(int)); + i = -1; + while (++i < argc - 3) { - cmd1 = fork(); - if (cmd1 < 0) + fk[i] = fork(); + if (fk < 0) return (1); - if (cmd1 == 0) - first_cmd(fd, data); + if (fk == 0) + { + if (i == 0 && data.fd1 > 0 && data.cmds[0]) + cmd(fd, data, i, 0); + else if (i == argc - 3) + cmd(fd, data, i, 1); + else + cmd(fd, data, i, 2); + } } - cmd2 = fork(); - if (cmd2 < 0) - return (1); - if (cmd2 == 0) - second_cmd(fd, data); - close(fd[0]); - close(fd[1]); - if (data.fd1 > 0 && data.cmd1) - waitpid(cmd1, NULL, 0); - waitpid(cmd2, NULL, 0); + close(fd[0][0]); + close(fd[0][1]); + close(fd[1][0]); + close(fd[1][1]); + if (data.fd1 > 0 && data.cmds[0]) + waitpid(fk[0], NULL, 0); + i = -1; + while (++i < argc - 3) + waitpid(fk[i], NULL, 0); return (0); } int main(int argc, char *argv[], char **env) { t_data data; + int i; if (argc < 5) return (1); - if (parsing(argv, &data)) + if (parsing(argc, argv, &data)) return (2); - data.cmd1 = find_command(env, data.cmd1); - if (data.cmd1 == NULL) - print_error(1, argv[2]); - data.cmd2 = find_command(env, data.cmd2); - if (data.cmd2 == NULL) + data.argc = argc; + i = -1; + while (++i < argc - 3) { - print_error(1, argv[3]); - return (free_all(&data)); + data.cmds[i] = find_command(env, data.cmds[i]); + if (data.cmds[i] == NULL) + { + print_error(1, argv[2 + i]); + if (i == argc - 4) + return (free_all(&data)); + } } data.env = env; - if (pipex(data)) + if (pipex(argc, data)) return (free_all(&data)); free_all(&data); return (0); diff --git a/bonus/pipex.h b/bonus/pipex.h new file mode 100644 index 0000000..57b68b3 --- /dev/null +++ b/bonus/pipex.h @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pipex.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/01/11 15:33:34 by erey-bet #+# #+# */ +/* Updated: 2023/01/20 17:48:08 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PIPEX_H +# define PIPEX_H + +# include "../libft/libft.h" +# include "../get_next_line/get_next_line.h" +# include +# include +# include +# include + +typedef struct s_data +{ + char **cmds; + char ***flgs; + char *fl1; + char *fl2; + int fd1; + int fd2; + char **env; + int argc; +} t_data; + +int get_next(char *cmd, char c); +int print_error(int type, char *error); +char **get_flags(char *cmd); +char *get_command(char *cmd); +char **add_cmd(char **flg, char *cmd); +int parsing(int argc, char *argv[], t_data *data); +char *test_acces(char **s, char **cmd, char **path, int *y); +char *cmd_exist(char **env, char **cmd, int y, char **s); +char *find_command(char **env, char *cmd); + +#endif diff --git a/bonus/pipex_parsing.c b/bonus/pipex_parsing.c index f621169..ea66801 100644 --- a/bonus/pipex_parsing.c +++ b/bonus/pipex_parsing.c @@ -6,24 +6,30 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/01/19 17:03:01 by erey-bet #+# #+# */ -/* Updated: 2023/01/19 17:05:16 by erey-bet ### ########.fr */ +/* Updated: 2023/01/20 17:49:08 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ -#include "../pipex.h" +#include "pipex.h" -int parsing(char *argv[], t_data *data) +int parsing(int argc, char *argv[], t_data *data) { + int i; + data->fd2 = open(argv[4], O_WRONLY | O_TRUNC | O_CREAT); if (data->fd2 < 0) return (1); data->fd1 = open(argv[1], O_RDONLY); if (data->fd1 < 0) print_error(0, argv[1]); - data->cmd1 = get_command(argv[2]); - data->cmd2 = get_command(argv[3]); - data->flg1 = ft_split(argv[2], ' '); - data->flg2 = ft_split(argv[3], ' '); + data->cmds = ft_calloc(argc - 2, sizeof(char *)); + data->flgs = ft_calloc(argc - 2, sizeof(char *)); + i = -1; + while (++i < argc - 4) + { + data->cmds[i] = get_command(argv[2 + i]); + data->flgs[i] = ft_split(argv[2 + i], ' '); + } data->fl1 = argv[1]; data->fl2 = argv[4]; return (0); diff --git a/bonus/pipex_utils.c b/bonus/pipex_utils.c index b95258f..1727914 100644 --- a/bonus/pipex_utils.c +++ b/bonus/pipex_utils.c @@ -6,11 +6,11 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/01/19 17:02:51 by erey-bet #+# #+# */ -/* Updated: 2023/01/19 17:05:24 by erey-bet ### ########.fr */ +/* Updated: 2023/01/20 17:48:55 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ -#include "../pipex.h" +#include "pipex.h" int get_next(char *cmd, char c) { diff --git a/mandatory/pipex.c b/mandatory/pipex.c index c3624a1..1101316 100644 --- a/mandatory/pipex.c +++ b/mandatory/pipex.c @@ -6,11 +6,11 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/01/18 18:04:58 by erey-bet #+# #+# */ -/* Updated: 2023/01/19 17:30:04 by erey-bet ### ########.fr */ +/* Updated: 2023/01/20 14:53:55 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ -#include "../pipex.h" +#include "pipex.h" int free_all(t_data *data) { diff --git a/pipex.h b/mandatory/pipex.h similarity index 91% rename from pipex.h rename to mandatory/pipex.h index 95e369e..7642500 100644 --- a/pipex.h +++ b/mandatory/pipex.h @@ -6,15 +6,15 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/01/11 15:33:34 by erey-bet #+# #+# */ -/* Updated: 2023/01/19 17:03:23 by erey-bet ### ########.fr */ +/* Updated: 2023/01/20 14:55:13 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef PIPEX_H # define PIPEX_H -# include "libft/libft.h" -# include "get_next_line/get_next_line.h" +# include "../libft/libft.h" +# include "../get_next_line/get_next_line.h" # include # include # include diff --git a/mandatory/pipex_parsing.c b/mandatory/pipex_parsing.c index f621169..ccc5e42 100644 --- a/mandatory/pipex_parsing.c +++ b/mandatory/pipex_parsing.c @@ -6,11 +6,11 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/01/19 17:03:01 by erey-bet #+# #+# */ -/* Updated: 2023/01/19 17:05:16 by erey-bet ### ########.fr */ +/* Updated: 2023/01/20 14:54:05 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ -#include "../pipex.h" +#include "pipex.h" int parsing(char *argv[], t_data *data) { diff --git a/mandatory/pipex_utils.c b/mandatory/pipex_utils.c index b95258f..16b582b 100644 --- a/mandatory/pipex_utils.c +++ b/mandatory/pipex_utils.c @@ -6,11 +6,11 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/01/19 17:02:51 by erey-bet #+# #+# */ -/* Updated: 2023/01/19 17:05:24 by erey-bet ### ########.fr */ +/* Updated: 2023/01/20 14:55:47 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ -#include "../pipex.h" +#include "pipex.h" int get_next(char *cmd, char c) { diff --git a/output b/output index b047e51..e69de29 100755 --- a/output +++ b/output @@ -1,4 +0,0 @@ -hello that's why we are here s, your name -what's your name -commandes are not wrking, if you want send me in private -emmm thats nice diff --git a/pipex_parsing.c b/pipex_parsing.c deleted file mode 100644 index dc1271f..0000000 --- a/pipex_parsing.c +++ /dev/null @@ -1,83 +0,0 @@ - -#include "../pipex.h" - -int parsing(char *argv[], t_data *data) -{ - data->fd2 = open(argv[4], O_WRONLY | O_TRUNC | O_CREAT); - if (data->fd2 < 0) - return (1); - data->fd1 = open(argv[1], O_RDONLY); - if (data->fd1 < 0) - print_error(0, argv[1]); - data->cmd1 = get_command(argv[2]); - data->cmd2 = get_command(argv[3]); - data->flg1 = ft_split(argv[2], ' '); - data->flg2 = ft_split(argv[3], ' '); - data->fl1 = argv[1]; - data->fl2 = argv[4]; - return (0); -} - -char *test_acces(char **s, char **cmd, char **path, int *y) -{ - *s = ft_strjoin_free(*s, "/", 1); - if (*s == NULL) - return (NULL); - *path = ft_strjoin_free(*s, *cmd, 1); - if (*path == NULL) - return (NULL); - if (access(*path, X_OK) == 0) - { - free(*cmd); - return (*path); - } - free(*path); - *path = NULL; - *y = 0; - return (NULL); -} - -char *cmd_exist(char **env, char **cmd, int y, char **s) -{ - char *path; - int i; - - i = 5; - while ((*env)[i]) - { - if ((*env)[i] != ':') - (*s)[y++] = (*env)[i]; - else - { - test_acces(s, cmd, &path, &y); - if (path) - return (path); - *s = ft_calloc(ft_strlen(*env) + 1, 1); - if (*s == NULL) - return (NULL); - } - i++; - } - free(*s); - free(*cmd); - return (NULL); -} - -char *find_command(char **env, char *cmd) -{ - char *s; - int y; - - while (*env != NULL) - { - if (ft_strnstr(*env, "PATH=/", ft_strlen(*env)) - && ft_strchr(*env, ':') > 0) - break ; - env++; - } - y = 0; - s = ft_calloc(ft_strlen(*env) + 1, 1); - if (s == NULL) - return (NULL); - return (cmd_exist(env, &cmd, y, &s)); -} diff --git a/pipex_utils.c b/pipex_utils.c deleted file mode 100644 index 86a82c1..0000000 --- a/pipex_utils.c +++ /dev/null @@ -1,106 +0,0 @@ - -#include "../pipex.h" - -int get_next(char *cmd, char c) -{ - int i; - - i = 0; - while (cmd[i] != c) - { - if (!cmd[i]) - return (-1); - i++; - } - return (i); -} - -int print_error(int type, char *error) -{ - if (type == 0) - { - write(2, "zsh: no such file or directory: ", 32); - write(2, error, ft_strlen(error)); - } - if (type == 1) - { - write(2, "zsh: command not found: ", 24); - if (get_next(error, ' ') == -1) - write(2, error, ft_strlen(error)); - else - write(2, error, ft_strlen(error) - - (ft_strlen(error) - get_next(error, ' '))); - } - write(2, "\n", 1); - return (1); -} - -char **get_flags(char *cmd) -{ - int i; - int y; - char *new_str; - char **flags; - - i = get_next(cmd, ' ') + 1; - if (i == 0) - { - flags = ft_calloc(2, sizeof(char *)); - return (flags); - } - new_str = ft_calloc(ft_strlen(cmd) - i + 1, 1); - if (new_str == NULL) - return (NULL); - y = 0; - while (cmd[i]) - new_str[y++] = cmd[i++]; - new_str[y] = '\0'; - flags = ft_split(new_str, ' '); - i = 0; - free(new_str); - return (flags); -} - -char *get_command(char *cmd) -{ - int i; - char *new_str; - - i = get_next(cmd, ' '); - if (i != -1) - new_str = ft_calloc(get_next(cmd, ' ') + 1, 1); - else - new_str = ft_calloc(ft_strlen(cmd) + 1, 1); - if (new_str == NULL) - return (NULL); - i = -1; - while (cmd[++i] != ' ' && cmd[i]) - new_str[i] = cmd[i]; - new_str[i] = '\0'; - return (new_str); -} - -char **add_cmd(char **flg, char *cmd) -{ - char *tmp; - int i; - int check; - - tmp = NULL; - i = 0; - check = 0; - while (check == 0) - { - if (flg[i] == NULL) - check = 1; - if (tmp != NULL) - flg[i] = tmp; - tmp = flg[i]; - i++; - } - flg[0] = ft_strdup(cmd); - if (flg[0] == NULL) - return (NULL); - flg[i] = NULL; - return (flg); -}