1POPEN(3) Linux Programmer's Manual POPEN(3)
2
3
4
6 popen, pclose - pipe stream to or from a process
7
9 #include <stdio.h>
10
11 FILE *popen(const char *command, const char *type);
12
13 int pclose(FILE *stream);
14
15 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
16
17 popen(), pclose(): _POSIX_C_SOURCE >= 2 || _XOPEN_SOURCE ||
18 _POSIX_SOURCE _BSD_SOURCE || _SVID_SOURCE
19
21 The popen() function opens a process by creating a pipe, forking, and
22 invoking the shell. Since a pipe is by definition unidirectional, the
23 type argument may specify only reading or writing, not both; the
24 resulting stream is correspondingly read-only or write-only.
25
26 The command argument is a pointer to a null-terminated string contain‐
27 ing a shell command line. This command is passed to /bin/sh using the
28 -c flag; interpretation, if any, is performed by the shell. The type
29 argument is a pointer to a null-terminated string which must contain
30 either the letter 'r' for reading or the letter 'w' for writing. Since
31 glibc 2.9, this argument can additionally include the letter 'e', which
32 causes the close-on-exec flag (FD_CLOEXEC) to be set on the underlying
33 file descriptor; see the description of the O_CLOEXEC flag in open(2)
34 for reasons why this may be useful.
35
36 The return value from popen() is a normal standard I/O stream in all
37 respects save that it must be closed with pclose() rather than
38 fclose(3). Writing to such a stream writes to the standard input of
39 the command; the command's standard output is the same as that of the
40 process that called popen(), unless this is altered by the command
41 itself. Conversely, reading from a "popened" stream reads the com‐
42 mand's standard output, and the command's standard input is the same as
43 that of the process that called popen().
44
45 Note that output popen() streams are fully buffered by default.
46
47 The pclose() function waits for the associated process to terminate and
48 returns the exit status of the command as returned by wait4(2).
49
51 The popen() function returns NULL if the fork(2) or pipe(2) calls fail,
52 or if it cannot allocate memory.
53
54 The pclose() function returns -1 if wait4(2) returns an error, or some
55 other error is detected.
56
58 The popen() function does not set errno if memory allocation fails. If
59 the underlying fork(2) or pipe(2) fails, errno is set appropriately.
60 If the type argument is invalid, and this condition is detected, errno
61 is set to EINVAL.
62
63 If pclose() cannot obtain the child status, errno is set to ECHILD.
64
66 POSIX.1-2001.
67
68 The 'e' value for type is a Linux extension.
69
71 Since the standard input of a command opened for reading shares its
72 seek offset with the process that called popen(), if the original
73 process has done a buffered read, the command's input position may not
74 be as expected. Similarly, the output from a command opened for writ‐
75 ing may become intermingled with that of the original process. The
76 latter can be avoided by calling fflush(3) before popen().
77
78 Failure to execute the shell is indistinguishable from the shell's
79 failure to execute command, or an immediate exit of the command. The
80 only hint is an exit status of 127.
81
83 sh(1), fork(2), pipe(2), wait4(2), fclose(3), fflush(3), fopen(3),
84 stdio(3), system(3)
85
87 This page is part of release 3.22 of the Linux man-pages project. A
88 description of the project, and information about reporting bugs, can
89 be found at http://www.kernel.org/doc/man-pages/.
90
91
92
93GNU 2008-10-10 POPEN(3)