-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipex.c
73 lines (67 loc) · 2.24 KB
/
pipex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bsyvasal <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/07 17:38:30 by bsyvasal #+# #+# */
/* Updated: 2024/03/01 11:39:56 by bsyvasal ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
/**
* Toggles carret character (^C, ^D, ^\) which are shown by default when
* ctrl+c, ctrl+d, ctrl+\ are pressed.
* is_on = 0 for prompt
* is_on = 1 during the command execution
* @param is_on 1 for enable carret characters from displayin in the shell
* 0 to disable carret characters from shell
*/
static void toggle_carret(int is_on)
{
struct termios new_attr;
tcgetattr(STDIN_FILENO, &new_attr);
if (!is_on)
new_attr.c_lflag &= ~ECHOCTL;
else
new_attr.c_lflag |= ECHOCTL;
tcsetattr(STDIN_FILENO, TCSANOW, &new_attr);
}
static void initialise(t_pipe *data)
{
data->pid = ft_calloc(sizeof(pid_t), data->cmdc);
if (!data->pid)
msg_freeall_exit("malloc", NULL, 1, data);
data->pipe[0][0] = -1;
data->pipe[0][1] = -1;
data->pipe[1][0] = -1;
data->pipe[1][1] = -1;
}
int pipex(t_pipe *data)
{
int i;
int exit_status;
initialise(data);
exit_status = 0;
i = -1;
toggle_carret(1);
while (++i < data->cmdc)
exit_status = execute(i, data);
i = -1;
while (++i < data->cmdc)
waitpid(data->pid[i], &data->status, 0);
toggle_carret(0);
free(data->pid);
if (exit_status == 4)
exit_status = 2;
if (exit_status)
return (exit_status);
if (WEXITSTATUS(data->status) != 0)
return (WEXITSTATUS(data->status));
if (data->status == 2)
return (printf("\n") + 127);
if (data->status == 3)
return (printf("Quit: 3\n") + 121);
return (0);
}