1POPEN(3P) POSIX Programmer's Manual POPEN(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 popen - initiate pipe streams to or from a process
13
15 #include <stdio.h>
16
17 FILE *popen(const char *command, const char *mode);
18
19
21 The popen() function shall execute the command specified by the string
22 command. It shall create a pipe between the calling program and the
23 executed command, and shall return a pointer to a stream that can be
24 used to either read from or write to the pipe.
25
26 The environment of the executed command shall be as if a child process
27 were created within the popen() call using the fork() function, and the
28 child invoked the sh utility using the call:
29
30
31 execl(shell path, "sh", "-c", command, (char *)0);
32
33 where shell path is an unspecified pathname for the sh utility.
34
35 The popen() function shall ensure that any streams from previous
36 popen() calls that remain open in the parent process are closed in the
37 new child process.
38
39 The mode argument to popen() is a string that specifies I/O mode:
40
41 1. If mode is r, when the child process is started, its file descrip‐
42 tor STDOUT_FILENO shall be the writable end of the pipe, and the
43 file descriptor fileno(stream) in the calling process, where stream
44 is the stream pointer returned by popen(), shall be the readable
45 end of the pipe.
46
47 2. If mode is w, when the child process is started its file descriptor
48 STDIN_FILENO shall be the readable end of the pipe, and the file
49 descriptor fileno(stream) in the calling process, where stream is
50 the stream pointer returned by popen(), shall be the writable end
51 of the pipe.
52
53 3. If mode is any other value, the result is undefined.
54
55 After popen(), both the parent and the child process shall be capable
56 of executing independently before either terminates.
57
58 Pipe streams are byte-oriented.
59
61 Upon successful completion, popen() shall return a pointer to an open
62 stream that can be used to read or write to the pipe. Otherwise, it
63 shall return a null pointer and may set errno to indicate the error.
64
66 The popen() function may fail if:
67
68 EMFILE {FOPEN_MAX} or {STREAM_MAX} streams are currently open in the
69 calling process.
70
71 EINVAL The mode argument is invalid.
72
73
74 The popen() function may also set errno values as described by fork()
75 or pipe().
76
77 The following sections are informative.
78
80 None.
81
83 Since open files are shared, a mode r command can be used as an input
84 filter and a mode w command as an output filter.
85
86 Buffered reading before opening an input filter may leave the standard
87 input of that filter mispositioned. Similar problems with an output
88 filter may be prevented by careful buffer flushing; for example, with
89 fflush().
90
91 A stream opened by popen() should be closed by pclose().
92
93 The behavior of popen() is specified for values of mode of r and w.
94 Other modes such as rb and wb might be supported by specific implemen‐
95 tations, but these would not be portable features. Note that historical
96 implementations of popen() only check to see if the first character of
97 mode is r. Thus, a mode of robert the robot would be treated as mode r,
98 and a mode of anything else would be treated as mode w.
99
100 If the application calls waitpid() or waitid() with a pid argument
101 greater than 0, and it still has a stream that was called with popen()
102 open, it must ensure that pid does not refer to the process started by
103 popen().
104
105 To determine whether or not the environment specified in the Shell and
106 Utilities volume of IEEE Std 1003.1-2001 is present, use the function
107 call:
108
109
110 sysconf(_SC_2_VERSION)
111
112 (See sysconf()).
113
115 The popen() function should not be used by programs that have set user
116 (or group) ID privileges. The fork() and exec family of functions
117 (except execlp() and execvp()), should be used instead. This prevents
118 any unforeseen manipulation of the environment of the user that could
119 cause execution of commands not anticipated by the calling program.
120
121 If the original and popen()ed processes both intend to read or write or
122 read and write a common file, and either will be using FILE-type C
123 functions ( fread(), fwrite(), and so on), the rules for sharing file
124 handles must be observed (see Interaction of File Descriptors and Stan‐
125 dard I/O Streams ).
126
128 None.
129
131 pclose(), pipe(), sysconf(), system(), the Base Definitions volume of
132 IEEE Std 1003.1-2001, <stdio.h>, the Shell and Utilities volume of
133 IEEE Std 1003.1-2001, sh
134
136 Portions of this text are reprinted and reproduced in electronic form
137 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
138 -- Portable Operating System Interface (POSIX), The Open Group Base
139 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
140 Electrical and Electronics Engineers, Inc and The Open Group. In the
141 event of any discrepancy between this version and the original IEEE and
142 The Open Group Standard, the original IEEE and The Open Group Standard
143 is the referee document. The original Standard can be obtained online
144 at http://www.opengroup.org/unix/online.html .
145
146
147
148IEEE/The Open Group 2003 POPEN(3P)