1PCLOSE(3P)                 POSIX Programmer's Manual                PCLOSE(3P)
2
3
4

PROLOG

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

NAME

12       pclose - close a pipe stream to or from a process
13

SYNOPSIS

15       #include <stdio.h>
16
17       int pclose(FILE *stream);
18
19

DESCRIPTION

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
35          IEEE Std 1003.1-2001 that 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

RETURN VALUE

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

ERRORS

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
63       The following sections are informative.
64

EXAMPLES

66       None.
67

APPLICATION USAGE

69       None.
70

RATIONALE

72       There  is  a  requirement  that  pclose()  not  return before the child
73       process terminates. This is intended to disallow  implementations  that
74       return  [EINTR]  if  a  signal  is  received while waiting. If pclose()
75       returned before the child terminated, there would be  no  way  for  the
76       application  to  discover  which  child  used to be associated with the
77       stream, and it could not do the cleanup itself.
78
79       If the stream pointed to by stream was not created by popen(), histori‐
80       cal  implementations  of  pclose() return -1 without setting errno.  To
81       avoid   requiring   pclose()   to   set    errno    in    this    case,
82       IEEE Std 1003.1-2001  makes  the  behavior unspecified.  An application
83       should not use pclose() to close any stream that  was  not  created  by
84       popen().
85
86       Some  historical implementations of pclose() either block or ignore the
87       signals SIGINT, SIGQUIT, and SIGHUP while waiting for the child process
88       to  terminate.  Since  this  behavior is not described for the pclose()
89       function in IEEE Std 1003.1-2001, such implementations are not conform‐
90       ing.  Also,  some historical implementations return [EINTR] if a signal
91       is received, even though the child process has  not  terminated.   Such
92       implementations are also considered non-conforming.
93
94       Consider, for example, an application that uses:
95
96
97              popen("command", "r")
98
99       to  start  command,  which  is part of the same application. The parent
100       writes a prompt to its standard output (presumably  the  terminal)  and
101       then reads from the popen()ed stream. The child reads the response from
102       the user, does some transformation on the response (pathname expansion,
103       perhaps)  and  writes  the  result  to its standard output.  The parent
104       process reads the result from the pipe, does  something  with  it,  and
105       prints  another prompt. The cycle repeats. Assuming that both processes
106       do appropriate buffer flushing, this would be expected to work.
107
108       To conform to IEEE Std 1003.1-2001, pclose()  must  use  waitpid(),  or
109       some similar function, instead of wait().
110
111       The  code  sample  below illustrates how the pclose() function might be
112       implemented on a system conforming to IEEE Std 1003.1-2001.
113
114
115              int pclose(FILE *stream)
116              {
117                  int stat;
118                  pid_t pid;
119
120
121                  pid = <pid for process created for stream by popen()>
122                  (void) fclose(stream);
123                  while (waitpid(pid, &stat, 0) == -1) {
124                      if (errno != EINTR){
125                          stat = -1;
126                          break;
127                      }
128                  }
129                  return(stat);
130              }
131

FUTURE DIRECTIONS

133       None.
134

SEE ALSO

136       fork(),  popen(),   waitpid(),   the   Base   Definitions   volume   of
137       IEEE Std 1003.1-2001, <stdio.h>
138
140       Portions  of  this text are reprinted and reproduced in electronic form
141       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
142       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
143       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
144       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
145       event of any discrepancy between this version and the original IEEE and
146       The  Open Group Standard, the original IEEE and The Open Group Standard
147       is the referee document. The original Standard can be  obtained  online
148       at http://www.opengroup.org/unix/online.html .
149
150
151
152IEEE/The Open Group                  2003                           PCLOSE(3P)
Impressum