2 '\" Copyright (c) 1993 The Regents of the University of California.
3 '\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
5 '\" See the file "license.terms" for information on usage and redistribution
6 '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
8 .TH close n 7.5 Tcl "Tcl Built-In Commands"
11 '\" Note: do not modify the .SH NAME line immediately below!
13 close \- Close an open channel
15 \fBclose \fIchannelId\fR ?r(ead)|w(rite)?
19 Closes or half-closes the channel given by \fIchannelId\fR.
21 \fIChannelId\fR must be an identifier for an open channel such as a
22 Tcl standard channel (\fBstdin\fR, \fBstdout\fR, or \fBstderr\fR),
23 the return value from an invocation of \fBopen\fR or \fBsocket\fR, or
24 the result of a channel creation command provided by a Tcl extension.
26 The single-argument form is a simple
28 all buffered output is flushed to the channel's output device,
29 any buffered input is discarded, the underlying file or device is closed,
30 and \fIchannelId\fR becomes unavailable for use.
32 If the channel is blocking, the command does not return until all output
34 If the channel is nonblocking and there is unflushed output, the
35 channel remains open and the command
36 returns immediately; output will be flushed in the background and the
37 channel will be closed when all the flushing is complete.
39 If \fIchannelId\fR is a blocking channel for a command pipeline then
40 \fBclose\fR waits for the child processes to complete.
42 If the channel is shared between interpreters, then \fBclose\fR
43 makes \fIchannelId\fR unavailable in the invoking interpreter but has no
44 other effect until all of the sharing interpreters have closed the
46 When the last interpreter in which the channel is registered invokes
47 \fBclose\fR, the cleanup actions described above occur. See the
48 \fBinterp\fR command for a description of channel sharing.
50 Channels are automatically closed when an interpreter is destroyed and
51 when the process exits.
53 From 8.6 on (TIP#398), nonblocking channels are no longer switched to blocking mode when exiting; this guarantees a timely exit even when the peer or a communication channel is stalled. To ensure proper flushing of stalled nonblocking channels on exit, one must now either (a) actively switch them back to blocking or (b) use the environment variable TCL_FLUSH_NONBLOCKING_ON_EXIT, which when set and not equal to "0" restores the previous behavior.
56 The command returns an empty string, and may generate an error if
57 an error occurs while flushing output. If a command in a command
58 pipeline created with \fBopen\fR returns an error, \fBclose\fR
59 generates an error (similar to the \fBexec\fR command.)
62 The two-argument form is a
64 given a bidirectional channel like a
65 socket or command pipeline and a (possibly abbreviated) direction, it closes
66 only the sub-stream going in that direction. This means a shutdown() on a
67 socket, and a close() of one end of a pipe for a command pipeline. Then, the
68 Tcl-level channel data structure is either kept or freed depending on whether
69 the other direction is still open.
71 A single-argument close on an already half-closed bidirectional channel is
73 .QW "finish the job" .
74 A half-close on an already closed half, or on a wrong-sided unidirectional
75 channel, raises an error.
77 In the case of a command pipeline, the child-reaping duty falls upon the
78 shoulders of the last close or half-close, which is thus allowed to report an
81 Currently only sockets and command pipelines support half-close. A future
82 extension will allow reflected and stacked channels to do so.
86 This illustrates how you can use Tcl to ensure that files get closed
87 even when errors happen by combining \fBcatch\fR, \fBclose\fR and
91 proc withOpenFile {filename channelVar script} {
92 upvar 1 $channelVar chan
93 set chan [open $filename]
98 return -options $options $result
102 file(n), open(n), socket(n), eof(n), Tcl_StandardChannels(3)
104 blocking, channel, close, nonblocking, half-close