POPEN(3P)

πŸ’– POPEN(3P) πŸ’–

okay bestie, welcome to the POSIX Programmer's Manual ✨ this one's about popen, and it's genuinely so useful it's giving main character energy fr fr 🌸


🚨 PROLOG 🚨

okay so first things first, bestie, this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so like, go check the corresponding Linux manual page for the deets on Linux behavior), or ngl the interface might just not be implemented on Linux at all πŸ’€


✨ NAME ✨

popen β€” initiate pipe streams to or from a process 🫢


πŸŽ€ SYNOPSIS πŸŽ€

#include <stdio.h>

FILE *popen(const char *command, const char *mode);

🌈 DESCRIPTION 🌈

The popen() function shall execute the command specified by the string command. It shall create a pipe between the calling program and the executed command, and shall return a pointer to a stream that can be used to either read from or write to the pipe. literally a whole little relationship between you and a command, iconic πŸ’…

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, so don't come for me asking what it is πŸ₯²)

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. so no leaking, we clean up after ourselves, periodt ✨

The mode argument to popen() is a string that specifies I/O mode:

  1. If mode is r, when the child process is started, its file descriptor STDOUT_FILENO shall be the writable end of the pipe, and the file descriptor fileno(stream) in the calling process, where stream is the stream pointer returned by popen(), shall be the readable end of the pipe. πŸ”₯
  2. If mode is w, when the child process is started its file descriptor STDIN_FILENO shall be the readable end of the pipe, and the file descriptor fileno(stream) in the calling process, where stream is the stream pointer returned by popen(), shall be the writable end of the pipe. πŸ”₯
  3. If mode is any other value, the result is unspecified. πŸ’€ do not test this, bestie, it's not worth it

After popen(), both the parent and the child process shall be capable of executing independently before either terminates. you both get to live your lives, it's giving healthy boundaries πŸ¦‹

Pipe streams are byte-oriented. ⭐


πŸ‘‘ 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. Otherwise, it shall return a null pointer and may set errno to indicate the error. so like, check for NULL, always, no cap πŸ™ˆ


πŸ’€ 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 β€” The mode argument is invalid.

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

The following sections are informative. 🫢 (so like, the real requirements stopped up there, but these are still super worth reading, bestie)

πŸ“ 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() */
    ...
}

look at that, she's beautiful, she's efficient πŸ’–


🧚 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. slay, filters are literally the whole vibe ✨

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(). so like, flush your buffers, bestie, it's self care 🫢

A stream opened by popen() should be closed by pclose(). don't leave her hanging πŸ’…

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. 😭 not robert the robot pls that's sending me, but fr that's how it works, they only look at character one

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(). πŸ‘‘ important!!

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

bestie, this one's serious. setuid + popen() = absolutely not. πŸ™…β™€οΈ use fork() and the exec family (except execlp() and execvp()) instead.

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.


πŸ’œ 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


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 .


okay that's the whole page bestie, hope you ate it up πŸ’… this lil love letter to pipes came to you from IEEE/The Open Group, dated 2017, and it's the POPEN(3P) page from the POSIX Programmer's Manual. go forth and pipe responsibly, ily πŸ¦‹βœ¨πŸ’–

πŸ“¦ where this page comes from