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

Go to the source code of this file.

Functions

static int open_and_redirect (t_ast *node, t_ms_data *data)
 redirect out ">" to the file output
 
int redirect_out (t_ast *node, t_ms_data *data)
 

Function Documentation

◆ open_and_redirect()

static int open_and_redirect ( t_ast node,
t_ms_data data 
)
static

redirect out ">" to the file output

--

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

Definition at line 67 of file redirect_out.c.

68{
69 int fd;
70
71 if (data->std_out == -1)
72 {
73 data->std_out = open_file(node->right, ">");
74 if (data->std_out == -1)
75 return (1);
76 }
77 else
78 {
79 fd = open_file(node->right, ">");
80 if (fd == -1)
81 return (1);
82 dup2(fd, STDOUT_FILENO);
83 close(fd);
84 }
85 return (0);
86}
int open_file(t_ast *node, char *direction)
open file in the context of redirection
Definition utils.c:34
struct s_ast * right
Definition tokens.h:54
int std_out
Definition shell.h:27

References open_file(), s_ast::right, and s_ms_data::std_out.

Referenced by redirect_out().

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

◆ redirect_out()

int redirect_out ( t_ast node,
t_ms_data data 
)

Definition at line 88 of file redirect_out.c.

89{
90 pid_t pid;
91 int status;
92
93 pid = fork();
94 if (pid == -1)
95 return (1);
96 if (pid == 0)
97 {
98 if (open_and_redirect(node, data) != 0)
99 exit(1);
100 execute_ast(node->left, data);
101 exit(0);
102 }
103 waitpid(pid, &status, 0);
104 return (WEXITSTATUS(status));
105}
int execute_ast(t_ast *node, t_ms_data *data)
execute Abstract Syntax Tree
Definition execute.c:38
static int open_and_redirect(t_ast *node, t_ms_data *data)
redirect out ">" to the file output
struct s_ast * left
Definition tokens.h:53

References execute_ast(), s_ast::left, and open_and_redirect().

Referenced by execute_ast().

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