/* File: popenDemo.cpp * Demo of automated pipe open function * Creates pipe, forks a child, closes unused ends, * execs a shell to execute the command, and waits for it to terminate */ #include #include #include #include #include #include #include #define MAX 5 int main(void){ FILE *fin; char buffer[PIPE_BUF]; int n; fin = popen("ls -l", "r"); /* run the ipcs command*/ while((n=read(fileno(fin), buffer, PIPE_BUF)) > 0) write(fileno(stdout), buffer, n); /* display ipcs output */ pclose(fin); exit(0); }