1 .\" Copyright 1991 The Regents of the University of California.
2 .\" All rights reserved.
4 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\" notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\" notice, this list of conditions and the following disclaimer in the
12 .\" documentation and/or other materials provided with the distribution.
13 .\" 3. All advertising materials mentioning features or use of this software
14 .\" must display the following acknowledgement:
15 .\" This product includes software developed by the University of
16 .\" California, Berkeley and its contributors.
17 .\" 4. Neither the name of the University nor the names of its contributors
18 .\" may be used to endorse or promote products derived from this software
19 .\" without specific prior written permission.
21 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 .\" @(#)popen.3 6.4 (Berkeley) 4/30/91
36 .\" Converted for Linux, Mon Nov 29 14:45:38 1993, faith@cs.unc.edu
37 .\" Modified Sat May 18 20:37:44 1996 by Martin Schulze (joey@linux.de)
38 .\" Modified 7 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk)
40 .TH POPEN 3 2013-04-19 "GNU" "Linux Programmer's Manual"
42 popen, pclose \- pipe stream to or from a process
47 .BI "FILE *popen(const char *" command ", const char *" type );
49 .BI "int pclose(FILE *" stream );
53 Feature Test Macro Requirements for glibc (see
54 .BR feature_test_macros (7)):
61 _POSIX_C_SOURCE\ >=\ 2 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE
67 function opens a process by creating a pipe, forking, and invoking the
69 Since a pipe is by definition unidirectional, the
71 argument may specify only reading or writing, not both; the resulting
72 stream is correspondingly read-only or write-only.
76 argument is a pointer to a null-terminated string containing a shell
78 This command is passed to
82 flag; interpretation, if any, is performed by the shell.
85 argument is a pointer to a null-terminated string which must contain
86 either the letter \(aqr\(aq for reading or the letter \(aqw\(aq for writing.
88 this argument can additionally include the letter \(aqe\(aq,
89 which causes the close-on-exec flag
91 to be set on the underlying file descriptor;
92 see the description of the
96 for reasons why this may be useful.
100 is a normal standard I/O stream in all respects save that it must be closed
105 Writing to such a stream writes to the standard input of the command; the
106 command's standard output is the same as that of the process that called
108 unless this is altered by the command itself.
109 Conversely, reading from
110 the stream reads the command's standard output, and the command's
111 standard input is the same as that of the process that called
116 streams are fully buffered by default.
120 function waits for the associated process to terminate and returns the exit
121 status of the command as returned by
126 function returns NULL if the
130 calls fail, or if it cannot allocate memory.
134 function returns \-1 if
135 .\" These conditions actually give undefined results, so I commented
138 .\" is not associated with a "popen()ed" command, if
140 .\" already "pclose()d", or if
142 returns an error, or some other error is detected.
143 In the event of an error, these functions set
145 to indicate the cause of the error.
149 function does not set
151 if memory allocation fails.
158 is set appropriately.
161 argument is invalid, and this condition is detected,
168 cannot obtain the child status,
175 The \(aqe\(aq value for
177 is a Linux extension.
179 Since the standard input of a command opened for reading shares its seek
180 offset with the process that called
182 if the original process has done a buffered read, the command's input
183 position may not be as expected.
184 Similarly, the output from a command
185 opened for writing may become intermingled with that of the original
187 The latter can be avoided by calling
192 Failure to execute the shell is indistinguishable from the shell's failure
193 to execute command, or an immediate exit of the command.
194 The only hint is an exit status of 127.
200 .\" function appeared in Version 7 AT&T UNIX.