1popen(3) Library Functions Manual popen(3)
2
3
4
6 popen, pclose - pipe stream to or from a process
7
9 Standard C library (libc, -lc)
10
12 #include <stdio.h>
13
14 FILE *popen(const char *command, const char *type);
15 int pclose(FILE *stream);
16
17 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19 popen(), pclose():
20 _POSIX_C_SOURCE >= 2
21 || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
22
24 The popen() function opens a process by creating a pipe, forking, and
25 invoking the shell. Since a pipe is by definition unidirectional, the
26 type argument may specify only reading or writing, not both; the re‐
27 sulting stream is correspondingly read-only or write-only.
28
29 The command argument is a pointer to a null-terminated string contain‐
30 ing a shell command line. This command is passed to /bin/sh using the
31 -c flag; interpretation, if any, is performed by the shell.
32
33 The type argument is a pointer to a null-terminated string which must
34 contain either the letter 'r' for reading or the letter 'w' for writ‐
35 ing. Since glibc 2.9, this argument can additionally include the let‐
36 ter 'e', which causes the close-on-exec flag (FD_CLOEXEC) to be set on
37 the underlying file descriptor; see the description of the O_CLOEXEC
38 flag in open(2) for reasons why this may be useful.
39
40 The return value from popen() is a normal standard I/O stream in all
41 respects save that it must be closed with pclose() rather than
42 fclose(3). Writing to such a stream writes to the standard input of
43 the command; the command's standard output is the same as that of the
44 process that called popen(), unless this is altered by the command it‐
45 self. Conversely, reading from the stream reads the command's standard
46 output, and the command's standard input is the same as that of the
47 process that called popen().
48
49 Note that output popen() streams are block buffered by default.
50
51 The pclose() function waits for the associated process to terminate and
52 returns the exit status of the command as returned by wait4(2).
53
55 popen(): on success, returns a pointer to an open stream that can be
56 used to read or write to the pipe; if the fork(2) or pipe(2) calls
57 fail, or if the function cannot allocate memory, NULL is returned.
58
59 pclose(): on success, returns the exit status of the command; if
60 wait4(2) returns an error, or some other error is detected, -1 is re‐
61 turned.
62
63 On failure, both functions set errno to indicate the error.
64
66 The popen() function does not set errno if memory allocation fails. If
67 the underlying fork(2) or pipe(2) fails, errno is set to indicate the
68 error. If the type argument is invalid, and this condition is de‐
69 tected, errno is set to EINVAL.
70
71 If pclose() cannot obtain the child status, errno is set to ECHILD.
72
74 For an explanation of the terms used in this section, see at‐
75 tributes(7).
76
77 ┌────────────────────────────────────────────┬───────────────┬─────────┐
78 │Interface │ Attribute │ Value │
79 ├────────────────────────────────────────────┼───────────────┼─────────┤
80 │popen(), pclose() │ Thread safety │ MT-Safe │
81 └────────────────────────────────────────────┴───────────────┴─────────┘
82
84 The 'e' value for type is a Linux extension.
85
87 POSIX.1-2008.
88
90 POSIX.1-2001.
91
93 Carefully read Caveats in system(3).
94
96 Since the standard input of a command opened for reading shares its
97 seek offset with the process that called popen(), if the original
98 process has done a buffered read, the command's input position may not
99 be as expected. Similarly, the output from a command opened for
100 writing may become intermingled with that of the original process. The
101 latter can be avoided by calling fflush(3) before popen().
102
103 Failure to execute the shell is indistinguishable from the shell's
104 failure to execute command, or an immediate exit of the command. The
105 only hint is an exit status of 127.
106
108 sh(1), fork(2), pipe(2), wait4(2), fclose(3), fflush(3), fopen(3),
109 stdio(3), system(3)
110
111
112
113Linux man-pages 6.05 2023-07-20 popen(3)