From b59f05eb77d648e9160b3f7d9d15102acfe10460 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Sun, 27 Nov 2016 23:52:36 +0100 Subject: [PATCH] test.c to understant pipe() --- 42sh/Makefile | 8 ++--- 42sh/includes/exec.h | 16 +++++++++ 42sh/includes/minishell.h | 2 +- 42sh/src/exec/exec_command.c | 2 +- 42sh/src/exec/exec_pipe.c | 14 +++++++- 42sh/src/exec/ft_exec.c | 12 +++++++ 42sh/src/line-editing/ft_key_enter.c | 2 +- 42sh/src/main/main.c | 6 ++-- 42sh/src/minishell-exec/ft_cmd.c | 12 +++++++ 42sh/src/parser/ft_parse.c | 2 +- 42sh/test.c | 50 ++++++++++++++++++++++++++++ 11 files changed, 114 insertions(+), 12 deletions(-) create mode 100644 42sh/test.c diff --git a/42sh/Makefile b/42sh/Makefile index 4afe0b9c..64ceac65 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -25,11 +25,11 @@ D_FLAGS = MKDIR = mkdir -p RM = /bin/rm -rf -.PHONY: all clean fclean re libft +.PHONY: all clean fclean re all: $(NAME) -$(NAME): $(DF_OBJ) libft +$(NAME): libft/libft.a $(DF_OBJ) $(CC) $(O_INC) $(O_SER) $(O_LIB) $(W_FLAGS) $(DF_OBJ) -o $@ $(D_FLAGS) $(D_OBJ)/%.o: $(D_SRC)/main/%.c includes/minishell.h @@ -67,7 +67,7 @@ $(D_OBJ)/%.o: $(D_SRC)/exec/%.c includes/exec.h @$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS) @echo "Compiling "$<"..." -libft: +libft/libft.a: @$(MAKE) -C libft/ 2>/dev/null clean: @@ -75,6 +75,6 @@ clean: fclean: clean $(RM) $(NAME) + @$(MAKE) fclean -C libft/ 2>/dev/null re: fclean all - @$(MAKE) re -C libft/ 2>/dev/null diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 65bb06fc..40cff420 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -1,7 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */ +/* Updated: 2016/11/27 22:57:06 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #ifndef EXEC_H # define EXEC_H # include "minishell.h" +# define PIPE_READ 0 +# define PIPE_WRITE 1 +# define STDIN 0 +# define STDOUT 1 typedef long long t_type; typedef struct s_exec t_exec; diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 039706c2..8cabd1b7 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ -/* Updated: 2016/11/14 11:26:24 by jhalford ### ########.fr */ +/* Updated: 2016/11/27 16:10:09 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_command.c b/42sh/src/exec/exec_command.c index 3eb92793..f2b2c848 100644 --- a/42sh/src/exec/exec_command.c +++ b/42sh/src/exec/exec_command.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 17:28:14 by jhalford #+# #+# */ -/* Updated: 2016/11/14 17:44:35 by jhalford ### ########.fr */ +/* Updated: 2016/11/27 23:46:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_pipe.c b/42sh/src/exec/exec_pipe.c index af5d1932..1c23f5f0 100644 --- a/42sh/src/exec/exec_pipe.c +++ b/42sh/src/exec/exec_pipe.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_pipe.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/27 21:13:23 by jhalford #+# #+# */ +/* Updated: 2016/11/27 23:49:35 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "exec.h" int exec_pipe(t_btree *ast, t_data *data) @@ -9,7 +21,7 @@ int exec_pipe(t_btree *ast, t_data *data) if ((dup2(filedes[1], 1)) == -1) return (-1); ft_exec(ast->left, data); - close(filedes[1]); + close(filedes[PIPE_WRITE]); dup2(filedes[0], 0); ft_exec(ast->right, data); close(filedes[0]); diff --git a/42sh/src/exec/ft_exec.c b/42sh/src/exec/ft_exec.c index 7c00c00e..6bca18c3 100644 --- a/42sh/src/exec/ft_exec.c +++ b/42sh/src/exec/ft_exec.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_exec.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/27 20:30:32 by jhalford #+# #+# */ +/* Updated: 2016/11/27 23:07:42 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "exec.h" t_exec g_exec[] = diff --git a/42sh/src/line-editing/ft_key_enter.c b/42sh/src/line-editing/ft_key_enter.c index f1aae9dc..f473d665 100644 --- a/42sh/src/line-editing/ft_key_enter.c +++ b/42sh/src/line-editing/ft_key_enter.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:44:30 by jhalford #+# #+# */ -/* Updated: 2016/11/14 17:58:14 by jhalford ### ########.fr */ +/* Updated: 2016/11/27 20:23:50 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index e68d5a39..5209ca6f 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* main.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ +/* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/10 13:35:14 by jhalford #+# #+# */ -/* Updated: 2016/11/15 09:14:30 by jhalford ### ########.fr */ +/* Created: 2016/11/27 21:13:34 by jhalford #+# #+# */ +/* Updated: 2016/11/27 23:17:56 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/minishell-exec/ft_cmd.c b/42sh/src/minishell-exec/ft_cmd.c index a3e2a8f3..6d5d7a77 100644 --- a/42sh/src/minishell-exec/ft_cmd.c +++ b/42sh/src/minishell-exec/ft_cmd.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_cmd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/27 21:13:18 by jhalford #+# #+# */ +/* Updated: 2016/11/27 22:55:31 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "minishell.h" extern pid_t g_pid; diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index e2316f1e..a21f9427 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 11:30:08 by jhalford #+# #+# */ -/* Updated: 2016/11/14 18:28:34 by jhalford ### ########.fr */ +/* Updated: 2016/11/27 20:25:43 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/test.c b/42sh/test.c new file mode 100644 index 00000000..6a6fd131 --- /dev/null +++ b/42sh/test.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* test.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/11/27 21:15:49 by jhalford #+# #+# */ +/* Updated: 2016/11/27 23:50:57 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft/includes/libft.h" + +extern char **environ; + +#define PIPE_READ 0 +#define PIPE_WRITE 1 +#define STDIN 0 +#define STDOUT 1 + +int main(void) +{ + pid_t pid; + int fds[2]; + + pipe(fds); + if ((pid = fork()) == 0) + { + /* child */ + dup2(fds[PIPE_WRITE], STDOUT); + /* close(fds[PIPE_READ]); */ + /* close(fds[PIPE_WRITE]); */ + execv("/bin/ls", (char*[2]){"/bin/ls"}); + } + else + wait(NULL); + close(fds[PIPE_WRITE]); + if ((pid = fork()) == 0) + { + /* parent */ + dup2(fds[PIPE_READ], STDIN); + /* close(fds[PIPE_READ]); */ + execv("/usr/bin/wc", (char*[2]){"/usr/bin/wc", "-l"}); + } + else + wait(NULL); + close(fds[PIPE_WRITE]); + close(fds[PIPE_READ]); +}