maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
redirect_append.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* redirect_append.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/11/08 15:13:15 by dmdemirk #+# #+# */
9/* Updated: 2024/11/08 15:13:40 by dmdemirk ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "tokens.h"
14#include "shell.h"
15#include "redirection.h"
16#include "execute.h"
17#include "pipe.h"
18#include <sys/wait.h>
19
20static int open_and_redirect(t_ast *node, t_ms_data *data)
21{
22 int fd;
23
24 if (data->std_out == -1)
25 {
26 data->std_out = open_file(node->right, ">>");
27 if (data->std_out == -1)
28 return (EXIT_FAILURE);
29 }
30 else
31 {
32 fd = open_file(node->right, ">>");
33 if (fd == -1)
34 return (EXIT_FAILURE);
35 dup2(fd, STDOUT_FILENO);
36 close(fd);
37 }
38 return (EXIT_SUCCESS);
39}
40
42{
43 pid_t pid;
44 int exec_status;
45 int status;
46
47 pid = fork();
48 if (pid == -1)
49 return (EXIT_FAILURE);
50 if (pid == 0)
51 {
52 if (open_and_redirect(node, data) != 0)
53 exit(EXIT_FAILURE);
54 exec_status = execute_ast(node->left, data);
55 exit(exec_status);
56 }
57 if (waitpid(pid, &status, 0) == -1)
58 return (EXIT_FAILURE);
59 if (WIFSIGNALED(status))
60 return (128 + WTERMSIG(status));
61 return (WEXITSTATUS(status));
62}
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 open_and_redirect(t_ast *node, t_ms_data *data)
int redirect_append(t_ast *node, t_ms_data *data)
int open_file(t_ast *node, char *direction)
open file in the context of redirection
Definition utils.c:34
Definition tokens.h:50
struct s_ast * right
Definition tokens.h:54
struct s_ast * left
Definition tokens.h:53
int std_out
Definition shell.h:27