-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe_handler.c
73 lines (71 loc) · 1.36 KB
/
pipe_handler.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
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include "shell.h"
#include <string.h>
int fildes[2];
int pipe_handler(char* s)
{
char cmd[1024], tmp[1024];
strcpy(cmd, s);
char** pipe_sep_cmds = parseStr(cmd, "|");
int no_of_pipes = 0;
for (int i=0; pipe_sep_cmds[i] != NULL; ++i, no_of_pipes++)
{
strcpy(tmp, pipe_sep_cmds[i]);
trim(tmp, pipe_sep_cmds[i]);
}
no_of_pipes--;
if (no_of_pipes == 0) return -1;
int in_fd = dup(0);
int out_fd = dup(1);
for (int i=0, pid, status, prev, nxt; i<=no_of_pipes; ++i)
{
char** args;
if (i == 0)
{
prev = 0;
nxt = 1;
}
else if (i == no_of_pipes)
{
prev = 1;
nxt = 0;
}
else nxt = prev = 1;
if (prev)
{
close(fildes[1]);
in_fd = dup(0);
if (dup2(fildes[0], 0) == -1)
fprintf(stderr, "Error: dup2 failed\n");
close(fildes[0]);
}
if (nxt)
{
pipe(fildes);
out_fd = dup(1);
if (dup2(fildes[1], 1) == -1)
fprintf(stderr, "Error: dup2 failed\n");
}
pid = fork();
if (pid == 0)
{
args = redirect_handler(pipe_sep_cmds[i]);
if (!args) exit(0);
int x = isAllowed(pipe_sep_cmds[i]);
if (x >= 0) executeCmd(pipe_sep_cmds[i], args, allowed_execs[x]);
else execvp(args[0], args);
exit(0);
}
else
{
waitpid(pid, &status, WUNTRACED);
dup2(in_fd,0);
dup2(out_fd,1);
}
}
return 0;
}