test.c to understant pipe()
This commit is contained in:
parent
b0e7229c54
commit
b59f05eb77
11 changed files with 114 additions and 12 deletions
|
|
@ -25,11 +25,11 @@ D_FLAGS =
|
||||||
MKDIR = mkdir -p
|
MKDIR = mkdir -p
|
||||||
RM = /bin/rm -rf
|
RM = /bin/rm -rf
|
||||||
|
|
||||||
.PHONY: all clean fclean re libft
|
.PHONY: all clean fclean re
|
||||||
|
|
||||||
all: $(NAME)
|
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)
|
$(CC) $(O_INC) $(O_SER) $(O_LIB) $(W_FLAGS) $(DF_OBJ) -o $@ $(D_FLAGS)
|
||||||
|
|
||||||
$(D_OBJ)/%.o: $(D_SRC)/main/%.c includes/minishell.h
|
$(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)
|
@$(CC) $(O_INC) $(W_FLAGS) -c $< -o $@ $(D_FLAGS)
|
||||||
@echo "Compiling "$<"..."
|
@echo "Compiling "$<"..."
|
||||||
|
|
||||||
libft:
|
libft/libft.a:
|
||||||
@$(MAKE) -C libft/ 2>/dev/null
|
@$(MAKE) -C libft/ 2>/dev/null
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
|
|
@ -75,6 +75,6 @@ clean:
|
||||||
|
|
||||||
fclean: clean
|
fclean: clean
|
||||||
$(RM) $(NAME)
|
$(RM) $(NAME)
|
||||||
|
@$(MAKE) fclean -C libft/ 2>/dev/null
|
||||||
|
|
||||||
re: fclean all
|
re: fclean all
|
||||||
@$(MAKE) re -C libft/ 2>/dev/null
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
#ifndef EXEC_H
|
||||||
# define EXEC_H
|
# define EXEC_H
|
||||||
|
|
||||||
# include "minishell.h"
|
# include "minishell.h"
|
||||||
|
# define PIPE_READ 0
|
||||||
|
# define PIPE_WRITE 1
|
||||||
|
# define STDIN 0
|
||||||
|
# define STDOUT 1
|
||||||
|
|
||||||
typedef long long t_type;
|
typedef long long t_type;
|
||||||
typedef struct s_exec t_exec;
|
typedef struct s_exec t_exec;
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/10 13:07:44 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/14 17:28:14 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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"
|
#include "exec.h"
|
||||||
|
|
||||||
int exec_pipe(t_btree *ast, t_data *data)
|
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)
|
if ((dup2(filedes[1], 1)) == -1)
|
||||||
return (-1);
|
return (-1);
|
||||||
ft_exec(ast->left, data);
|
ft_exec(ast->left, data);
|
||||||
close(filedes[1]);
|
close(filedes[PIPE_WRITE]);
|
||||||
dup2(filedes[0], 0);
|
dup2(filedes[0], 0);
|
||||||
ft_exec(ast->right, data);
|
ft_exec(ast->right, data);
|
||||||
close(filedes[0]);
|
close(filedes[0]);
|
||||||
|
|
|
||||||
|
|
@ -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"
|
#include "exec.h"
|
||||||
|
|
||||||
t_exec g_exec[] =
|
t_exec g_exec[] =
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/10 13:44:30 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,10 @@
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* main.c :+: :+: :+: */
|
/* main.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/10 13:35:14 by jhalford #+# #+# */
|
/* Created: 2016/11/27 21:13:34 by jhalford #+# #+# */
|
||||||
/* Updated: 2016/11/15 09:14:30 by jhalford ### ########.fr */
|
/* Updated: 2016/11/27 23:17:56 by jhalford ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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"
|
#include "minishell.h"
|
||||||
extern pid_t g_pid;
|
extern pid_t g_pid;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/14 11:30:08 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
50
42sh/test.c
Normal file
50
42sh/test.c
Normal 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]);
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue