diff --git a/.cache/clangd/index/main.c.259C23514E408AC0.idx b/.cache/clangd/index/main.c.259C23514E408AC0.idx index 999cb92..4b79579 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 9926222..f20beef 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 index 208cc45..f5761bc 100644 Binary files a/.cache/clangd/index/pipex.h.D099372D6E124AAF.idx and b/.cache/clangd/index/pipex.h.D099372D6E124AAF.idx differ diff --git a/asdf b/asdf new file mode 100644 index 0000000..e69de29 diff --git a/fff b/fff new file mode 100644 index 0000000..c268aaf --- /dev/null +++ b/fff @@ -0,0 +1,9 @@ +asdf +compile_commands.json +fff +include +libft +Makefile +obj +pipex +src diff --git a/include/pipex.h b/include/pipex.h index f4d56fa..d224d33 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/06 17:26:47 by adjoly ### ########.fr */ +/* Updated: 2024/04/07 17:02:48 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,5 +33,8 @@ 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); +void ft_senderror(t_pipex *pipex, char *msg); +void ft_senderror_file(t_pipex *pipex, char *msg); +void free_pcmd(t_pcmd *cmd); #endif diff --git a/lol b/lol deleted file mode 100644 index 439cdc4..0000000 --- a/lol +++ /dev/null @@ -1,3 +0,0 @@ - "src/main.c",$ - "obj/src/main.o"$ - "file": "src/main.c"$ diff --git a/obj/src/main.o b/obj/src/main.o index 0ab33dd..dde18c5 100644 Binary files a/obj/src/main.o and b/obj/src/main.o differ diff --git a/obj/src/utils/ft_freetab.o b/obj/src/utils/ft_freearr.o similarity index 88% rename from obj/src/utils/ft_freetab.o rename to obj/src/utils/ft_freearr.o index f8b2a9c..2f98361 100644 Binary files a/obj/src/utils/ft_freetab.o and b/obj/src/utils/ft_freearr.o differ diff --git a/obj/src/utils/ft_senderror.o b/obj/src/utils/ft_senderror.o new file mode 100644 index 0000000..3fabcdb Binary files /dev/null and b/obj/src/utils/ft_senderror.o differ diff --git a/obj/src/utils/getpath.o b/obj/src/utils/getpath.o index 99f758f..2966683 100644 Binary files a/obj/src/utils/getpath.o and b/obj/src/utils/getpath.o differ diff --git a/obj/src/utils/set_path.o b/obj/src/utils/set_path.o index 562b000..7f3f604 100644 Binary files a/obj/src/utils/set_path.o and b/obj/src/utils/set_path.o differ diff --git a/pipex b/pipex index 5c0ae18..49ae89c 100755 Binary files a/pipex and b/pipex differ diff --git a/src/main.c b/src/main.c index 44d84e6..e14c69a 100644 --- a/src/main.c +++ b/src/main.c @@ -6,31 +6,15 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/21 10:03:04 by adjoly #+# #+# */ -/* Updated: 2024/04/07 11:03:19 by adjoly ### ########.fr */ +/* Updated: 2024/04/08 12:49:21 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" #include "parsing.h" #include "pipex.h" -#include -#include -void free_pcmd(t_pcmd *cmd) -{ - t_pcmd *tmp; - - tmp = cmd; - while (tmp && tmp->cmd) - { - free(tmp->cmd); - ft_freearr(tmp->option); - tmp++; - } - free(cmd); -} - -void check_empty_args(char **av) +void check_empty_args(char **av, t_pipex *pipex) { char **tmp; char *tmp_av; @@ -40,8 +24,6 @@ void check_empty_args(char **av) while (*tmp) { tmp_av = *tmp; - if (!*tmp_av) - ft_senderror(NULL, "You have empty argument"); while (*tmp_av) { if (*tmp_av != 32) @@ -49,23 +31,28 @@ void check_empty_args(char **av) tmp_av++; } if (!*tmp_av) - ft_senderror(NULL, "You have empty argument"); + { + ft_putendl_fd("You have empty argument", STDERR_FILENO); + free(pipex); + exit(EXIT_FAILURE); + } tmp++; } } -void ft_senderror(t_pipex *pipex, char *msg) +void check_files(int ac, char **av, t_pipex *pipex) { - (void)pipex; - 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); - exit(EXIT_FAILURE); + int out; + + pipex->infile = open(av[1], O_RDONLY); + pipex->outfile = av[ac - 1]; + if (pipex->infile == -1) + ft_senderror_file(pipex, "Error: Infile can't be opened"); + dup2(pipex->infile, STDIN_FILENO); + out = open(pipex->outfile, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (out < 0) + ft_senderror_file(pipex, "Error: Outfile can't be opened"); + close(out); } int main(int ac, char **av, char **env) @@ -74,18 +61,18 @@ int main(int ac, char **av, char **env) 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); - dup2(pipex->infile, STDIN_FILENO); - pipex->outfile = av[ac - 1]; + { + free(pipex); + ft_putendl_fd("Error : Invalid number of args", STDERR_FILENO); + exit(EXIT_FAILURE); + } + check_empty_args(av, pipex); + check_files(ac, av, pipex); pipex->cmd = parse_cmd(ac - 3, av); 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); diff --git a/src/utils/ft_senderror.c b/src/utils/ft_senderror.c new file mode 100644 index 0000000..29fc11d --- /dev/null +++ b/src/utils/ft_senderror.c @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_senderror.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/07 16:59:53 by adjoly #+# #+# */ +/* Updated: 2024/04/07 17:14:06 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "pipex.h" +#include + +void free_pcmd(t_pcmd *cmd) +{ + t_pcmd *tmp; + + tmp = cmd; + while (tmp && tmp->cmd) + { + free(tmp->cmd); + ft_freearr(tmp->option); + tmp++; + } + free(cmd); +} + +void ft_senderror(t_pipex *pipex, char *msg) +{ + ft_putendl_fd(msg, STDERR_FILENO); + if (!pipex) + exit(EXIT_FAILURE); + if (pipex->infile > -1) + close(pipex->infile); + if (pipex->cmd) + free_pcmd(pipex->cmd); + if (pipex->path) + ft_freearr(pipex->path); + free(pipex); + exit(EXIT_FAILURE); +} + +void ft_senderror_file(t_pipex *pipex, char *msg) +{ + int out; + + ft_putendl_fd(msg, STDERR_FILENO); + if (pipex->infile > -1) + close(pipex->infile); + out = open(pipex->outfile, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (out == -1) + { + free(pipex); + exit(EXIT_FAILURE); + } + close(out); + free(pipex); + exit(EXIT_FAILURE); +} diff --git a/src/utils/getpath.c b/src/utils/getpath.c index 61276d4..478c34f 100644 --- a/src/utils/getpath.c +++ b/src/utils/getpath.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/04 15:32:56 by adjoly #+# #+# */ -/* Updated: 2024/04/07 11:01:17 by adjoly ### ########.fr */ +/* Updated: 2024/04/07 16:20:05 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ void getpath(t_pipex *pipex) { if (!ft_strncmp(*env, "PATH=", 5)) { - pipex->path = ft_split(*env, ':'); + pipex->path = ft_split((*env) + 5, ':'); return ; } env++; diff --git a/src/utils/set_path.c b/src/utils/set_path.c index 6f9751c..75fbfe8 100644 --- a/src/utils/set_path.c +++ b/src/utils/set_path.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/06 13:31:15 by adjoly #+# #+# */ -/* Updated: 2024/04/06 17:30:03 by adjoly ### ########.fr */ +/* Updated: 2024/04/08 12:53:46 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,6 +28,8 @@ char *get_cmd_path(t_pipex *pipex, char *cmd) char **path; char *tmp; + if (access(cmd, X_OK) == 0) + return (cmd); path = pipex->path; while (*path) { @@ -42,6 +44,7 @@ char *get_cmd_path(t_pipex *pipex, char *cmd) free(tmp); path++; } + free(cmd); return (NULL); } @@ -53,6 +56,16 @@ void get_arrcmd_path(t_pipex *pipex) while ((*cmd).option) { (*cmd).cmd = get_cmd_path(pipex, (*cmd).cmd); + if (!(*cmd).cmd) + { + ft_freearr((*cmd).option); + if (pipex->cmd[1].cmd) + { + ft_freearr(pipex->cmd[1].option); + free(pipex->cmd[1].cmd); + } + ft_senderror(pipex, "Error: Command not found"); + } cmd++; } }