diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5be5f8c --- /dev/null +++ b/Makefile @@ -0,0 +1,61 @@ +#SETUP +NAME = pipex +CC = gcc +FLAGS = -Wall -Wextra -Werror +RM = rm -rf + +#FILES AND PATH +HEADER_SRCS = pipex.h pipex_bonus.h +HEADER_DIR = includes/ +HEADER = $(addprefix $(HEADER_DIR), $(HEADER_SRCS)) + +MPATH_SRCS = pipex.c childs.c error.c free.c +MPATH_DIR = mandatory/ +MPATH = $(addprefix $(MPATH_DIR), $(MPATH_SRCS)) +OBJ_M = $(MPATH:.c=.o) + +BPATH_SRCS = pipex_bonus.c error_bonus.c here_doc_bonus.c\ + files_bonus.c free_bonus.c child_bonus.c +BPATH_DIR = bonus/ +BPATH = $(addprefix $(BPATH_DIR), $(BPATH_SRCS)) +OBJ_B = $(BPATH:.c=.o) + +FUNC_SRCS = ft_strncmp.c ft_strdup.c ft_split.c ft_strjoin.c +FUNC_DIR = functions/ +FUNC = $(addprefix $(FUNC_DIR), $(FUNC_SRCS))\ + gnl/get_next_line_utils.c gnl/get_next_line.c +OBJ_F = $(FUNC:.c=.o) + +#COMMANDS +%.o: %.c $(HEADER) Makefile + @${CC} ${FLAGS} -c $< -o $@ + +$(NAME): $(OBJ_F) $(OBJ_M) + @$(CC) $(OBJ_F) $(OBJ_M) -o $(NAME) + @echo -e "$(GREEN)$(NAME) created!$(DEFAULT)" + +all: $(NAME) + +bonus: $(OBJ_F) $(OBJ_B) + @$(CC) $(OBJ_F) $(OBJ_B) -o $(NAME) + @echo -e "$(GREEN)$(NAME) created!$(DEFAULT)" + +clean: + @$(RM) $(OBJ_M) + @$(RM) $(OBJ_F) + @$(RM) $(OBJ_B) + @echo -e "$(YELLOW)object files deleted!$(DEFAULT)" + +fclean: clean + @$(RM) $(NAME) + @echo -e "$(RED)all deleted!$(DEFAULT)" + +re: fclean all + +.PHONY: all clean fclean bonus re + +#COLORS +RED = \033[1;31m +GREEN = \033[1;32m +YELLOW = \033[1;33m +DEFAULT = \033[0m diff --git a/includes/pipex.h b/includes/pipex.h new file mode 100644 index 0000000..9326a6a --- /dev/null +++ b/includes/pipex.h @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pipex.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bcaffere +# include +# include + +/* malloc, free, exit */ +# include + +/* open, unlink */ +# include + +/* waitpid, wait */ +# include + +/* strerror */ +# include + +/*to perror*/ +# include + +/* to get_next_line */ +# include "../gnl/get_next_line.h" + +# define ERR_INFILE "Infile" +# define ERR_OUTFILE "Outfile" +# define ERR_INPUT "Invalid number of arguments.\n" +# define ERR_PIPE "Pipe" +# define ERR_CMD "Command not found\n" + +typedef struct s_pipex +{ + pid_t pid1; + pid_t pid2; + int tube[2]; + int infile; + int outfile; + char *paths; + char **cmd_paths; + char **cmd_args; + char *cmd; +}t_pipex; + +/* childs.c */ +void first_child(t_pipex pipex, char *argv[], char *envp[]); +void second_child(t_pipex pipex, char *argv[], char *envp[]); + +/* free.c */ +void parent_free(t_pipex *pipex); +void child_free(t_pipex *pipex); + +/* error.c */ +void msg_error(char *err); +int msg(char *err); + +/* funcions */ +char *ft_strjoin(char const *s1, char const *s2); +char *ft_strdup(const char *src); +char **ft_split(char const *s, char c); +int ft_strncmp(const char *s1, const char *s2, size_t n); + +#endif diff --git a/includes/pipex_bonus.h b/includes/pipex_bonus.h new file mode 100644 index 0000000..7e03c9d --- /dev/null +++ b/includes/pipex_bonus.h @@ -0,0 +1,94 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pipex_bonus.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bcaffere +# include +# include + +/* malloc, free, exit */ +# include + +/* open, unlink */ +# include + +/* waitpid, wait */ +# include + +/* strerror */ +# include + +/*to perror*/ +# include + +/* to get_next_line */ +# include "../gnl/get_next_line.h" + +# define ERR_INFILE "Infile" +# define ERR_OUTFILE "Outfile" +# define ERR_INPUT "Invalid number of arguments.\n" +# define ERR_PIPE "Pipe" +# define ERR_ENVP "Environment" +# define ERR_CMD "Command not found: " +# define ERR_HEREDOC "here_doc" + +typedef struct s_ppxb +{ + int infile; + int outfile; + char *env_path; + char **cmd_paths; + char *cmd; + char **cmd_args; + int here_doc; + pid_t pid; + int cmd_nmbs; + int pipe_nmbs; + int *pipe; + int idx; +}t_ppxb; + +/* pipex_bonus.c */ +void close_pipes(t_ppxb *pipex); + +/* child_bonus.c */ +void child(t_ppxb pipex, char **argv, char **envp); + +/* free.c */ +void parent_free(t_ppxb *pipex); +void child_free(t_ppxb *pipex); +void pipe_free(t_ppxb *pipex); + +/* files_bonus.c */ +char *find_path(char **envp); +void get_infile(char **argv, t_ppxb *pipex); +void get_outfile(char *argv, t_ppxb *pipex); + +/* here_doc_bonus.c */ +int args_in(char *arg, t_ppxb *pipex); +void here_doc(char *argv, t_ppxb *pipex); + +/* error_bonus.c */ +void msg_error(char *err); +void msg_pipe(char *arg); +int msg(char *err); + +/* funcions */ +char *ft_strjoin(char const *s1, char const *s2); +char *ft_strdup(const char *src); +char **ft_split(char const *s, char c); +int ft_strncmp(const char *s1, const char *s2, size_t n); + +#endif diff --git a/mandatory/childs.c b/mandatory/childs.c new file mode 100644 index 0000000..cb3753a --- /dev/null +++ b/mandatory/childs.c @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* childs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bcaffere infile); + close(pipex->outfile); + while (pipex->cmd_paths[i]) + { + free(pipex->cmd_paths[i]); + i++; + } + free(pipex->cmd_paths); +} + +void child_free(t_pipex *pipex) +{ + int i; + + i = 0; + while (pipex->cmd_args[i]) + { + free(pipex->cmd_args[i]); + i++; + } + free(pipex->cmd_args); + free(pipex->cmd); +} diff --git a/mandatory/pipex.c b/mandatory/pipex.c new file mode 100644 index 0000000..0cc0e37 --- /dev/null +++ b/mandatory/pipex.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pipex.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bcaffere tube[0]); + close(pipex->tube[1]); +} + +int main(int argc, char *argv[], char *envp[]) +{ + t_pipex pipex; + + if (argc != 5) + return (msg(ERR_INPUT)); + pipex.infile = open(argv[1], O_RDONLY); + if (pipex.infile < 0) + msg_error(ERR_INFILE); + pipex.outfile = open(argv[argc - 1], O_TRUNC | O_CREAT | O_RDWR, 0000644); + if (pipex.outfile < 0) + msg_error(ERR_OUTFILE); + if (pipe(pipex.tube) < 0) + msg_error(ERR_PIPE); + pipex.paths = find_path(envp); + pipex.cmd_paths = ft_split(pipex.paths, ':'); + pipex.pid1 = fork(); + if (pipex.pid1 == 0) + first_child(pipex, argv, envp); + pipex.pid2 = fork(); + if (pipex.pid2 == 0) + second_child(pipex, argv, envp); + close_pipes(&pipex); + waitpid(pipex.pid1, NULL, 0); + waitpid(pipex.pid2, NULL, 0); + parent_free(&pipex); + return (0); +}