maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
pipe.c File Reference
#include "shell.h"
#include <unistd.h>
#include "libft.h"
#include "execute.h"
#include <stdio.h>
#include <sys/wait.h>
#include "pipe.h"
Include dependency graph for pipe.c:

Go to the source code of this file.

Functions

int builtin_pipe (t_ast *node, t_ms_data *data)
 
pid_t execute_child (t_ast *node, t_ms_data *data, int fd[2], int direction)
 execute child process in the pipe context
 
static int setup_pipe_processes (t_ast *node, t_ms_data *data, pid_t *pid_1, pid_t *pid_2)
 

Function Documentation

◆ builtin_pipe()

int builtin_pipe ( t_ast node,
t_ms_data data 
)

Definition at line 46 of file pipe.c.

47{
48 pid_t pid_1;
49 pid_t pid_2;
50 int status_1;
51 int status_2;
52 int setup_result;
53
54 status_1 = 0;
55 status_2 = 0;
56 setup_result = setup_pipe_processes(node, data, &pid_1, &pid_2);
57 if (setup_result == WAIT_NEXT_COMMAND)
58 return (WAIT_NEXT_COMMAND);
59 if (pid_1 > 0 && waitpid(pid_1, &status_1, 0) == -1)
60 return (ft_perror("waitpid"));
61 if (pid_2 > 0)
62 {
63 if (waitpid(pid_2, &status_2, 0) == -1)
64 return (ft_perror("waitpid"));
65 return (WEXITSTATUS(status_2));
66 }
67 return (WEXITSTATUS(status_1));
68}
int ft_perror(char *str)
static int setup_pipe_processes(t_ast *node, t_ms_data *data, pid_t *pid_1, pid_t *pid_2)
Definition pipe.c:25
#define WAIT_NEXT_COMMAND
Definition pipe.h:19

References ft_perror(), setup_pipe_processes(), and WAIT_NEXT_COMMAND.

Referenced by builtin_pipe_test(), and execute_ast().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ execute_child()

pid_t execute_child ( t_ast node,
t_ms_data data,
int  fd[2],
int  direction 
)

execute child process in the pipe context

--

Parameters
nodecurrent node in the AST
dataminishell structure data
fdfile descriptors
directiondirection of the pipe if 0 - node_left, if 1 - node_right
Returns
pid_t return the process id

Definition at line 79 of file pipe.c.

81{
82 pid_t pid;
83 int status;
84
85 pid = fork();
86 if (pid == -1)
87 ft_perror("fork");
88 if (pid == 0)
89 {
90 if (direction == 0)
91 dup2(fd[1], STDOUT_FILENO);
92 else
93 dup2(fd[0], STDIN_FILENO);
94 close_fds(fd[0], fd[1]);
95 status = execute_ast(node, data);
96 exit(status);
97 }
98 return (pid);
99}
int execute_ast(t_ast *node, t_ms_data *data)
execute Abstract Syntax Tree
Definition execute.c:38
void close_fds(int in, int out)
close two file descriptors
Definition utils_0.c:28

References close_fds(), execute_ast(), and ft_perror().

Referenced by setup_pipe_processes().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setup_pipe_processes()

static int setup_pipe_processes ( t_ast node,
t_ms_data data,
pid_t *  pid_1,
pid_t *  pid_2 
)
static

Definition at line 25 of file pipe.c.

27{
28 int fd[2];
29
30 *pid_2 = -1;
31 if (pipe(fd) == -1)
32 return (ft_perror("pipe"));
33 *pid_1 = execute_child(node->left, data, fd, 0);
34 if (node->right != NULL)
35 *pid_2 = execute_child(node->right, data, fd, 1);
36 else
37 {
38 close(fd[1]);
39 data->std_in = fd[0];
40 return (WAIT_NEXT_COMMAND);
41 }
42 close_fds(fd[0], fd[1]);
43 return (EXIT_SUCCESS);
44}
#define EXIT_SUCCESS
Definition exit_status.h:16
pid_t execute_child(t_ast *node, t_ms_data *data, int fd[2], int direction)
execute child process in the pipe context
Definition pipe.c:79
struct s_ast * right
Definition tokens.h:54
struct s_ast * left
Definition tokens.h:53
int std_in
Definition shell.h:26

References close_fds(), execute_child(), EXIT_SUCCESS, ft_perror(), s_ast::left, s_ast::right, s_ms_data::std_in, and WAIT_NEXT_COMMAND.

Referenced by builtin_pipe().

Here is the call graph for this function:
Here is the caller graph for this function: