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)
 execute pipe when | is found in the command
 
pid_t execute_child (t_ast *node, t_ms_data *data, int fd[2], int direction)
 execute child process in the pipe context
 

Function Documentation

◆ builtin_pipe()

int builtin_pipe ( t_ast node,
t_ms_data data 
)

execute pipe when | is found in the command

--

  • Parameters
    nodecurrent node in the AST
  • Parameters
    dataminishell structure data
  • Returns
    int return status:
  • - 0: success
  • - 1: error

Definition at line 35 of file pipe.c.

36{
37 int fd[2];
38 pid_t pid_1;
39 pid_t pid_2;
40 int status;
41
42 pid_2 = -1;
43 if (pipe(fd) == -1)
44 ft_perror("pipe");
45 pid_1 = execute_child(node->left, data, fd, 0);
46 if (node->right != NULL)
47 pid_2 = execute_child(node->right, data, fd, 1);
48 else
49 {
50 close(fd[1]);
51 data->std_in = fd[0];
52 return (WAIT_NEXT_COMMAND);
53 }
54 close_fds(fd[0], fd[1]);
55 if (pid_1 > 0)
56 waitpid(pid_1, &status, 0);
57 if (node->right != NULL && pid_2 > 0)
58 waitpid(pid_2, &status, 0);
59 return (WEXITSTATUS(status));
60}
void close_fds(int in, int out)
close two file descriptors
Definition utils_0.c:28
int ft_perror(char *str)
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:71
#define WAIT_NEXT_COMMAND
Definition pipe.h:19
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(), ft_perror(), s_ast::left, s_ast::right, s_ms_data::std_in, 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 71 of file pipe.c.

73{
74 pid_t pid;
75
76 pid = fork();
77 if (pid == -1)
78 ft_perror("fork");
79 if (pid == 0)
80 {
81 if (direction == 0)
82 dup2(fd[1], STDOUT_FILENO);
83 else
84 dup2(fd[0], STDIN_FILENO);
85 close_fds(fd[0], fd[1]);
86 execute_ast(node, data);
87 exit(EXIT_SUCCESS);
88 }
89 return (pid);
90}
int execute_ast(t_ast *node, t_ms_data *data)
execute Abstract Syntax Tree
Definition execute.c:38
#define EXIT_SUCCESS
Definition exit_status.h:16

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

Referenced by builtin_pipe().

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