Hi,
below is a simplified version of my program. I am trying to
implement a version of popen() where my keyboard input goes to a
process's standard input; and its standard output goes to my screen
(well - without just running the program directly :-)) However, it's
not working, the child-to-parent pipe c2p never gets written to (at
least with /bin/ls and /bin/date). Is it wrong to close c2p[0] and
p2c[1] in child?
Invoke as:
e.g.
#include
#include
#include
int main (int argc, char *argv[])
{
int p2c[2], c2p[2], count;
char buf[1024];
pipe(p2c); pipe(c2p);
if (fork()) {
fd_set r;
close(p2c[0]); close(c2p[1]);
while (1) {
FD_ZERO(&r);
FD_SET(0, &r);
FD_SET(c2p[0], &r);
select(c2p[0], &r, NULL, NULL, NULL);
if (FD_ISSET(c2p[0], &r)) {
if ((count = read(c2p[0], buf, 1024))
> 0) {
write(1, buf, count);
}
}
if (FD_ISSET(0, &r)) {
if ((count = read(0, buf, 1024)) > 0)
{
write(p2c[1], buf, count);
}
}
}
} else {
close(0);
dup(p2c[0]);
close(p2c[0]);
close(1);
dup(c2p[1]);
close(c2p[1]);
execvp(argv[1], &argv[1]);
}
return 0;
}