OSDN Git Service

(split) LDP man-pages の original/ を v3.29 に更新。
[linuxjm/LDP_man-pages.git] / original / man3 / popen.3
1 .\" Copyright 1991 The Regents of the University of California.
2 .\" 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. All advertising materials mentioning features or use of this software
13 .\"    must display the following acknowledgement:
14 .\"     This product includes software developed by the University of
15 .\"     California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\"    may be used to endorse or promote products derived from this software
18 .\"    without specific prior written permission.
19 .\"
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" SUCH DAMAGE.
31 .\"
32 .\"     @(#)popen.3     6.4 (Berkeley) 4/30/91
33 .\"
34 .\" Converted for Linux, Mon Nov 29 14:45:38 1993, faith@cs.unc.edu
35 .\" Modified Sat May 18 20:37:44 1996 by Martin Schulze (joey@linux.de)
36 .\" Modified 7 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk)
37 .\"
38 .TH POPEN 3  2010-02-03 "GNU" "Linux Programmer's Manual"
39 .SH NAME
40 popen, pclose \- pipe stream to or from a process
41 .SH SYNOPSIS
42 .nf
43 .B #include <stdio.h>
44 .sp
45 .BI "FILE *popen(const char *" command ", const char *" type );
46 .sp
47 .BI "int pclose(FILE *" stream );
48 .fi
49 .sp
50 .in -4n
51 Feature Test Macro Requirements for glibc (see
52 .BR feature_test_macros (7)):
53 .ad l
54 .in
55 .sp
56 .BR popen (),
57 .BR pclose ():
58 .RS 4
59 _POSIX_C_SOURCE\ >=\ 2 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE
60 .RE
61 .ad b
62 .SH DESCRIPTION
63 The
64 .BR popen ()
65 function opens a process by creating a pipe, forking, and invoking the
66 shell.
67 Since a pipe is by definition unidirectional, the
68 .I type
69 argument may specify only reading or writing, not both; the resulting
70 stream is correspondingly read-only or write-only.
71 .PP
72 The
73 .I command
74 argument is a pointer to a null-terminated string containing a shell
75 command line.
76 This command is passed to
77 .I /bin/sh
78 using the
79 .B \-c
80 flag; interpretation, if any, is performed by the shell.
81 The
82 .I type
83 argument is a pointer to a null-terminated string which must contain
84 either the letter \(aqr\(aq for reading or the letter \(aqw\(aq for writing.
85 Since glibc 2.9,
86 this argument can additionally include the letter \(aqe\(aq,
87 which causes the close-on-exec flag
88 .RB ( FD_CLOEXEC )
89 to be set on the underlying file descriptor;
90 see the description of the
91 .B O_CLOEXEC
92 flag in
93 .BR open (2)
94 for reasons why this may be useful.
95 .PP
96 The return value from
97 .BR popen ()
98 is a normal standard I/O stream in all respects save that it must be closed
99 with
100 .BR pclose ()
101 rather than
102 .BR fclose (3).
103 Writing to such a stream writes to the standard input of the command; the
104 command's standard output is the same as that of the process that called
105 .BR popen (),
106 unless this is altered by the command itself.
107 Conversely, reading from a
108 "popened" stream reads the command's standard output, and the command's
109 standard input is the same as that of the process that called
110 .BR popen ().
111 .PP
112 Note that output
113 .BR popen ()
114 streams are fully buffered by default.
115 .PP
116 The
117 .BR pclose ()
118 function waits for the associated process to terminate and returns the exit
119 status of the command as returned by
120 .BR wait4 (2).
121 .SH "RETURN VALUE"
122 The
123 .BR popen ()
124 function returns NULL if the
125 .BR fork (2)
126 or
127 .BR pipe (2)
128 calls fail, or if it cannot allocate memory.
129 .PP
130 The
131 .BR pclose ()
132 function returns \-1 if
133 .\" These conditions actually give undefined results, so I commented
134 .\" them out.
135 .\" .I stream
136 .\" is not associated with a "popen()ed" command, if
137 .\".I stream
138 .\" already "pclose()d", or if
139 .BR wait4 (2)
140 returns an error, or some other error is detected.
141 .SH ERRORS
142 The
143 .BR popen ()
144 function does not set
145 .I errno
146 if memory allocation fails.
147 If the underlying
148 .BR fork (2)
149 or
150 .BR pipe (2)
151 fails,
152 .I errno
153 is set appropriately.
154 If the
155 .I type
156 argument is invalid, and this condition is detected,
157 .I errno
158 is set to
159 .BR EINVAL .
160 .PP
161 If
162 .BR pclose ()
163 cannot obtain the child status,
164 .I errno
165 is set to
166 .BR ECHILD .
167 .SH "CONFORMING TO"
168 POSIX.1-2001.
169
170 The \(aqe\(aq value for
171 .I type
172 is a Linux extension.
173 .SH BUGS
174 Since the standard input of a command opened for reading shares its seek
175 offset with the process that called
176 .BR popen (),
177 if the original process has done a buffered read, the command's input
178 position may not be as expected.
179 Similarly, the output from a command
180 opened for writing may become intermingled with that of the original
181 process.
182 The latter can be avoided by calling
183 .BR fflush (3)
184 before
185 .BR popen ().
186 .PP
187 Failure to execute the shell is indistinguishable from the shell's failure
188 to execute command, or an immediate exit of the command.
189 The only hint is an exit status of 127.
190 .\" .SH HISTORY
191 .\" A
192 .\" .BR popen ()
193 .\" and a
194 .\" .BR pclose ()
195 .\" function appeared in Version 7 AT&T UNIX.
196 .SH "SEE ALSO"
197 .BR sh (1),
198 .BR fork (2),
199 .BR pipe (2),
200 .BR wait4 (2),
201 .BR fclose (3),
202 .BR fflush (3),
203 .BR fopen (3),
204 .BR stdio (3),
205 .BR system (3)