test.c to understant pipe()

This commit is contained in:
Jack Halford 2016-11-27 23:52:36 +01:00
parent b0e7229c54
commit b59f05eb77
11 changed files with 114 additions and 12 deletions

View file

@ -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

View file

@ -1,7 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_pipe.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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]);

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_exec.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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[] =

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_cmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

50
42sh/test.c Normal file
View file

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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]);
}