-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe1.c
158 lines (138 loc) · 3.41 KB
/
pipe1.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <sys/wait.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
void fork_last(int fdp[2], char *cmd, char **args)
{
if (!fork()) {
close(STDIN_FILENO);
fprintf(stderr, "cmd: %s\tset Stdin=%d\n", cmd, fdp[0]);
dup2(fdp[0], STDIN_FILENO);
close(fdp[1]);
close(fdp[0]);
execvp(cmd, args);
return;
} else { }
}
void fork_mid(int *fdp, char *cmd, char **args)
{
if (!fork()) {
close(STDOUT_FILENO);
// dup2(fdp[1], STDOUT_FILENO);
fprintf(stderr, "cmd %s\tset StdOut=%d\n", cmd, fdp[1]);
dup2(*(fdp+1), STDOUT_FILENO);
close(STDIN_FILENO);
// close(*(pfc+1)); close(*pfc);
int *pfc = fdp+2;
// pfc+=2;
// dup2(fdp[0], STDIN_FILENO);
fprintf(stderr, "cmd %s\tset StdIn=%d\n", cmd, *(pfc));
dup2(*(pfc), STDIN_FILENO);
// close(*(pfc+1)); close(*pfc);
execvp(cmd, args);
return;
} else { }
}
void fork_first(int fdp[2], char *cmd, char **args)
{
if (!fork()) {
close(STDOUT_FILENO);
fprintf(stderr, "cmd %s\tset StdOut=%d\n", cmd, fdp[1]);
dup2(fdp[1], STDOUT_FILENO);
close(fdp[1]);
close(fdp[0]);
execvp(cmd, args);
return;
} else { }
}
char * get_cmd(char *str, int s, int e)
{
char *c, *cmd=NULL;
for(c = str+s; c < str+e; c++) {
// fprintf(stderr, "'%c' ", *c);
if (*c != ' ' && cmd == NULL)
{ cmd = c; continue; }
if (*c == ' ' && cmd != NULL)
{ *c = 0; break; }
} // for
fprintf(stderr, "cmd: =%s=\n", cmd);
return cmd;
}
int main(int argc, char **argv)
{
#define MAX_LEN (1024)
#define MAX_CMDS (8)
char buf[MAX_LEN];
ssize_t n = read(0, &buf, MAX_LEN); // from STDIN
if ( n == -1) { perror("read"); return(1);
} else printf("read ret: %d\n", n);
buf[n] = 0;
char *args[8]; bzero(args, sizeof(char *) * 8);
printf("%s", buf);
//#define FPID ("/home/box/pid")
char *cmd; // = buf;
int i,j; char *c;
int pfd[2];
int PFD[8][2]; int *PFC;
for(i=0; i < 8; i++) {
pipe(&PFD[i][0]);
// fprintf(stderr, "pfd[%d]: %d %d\n", i, PFD[i][0], PFD[i][1]);
}
pipe(pfd);
int numc = 1; // number of commands
int ixs[MAX_CMDS]; bzero(ixs, sizeof(int) * MAX_CMDS);
for(i=0, j=1; i < n; i++) if (buf[i] == '|') {
numc++;
buf[i] = ' '; // XX
ixs[j++] = i;
}
ixs[j]=n; for(i=0; i <= j; i++)
fprintf(stderr, "ixs[%d]=%d\n", i, ixs[i]);
/*
char *args_who[] = {"wc", NULL };
char *args_wc[] = {"wc", NULL };
fork_last(PFD[0], "wc", args_wc);
fork_first(PFD[0], "who", args_who);
*/
PFC = PFD[0];
cmd = get_cmd(buf, ixs[j-1], ixs[j]);
// fprintf(stderr, "cmd last: =%s=\n", cmd);
args[0]=cmd;
// fork_last(PFD[0], cmd, args); bzero(args, sizeof(char *) * 8);
fork_last(PFC, cmd, args); bzero(args, sizeof(char *) * 8);
j--;
// TODO: Pid pair!!
if (j > 1) {
cmd = get_cmd(buf, ixs[j-1], ixs[j]);
args[0]=cmd;
// fprintf(stderr, "cmd %i: %s\n", j, cmd);
fork_mid(PFC, cmd, args);
j--;
PFC++;
PFC++;
}
if (j = 1) { fprintf(stderr, "j=%d\n", j);
cmd = get_cmd(buf, ixs[j-1], ixs[j]);
args[0]=cmd;
// fprintf(stderr, "cmd 1st: %s\n", cmd);
// fork_first(PFD[0], cmd, args);
fork_first(PFC, cmd, args);
}
for(i=0, j=0; i < n; i++) {
if ( ' ' == buf[i] ) {
buf[i]=0;
if (buf[i+1] == '|') {
printf("going to execute: %s\n", cmd);
if(!fork) { // child
close(STDOUT_FILENO);
execvp(cmd, args);
// execlp(cmd, cmd, NULL);
return 0;
} else { // parent
}
}
break;
}
}
return 0;
}