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

Go to the source code of this file.

Functions

int redirect_in (t_ast *node, t_ms_data *data)
 
static int setup_redirection (t_ast *node, int *original_stdin)
 

Function Documentation

◆ redirect_in()

int redirect_in ( t_ast node,
t_ms_data data 
)

Definition at line 45 of file redirect_in.c.

46{
47 int status;
48 int original_stdin;
49
50 if (setup_redirection(node, &original_stdin) == -1)
51 return (EXIT_FAILURE);
52 if (!node->left->args[0])
53 status = EXIT_SUCCESS;
54 else
55 status = execute_ast(node->left, data);
56 if (dup2(original_stdin, STDIN_FILENO) == -1)
57 status = EXIT_FAILURE;
58 close(original_stdin);
59 return (status);
60}
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
#define EXIT_FAILURE
Definition exit_status.h:17
static int setup_redirection(t_ast *node, int *original_stdin)
Definition redirect_in.c:22
char ** args
Definition tokens.h:52
struct s_ast * left
Definition tokens.h:53

References s_ast::args, execute_ast(), EXIT_FAILURE, EXIT_SUCCESS, s_ast::left, and setup_redirection().

Referenced by execute_ast().

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

◆ setup_redirection()

static int setup_redirection ( t_ast node,
int *  original_stdin 
)
static

Definition at line 22 of file redirect_in.c.

23{
24 int fd;
25
26 *original_stdin = dup(STDIN_FILENO);
27 if (*original_stdin == -1)
28 return (-1);
29 fd = open_file(node->right, "<");
30 if (fd == -1)
31 {
32 close(*original_stdin);
33 return (-1);
34 }
35 if (dup2(fd, STDIN_FILENO) == -1)
36 {
37 close(fd);
38 close(*original_stdin);
39 return (-1);
40 }
41 close(fd);
42 return (EXIT_SUCCESS);
43}
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

References EXIT_SUCCESS, open_file(), and s_ast::right.

Referenced by redirect_in().

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