Description | popen() executes the command specified by the string command, creates a pipe between the calling program and the executed command, and returns a pointer to a stream that can be used to either read from (I/O mode r ) or write to (I/O mode w ) the pipe.
The environment of the executed command in an XPG4-conformant implementation will be as if a child process were created within the popen() call using fork() , and the child invoked the sh utility using the call:
execl ( shell_path, "sh", "-c", command, (char *)0); where shell_path is an unspecified name for the sh utility. popen() ensures that any streams from previous popen calls that remain open in the parent process are closed in the new child process. mode is a string that specifies I/O mode:
If mode is r when the child process is started, the standard output of the command will be redirected to the pipe. The file descriptor STDOUT_FILENO will be the writable end of the pipe, and the file descriptor fileno(stream), where stream is the stream pointer returned by popen() , will be the readable end of the pipe. If mode is w when the child process is started, the standard output of the command will be redirected to the pipe. The file descriptor STDIN_FILENO will be the readable end of the pipe, and the file descriptor fileno(stream), where stream is the stream pointer returned by popen() , will be the writable end of the pipe.
After popen() , both the parent and the child process will be capable of executing independently before either terminates. |
Notes | If the parent process and the process created by popen() read or write a file simultaneously, neither of the processes may use buffered I/O. Problems with an output filter can be avoided by taking the precaution of flushing the buffers, e.g. with flush() (see also
fclose()) .
popen() is executed only for POSIX files. |
See also | pclose() , pipe() , sysconf() , system() , stdio.h , the sh command in the manual "POSIX Commands" [2 (Related publications)].
|