139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
...
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
// note ptys and pipes use basically the same
// interface; you can use one as a drop-in
// replacement for the other, tho the creation
// function syntax is a bit different.
if(fork()) goto master;
else goto child;
master: {
close(wr);
char buf[256]; size_t sz;
while(sz = read(rd, buf, 255)) {
buf[sz]=0;
for (size_t i=0;i<sz;++i) {
................................................................................
}
leave: {
close(rd);
return 0;
}
child: {
close(rd);
// redirect stdout (NOT stderr) to our pty master
dup2(wr, 1);
close(wr);
execv(argv[1],argv+2);
return 1; // THIS SHOULD NEVER HAPPEN
}
}
|
|
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
...
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
// note ptys and pipes use basically the same
// interface; you can use one as a drop-in
// replacement for the other, tho the creation
// function syntax is a bit different.
if(fork()) goto master;
else goto slave;
master: {
close(wr);
char buf[256]; size_t sz;
while(sz = read(rd, buf, 255)) {
buf[sz]=0;
for (size_t i=0;i<sz;++i) {
................................................................................
}
leave: {
close(rd);
return 0;
}
slave: {
close(rd);
// redirect stdout (NOT stderr) to our pty master
dup2(wr, 1);
close(wr);
execv(argv[1],argv+2);
return 1; // THIS SHOULD NEVER HAPPEN
}
}
|