From d90e7e789aaf84b023e1042d23f0f8fe1950850f Mon Sep 17 00:00:00 2001 From: Akhmed Dovletov Date: Wed, 15 May 2024 04:03:45 +0200 Subject: [PATCH] got 99 problems but --- Makefile | 25 +++++++++-------- include/pipex.h | 44 +++++++++++++++++++---------- lib/libft/Makefile | 3 +- lib/libft/include/libft.h | 3 +- lib/libft/src/ft_strcmp.c | 25 +++++++++++++++++ src/ak_pipe.c | 37 +------------------------ src/ak_pipeout.c | 49 +++++++++++++++++++++++++++++++++ src/check_file.c | 4 +-- src/dr_here.c | 15 ++-------- src/{parsing.c => env_access.c} | 8 ++---- src/{free.c => free_exit.c} | 38 ++++++++++++------------- src/main.c | 4 +-- src/px_split.c | 3 +- src/setup.c | 4 +-- 14 files changed, 151 insertions(+), 111 deletions(-) create mode 100644 lib/libft/src/ft_strcmp.c create mode 100644 src/ak_pipeout.c rename src/{parsing.c => env_access.c} (84%) rename src/{free.c => free_exit.c} (89%) diff --git a/Makefile b/Makefile index bd723c3..bc0a109 100644 --- a/Makefile +++ b/Makefile @@ -6,21 +6,22 @@ # By: akdovlet +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/01/01 13:57:12 by akdovlet #+# #+# # -# Updated: 2024/05/15 00:48:39 by akdovlet ### ########.fr # +# Updated: 2024/05/15 03:37:02 by akdovlet ### ########.fr # # # # **************************************************************************** # -NAME = pipex -LIBDIR = lib -LIBFT = $(LIBDIR)/libft/libft.a - -SRC := check_file.c \ - free.c \ - main.c \ - dr_here.c \ - ak_pipe.c \ - parsing.c \ - px_split.c \ +NAME := pipex +LIBDIR := lib +LIBFT := $(LIBDIR)/libft/libft.a + +SRC := ak_pipe.c \ + ak_pipeout.c \ + check_file.c \ + dr_here.c \ + env_access.c \ + free_exit.c \ + main.c \ + px_split.c \ seek_and_execute.c \ setup.c SRC_DIR := src diff --git a/include/pipex.h b/include/pipex.h index 1ab3d0c..2811c47 100644 --- a/include/pipex.h +++ b/include/pipex.h @@ -6,7 +6,7 @@ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/20 23:07:37 by akdovlet #+# #+# */ -/* Updated: 2024/05/15 01:32:15 by akdovlet ### ########.fr */ +/* Updated: 2024/05/15 03:35:50 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,37 +43,51 @@ typedef struct s_data int here_doc; } t_data; -/******************************check_file.c***********************************/ -int check_file(char *file); +/******************************ak_pipe.c**************************************/ +// Child function for every command except for the last one, executes cmd +void child(int fd[2], t_data *data, int i); +// Will pipe and fork making every cmd communicate, calls child +void ak_pipe(t_data *data, int i); +/******************************ak_pipeout.c***********************************/ -void dr_here(t_data *data); -int ft_strcmp(char *s1, char *s2); +// Child function for the last cmd +void child_out(t_data *data); +// Will fork the last cmd and will redirect it to outfile +void ak_pipeout(t_data *data, int i); -/********************************free.c***************************************/ -void ft_free(char **str); -void clear_all(t_data *data); -void clear_exit(t_data *data, int exit_code); +/******************************check_file.c***********************************/ -/******************************ak_pipe.c*********************************/ -void ak_pipe(t_data *data, int i); -void ak_pipeout(t_data *data, int i); +// Checks for reading rights on a given file, opens it if ok +int check_file(char *file); +/******************************dr_here.c**************************************/ +// Forgot about Dre +void dr_dre(t_data *data, int *fd); +// +void dr_here(t_data *data); +/********************************env_access.c**********************************/ char **parse_env(char **env); bool file_access(char *file, int check); -void find_exec(char *cmd, t_data *data); -/******************************check_file.c***********************************/ +/********************************free_exit.c***********************************/ +void ft_free(char **str); +void clear_all(t_data *data); +void clear_exit(t_data *data, int exit_code); + +/******************************px_split.c**************************************/ char **px_split(char const *s, char c); /******************************seek_and_execute.c*****************************/ -void seek_and_execute(t_data *data); void cmd_exe(t_data *data); +void find_exec(char *cmd, t_data *data); +void seek_and_execute(t_data *data); /******************************setup.c****************************************/ +void infile_setup(t_data *data, char **av); bool setup(t_data *data, int ac, char **av, char **env); #endif diff --git a/lib/libft/Makefile b/lib/libft/Makefile index 509016d..32abc70 100644 --- a/lib/libft/Makefile +++ b/lib/libft/Makefile @@ -6,7 +6,7 @@ # By: akdovlet +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/09/15 16:05:42 by akdovlet #+# #+# # -# Updated: 2024/05/09 18:01:33 by akdovlet ### ########.fr # +# Updated: 2024/05/15 03:08:03 by akdovlet ### ########.fr # # # # **************************************************************************** # @@ -43,6 +43,7 @@ SRC := ak_countword.c \ ft_putnbr_fd.c \ ft_calloc.c \ ft_putendl_fd.c \ + ft_strcmp.c \ ft_strncmp.c \ ft_bzero.c \ ft_strchr.c \ diff --git a/lib/libft/include/libft.h b/lib/libft/include/libft.h index b0f0402..4fe15a1 100644 --- a/lib/libft/include/libft.h +++ b/lib/libft/include/libft.h @@ -6,7 +6,7 @@ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/10 17:27:13 by adovleto #+# #+# */ -/* Updated: 2024/05/10 15:54:52 by akdovlet ### ########.fr */ +/* Updated: 2024/05/15 03:07:42 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -69,6 +69,7 @@ size_t ft_strlcat(char *dst, const char *src, size_t size); size_t ft_strlcpy(char *dst, const char *src, size_t size); size_t ft_strlen(const char *s); char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +int ft_strcmp(char *s1, char *s2); int ft_strncmp(const char *s1, const char *s2, size_t n); char *ft_strnstr(const char *big, const char *little, size_t len); char *ft_strrchr(const char *s, int c); diff --git a/lib/libft/src/ft_strcmp.c b/lib/libft/src/ft_strcmp.c new file mode 100644 index 0000000..0c65a49 --- /dev/null +++ b/lib/libft/src/ft_strcmp.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: akdovlet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/15 03:06:59 by akdovlet #+# #+# */ +/* Updated: 2024/05/15 03:07:05 by akdovlet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strcmp(char *s1, char *s2) +{ + while (*s1 && *s2) + { + if (*s1 != *s2) + return (*s1 - *s2); + s1++; + s2++; + } + return (*s1 - *s2); +} diff --git a/src/ak_pipe.c b/src/ak_pipe.c index 1a9f6d0..461b121 100644 --- a/src/ak_pipe.c +++ b/src/ak_pipe.c @@ -18,7 +18,7 @@ void child(int fd[2], t_data *data, int i) if (dup2(data->hermes, STDIN_FILENO) == -1) return (perror("pipex1"), clear_exit(data, EXIT_FAILURE)); close(data->hermes); - if(dup2(fd[1], STDOUT_FILENO) == -1) + if (dup2(fd[1], STDOUT_FILENO) == -1) return (perror("pipex2"), clear_exit(data, EXIT_FAILURE)); close(fd[1]); cmd_exe(data); @@ -41,38 +41,3 @@ void ak_pipe(t_data *data, int i) close(data->hermes); data->hermes = fd[0]; } - -void child_out(t_data *data) -{ - if (dup2(data->hermes, STDIN_FILENO) == -1) - clear_exit(data, EXIT_FAILURE); - close(data->hermes); - if (dup2(data->last, STDOUT_FILENO) == -1) - clear_exit(data, EXIT_FAILURE); - close(data->last); - cmd_exe(data); -} - -void ak_pipeout(t_data *data, int i) -{ - int status; - int j; - - j = 0; - status = 0; - data->last = open(data->av[data->ac - 1], O_WRONLY | O_CREAT | O_TRUNC, 0644); - if (data->last < 0) - return (perror(data->av[data->ac -1]), clear_exit(data, EXIT_FAILURE)); - data->ids[i - (2 + data->here_doc)] = fork(); - if (data->ids[i - (2 + data->here_doc)] < 0) - return (perror("pipex"), clear_exit(data, EXIT_FAILURE)); - if (!data->ids[i - (2 + data->here_doc)]) - child_out(data); - close(data->hermes); - while (j < data->ac - 3) - { - waitpid(data->ids[j],&status, 0); - data->exit_code = WEXITSTATUS(status); - j++; - } -} diff --git a/src/ak_pipeout.c b/src/ak_pipeout.c new file mode 100644 index 0000000..f108e7e --- /dev/null +++ b/src/ak_pipeout.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ak_pipeout.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: akdovlet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/15 03:05:49 by akdovlet #+# #+# */ +/* Updated: 2024/05/15 03:06:25 by akdovlet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "pipex.h" + +void child_out(t_data *data) +{ + if (dup2(data->hermes, STDIN_FILENO) == -1) + clear_exit(data, EXIT_FAILURE); + close(data->hermes); + if (dup2(data->last, STDOUT_FILENO) == -1) + clear_exit(data, EXIT_FAILURE); + close(data->last); + cmd_exe(data); +} + +void ak_pipeout(t_data *data, int i) +{ + int status; + int j; + + j = 0; + status = 0; + data->last = open(data->av[data->ac - 1], \ + O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (data->last < 0) + return (perror(data->av[data->ac -1]), clear_exit(data, EXIT_FAILURE)); + data->ids[i - (2 + data->here_doc)] = fork(); + if (data->ids[i - (2 + data->here_doc)] < 0) + return (perror("pipex"), clear_exit(data, EXIT_FAILURE)); + if (!data->ids[i - (2 + data->here_doc)]) + child_out(data); + close(data->hermes); + while (j < data->ac - 3) + { + waitpid(data->ids[j], &status, 0); + data->exit_code = WEXITSTATUS(status); + j++; + } +} diff --git a/src/check_file.c b/src/check_file.c index e72917c..cc178ed 100644 --- a/src/check_file.c +++ b/src/check_file.c @@ -6,7 +6,7 @@ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/06 23:36:19 by akdovlet #+# #+# */ -/* Updated: 2024/05/12 15:12:57 by akdovlet ### ########.fr */ +/* Updated: 2024/05/15 02:57:09 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ int check_file(char *file) int fd; if (!file_access(file, R_OK)) - return(perror(file), -1); + return (perror(file), -1); else { fd = open(file, O_RDONLY); diff --git a/src/dr_here.c b/src/dr_here.c index 159c056..db9194e 100644 --- a/src/dr_here.c +++ b/src/dr_here.c @@ -6,24 +6,12 @@ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/15 00:17:44 by akdovlet #+# #+# */ -/* Updated: 2024/05/15 02:06:07 by akdovlet ### ########.fr */ +/* Updated: 2024/05/15 03:36:38 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ #include "pipex.h" -int ft_strcmp(char *s1, char *s2) -{ - while (*s1 && *s2) - { - if (*s1 != *s2) - return (*s1 - *s2); - s1++; - s2++; - } - return (*s1 - *s2); -} - void dr_dre(t_data *data, int *fd) { char *line; @@ -43,6 +31,7 @@ void dr_dre(t_data *data, int *fd) write(fd[1], line, ft_strlen(line)); free(line); } + close(fd[1]); exit(EXIT_SUCCESS); } diff --git a/src/parsing.c b/src/env_access.c similarity index 84% rename from src/parsing.c rename to src/env_access.c index 64f40b0..09c8d02 100644 --- a/src/parsing.c +++ b/src/env_access.c @@ -1,20 +1,16 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* parsing.c :+: :+: :+: */ +/* env_access.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 17:52:22 by akdovlet #+# #+# */ -/* Updated: 2024/05/14 18:15:23 by akdovlet ### ########.fr */ +/* Updated: 2024/05/15 03:24:00 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ #include "pipex.h" -#include "libft.h" -#include -#include -#include bool file_access(char *file, int check) { diff --git a/src/free.c b/src/free_exit.c similarity index 89% rename from src/free.c rename to src/free_exit.c index 8ba6384..a31a618 100644 --- a/src/free.c +++ b/src/free_exit.c @@ -1,32 +1,16 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* free.c :+: :+: :+: */ +/* free_exit.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/07 00:41:28 by akdovlet #+# #+# */ -/* Updated: 2024/05/14 19:47:09 by akdovlet ### ########.fr */ +/* Updated: 2024/05/15 03:22:25 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ -# include "pipex.h" - -void clear_all(t_data *data) -{ - ft_free(data->path); - ft_free(data->cmd); - free(data->ids); - data->ids = NULL; - if (data->first > 0) - close(data->first); - data->first = 0; - if (data->hermes > 0) - close(data->hermes); - data->hermes = 0; - if (data->last > 0) - close(data->last); -} +#include "pipex.h" void ft_free(char **str) { @@ -44,6 +28,22 @@ void ft_free(char **str) str = NULL; } +void clear_all(t_data *data) +{ + ft_free(data->path); + ft_free(data->cmd); + free(data->ids); + data->ids = NULL; + if (data->first > 0) + close(data->first); + data->first = 0; + if (data->hermes > 0) + close(data->hermes); + data->hermes = 0; + if (data->last > 0) + close(data->last); +} + void clear_exit(t_data *data, int exit_code) { clear_all(data); diff --git a/src/main.c b/src/main.c index 21b5ee0..ccbeaa1 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/20 23:07:20 by akdovlet #+# #+# */ -/* Updated: 2024/05/14 23:14:01 by akdovlet ### ########.fr */ +/* Updated: 2024/05/15 02:57:25 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ // Si une commande n'existe pas il va essayer d'executer la prochaine // Si il n'y a pas d'infile, la commande ne va pas s'executer et tout // l'output sera vide et pipe renvoie 1 -int main(int ac, char **av, char **env) +int main(int ac, char **av, char **env) { int exit_code; t_data data; diff --git a/src/px_split.c b/src/px_split.c index df4c39d..835db29 100644 --- a/src/px_split.c +++ b/src/px_split.c @@ -6,12 +6,11 @@ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/25 19:50:00 by akdovlet #+# #+# */ -/* Updated: 2024/04/25 20:08:57 by akdovlet ### ########.fr */ +/* Updated: 2024/05/15 03:13:40 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ #include "pipex.h" -#include "libft.h" static int px_countword(char const *str, char c) { diff --git a/src/setup.c b/src/setup.c index cb5cbc7..e437642 100644 --- a/src/setup.c +++ b/src/setup.c @@ -6,13 +6,13 @@ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/06 23:41:38 by akdovlet #+# #+# */ -/* Updated: 2024/05/15 01:06:15 by akdovlet ### ########.fr */ +/* Updated: 2024/05/15 03:15:19 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ #include "pipex.h" -void infile_setup(t_data *data, char **av) +void infile_setup(t_data *data, char **av) { data->here_doc = 0; data->first = check_file(av[1]);