]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/popen.3
b955f318249523f0a954a9bd710342b1f040bc00
[thirdparty/man-pages.git] / man3 / popen.3
1 '\" t
2 .\" Copyright 1991 The Regents of the University of California.
3 .\" All rights reserved.
4 .\"
5 .\" SPDX-License-Identifier: BSD-4-Clause-UC
6 .\"
7 .\" @(#)popen.3 6.4 (Berkeley) 4/30/91
8 .\"
9 .\" Converted for Linux, Mon Nov 29 14:45:38 1993, faith@cs.unc.edu
10 .\" Modified Sat May 18 20:37:44 1996 by Martin Schulze (joey@linux.de)
11 .\" Modified 7 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk)
12 .\"
13 .TH popen 3 (date) "Linux man-pages (unreleased)"
14 .SH NAME
15 popen, pclose \- pipe stream to or from a process
16 .SH LIBRARY
17 Standard C library
18 .RI ( libc ", " \-lc )
19 .SH SYNOPSIS
20 .nf
21 .B #include <stdio.h>
22 .PP
23 .BI "FILE *popen(const char *" command ", const char *" type );
24 .BI "int pclose(FILE *" stream );
25 .fi
26 .PP
27 .RS -4
28 Feature Test Macro Requirements for glibc (see
29 .BR feature_test_macros (7)):
30 .RE
31 .PP
32 .BR popen (),
33 .BR pclose ():
34 .nf
35 _POSIX_C_SOURCE >= 2
36 || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
37 .fi
38 .SH DESCRIPTION
39 The
40 .BR popen ()
41 function opens a process by creating a pipe, forking, and invoking the
42 shell.
43 Since a pipe is by definition unidirectional, the
44 .I type
45 argument may specify only reading or writing, not both; the resulting
46 stream is correspondingly read-only or write-only.
47 .PP
48 The
49 .I command
50 argument is a pointer to a null-terminated string containing a shell
51 command line.
52 This command is passed to
53 .I /bin/sh
54 using the
55 .B \-c
56 flag; interpretation, if any, is performed by the shell.
57 .PP
58 The
59 .I type
60 argument is a pointer to a null-terminated string which must contain
61 either the letter \[aq]r\[aq] for reading or the letter \[aq]w\[aq] for writing.
62 Since glibc 2.9,
63 this argument can additionally include the letter \[aq]e\[aq],
64 which causes the close-on-exec flag
65 .RB ( FD_CLOEXEC )
66 to be set on the underlying file descriptor;
67 see the description of the
68 .B O_CLOEXEC
69 flag in
70 .BR open (2)
71 for reasons why this may be useful.
72 .PP
73 The return value from
74 .BR popen ()
75 is a normal standard I/O stream in all respects save that it must be closed
76 with
77 .BR pclose ()
78 rather than
79 .BR fclose (3).
80 Writing to such a stream writes to the standard input of the command; the
81 command's standard output is the same as that of the process that called
82 .BR popen (),
83 unless this is altered by the command itself.
84 Conversely, reading from
85 the stream reads the command's standard output, and the command's
86 standard input is the same as that of the process that called
87 .BR popen ().
88 .PP
89 Note that output
90 .BR popen ()
91 streams are block buffered by default.
92 .PP
93 The
94 .BR pclose ()
95 function waits for the associated process to terminate and returns the exit
96 status of the command as returned by
97 .BR wait4 (2).
98 .SH RETURN VALUE
99 .BR popen ():
100 on success, returns a pointer to an open stream that
101 can be used to read or write to the pipe;
102 if the
103 .BR fork (2)
104 or
105 .BR pipe (2)
106 calls fail, or if the function cannot allocate memory,
107 NULL is returned.
108 .PP
109 .BR pclose ():
110 on success, returns the exit status of the command; if
111 .\" These conditions actually give undefined results, so I commented
112 .\" them out.
113 .\" .I stream
114 .\" is not associated with a "popen()ed" command, if
115 .\".I stream
116 .\" already "pclose()d", or if
117 .BR wait4 (2)
118 returns an error, or some other error is detected,
119 \-1 is returned.
120 .PP
121 On failure, both functions set
122 .I errno
123 to indicate the error.
124 .SH ERRORS
125 The
126 .BR popen ()
127 function does not set
128 .I errno
129 if memory allocation fails.
130 If the underlying
131 .BR fork (2)
132 or
133 .BR pipe (2)
134 fails,
135 .I errno
136 is set to indicate the error.
137 If the
138 .I type
139 argument is invalid, and this condition is detected,
140 .I errno
141 is set to
142 .BR EINVAL .
143 .PP
144 If
145 .BR pclose ()
146 cannot obtain the child status,
147 .I errno
148 is set to
149 .BR ECHILD .
150 .SH ATTRIBUTES
151 For an explanation of the terms used in this section, see
152 .BR attributes (7).
153 .ad l
154 .nh
155 .TS
156 allbox;
157 lbx lb lb
158 l l l.
159 Interface Attribute Value
160 T{
161 .BR popen (),
162 .BR pclose ()
163 T} Thread safety MT-Safe
164 .TE
165 .hy
166 .ad
167 .sp 1
168 .SH VERSIONS
169 The \[aq]e\[aq] value for
170 .I type
171 is a Linux extension.
172 .SH STANDARDS
173 POSIX.1-2008.
174 .SH HISTORY
175 POSIX.1-2001.
176 .SH CAVEATS
177 Carefully read Caveats in
178 .BR system (3).
179 .SH BUGS
180 Since the standard input of a command opened for reading shares its seek
181 offset with the process that called
182 .BR popen (),
183 if the original process has done a buffered read, the command's input
184 position may not be as expected.
185 Similarly, the output from a command
186 opened for writing may become intermingled with that of the original
187 process.
188 The latter can be avoided by calling
189 .BR fflush (3)
190 before
191 .BR popen ().
192 .PP
193 Failure to execute the shell is indistinguishable from the shell's failure
194 to execute command, or an immediate exit of the command.
195 The only hint is an exit status of 127.
196 .\" .SH HISTORY
197 .\" A
198 .\" .BR popen ()
199 .\" and a
200 .\" .BR pclose ()
201 .\" function appeared in Version 7 AT&T UNIX.
202 .SH SEE ALSO
203 .BR sh (1),
204 .BR fork (2),
205 .BR pipe (2),
206 .BR wait4 (2),
207 .BR fclose (3),
208 .BR fflush (3),
209 .BR fopen (3),
210 .BR stdio (3),
211 .BR system (3)