Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

popen - initiate pipe stream to or from process

&pagelevel(4)&pagelevel

Syntax

#include <stdio.h>

FILE *popen (const char *command, const char *mode);

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:

  1. 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.

  2. 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.

Return val.

Pointer to a stream


if successful.

Null pointer

if files or processes cannot be created.

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)].