POPEN(3POSIX)
POPEN(3POSIX) ๐
okay bestie, this one lives in the POSIX Programmer's Manual, and it's the whole popen situation. Buckle up, she's iconic โจ
๐จ PROLOG ๐จ
real talk first, no cap: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so like, go read the corresponding Linux manual page for the actual Linux tea โ), or honestly the interface might just not be implemented on Linux at all. iykyk ๐
โจ NAME โจ
popen โ initiate pipe streams to or from a process ๐ซถ
๐ SYNOPSIS ๐
#include <stdio.h>
FILE *popen(const char *command, const char *mode);
๐ฆ DESCRIPTION ๐ฆ
So the popen() function shall execute the command specified by the string command. She shall create a pipe between the calling program and the executed command, and she shall return a pointer to a stream that can be used to either read from or write to the pipe. main character behaviour fr fr ๐
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. (unspecified = don't ask her where she lives, she's mysterious) ๐
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. yes bestie, she cleans up after herself ๐
The mode argument to popen() is a string that specifies I/O mode:
- If
modeisr, when the child process is started, its file descriptorSTDOUT_FILENOshall be the writable end of the pipe, and the file descriptorfileno(stream)in the calling process โ wherestreamis the stream pointer returned bypopen()โ shall be the readable end of the pipe. - If
modeisw, when the child process is started its file descriptorSTDIN_FILENOshall be the readable end of the pipe, and the file descriptorfileno(stream)in the calling process โ wherestreamis the stream pointer returned bypopen()โ shall be the writable end of the pipe. - If
modeis any other value, the result is unspecified. ๐ซ (translation: she simply will not be responsible for what happens)
After popen(), both the parent and the child process shall be capable of executing independently before either terminates. two queens, one pipe, zero dependency ๐
Pipe streams are byte-oriented. periodt. ๐
๐ซ RETURN VALUE ๐ซ
Upon successful completion, popen() shall return a pointer to an open stream that can be used to read or write to the pipe. slay โจ
Otherwise, it shall return a null pointer and may set errno to indicate the error. ๐ญ
๐ฅ ERRORS ๐ฅ
The popen() function shall fail if:
EMFILEโ{STREAM_MAX}streams are currently open in the calling process.
The popen() function may fail if:
EMFILEโ{FOPEN_MAX}streams are currently open in the calling process.EINVALโ themodeargument is invalid. ๐ซ
The popen() function may also set errno values as described by fork() or pipe().
heads up: the following sections are informative. they're the vibes, not the rules ๐
๐ธ EXAMPLES ๐ธ
๐ Using popen() to Obtain a List of Files from the ls Utility
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() */
...
}
literally the blueprint bestie, copy it, it's cute ๐
๐ APPLICATION USAGE ๐
Since 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(). please. she deserves a proper goodbye. ๐ซถ
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. ๐ค robert the robot is a real and valid mode in spirit
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)
(See sysconf().)
๐ RATIONALE ๐
The popen() function should not be used by programs that have set user (or group) ID privileges. not the vibe, bestie. The fork() and exec family of functions (except execlp() and execvp()) should be used instead. This prevents any unforeseen manipulation of the environment of the user that could cause execution of commands not anticipated by the calling program. ๐จ
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).
๐ฎ FUTURE DIRECTIONS ๐ฎ
None. she's done evolving, she peaked, respect it ๐
๐ง SEE ALSO ๐ง
- Section 2.5, Standard I/O Streams
fork()pclose()pipe()sysconf()system()wait()waitid()
The Base Definitions volume of POSIX.1โ2017, <stdio.h>
The Shell and Utilities volume of POSIX.1โ2017, sh
โญ COPYRIGHT โญ
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
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 .
and that's the whole page bestie!! ๐ซถโจ this one came from IEEE/The Open Group, 2017, and yeah it's the POPEN(3POSIX) page, mwah, go pipe something cute ๐๐๐ฆ
๐ฆ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 ยท Other
- debian trixie: manpages-posix-dev 2017a-2 ยท Other
- ubuntu jammy: manpages-posix-dev 2017a-2 ยท Other
- ubuntu noble: manpages-posix-dev 2017a-2 ยท Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 ยท Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 ยท Other
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms