diff --git a/.cache/clangd/index/main.c.259C23514E408AC0.idx b/.cache/clangd/index/main.c.259C23514E408AC0.idx index 829a1c3..633aa6a 100644 Binary files a/.cache/clangd/index/main.c.259C23514E408AC0.idx and b/.cache/clangd/index/main.c.259C23514E408AC0.idx differ diff --git a/.cache/clangd/index/parsing.c.B79530D8B421E683.idx b/.cache/clangd/index/parsing.c.B79530D8B421E683.idx index b9700ac..a63f21e 100644 Binary files a/.cache/clangd/index/parsing.c.B79530D8B421E683.idx and b/.cache/clangd/index/parsing.c.B79530D8B421E683.idx differ diff --git a/.cache/clangd/index/pipex.h.D099372D6E124AAF.idx b/.cache/clangd/index/pipex.h.D099372D6E124AAF.idx new file mode 100644 index 0000000..8243509 Binary files /dev/null and b/.cache/clangd/index/pipex.h.D099372D6E124AAF.idx differ diff --git a/include/parsing.h b/include/parsing.h index 6d3dfd3..03dbe73 100644 --- a/include/parsing.h +++ b/include/parsing.h @@ -6,13 +6,15 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/28 16:54:40 by adjoly #+# #+# */ -/* Updated: 2024/03/29 10:41:40 by adjoly ### ########.fr */ +/* Updated: 2024/04/03 13:56:08 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef PARSING_H # define PARSING_H +# include "libft.h" + typedef struct s_pcmd { char *cmd; diff --git a/include/pipex.h b/include/pipex.h index a651169..6227919 100644 --- a/include/pipex.h +++ b/include/pipex.h @@ -6,16 +6,27 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/22 21:14:06 by adjoly #+# #+# */ -/* Updated: 2024/03/22 21:16:23 by adjoly ### ########.fr */ +/* Updated: 2024/04/03 14:24:09 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef PIPEX_H # define PIPEX_H -typedef struct t_pipex -{ +# include "parsing.h" +# include "libft.h" -} s_pipex; +# include +# include +# include + +typedef struct s_pipex +{ + t_pcmd *cmd; + int infile; + int outfile; +} t_pipex; + +void ft_senderror(t_pipex *pipex, char *msg); #endif diff --git a/obj/src/main.o b/obj/src/main.o new file mode 100644 index 0000000..c8b672c Binary files /dev/null and b/obj/src/main.o differ diff --git a/obj/src/parsing/parsing.o b/obj/src/parsing/parsing.o new file mode 100644 index 0000000..ee7283f Binary files /dev/null and b/obj/src/parsing/parsing.o differ diff --git a/obj/src/test.o b/obj/src/test.o new file mode 100644 index 0000000..437b003 Binary files /dev/null and b/obj/src/test.o differ diff --git a/obj/src/utils/ft_freetab.o b/obj/src/utils/ft_freetab.o new file mode 100644 index 0000000..f8b2a9c Binary files /dev/null and b/obj/src/utils/ft_freetab.o differ diff --git a/pipex b/pipex new file mode 100755 index 0000000..78af5bf Binary files /dev/null and b/pipex differ diff --git a/src/main.c b/src/main.c index bd7f577..1b7f7ac 100644 --- a/src/main.c +++ b/src/main.c @@ -6,40 +6,83 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/21 10:03:04 by adjoly #+# #+# */ -/* Updated: 2024/04/02 11:08:15 by adjoly ### ########.fr */ +/* Updated: 2024/04/03 16:30:07 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ +//#include "libft.h" #include "libft.h" #include "parsing.h" +#include "pipex.h" +#include +#include -int main(int ac, char **av) +void free_pcmd(t_pcmd *cmd) { - t_pcmd *cmd; t_pcmd *tmp; - char **option_tmp; - cmd = parse_cmd(ac, av + 2); tmp = cmd; - while (tmp && (*tmp).cmd) + while (tmp && tmp->cmd) { - ft_printf("cmd : %s\n", (*tmp).cmd); - option_tmp = (*tmp).option; - while (*option_tmp) - { - ft_printf("%s\n", *option_tmp); - option_tmp++; - } - tmp++; - } - tmp = cmd; - while (tmp && (*tmp).cmd) - { - if (tmp->option) - ft_freearr(tmp->option); free(tmp->cmd); + ft_freearr(tmp->option); tmp++; } free(cmd); +} + +void check_empty_args(char **av) +{ + char **tmp; + char *tmp_av; + + tmp = av; + tmp++; + while (*tmp) + { + tmp_av = *tmp; + if (!*tmp_av) + ft_senderror(NULL, "You have empty argument"); + while (*tmp_av) + { + if (*tmp_av != 32) + break ; + tmp_av++; + } + if (!*tmp_av) + ft_senderror(NULL, "You have empty argument"); + tmp++; + } +} + +void ft_senderror(t_pipex *pipex, char *msg) +{ + (void)pipex; + ft_putendl_fd(msg, STDERR_FILENO); + if (!pipex) + exit(EXIT_FAILURE); + if (pipex->cmd) + free_pcmd(pipex->cmd); + free(pipex); + exit(EXIT_FAILURE); +} + +int main(int ac, char **av, char **env) +{ + t_pipex *pipex; + + check_empty_args(av); + pipex = malloc(sizeof(t_pipex)); + pipex->infile = open(av[1], O_RDONLY); + pipex->outfile = open(av[ac - 1], O_RDONLY); + pipex->cmd = parse_cmd(ac - 3, av); + while (*env) + { + ft_putstr_fd(*env, STDOUT_FILENO); + env++; + } + free_pcmd(pipex->cmd); + free(pipex); return (EXIT_SUCCESS); } + diff --git a/src/parsing/parsing.c b/src/parsing/parsing.c index 42624eb..e20573a 100644 --- a/src/parsing/parsing.c +++ b/src/parsing/parsing.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/22 21:13:26 by adjoly #+# #+# */ -/* Updated: 2024/04/02 11:08:57 by adjoly ### ########.fr */ +/* Updated: 2024/04/03 14:20:56 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,18 +44,20 @@ t_pcmd split_cmd(char *cmd_av) t_pcmd *parse_cmd(int ac, char **av) { char **tmp; + int i; t_pcmd *cmd_arr; t_pcmd *cmd_tmp; - (void)ac; - cmd_arr = ft_calloc(ac, sizeof(t_pcmd) + 1); + i = 0; + cmd_arr = ft_calloc(ac + 1, sizeof(t_pcmd)); cmd_tmp = cmd_arr; - tmp = av; - while (*tmp) + tmp = av + 2; + while (i < ac) { *cmd_tmp = split_cmd(*tmp); cmd_tmp++; tmp++; + i++; } return (cmd_arr); } diff --git a/test.c b/test.c new file mode 100644 index 0000000..4121ad5 --- /dev/null +++ b/test.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/03 15:36:42 by adjoly #+# #+# */ +/* Updated: 2024/04/03 16:27:03 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + + +#include +#include +#include + +int main() +{ + char *cmd[] = {"sleep", "5", 0}; + char *path = "/bin/sleep"; + char *cmd2[] = {"sleep", "5", 0}; + char *path2 = "/bin/sleep"; + int fd[2]; + if (pipe(fd) == -1) + return(1); + int pid = fork(); + if (!pid) + { + dup2(fd[1], STDOUT_FILENO); + close(fd[0]); + close(fd[1]); + execve(path, cmd, NULL); + } + close(fd[1]); + int pid2 = fork(); + if (!pid2) + { + dup2(fd[0], STDIN_FILENO); + close(fd[0]); + execve(path2, cmd2, NULL); + } + close(fd[1]); + waitpid(pid, NULL, 0); + waitpid(pid2, NULL, 0); +}