-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathch.c
More file actions
51 lines (40 loc) · 855 Bytes
/
Copy pathch.c
File metadata and controls
51 lines (40 loc) · 855 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <unistd.h>
struct command{
const char **argv;
};
void spawn_proc (int in, int out, struct command *cmd) {
if (!fork ()){
if (in != 0){
dup2 (in, 0);
close (in);
}
if (out != 1){
dup2 (out, 1);
close (out);
}
execvp (cmd->argv [0], (char * const *)cmd->argv);
}
}
void fork_pipes (int n, struct command *cmd){
int i;
int in, fd [2];
in = 0;
for (i = 0; i < n - 1; ++i){
pipe (fd);
spawn_proc (in, fd [1], cmd + i);
close (fd [1]);
in = fd [0];
}
if (in != 0)
dup2 (in, 0);
execvp (cmd [i].argv [0], (char * const *)cmd [i].argv);
}
int main (){
const char *ls[] = { "ls", "-l", 0 };
const char *awk[] = { "awk", "{print $1}", 0 };
const char *sort[] = { "sort", 0 };
const char *uniq[] = { "uniq", 0 };
struct command cmd [] = { {ls}, {sort}, {uniq} };
fork_pipes (4, cmd);
return 0;
}