1PCLOSE(3P) POSIX Programmer's Manual PCLOSE(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
11
13 pclose — close a pipe stream to or from a process
14
16 #include <stdio.h>
17
18 int pclose(FILE *stream);
19
21 The pclose() function shall close a stream that was opened by popen(),
22 wait for the command to terminate, and return the termination status of
23 the process that was running the command language interpreter. How‐
24 ever, if a call caused the termination status to be unavailable to
25 pclose(), then pclose() shall return −1 with errno set to [ECHILD] to
26 report this situation. This can happen if the application calls one of
27 the following functions:
28
29 * wait()
30
31 * waitpid() with a pid argument less than or equal to 0 or equal to
32 the process ID of the command line interpreter
33
34 * Any other function not defined in this volume of POSIX.1‐2008 that
35 could do one of the above
36
37 In any case, pclose() shall not return before the child process created
38 by popen() has terminated.
39
40 If the command language interpreter cannot be executed, the child ter‐
41 mination status returned by pclose() shall be as if the command lan‐
42 guage interpreter terminated using exit(127) or _exit(127).
43
44 The pclose() function shall not affect the termination status of any
45 child of the calling process other than the one created by popen() for
46 the associated stream.
47
48 If the argument stream to pclose() is not a pointer to a stream created
49 by popen(), the result of pclose() is undefined.
50
52 Upon successful return, pclose() shall return the termination status of
53 the command language interpreter. Otherwise, pclose() shall return −1
54 and set errno to indicate the error.
55
57 The pclose() function shall fail if:
58
59 ECHILD The status of the child process could not be obtained, as
60 described above.
61
62 The following sections are informative.
63
65 None.
66
68 None.
69
71 There is a requirement that pclose() not return before the child
72 process terminates. This is intended to disallow implementations that
73 return [EINTR] if a signal is received while waiting. If pclose()
74 returned before the child terminated, there would be no way for the
75 application to discover which child used to be associated with the
76 stream, and it could not do the cleanup itself.
77
78 If the stream pointed to by stream was not created by popen(), histori‐
79 cal implementations of pclose() return −1 without setting errno. To
80 avoid requiring pclose() to set errno in this case, POSIX.1‐2008 makes
81 the behavior unspecified. An application should not use pclose() to
82 close any stream that was not created by popen().
83
84 Some historical implementations of pclose() either block or ignore the
85 signals SIGINT, SIGQUIT, and SIGHUP while waiting for the child process
86 to terminate. Since this behavior is not described for the pclose()
87 function in POSIX.1‐2008, such implementations are not conforming.
88 Also, some historical implementations return [EINTR] if a signal is
89 received, even though the child process has not terminated. Such imple‐
90 mentations are also considered non-conforming.
91
92 Consider, for example, an application that uses:
93
94 popen("command", "r")
95
96 to start command, which is part of the same application. The parent
97 writes a prompt to its standard output (presumably the terminal) and
98 then reads from the popen()ed stream. The child reads the response from
99 the user, does some transformation on the response (pathname expansion,
100 perhaps) and writes the result to its standard output. The parent
101 process reads the result from the pipe, does something with it, and
102 prints another prompt. The cycle repeats. Assuming that both processes
103 do appropriate buffer flushing, this would be expected to work.
104
105 To conform to POSIX.1‐2008, pclose() must use waitpid(), or some simi‐
106 lar function, instead of wait().
107
108 The code sample below illustrates how the pclose() function might be
109 implemented on a system conforming to POSIX.1‐2008.
110
111 int pclose(FILE *stream)
112 {
113 int stat;
114 pid_t pid;
115
116 pid = <pid for process created for stream by popen()>
117 (void) fclose(stream);
118 while (waitpid(pid, &stat, 0) == -1) {
119 if (errno != EINTR){
120 stat = -1;
121 break;
122 }
123 }
124 return(stat);
125 }
126
128 None.
129
131 fork(), popen(), wait()
132
133 The Base Definitions volume of POSIX.1‐2008, <stdio.h>
134
136 Portions of this text are reprinted and reproduced in electronic form
137 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
138 -- Portable Operating System Interface (POSIX), The Open Group Base
139 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
140 cal and Electronics Engineers, Inc and The Open Group. (This is
141 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
142 event of any discrepancy between this version and the original IEEE and
143 The Open Group Standard, the original IEEE and The Open Group Standard
144 is the referee document. The original Standard can be obtained online
145 at http://www.unix.org/online.html .
146
147 Any typographical or formatting errors that appear in this page are
148 most likely to have been introduced during the conversion of the source
149 files to man page format. To report such errors, see https://www.ker‐
150 nel.org/doc/man-pages/reporting_bugs.html .
151
152
153
154IEEE/The Open Group 2013 PCLOSE(3P)