-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpipe.c
More file actions
37 lines (35 loc) · 735 Bytes
/
Copy pathpipe.c
File metadata and controls
37 lines (35 loc) · 735 Bytes
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
#include "headers.h"
void piping(char *comm)
{
char *token = strtok (comm, "|");
char **args = (char **)malloc(256 * sizeof(char *));
ll num = 0;
while(token != NULL)
{
args[num] = token;
token = strtok(NULL, "|");
num++;
}
int type = 0, fd[2], fdesc = 0;
pid_t ps;
for(int j=0; j < num; j++)
{
pipe(fd);
ps = fork();
if(ps == 0)
{
dup2(fdesc, 0);
if(args[j+1] != NULL)
dup2(fd[1], 1);
close(fd[0]);
execute_com(args[j]);
exit(2);
}
else
{
wait(NULL);
close(fd[1]);
fdesc = fd[0];
}
}
}