|
NAMEpopen , pclose —
process I/O
LIBRARYStandard C Library (libc, -lc)SYNOPSIS#include <stdio.h>
FILE *
int
DESCRIPTIONThepopen () function “opens” a process by
creating a bidirectional pipe forking, and invoking the shell. Any streams
opened by previous popen () calls in the parent process
are closed in the new child process. Historically,
popen () was implemented with a unidirectional pipe;
hence many implementations of popen () only allow the
type argument to specify reading or writing, not both.
Since popen () is now implemented using a bidirectional
pipe, the type argument may request a bidirectional data
flow. The type argument is a pointer to a
null-terminated string which must be ‘r ’
for reading, ‘w ’ for writing, or
‘r+ ’ for reading and writing.
A letter ‘ The command argument is a pointer to a
null-terminated string containing a shell command line. This command is
passed to /bin/sh using the
The return value from Note that output The RETURN VALUESThepopen () function returns
NULL if the
fork(2) or
pipe(2)
calls fail, or if it cannot allocate memory.
The ERRORSThepopen () function does not reliably set
errno.
SEE ALSOsh(1), fork(2), pipe(2), wait4(2), fclose(3), fflush(3), fopen(3), stdio(3), system(3)HISTORYApopen () and a pclose ()
function appeared in Version 7 AT&T UNIX.
Bidirectional functionality was added in FreeBSD 2.2.6. BUGSSince the standard input of a command opened for reading shares its seek offset with the process that calledpopen (), if the original
process has done a buffered read, the command's input position may not be as
expected. Similarly, the output from a command opened for writing may become
intermingled with that of the original process. The latter can be avoided by
calling
fflush(3)
before popen ().
Failure to execute the shell is indistinguishable from the shell's failure to execute command, or an immediate exit of the command. The only hint is an exit status of 127. The
Visit the GSP FreeBSD Man Page Interface. |