OSDN Git Service

(split) LDP v3.30, v3.31 の定型的な変更内容を反映。
[linuxjm/LDP_man-pages.git] / draft / 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 .\" Japanese Version Copyright (c) 1997 Takashi Yoshino
39 .\"       all rights reserved.
40 .\" Translated 1997-01-21, Takashi Yoshino <tyoshino@eng.toyo.ac.jp>
41 .\" Modified 2007-05-03, Akihiro MOTOKI
42 .\" Modified 2008-11-09, Akihiro MOTOKI, LDP v3.13
43 .\"
44 .TH POPEN 3  2010-02-03 "GNU" "Linux Programmer's Manual"
45 .\"O .SH NAME
46 .\"O popen, pclose \- pipe stream to or from a process
47 .SH Ì¾Á°
48 popen, pclose \- ¥×¥í¥»¥¹¤È¤ÎÆþÎÏ/½ÐÎÏÍѤΥѥ¤¥×¡¦¥¹¥È¥ê¡¼¥à
49 .\"O .SH SYNOPSIS
50 .SH ½ñ¼°
51 .nf
52 .B #include <stdio.h>
53 .sp
54 .BI "FILE *popen(const char *" command ", const char *" type );
55 .sp
56 .BI "int pclose(FILE *" stream );
57 .fi
58 .sp
59 .in -4n
60 .\"O Feature Test Macro Requirements for glibc (see
61 .\"O .BR feature_test_macros (7)):
62 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
63 .RB ( feature_test_macros (7)
64 »²¾È):
65 .ad l
66 .in
67 .sp
68 .BR popen (),
69 .BR pclose ():
70 .RS 4
71 _POSIX_C_SOURCE\ >=\ 2 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE
72 .RE
73 .ad b
74 .\"O .SH DESCRIPTION
75 .SH ÀâÌÀ
76 .\"O The
77 .\"O .BR popen ()
78 .\"O function opens a process by creating a pipe, forking, and invoking the
79 .\"O shell.
80 .\"O Since a pipe is by definition unidirectional, the
81 .\"O .I type
82 .\"O argument may specify only reading or writing, not both; the resulting
83 .\"O stream is correspondingly read-only or write-only.
84 .BR popen ()
85 ´Ø¿ô¤Ï¡¢¥×¥í¥»¥¹¤ò¥ª¡¼¥×¥ó¤¹¤ë¡£¶ñÂÎŪ¤Ë¤Ï¡¢
86 ¥Ñ¥¤¥×¤òÀ¸À®¤·¡¢¥Õ¥©¡¼¥¯¤ò¹Ô¤¤¡¢¥·¥§¥ë¤òµ¯Æ°¤¹¤ë¡£
87 ÄêµÁ¤«¤éʬ¤«¤ë¤è¤¦¤Ë¡¢¥Ñ¥¤¥×¤Ï°ìÊý¸þ¤Ê¤Î¤Ç¡¢
88 .I type
89 °ú¤­¿ô¤Ë¤ÏÆɤ߹þ¤ß¤«½ñ¤­¹þ¤ß¤Î¤É¤Á¤é¤«°ìÊý¤À¤±¤ò»ØÄê¤Ç¤­¤ë
90 (ξÊý¤Ï»ØÄê¤Ç¤­¤Ê¤¤)¡£
91 À¸À®¤µ¤ì¤ë¥¹¥È¥ê¡¼¥à¤Ï¡¢¤³¤Î»ØÄê¤ËÂбþ¤·¤Æ¡¢Æɤ߼è¤êÀìÍѤޤ¿¤Ï
92 ½ñ¤­¹þ¤ßÀìÍѤΤ¤¤º¤ì¤«¤È¤Ê¤ë¡£
93 .PP
94 .\"O The
95 .\"O .I command
96 .\"O argument is a pointer to a null-terminated string containing a shell
97 .\"O command line.
98 .\"O This command is passed to
99 .\"O .I /bin/sh
100 .\"O using the
101 .\"O .B \-c
102 .\"O flag; interpretation, if any, is performed by the shell.
103 .\"O The
104 .\"O .I type
105 .\"O argument is a pointer to a null-terminated string which must contain
106 .\"O either the letter \(aqr\(aq for reading or the letter \(aqw\(aq for writing.
107 .I command
108 °ú¤­¿ô¤Ï¡¢¥·¥§¥ë¤Î¥³¥Þ¥ó¥É¥é¥¤¥ó¤ò´Þ¤à
109 NULL ½ªÃ¼¤µ¤ì¤¿Ê¸»úÎó¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¡£
110 ¤³¤Î¥³¥Þ¥ó¥É¤Ï
111 .B \-c
112 ¥Õ¥é¥°¤òÍѤ¤¤Æ
113 .I /bin/sh
114 ¤ËÅϤµ¤ì¤ë¡£
115 ¥³¥Þ¥ó¥É¤Î²ò¼á¤Ï (¤â¤·É¬Íפʤé¤Ð) ¥·¥§¥ë¤Ë¤è¤Ã¤Æ¹Ô¤ï¤ì¤ë¡£
116 .I type
117 °ú¤­¿ô¤Ï¡¢NULL ½ªÃ¼¤µ¤ì¤¿Ê¸»úÎó¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¡¢
118 Æɤ߹þ¤ß¤ò¼¨¤¹Ê¸»ú \(aqr\(aq ¤«¡¢½ñ¤­¹þ¤ß¤ò¼¨¤¹Ê¸»ú \(aqw\(aq ¤Î
119 ¤É¤Á¤é¤«°ìÊý¤ò»ØÄꤷ¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
120 .\"O Since glibc 2.9,
121 .\"O this argument can additionally include the letter \(aqe\(aq,
122 .\"O which causes the close-on-exec flag
123 .\"O .RB ( FD_CLOEXEC )
124 .\"O to be set on the underlying file descriptor;
125 .\"O see the description of the
126 .\"O .B O_CLOEXEC
127 .\"O flag in
128 .\"O .BR open (2)
129 .\"O for reasons why this may be useful.
130 glibc 2.9 °Ê¹ß¤Ç¤Ï¡¢¤³¤Î°ú¤­¿ô¤Ëʸ»ú \(aqe\(aq ¤òÄɲäǻØÄê¤Ç¤­¤ë¡£
131 ʸ»ú \(aqe\(aq ¤ò»ØÄꤹ¤ë¤È¡¢
132 Âбþ¤¹¤ë¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ë¤ª¤¤¤Æ¡¢
133 close-on-exec ¥Õ¥é¥°
134 .RB ( FD_CLOEXEC )
135 ¤¬¥»¥Ã¥È¤µ¤ì¤ë¡£
136 ¤³¤ì¤¬Ìò¤ËΩ¤ÄÍýͳ¤Ë¤Ä¤¤¤Æ¤Ï¡¢
137 .BR open (2)
138 ¤Î
139 .B O_CLOEXEC
140 ¥Õ¥é¥°¤ÎÀâÌÀ¤ò»²¾È¤Î¤³¤È¡£
141 .PP
142 .\"O The return value from
143 .\"O .BR popen ()
144 .\"O is a normal standard I/O stream in all respects save that it must be closed
145 .\"O with
146 .\"O .BR pclose ()
147 .\"O rather than
148 .\"O .BR fclose (3).
149 .BR popen ()
150 ¤«¤é¤ÎÊÖ¤êÃͤϡ¢Ä̾ï¤Îɸ½à I/O ¥¹¥È¥ê¡¼¥à¤ÈƱ¤¸¤Ç¤¢¤ë¤¬¡¢
151 .BR fclose (3)
152 ¤Ç¤Ï¤Ê¤¯
153 .BR pclose ()
154 ¤ÇÊĤ¸¤Ê¤¯¤Æ¤Ï¤Ê¤é¤Ê¤¤¤³¤È¤À¤±¤¬°Û¤Ê¤ë¡£
155 .\"O Writing to such a stream writes to the standard input of the command; the
156 .\"O command's standard output is the same as that of the process that called
157 .\"O .BR popen (),
158 .\"O unless this is altered by the command itself.
159 .\"O Conversely, reading from a
160 .\"O "popened" stream reads the command's standard output, and the command's
161 .\"O standard input is the same as that of the process that called
162 .\"O .BR popen ().
163 ¤³¤Î¥¹¥È¥ê¡¼¥à¤Ø½ñ¤­¹þ¤ó¤À·ë²Ì¤Ï¥³¥Þ¥ó¥É¤Îɸ½àÆþÎϤ˽ñ¤­¹þ¤Þ¤ì¤ë¡£
164 ¤½¤·¤Æ¡¢¥³¥Þ¥ó¥É¤Îɸ½à½ÐÎϤϡ¢
165 ¥³¥Þ¥ó¥É¤½¤Î¤â¤Î¤¬ÃÖ¤­´¹¤ï¤Ã¤Æ¤·¤Þ¤ï¤Ê¤¤¸Â¤ê¡¢
166 .BR popen ()
167 ¤ò¸Æ¤ó¤À¥×¥í¥»¥¹¤Îɸ½à½ÐÎϤÈƱ¤¸¤³¤È¤Ë¤Ê¤ë¡£
168 µÕ¤Ë¡¢"popened"
169 .RB ( popen ()
170 ¤Ë¤è¤Ã¤Æ³«¤«¤ì¤¿) ¥¹¥È¥ê¡¼¥à¤«¤é¤ÎÆɤ߹þ¤ß¤Ï¡¢
171 ¤½¤Î¥³¥Þ¥ó¥É¤Îɸ½à½ÐÎϤòÆɤ߹þ¤à¤³¤È¤Ë¤Ê¤ë¡£
172 ¤½¤·¤Æ¡¢¤½¤Î¥³¥Þ¥ó¥É¤Îɸ½àÆþÎϤÏ
173 .BR popen ()
174 ¤ò¸Æ¤ó¤À¥×¥í¥»¥¹¤Îɸ½àÆþÎϤÈƱ°ì¤Ç¤¢¤ë¡£
175 .PP
176 .\"O Note that output
177 .\"O .BR popen ()
178 .\"O streams are fully buffered by default.
179 ¥Ç¥Õ¥©¥ë¥È¤Ç¤Ï¡¢
180 .BR popen ()
181 ¤Î½ÐÎÏ¥¹¥È¥ê¡¼¥à¤Ï´°Á´¤Ë¥Ð¥Ã¥Õ¥¡¥ê¥ó¥°¤µ¤ì¤ë¤³¤È¤ËÃí°Õ¤·¤è¤¦¡£
182 .PP
183 .\"O The
184 .\"O .BR pclose ()
185 .\"O function waits for the associated process to terminate and returns the exit
186 .\"O status of the command as returned by
187 .\"O .BR wait4 (2).
188 .BR pclose ()
189 ´Ø¿ô¤Ï¡¢(¥Ñ¥¤¥×¤Ë) ´ØÏ¢¤Å¤±¤é¤ì¤¿¥×¥í¥»¥¹¤¬½ªÎ»¤¹¤ë¤Î¤òÂÔ¤Á¡¢
190 .BR wait4 (2)
191 ¤Ë¤è¤Ã¤ÆÊÖ¤µ¤ì¤¿¥³¥Þ¥ó¥É¤Î½ªÎ»¾õÂÖ¤òÊÖ¤¹¡£
192 .\"O .SH "RETURN VALUE"
193 .SH ÊÖ¤êÃÍ
194 .\"O The
195 .\"O .BR popen ()
196 .\"O function returns NULL if the
197 .\"O .BR fork (2)
198 .\"O or
199 .\"O .BR pipe (2)
200 .\"O calls fail, or if it cannot allocate memory.
201 .BR popen ()
202 ´Ø¿ô¤Ï¡¢
203 .BR fork (2)
204 ¤Þ¤¿¤Ï
205 .BR pipe (2)
206 ¸Æ¤Ó½Ð¤·¤¬¼ºÇÔ¤·¤¿¾ì¹ç¤ä¡¢
207 ¥á¥â¥ê³ä¤êÅö¤Æ¤¬¤Ç¤­¤Ê¤«¤Ã¤¿¾ì¹ç¡¢ NULL ¤òÊÖ¤¹¡£
208 .PP
209 .\"O The
210 .\"O .BR pclose ()
211 .\"O function returns \-1 if
212 .\"O .\" These conditions actually give undefined results, so I commented
213 .\"O .\" them out.
214 .\"O .\" .I stream
215 .\"O .\" is not associated with a "popen()ed" command, if
216 .\"O .\".I stream
217 .\"O .\" already "pclose()d", or if
218 .\"O .BR wait4 (2)
219 .\"O returns an error, or some other error is detected.
220 .BR pclose ()
221 ´Ø¿ô¤Ï¡¢
222 .BR wait4 (2)
223 ¤¬¥¨¥é¡¼¤òÊÖ¤·¤¿¤ê¡¢²¿¤«Â¾¤Î¥¨¥é¡¼¤¬¸«¤Ä¤«¤Ã¤¿¾ì¹ç¡¢
224 \-1 ¤òÊÖ¤¹¡£
225 .\"O .SH ERRORS
226 .SH ¥¨¥é¡¼
227 .\"O The
228 .\"O .BR popen ()
229 .\"O function does not set
230 .\"O .I errno
231 .\"O if memory allocation fails.
232 .\"O If the underlying
233 .\"O .BR fork (2)
234 .\"O or
235 .\"O .BR pipe (2)
236 .\"O fails,
237 .\"O .I errno
238 .\"O is set appropriately.
239 .\"O If the
240 .\"O .I type
241 .\"O argument is invalid, and this condition is detected,
242 .\"O .I errno
243 .\"O is set to
244 .\"O .BR EINVAL .
245 .BR popen ()
246 ´Ø¿ô¤Ï¡¢¥á¥â¥ê¥¢¥í¥±¡¼¥·¥ç¥ó¤Ë¼ºÇÔ¤·¤Æ¤â
247 .I errno
248 ¤ò¥»¥Ã¥È¤·¤Ê¤¤¡£
249 .BR popen ()
250 ¤¬Ãæ¤Ç¸Æ¤Ó½Ð¤¹
251 .BR fork (2)
252 ¤ä
253 .BR pipe (2)
254 ¤¬¼ºÇÔ¤·¤¿¾ì¹ç¤Ë¤Ï¡¢
255 .I errno
256 ¤¬Å¬Àڤ˥»¥Ã¥È¤µ¤ì¤ë¡£
257 °ú¤­¿ô
258 .I type
259 ¤¬Ìµ¸ú¤Ç¤¢¤ê¡¢¤³¤Î¾õÂÖ¤¬¸¡ÃΤµ¤ì¤¿¾ì¹ç¤Ë¤Ï¡¢
260 .I errno
261 ¤¬
262 .B EINVAL
263 ¤Ë¥»¥Ã¥È¤µ¤ì¤ë¡£
264 .PP
265 .\"O If
266 .\"O .BR pclose ()
267 .\"O cannot obtain the child status,
268 .\"O .I errno
269 .\"O is set to
270 .\"O .BR ECHILD .
271 .BR pclose ()
272 ¤¬¡¢»Ò¥×¥í¥»¥¹¤Î¾õÂÖ¤ò¼èÆÀ¤Ç¤­¤Ê¤«¤Ã¤¿¾ì¹ç¡¢
273 .I errno
274 ¤¬
275 .B ECHILD
276 ¤Ë¥»¥Ã¥È¤µ¤ì¤ë¡£
277 .\"O .SH "CONFORMING TO"
278 .SH ½àµò
279 POSIX.1-2001.
280
281 .\"O The \(aqe\(aq value for
282 .\"O .I type
283 .\"O is a Linux extension.
284 .I type
285 ¤Ë»ØÄê¤Ç¤­¤ë \(aqe\(aq ¤Ï Linux ¤Ç¤Î³ÈÄ¥¤Ç¤¢¤ë¡£
286 .\"O .SH BUGS
287 .SH ¥Ð¥°
288 .\"O Since the standard input of a command opened for reading shares its seek
289 .\"O offset with the process that called
290 .\"O .BR popen (),
291 .\"O if the original process has done a buffered read, the command's input
292 .\"O position may not be as expected.
293 .\"O Similarly, the output from a command
294 .\"O opened for writing may become intermingled with that of the original
295 .\"O process.
296 .\"O The latter can be avoided by calling
297 .\"O .BR fflush (3)
298 .\"O before
299 .\"O .BR popen ().
300 Æɤ߹þ¤ß¤Î¤¿¤á¤Ë³«¤«¤ì¤¿¥³¥Þ¥ó¥É¤Îɸ½àÆþÎϤÏ
301 .BR popen (),
302 ¤ò¸Æ¤ó¤À¥×¥í¥»¥¹¤È°ì½ï¤Ë¡¢¤½¤ÎÆɤ߼è¤ê°ÌÃÖ¤ò¶¦Í­¤¹¤ë¡£
303 ¤½¤Î¤¿¤á¡¢¤â¤È¤Î¥×¥í¥»¥¹¤¬¥Ð¥Ã¥Õ¥¡¥ê¥ó¥°¤µ¤ì¤¿Æɤ߼è¤ê¤ò½ªÎ»¤·¤¿¤é¡¢
304 ¤½¤Î¥³¥Þ¥ó¥É¤ÎÆþÎÏ°ÌÃÖ¤ÏͽÁÛ¤µ¤ì¤¿¤â¤Î¤Ë¤Ï
305 ¤Ê¤Ã¤Æ¤¤¤Ê¤¤¤«¤â¤·¤ì¤Ê¤¤¡£
306 ƱÍͤˡ¢½ñ¤­¹þ¤ß¤Î¤¿¤á¤Ë³«¤«¤ì¤¿¥³¥Þ¥ó¥É¤«¤é¤Î½ÐÎϤϡ¢
307 ¤â¤È¤Î¥×¥í¥»¥¹¤Î½ÐÎϤȺ®¤¶¤ê¹ç¤¦¤³¤È¤Ë¤Ê¤ë¤«¤â¤·¤ì¤Ê¤¤¡£
308 ¸å¼Ô¤Ï
309 .BR popen ()
310 ¤ÎÁ°¤Ë
311 .BR fflush (3)
312 ¤ò¸Æ¤Ó½Ð¤¹¤³¤È¤Ë¤è¤Ã¤Æ²óÈò²Äǽ¤Ç¤¢¤ë¡£
313 .PP
314 .\"O Failure to execute the shell is indistinguishable from the shell's failure
315 .\"O to execute command, or an immediate exit of the command.
316 .\"O The only hint is an exit status of 127.
317 ¥·¥§¥ë¤Î¼Â¹Ô¤Î¼ºÇԤϡ¢
318 ¥·¥§¥ë¤¬¥³¥Þ¥ó¥É¤Î¼Â¹Ô¤Ë¼ºÇÔ¤·¤¿¤³¤È¤ä¡¢
319 ¥³¥Þ¥ó¥É¤¬¤¹¤°¤Ë½ªÎ»¤·¤Æ¤·¤Þ¤Ã¤¿¤³¤È¤È¡¢¶èÊ̤¬¤Ä¤«¤Ê¤¤¡£
320 Í£°ì¤Î¥Ò¥ó¥È¤Ï½ªÎ»¾õÂÖ¤¬ 127 ¤Ë¤Ê¤ë¤³¤È¤Ç¤¢¤ë¡£
321 .\"O .\" .SH HISTORY
322 .\" .SH ÍúÎò
323 .\"O .\" A
324 .\"O .\" .BR popen ()
325 .\"O .\" and a
326 .\"O .\" .BR pclose ()
327 .\"O .\" function appeared in Version 7 AT&T UNIX.
328 .\" .BR popen ()
329 .\" ´Ø¿ô¤È
330 .\" .BR pclose ()
331 .\" ´Ø¿ô¤Ï AT&T UNIX ¤Î Version 7 ¤«¤éƳÆþ¤µ¤ì¤¿¡£
332 .\"O .SH "SEE ALSO"
333 .SH ´ØÏ¢¹àÌÜ
334 .BR sh (1),
335 .BR fork (2),
336 .BR pipe (2),
337 .BR wait4 (2),
338 .BR fclose (3),
339 .BR fflush (3),
340 .BR fopen (3),
341 .BR stdio (3),
342 .BR system (3)