-
Notifications
You must be signed in to change notification settings - Fork 5
/
demux.cpp
70 lines (68 loc) · 1.7 KB
/
demux.cpp
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
#include <assert.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int fds[2];
int nfds;
int main(int argc, char *argv[])
{
if (argc < 3) {
fprintf(stderr, "usage: demux leftcmd rightcmd\n");
exit(1);
}
for (int a = 1; a < 3; a++) {
int pin[2];
if (pipe(pin) != 0) {
perror("pipe");
exit(1);
}
pid_t child = fork();
if (child == -1) {
perror("fork");
exit(1);
}
if (child == 0) {
dup2(pin[0], 0);
close(pin[1]);
execl("/bin/sh", "sh", "-c", argv[a], NULL);
perror("execl");
exit(127);
}
close(pin[0]);
fds[nfds++] = pin[1];
}
for (;;) {
char buf[4096];
ssize_t n = read(0, buf, sizeof(buf));
if (n < 0) {
perror("read");
break;
} else if (n == 0) {
fprintf(stderr, "demux: eof on input\n");
break;
}
assert(n % (nfds*2) == 0);
char out[2][sizeof(buf)/2];
for (ssize_t i = 0; i < n; ) {
for (int j = 0; j < nfds; j++) {
*(short *)&out[j][i/(nfds*2)*2] = *(short *)&buf[i];
i += 2;
}
}
for (int i = 0; i < nfds; i++) {
ssize_t w = write(fds[i], out[i], n/nfds);
if (w < 0) {
perror("write");
exit(1);
}
if (w < n/nfds) {
fprintf(stderr, "short write: %d < %d\n", (int)w, (int)(n/nfds));
exit(1);
}
}
}
for (int i = 0; i < nfds; i++) {
close(fds[i]);
}
return 0;
}