popen(3)

๐Ÿ’– popen(3) โ€” bestie, let's talk about piping to a whole entire process ๐Ÿ’–

okay so this is popen(3), a member of the Library Functions Manual squad, and I need you to sit down because this one is lowkey iconic. It's part of Standard C library (libc, -lc), it's been carrying Unix on its back for decades, and it's giving main character energy. โœจ


๐ŸŒธ NAME ๐ŸŒธ

popen, pclose โ€” pipe stream to or from a process ๐Ÿซถ

๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, -lc) ๐Ÿ’…


โœจ SYNOPSIS โœจ

#include <stdio.h>

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

๐ŸŽ€ Feature Test Macro Requirements for glibc (see feature_test_macros(7)) ๐ŸŽ€

popen(), pclose():
    _POSIX_C_SOURCE >= 2
        || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
translation bestie: you gotta have _POSIX_C_SOURCE set to 2 or higher to get these, OR if you're on glibc 2.19 or older, _BSD_SOURCE or _SVID_SOURCE will also do the trick ๐Ÿ’œ

๐Ÿ’… DESCRIPTION ๐Ÿ’…

So what does popen() actually DO. Bestie. It opens a process by creating a pipe, forking, and invoking the shell. Three steps, one function, absolutely unhinged efficiency. ๐Ÿ”ฅ

Since a pipe is by definition unidirectional (<- remember this word, it comes back), the type argument may specify only reading or only writing, not both. So the resulting stream is correspondingly read-only or write-only. You do not get to have both. It's giving monogamous. ๐Ÿ™ˆ

The command argument is a pointer to a null-terminated string containing a shell command line. This command is passed to /bin/sh using the -c flag; interpretation, if any, is performed by the shell. (so like, globs and pipes and $VARS work, but that's the shell's doing, not popen's โ€” ikyk ๐Ÿ˜Œ)

The type argument is a pointer to a null-terminated string which must contain either the letter 'r' for reading or the letter 'w' for writing. Not optional, not negotiable, pick one.

Since glibc 2.9, this argument can additionally include the letter 'e', which causes the close-on-exec flag (FD_CLOEXEC) to be set on the underlying file descriptor. Why would you want that? See the description of the O_CLOEXEC flag in open(2) โ€” it's a whole vibe about not leaking fds into random children. ๐Ÿ‘‘

The return value from popen() is a normal standard I/O stream in all respects save that it must be closed with pclose() rather than fclose(3). Like, it looks like a FILE *, it acts like a FILE *, but it has โœจcommitment issuesโœจ โ€” you must break up with it via pclose().

  • Writing to such a stream writes to the standard input of the command ๐Ÿ’Œ
  • The command's standard output is the same as that of the process that called popen(), unless this is altered by the command itself.
  • Conversely, reading from the stream reads the command's standard output, and the command's standard input is the same as that of the process that called popen().
๐Ÿšจ note that output popen() streams are block buffered by default. no cap. plan accordingly.

The pclose() function waits for the associated process to terminate and returns the exit status of the command as returned by wait4(2). So pclose is the one who actually closes the door and then waits outside to make sure your ex got home safe. ๐Ÿฅฒ


๐Ÿฆ‹ RETURN VALUE ๐Ÿฆ‹

  • popen(): on success, returns a pointer to an open stream that can be used to read or write to the pipe. If the fork(2) or pipe(2) calls fail, or if the function cannot allocate memory, NULL is returned. ๐Ÿ’€
  • pclose(): on success, returns the exit status of the command. If wait4(2) returns an error, or some other error is detected, -1 is returned.
  • On failure, both functions set errno to indicate the error.

๐ŸŒˆ ERRORS ๐ŸŒˆ

okay this part is important, listen up ๐Ÿ“ข

  • The popen() function does not set errno if memory allocation fails. (yeah. I know. rude. ๐Ÿ˜ญ)
  • If the underlying fork(2) or pipe(2) fails, errno is set to indicate the error.
  • If the type argument is invalid, and this condition is detected, errno is set to EINVAL.
  • If pclose() cannot obtain the child status, errno is set to ECHILD.

๐ŸŽ€ ATTRIBUTES ๐ŸŽ€

For an explanation of the terms used in this section, see attributes(7).

  • popen(), pclose() โ€” Thread safety: MT-Safe ๐Ÿซถ (yes bestie, both. safe. go off.)

โญ VERSIONS โญ

The 'e' value for type is a Linux extension. (so cute, so Linux-specific, so not portable โ€” you've been warned ๐ŸŒธ)


๐Ÿ‘‘ STANDARDS ๐Ÿ‘‘

POSIX.1-2008.


๐Ÿฆ„ HISTORY ๐Ÿฆ„

POSIX.1-2001.


โš ๏ธ CAVEATS โš ๏ธ

Carefully read Caveats in system(3). Like, actually go read them. This is not a drill, it's a cross-reference, and cross-references in manpages are load-bearing. ๐Ÿ”ฅ


๐Ÿ’€ BUGS ๐Ÿ’€

ngl this section is giving "we tried our best" โ€” but you need to know it:

Since the standard input of a command opened for reading shares its seek offset with the process that called popen(), if the original process has done a buffered read, the command's input position may not be as expected. ๐Ÿ˜ญ

Similarly, the output from a command opened for writing may become intermingled with that of the original process. โœจchaosโœจ

The latter can be avoided by calling fflush(3) before popen(). (that's your one little hack, use it ๐Ÿ’…)

Failure to execute the shell is indistinguishable from the shell's failure to execute the command, or an immediate exit of the command. The only hint is an exit status of 127. That's it. That's the whole hint. 127. Remember it. ๐Ÿซก


๐Ÿ” SEE ALSO ๐Ÿ”

sh(1), fork(2), pipe(2), wait4(2), fclose(3), fflush(3), fopen(3), stdio(3), system(3)


๐Ÿ’– and that's the whole page bestie, go forth and pipe responsibly ๐Ÿ’–

Linux man-pages 6.20, dated 2025-09-21 โ€” this page said what it said, and it ate. periodt. ๐Ÿ’…๐Ÿ‘‘๐ŸŒˆ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: BSD-4-Clause-UC
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms