]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/gen/popen.3
zfs: merge openzfs/zfs@043c6ee3b
[FreeBSD/FreeBSD.git] / lib / libc / gen / popen.3
1 .\" Copyright (c) 1991, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 3. Neither the name of the University nor the names of its contributors
13 .\"    may be used to endorse or promote products derived from this software
14 .\"    without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     @(#)popen.3     8.2 (Berkeley) 5/3/95
29 .\"
30 .Dd May 20, 2013
31 .Dt POPEN 3
32 .Os
33 .Sh NAME
34 .Nm popen ,
35 .Nm pclose
36 .Nd process
37 .Tn I/O
38 .Sh LIBRARY
39 .Lb libc
40 .Sh SYNOPSIS
41 .In stdio.h
42 .Ft FILE *
43 .Fn popen "const char *command" "const char *type"
44 .Ft int
45 .Fn pclose "FILE *stream"
46 .Sh DESCRIPTION
47 The
48 .Fn popen
49 function
50 .Dq opens
51 a process by creating a bidirectional pipe
52 forking,
53 and invoking the shell.
54 Any streams opened by previous
55 .Fn popen
56 calls in the parent process are closed in the new child process.
57 Historically,
58 .Fn popen
59 was implemented with a unidirectional pipe;
60 hence many implementations of
61 .Fn popen
62 only allow the
63 .Fa type
64 argument to specify reading or writing, not both.
65 Since
66 .Fn popen
67 is now implemented using a bidirectional pipe, the
68 .Fa type
69 argument may request a bidirectional data flow.
70 The
71 .Fa type
72 argument is a pointer to a null-terminated string
73 which must be
74 .Ql r
75 for reading,
76 .Ql w
77 for writing, or
78 .Ql r+
79 for reading and writing.
80 .Pp
81 A letter
82 .Ql e
83 may be appended to that to request that the underlying file descriptor
84 be set close-on-exec.
85 .Pp
86 The
87 .Fa command
88 argument is a pointer to a null-terminated string
89 containing a shell command line.
90 This command is passed to
91 .Pa /bin/sh
92 using the
93 .Fl c
94 flag; interpretation, if any, is performed by the shell.
95 .Pp
96 The return value from
97 .Fn popen
98 is a normal standard
99 .Tn I/O
100 stream in all respects
101 save that it must be closed with
102 .Fn pclose
103 rather than
104 .Fn fclose .
105 Writing to such a stream
106 writes to the standard input of the command;
107 the command's standard output is the same as that of the process that called
108 .Fn popen ,
109 unless this is altered by the command itself.
110 Conversely, reading from a
111 .Dq popened
112 stream reads the command's standard output, and
113 the command's standard input is the same as that of the process that called
114 .Fn popen .
115 .Pp
116 Note that output
117 .Fn popen
118 streams are fully buffered by default.
119 .Pp
120 The
121 .Fn pclose
122 function waits for the associated process to terminate
123 and returns the exit status of the command
124 as returned by
125 .Xr wait4 2 .
126 .Sh RETURN VALUES
127 The
128 .Fn popen
129 function returns
130 .Dv NULL
131 if the
132 .Xr fork 2
133 or
134 .Xr pipe 2
135 calls fail,
136 or if it cannot allocate memory.
137 .Pp
138 The
139 .Fn pclose
140 function
141 returns \-1 if
142 .Fa stream
143 is not associated with a
144 .Dq popened
145 command, if
146 .Fa stream
147 already
148 .Dq pclosed ,
149 or if
150 .Xr wait4 2
151 returns an error.
152 .Sh ERRORS
153 The
154 .Fn popen
155 function does not reliably set
156 .Va errno .
157 .Sh SEE ALSO
158 .Xr sh 1 ,
159 .Xr fork 2 ,
160 .Xr pipe 2 ,
161 .Xr wait4 2 ,
162 .Xr fclose 3 ,
163 .Xr fflush 3 ,
164 .Xr fopen 3 ,
165 .Xr stdio 3 ,
166 .Xr system 3
167 .Sh HISTORY
168 A
169 .Fn popen
170 and a
171 .Fn pclose
172 function appeared in
173 .At v7 .
174 .Pp
175 Bidirectional functionality was added in
176 .Fx 2.2.6 .
177 .Sh BUGS
178 Since the standard input of a command opened for reading
179 shares its seek offset with the process that called
180 .Fn popen ,
181 if the original process has done a buffered read,
182 the command's input position may not be as expected.
183 Similarly, the output from a command opened for writing
184 may become intermingled with that of the original process.
185 The latter can be avoided by calling
186 .Xr fflush 3
187 before
188 .Fn popen .
189 .Pp
190 Failure to execute the shell
191 is indistinguishable from the shell's failure to execute command,
192 or an immediate exit of the command.
193 The only hint is an exit status of 127.
194 .Pp
195 The
196 .Fn popen
197 function
198 always calls
199 .Xr sh 1 ,
200 never calls
201 .Xr csh 1 .