The Single UNIX ® Specification, Version 2
Copyright © 1997 The Open Group

 NAME

popen - initiate pipe streams to or from a process

 SYNOPSIS



#include <stdio.h>

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

 DESCRIPTION

The popen() function executes the command specified by the string command. It 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 or write to the pipe.

If the implementation supports the referenced XCU specification, the environment of the executed command 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 pathname for the sh utility.

The popen() function ensures that any streams from previous popen() calls that remain open in the parent process are closed in the new child process.

The mode argument to popen() is a string that specifies I/O mode:

  1. If mode is r, when the child process is started its file descriptor STDOUT_FILENO will be the writable end of the pipe, and the file descriptor fileno(stream) in the calling process, 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 its file descriptor STDIN_FILENO will be the readable end of the pipe, and the file descriptor fileno(stream) in the calling process, where stream is the stream pointer returned by popen(), will be the writable end of the pipe.

  3. If mode is any other value, the result is undefined.

After popen(), both the parent and the child process will be capable of executing independently before either terminates.

Pipe streams are byte oriented.

 RETURN VALUE

On successful completion, popen() returns a pointer to an open stream that can be used to read or write to the pipe. Otherwise, it returns a null pointer and may set errno to indicate the error.

 ERRORS

The popen() function may fail if:
[EMFILE]
{FOPEN_MAX} or {STREAM_MAX} streams are currently open in the calling process.
[EINVAL]
The mode argument is invalid.

The popen() function may also set errno values as described by fork() or pipe().

 EXAMPLES

None.

 APPLICATION USAGE

Because open files are shared, a mode r command can be used as an input filter and a mode w command as an output filter.

Buffered reading before opening an input filter may leave the standard input of that filter mispositioned. Similar problems with an output filter may be prevented by careful buffer flushing, for example, with fflush().

A stream opened by popen() should be closed by pclose().

The behaviour of popen() is specified for values of mode of r and w. Other modes such as rb and wb might be supported by specific implementations, but these would not be portable features. Note that historical implementations of popen() only check to see if the first character of mode is r. Thus, a mode of robert the robot would be treated as mode r, and a mode of anything else would be treated as mode w.

If the application calls waitpid() or waitid() with a pid argument greater than 0, and it still has a stream that was called with popen() open, it must ensure that pid does not refer to the process started by popen().

To determine whether or not the XCU specification environment is present, use the function call:


sysconf(_SC_2_VERSION)

(See sysconf()).

 FUTURE DIRECTIONS

None.

 SEE ALSO

sh, pclose(), pipe(), sysconf(), system(), <stdio.h>.

DERIVATION

Derived from Issue 1 of the SVID.

UNIX ® is a registered Trademark of The Open Group.
Copyright © 1997 The Open Group
[ Main Index | XSH | XCU | XBD | XCURSES | XNS ]