popen, pclose
Purpose
Initiates a pipe to or from a process.
Library
Standard I/O Library (libc.a)
Syntax
#include <stdio.h>
FILE *popen (command, type) int pclose (stream)
char *command, *type; FILE *stream;
Description
The popen subroutine creates a pipe between the calling
program and a shell command to be executed.
The command parameter points to a null-terminated string
containing a shell command line. The type parameter
pointers to a null-terminated string containing an I/O
mode, either ""r"" for reading or ""w"" for writing.
The popen subroutine returns a pointer to a FILE struc-
ture for the stream. If the type parameter is ""r"", you
can read from the standard output of the command by
reading from the file stream. If the type parameter is
""w"", you can write to the standard input of the command
by writing to the file stream.
Use the pclose subroutine to close any stream you have
opened with the popen subroutine. The pclose subroutine
waits for the associated process to terminate and then
returns the exit status of the command.
Because open files are shared, a type ""r"" command can
be used as an input filter and a type ""w"" as an output
filter.
Warning: If the original processes and the process
started with popen concurrently read or write a common
file, neither should use buffered I/O. If they do, the
results are unpredictable.
Some problems with an output filter can be prevented by
taking care to flush the buffer with the fflush subrou-
tine (see "fclose, fflush").
The popen subroutine returns a NULL pointer if files or
processes cannot be created, or if the shell cannot be
accessed.
The pclose subroutine returns -1 if stream is not associ-
ated with a popen command.
Related Information
In this book: "pipe," "wait," "fclose, fflush," "fopen,
freopen, fdopen," "standard i/o library," and "system."