#include <stdio.h> FILE *popen(const char *command, const char *mode);
The environment of the executed command shall be as if a child process were created within the popen() call using the fork() function, 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 shall ensure 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:
After popen(), both the parent and the child process shall be capable of executing independently before either terminates.
Pipe streams are byte-oriented.
The popen() function may fail if:
The popen() function may also set errno values as described by fork() or pipe().
The following sections are informative.
The following example demonstrates the use of popen() and pclose() to execute the command ls* in order to obtain a list of files in the current directory:
#include <stdio.h> ... FILE *fp; int status; char path[PATH_MAX]; fp = popen("ls *", "r"); if (fp == NULL) /* Handle error */; while (fgets(path, PATH_MAX, fp) != NULL) printf("%s", path); status = pclose(fp); if (status == -1) { /* Error reported by pclose() */ ... } else { /* Use macros described under wait() to inspect `status' in order to determine success/failure of command executed by popen() */ ... }
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 behavior 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 environment specified in the Shell and Utilities volume of POSIX.1-2017 is present, use the function call:
sysconf(_SC_2_VERSION)
If the original and popen()ed processes both intend to read or write or read and write a common file, and either will be using FILE-type C functions (fread(), fwrite(), and so on), the rules for sharing file handles must be observed (see Section 2.5.1, Interaction of File Descriptors and Standard I/O Streams).
The Base Definitions volume of POSIX.1-2017, <stdio.h>
The Shell and Utilities volume of POSIX.1-2017, sh
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .