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
|
#include <u.h>
#include <libc.h>
#include <bio.h>
#include "awk.h"
#define MAXFORKS 20
#define NSYSFILE 3
#define tst(a,b) (mode == OREAD? (b) : (a))
#define RDR 0
#define WTR 1
struct a_fork {
short done;
short fd;
int pid;
char status[128];
};
static struct a_fork the_fork[MAXFORKS];
Biobuf*
popen(char *cmd, int mode)
{
int p[2];
int myside, hisside, pid;
int i, ind;
for (ind = 0; ind < MAXFORKS; ind++)
if (the_fork[ind].pid == 0)
break;
if (ind == MAXFORKS)
return nil;
if(pipe(p) < 0)
return nil;
myside = tst(p[WTR], p[RDR]);
hisside = tst(p[RDR], p[WTR]);
switch (pid = fork()) {
case -1:
return nil;
case 0:
/* myside and hisside reverse roles in child */
close(myside);
dup(hisside, tst(0, 1));
for (i=NSYSFILE; i<FOPEN_MAX; i++)
close(i);
execl("/bin/rc", "rc", "-c", cmd, nil);
exits("exec failed");
default:
the_fork[ind].pid = pid;
the_fork[ind].fd = myside;
the_fork[ind].done = 0;
close(hisside);
return(Bfdopen(myside, mode));
}
}
int
pclose(Biobuf *ptr)
{
int f, r, ind;
Waitmsg *status;
f = Bfildes(ptr);
Bterm(ptr);
for (ind = 0; ind < MAXFORKS; ind++)
if (the_fork[ind].fd == f && the_fork[ind].pid != 0)
break;
if (ind == MAXFORKS)
return -1;
if (!the_fork[ind].done) {
do {
if((status = wait()) == nil)
r = -1;
else
r = status->pid;
for (f = 0; f < MAXFORKS; f++) {
if (r == the_fork[f].pid) {
the_fork[f].done = 1;
strecpy(the_fork[f].status, the_fork[f].status+512, status->msg);
break;
}
}
free(status);
} while(r != the_fork[ind].pid && r != -1);
if(r == -1)
strcpy(the_fork[ind].status, "No loved ones to wait for");
}
the_fork[ind].pid = 0;
if(the_fork[ind].status[0] != '\0')
return 1;
return 0;
}
|