diff --git a/.cache/clangd/index/libft.h.F7455B850A972A1D.idx b/.cache/clangd/index/libft.h.F7455B850A972A1D.idx index d980302..60d1674 100644 Binary files a/.cache/clangd/index/libft.h.F7455B850A972A1D.idx and b/.cache/clangd/index/libft.h.F7455B850A972A1D.idx differ diff --git a/.cache/clangd/index/main.c.259C23514E408AC0.idx b/.cache/clangd/index/main.c.259C23514E408AC0.idx index 633aa6a..999cb92 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 a63f21e..9926222 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/parsing.h.E14E231C05813896.idx b/.cache/clangd/index/parsing.h.E14E231C05813896.idx index 0e748c4..58f2ed7 100644 Binary files a/.cache/clangd/index/parsing.h.E14E231C05813896.idx and b/.cache/clangd/index/parsing.h.E14E231C05813896.idx differ diff --git a/.cache/clangd/index/pipex.h.D099372D6E124AAF.idx b/.cache/clangd/index/pipex.h.D099372D6E124AAF.idx index 8243509..208cc45 100644 Binary files a/.cache/clangd/index/pipex.h.D099372D6E124AAF.idx and b/.cache/clangd/index/pipex.h.D099372D6E124AAF.idx differ diff --git a/include/exec.h b/include/exec.h new file mode 100644 index 0000000..535f50c --- /dev/null +++ b/include/exec.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/06 14:15:47 by adjoly #+# #+# */ +/* Updated: 2024/04/06 14:18:27 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef EXEC_H +# define EXEC_H + +# include + +#include "pipex.h" + +void exec_pipe(t_pipex *pipex); + +#endif diff --git a/include/pipex.h b/include/pipex.h index 6227919..f4d56fa 100644 --- a/include/pipex.h +++ b/include/pipex.h @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/22 21:14:06 by adjoly #+# #+# */ -/* Updated: 2024/04/03 14:24:09 by adjoly ### ########.fr */ +/* Updated: 2024/04/06 17:26:47 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,10 +23,15 @@ typedef struct s_pipex { t_pcmd *cmd; + char **path; + char **env; int infile; - int outfile; + char *outfile; } t_pipex; void ft_senderror(t_pipex *pipex, char *msg); +void getpath(t_pipex *pipex); +void get_arrcmd_path(t_pipex *pipex); +void exec_pipe(t_pipex *pipex); #endif diff --git a/lol b/lol new file mode 100644 index 0000000..439cdc4 --- /dev/null +++ b/lol @@ -0,0 +1,3 @@ + "src/main.c",$ + "obj/src/main.o"$ + "file": "src/main.c"$ diff --git a/obj/src/exec/exec.o b/obj/src/exec/exec.o new file mode 100644 index 0000000..b9bb73e Binary files /dev/null and b/obj/src/exec/exec.o differ diff --git a/obj/src/main.o b/obj/src/main.o index c8b672c..0ab33dd 100644 Binary files a/obj/src/main.o and b/obj/src/main.o differ diff --git a/obj/src/test.o b/obj/src/test.o deleted file mode 100644 index 437b003..0000000 Binary files a/obj/src/test.o and /dev/null differ diff --git a/obj/src/utils/getpath.o b/obj/src/utils/getpath.o new file mode 100644 index 0000000..99f758f Binary files /dev/null and b/obj/src/utils/getpath.o differ diff --git a/obj/src/utils/set_path.o b/obj/src/utils/set_path.o new file mode 100644 index 0000000..562b000 Binary files /dev/null and b/obj/src/utils/set_path.o differ diff --git a/pipex b/pipex index 78af5bf..5c0ae18 100755 Binary files a/pipex and b/pipex differ diff --git a/src/exec/exec.c b/src/exec/exec.c new file mode 100644 index 0000000..f72543a --- /dev/null +++ b/src/exec/exec.c @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/06 14:07:27 by adjoly #+# #+# */ +/* Updated: 2024/04/07 11:01:39 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec.h" +#include "pipex.h" +#include + +//void ft_printtamerelaputedemaelysdearraydemerde(char **arr) +//{ +// char **ll = arr; +// +// while (*ll) +// { +// ft_putendl_fd(*ll, STDOUT_FILENO); +// ll++; +// } +//} +// +//void ft_printstdindemerdemaelystagueule_ps_flocon_est_mims(int fd) +//{ +//// char *s = malloc(1); +// ft_putnbr_fd(fd, 2); +// /*while (read(fd, s, 1)) +// { +// ft_putchar_fd(*s, 2); +// }*/ +//} +// + +pid_t ft_exec_fst_pipe(t_pipex *pipex, int fd[2]) +{ + pid_t pid; + + pid = fork(); + if (!pid) + { + dup2(pipex->infile, STDIN_FILENO); + dup2(fd[1], STDOUT_FILENO); + close(pipex->infile); + close(fd[0]); + close(fd[1]); + execve(pipex->cmd[0].cmd, pipex->cmd[0].option, pipex->env); + } + return (pid); +} + +pid_t ft_exec_scnd_pipe(t_pipex *pipex, int fd[2]) +{ + pid_t pid; + int outfile; + + pid = fork(); + if (!pid) + { + outfile = open(pipex->outfile, O_CREAT | O_WRONLY | O_TRUNC, 0644); + dup2(fd[0], STDIN_FILENO); + dup2(outfile, STDOUT_FILENO); + close(outfile); + close(fd[0]); + close(fd[1]); + execve(pipex->cmd[1].cmd, pipex->cmd[1].option, pipex->env); + } + return (pid); +} + +void exec_pipe(t_pipex *pipex) +{ + pid_t pid; + pid_t pid2; + int fd[2]; + + if (pipe(fd) == -1) + ft_senderror(pipex, "Error in opening the pipe"); + pid = ft_exec_fst_pipe(pipex, fd); + pid2 = ft_exec_scnd_pipe(pipex, fd); + close(fd[1]); + close(pipex->infile); + close(fd[1]); + close(fd[0]); + waitpid(pid, NULL, 0); + waitpid(pid2, NULL, 0); +} diff --git a/src/main.c b/src/main.c index 1b7f7ac..44d84e6 100644 --- a/src/main.c +++ b/src/main.c @@ -6,11 +6,10 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/21 10:03:04 by adjoly #+# #+# */ -/* Updated: 2024/04/03 16:30:07 by adjoly ### ########.fr */ +/* Updated: 2024/04/07 11:03:19 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -//#include "libft.h" #include "libft.h" #include "parsing.h" #include "pipex.h" @@ -61,6 +60,8 @@ void ft_senderror(t_pipex *pipex, char *msg) ft_putendl_fd(msg, STDERR_FILENO); if (!pipex) exit(EXIT_FAILURE); + if (pipex->path) + ft_freearr(pipex->path); if (pipex->cmd) free_pcmd(pipex->cmd); free(pipex); @@ -71,18 +72,24 @@ int main(int ac, char **av, char **env) { t_pipex *pipex; - check_empty_args(av); pipex = malloc(sizeof(t_pipex)); + if (ac != 5) + ft_senderror(pipex, "Error : Invalid number of args"); + check_empty_args(av); pipex->infile = open(av[1], O_RDONLY); - pipex->outfile = open(av[ac - 1], O_RDONLY); + dup2(pipex->infile, STDIN_FILENO); + pipex->outfile = av[ac - 1]; pipex->cmd = parse_cmd(ac - 3, av); - while (*env) - { - ft_putstr_fd(*env, STDOUT_FILENO); - env++; - } + pipex->env = env; + getpath(pipex); + if (pipex->path == NULL) + ft_senderror(pipex, "Error : Can't find path"); + ft_strlcpy(*(pipex->path), *(pipex->path) + 5, \ + ft_strlen(*(pipex->path) + 5) + 1); + get_arrcmd_path(pipex); + exec_pipe(pipex); + ft_freearr(pipex->path); free_pcmd(pipex->cmd); free(pipex); return (EXIT_SUCCESS); } - diff --git a/src/parsing/parsing.c b/src/parsing/parsing.c index e20573a..50968d6 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/03 14:20:56 by adjoly ### ########.fr */ +/* Updated: 2024/04/07 11:03:02 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -47,7 +47,7 @@ t_pcmd *parse_cmd(int ac, char **av) int i; t_pcmd *cmd_arr; t_pcmd *cmd_tmp; - + i = 0; cmd_arr = ft_calloc(ac + 1, sizeof(t_pcmd)); cmd_tmp = cmd_arr; diff --git a/src/utils/ft_freetab.c b/src/utils/ft_freearr.c similarity index 85% rename from src/utils/ft_freetab.c rename to src/utils/ft_freearr.c index 6a0e435..fd418b0 100644 --- a/src/utils/ft_freetab.c +++ b/src/utils/ft_freearr.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_freetab.c :+: :+: :+: */ +/* ft_freearr.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/28 22:34:09 by adjoly #+# #+# */ -/* Updated: 2024/03/30 11:58:39 by adjoly ### ########.fr */ +/* Updated: 2024/04/07 11:02:43 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ void ft_freearr(char **arr) char **tmp; tmp = arr; - while(tmp && *tmp) + while (tmp && *tmp) { free(*tmp); tmp++; diff --git a/src/utils/getpath.c b/src/utils/getpath.c new file mode 100644 index 0000000..61276d4 --- /dev/null +++ b/src/utils/getpath.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* getpath.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/04 15:32:56 by adjoly #+# #+# */ +/* Updated: 2024/04/07 11:01:17 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "pipex.h" + +void getpath(t_pipex *pipex) +{ + char **env; + + env = pipex->env; + while (*env) + { + if (!ft_strncmp(*env, "PATH=", 5)) + { + pipex->path = ft_split(*env, ':'); + return ; + } + env++; + } + pipex->path = NULL; +} diff --git a/src/utils/set_path.c b/src/utils/set_path.c new file mode 100644 index 0000000..6f9751c --- /dev/null +++ b/src/utils/set_path.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* set_path.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/06 13:31:15 by adjoly #+# #+# */ +/* Updated: 2024/04/06 17:30:03 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "pipex.h" + +char *join_cmd(char *path, char *cmd) +{ + char *str; + char *ret; + + str = ft_strjoin(path, "/"); + ret = ft_strjoin(str, cmd); + free(str); + return (ret); +} + +char *get_cmd_path(t_pipex *pipex, char *cmd) +{ + char **path; + char *tmp; + + path = pipex->path; + while (*path) + { + tmp = join_cmd(*path, cmd); + if (access(tmp, X_OK) == 0) + { + free(cmd); + cmd = ft_strdup(tmp); + free (tmp); + return (cmd); + } + free(tmp); + path++; + } + return (NULL); +} + +void get_arrcmd_path(t_pipex *pipex) +{ + t_pcmd *cmd; + + cmd = pipex->cmd; + while ((*cmd).option) + { + (*cmd).cmd = get_cmd_path(pipex, (*cmd).cmd); + cmd++; + } +} diff --git a/test.c b/test.c index 4121ad5..08863ee 100644 --- a/test.c +++ b/test.c @@ -6,11 +6,10 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/03 15:36:42 by adjoly #+# #+# */ -/* Updated: 2024/04/03 16:27:03 by adjoly ### ########.fr */ +/* Updated: 2024/04/04 15:20:24 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ - #include #include #include @@ -24,7 +23,8 @@ int main() int fd[2]; if (pipe(fd) == -1) return(1); - int pid = fork(); + int pid; + pid = fork(); if (!pid) { dup2(fd[1], STDOUT_FILENO);