Go to main content

man pages section 3: Basic Library Functions

Exit Print View

Updated: Wednesday, July 27, 2022
 
 

pclose(3C)

Name

popen, pclose - initiate a pipe to or from a process

Synopsis

#include <stdio.h>

FILE *popen(const char *command, const char *mode);
int pclose(FILE *stream);

Description

The popen() function creates a pipe between the calling program and the command to be executed. The arguments to popen() are pointers to null-terminated strings. The command argument consists of a shell command line. The mode argument is an I/O mode, either starting with r for reading or w for writing. The value returned is a stream pointer such that one can write to the standard input of the command, if the I/O mode starts with w, by writing to the file stream (see Intro(3)); and one can read from the standard output of the command, if the I/O mode starts with r, by reading from the file stream. Because open files are shared, a type r command may be used as an input filter and a type w as an output filter. An additional e and/or f character can also be included in the mode argument; it indicates that the underlying file descriptor of the returned FILE has the FD_CLOEXEC and/or FD_CLOFORK flag, respectively, set.

The environment of the executed command will be as if a child process were created within the popen() call using fork(2). If the application is standard-conforming (see standards(7)), the child is created as if invoked with the call:

execl("/usr/xpg4/bin/sh", "sh", "-c", command, NULL);

otherwise, the child is created as if invoked with the call:

execl("/usr/bin/sh", "sh",	"-c", command, NULL);

The pclose() function closes a stream opened by popen() by closing the pipe. It waits for the associated process to terminate and returns the termination status of the process running the command language interpreter. This is the value returned by waitpid(3C). See wait.h(3HEAD) for more information on termination status. If, however, a call to waitpid() with a pid argument equal to the process ID of the command line interpreter causes the termination status to be unavailable to pclose(), then pclose() returns −1 with errno set to ECHILD to report this condition.

Return Values

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

Upon successful completion, pclose() returns the termination status of the command language interpreter as returned by waitpid(). Otherwise, it returns −1 and sets errno to indicate the error.

Errors

The pclose() function will fail if:

ECHILD

The status of the child process could not be obtained, as described in the DESCRIPTION.

The popen() function may fail if:

EMFILE

There are currently FOPEN_MAX or STREAM_MAX streams open in the calling process.

EINVAL

The mode argument is invalid.

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

Usage

If the original and popen() processes concurrently read or write a common file, neither should use buffered I/O. Problems with an output filter may be forestalled by careful buffer flushing, for example, with fflush(3C).

The IFS and PATH environment variables should be set to values you control before calling popen(), to avoid the users environment variables causing the use of different programs than expected, especially if the program is run with privileges the user does not normally have. PATH should contain only absolute paths, with no relative paths or empty entries. IFS should be set to space and tab: " \t".

The SIGCHLD signal does not apply to processes run from popen(). If a process sets a signal handler for SIGCHLD, that signal handler will not be called when the popen() process terminates.

A wait(3C) call from another thread in the same process will not interfere with the return value of pclose().

Examples

Example 1 popen() example

The following program will print on the standard output (see stdio(3C)) the names of files in the current directory with a .c suffix.

#include <stdio.h>
#include <stdlib.h>
main( )
{
        const char *cmd = "/usr/bin/ls *.c";
        char buf[BUFSIZ];
        FILE *ptr;

        if ((ptr = popen(cmd, "r")) != NULL) {
                while (fgets(buf, BUFSIZ, ptr) != NULL)
                        (void) printf("%s", buf);
                (void) pclose(ptr);
        }
        return 0;
}
Example 2 system() replacement

The following function can be used in a multithreaded process in place of the most common usage of the Unsafe system(3C) function:

int my_system(const char *cmd)
{
        FILE *p;

        if ((p = popen(cmd, "w")) == NULL)
                return (-1);
        return (pclose(p));
}

Attributes

See attributes(7) for descriptions of the following attributes:

ATTRIBUTE TYPE
ATTRIBUTE VALUE
Interface Stability
Committed
MT-Level
Safe
Standard

See Also

ksh(1), pipe(2), fclose(3C), fopen(3C), posix_spawn(3C), stdio(3C), system(3C), wait(3C), waitpid(3C), wait.h(3HEAD), attributes(7), standards(7)