maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
redirect_in.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* redirect_in.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/07/11 14:32:40 by dmdemirk #+# #+# */
9/* Updated: 2024/11/08 13:14:21 by dmdemirk ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "tokens.h"
14#include "shell.h"
15#include "redirection.h"
16#include "execute.h"
17#include <string.h>
18#include <sys/wait.h>
19
20int redirect_in(t_ast *node, t_ms_data *data);
21
22static int setup_redirection(t_ast *node, int *original_stdin)
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}
44
45int redirect_in(t_ast *node, t_ms_data *data)
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
int redirect_in(t_ast *node, t_ms_data *data)
Definition redirect_in.c:45
int open_file(t_ast *node, char *direction)
open file in the context of redirection
Definition utils.c:34
Definition tokens.h:50
char ** args
Definition tokens.h:52
struct s_ast * right
Definition tokens.h:54
struct s_ast * left
Definition tokens.h:53