fcopy(n)
π fcopy(n) β bestie edition π
okay so this is fcopy, a Tcl Built-In Command, and it's literally the "move the data from here to there" girlie. no cap, she's iconic.
β¨ NAME β¨
fcopy β copy data from one channel to another π¦
πΈ SYNOPSIS πΈ
fcopy inputChan outputChan ?-size size? ?-command callback?
π DESCRIPTION π
so fcopy copies data from one I/O channel, inchan, to another I/O channel, outchan. that's it, that's the whole vibe. and the way she does it is chef's kiss β she leverages the buffering in the Tcl I/O system so she can avoid extra copies AND avoid buffering too much data in main memory when you're copying big files to destinations like network sockets. efficient queen π
π DATA QUANTITY π
all data until EOF gets copied. periodt. also, the quantity of copied data may be specified with the -size option.
the given size is in bytes if the input channel is in binary mode. otherwise, it's in characters.
β οΈ deprecated feature alert (the original spells it "Depreciated", and we're keeping that exactly as-is, mwah): the transfer is treated as a binary transfer if the encoding profile is set to "tcl8" and the input encoding matches the output encoding. in this case, eventual encoding errors are not handled. and an eventually given size is in bytes in this case.
π BLOCKING OPERATION MODE π
without the -command option, fcopy blocks until the copy is complete and returns the number of bytes or characters (using the same rules as for the -size option) written to outchan. she just stands there. patiently. until it's done. main character energy honestly.
π BACKGROUND OPERATION MODE π
the -command argument makes fcopy work in the background. she returns immediately and the callback is invoked later when the copy completes. the callback is called with one or two additional arguments that indicate how many bytes were written to outchan. if an error occurred during the background copy, the second argument is the error string associated with the error.
with a background copy, it is not necessary to put inchan or outchan into non-blocking mode β fcopy takes care of that automatically, she's got you π«Ά. however, it is necessary to enter the event loop by using the vwait command or by using Tk.
π¨ you are not allowed to do other input operations withinchan, or output operations withoutchan, during a backgroundfcopy. the converse is entirely legitimate, as exhibited by the bidirectionalfcopyexample below. iykyk.
if either inchan or outchan get closed while the copy is in progress, the current copy is stopped and the command callback is not made. if inchan is closed, then all data already queued for outchan is written out.
note that inchan can become readable during a background copy. you should turn off any fileevent handlers during a background copy so those handlers do not interfere with the copy. any wrong-sided I/O attempted (by a fileevent handler or otherwise) will get a "channel busy" error. π
π§ CHANNEL TRANSLATION OPTIONS π§
fcopy translates end-of-line sequences in inchan and outchan according to the -translation option for these channels. see the manual entry for fconfigure for details on the -translation option.
the translations mean that the number of bytes read from inchan can be different than the number of bytes written to outchan. only the number of bytes written to outchan is reported, either as the return value of a synchronous fcopy or as the argument to the callback for an asynchronous fcopy. so don't be shocked when the numbers don't match, bestie, it's not a glitch β¨
π CHANNEL ENCODING OPTIONS π
fcopy obeys the encodings, profiles and character translations configured for the channels. this means the incoming characters are converted internally first to UTF-8 and then into the encoding of the channel fcopy writes to. see the manual entry for fconfigure for details on the -encoding and -profile options.
no conversion is done if both channels are set to encoding "binary" and have matching translations.
if only the output channel is set to encoding "binary", the system will write the internal UTF-8 representation of the incoming characters.
if only the input channel is set to encoding "binary", the system will assume that the incoming bytes are valid UTF-8 characters and convert them according to the output encoding.
π the behaviour of the system for bytes which are not valid UTF-8 characters is undefined in this case. undefined. no cap. do not test fate.
fcopy may throw encoding errors (error code EILSEQ) if input or output channel is configured to the "strict" encoding profile.
if an encoding error arises on the input channel, any data before the error byte is written to the output channel. the input file pointer is located just before the values causing the encoding error. error inspection or recovery is possible by changing the encoding parameters and invoking a file command (read, fcopy).
if an encoding error arises on the output channel, the erroneous data is lost. to make the difference between the input error case and the output error case, only the error message may be inspected (read or write), as both throw the error code EILSEQ.
β EXAMPLES β
the first example transfers the contents of one channel exactly to another. note that when copying one file to another, it is better to use file copy which also copies file metadata (e.g. the file access permissions) where possible.
fconfigure $in -translation binary
fconfigure $out -translation binary
fcopy $in $out
this second example shows how the callback gets passed the number of bytes transferred. it also uses vwait to put the application into the event loop. of course, this simplified example could be done without the command callback.
proc Cleanup {in out bytes {error {}}} {
global total
set total $bytes
close $in
close $out
if {[string length $error] != 0} {
# error occurred during the copy
}
}
set in [open $file1]
set out [socket $server $port]
fcopy $in $out -command [list Cleanup $in $out]
vwait total
the third example copies in chunks and tests for end of file in the command callback.
proc CopyMore {in out chunk bytes {error {}}} {
global total done
incr total $bytes
if {([string length $error] != 0) || [eof $in]} {
set done $total
close $in
close $out
} else {
fcopy $in $out -size $chunk \
-command [list CopyMore $in $out $chunk]
}
}
set in [open $file1]
set out [socket $server $port]
set chunk 1024
set total 0
fcopy $in $out -size $chunk \
-command [list CopyMore $in $out $chunk]
vwait done
the fourth example starts an asynchronous, bidirectional fcopy between two sockets. those could also be pipes from two [open "|hal 9000" r+] (though their conversation would remain secret to the script, since all four fileevent slots are busy). π€«
set flows 2
proc Done {dir args} {
global flows done
puts "$dir is over."
incr flows -1
if {$flows<=0} {set done 1}
}
fcopy $sok1 $sok2 -command [list Done UP]
fcopy $sok2 $sok1 -command [list Done DOWN]
vwait done
π₯ SEE ALSO π₯
eof(n), fblocked(n), fconfigure(n), file(n)
π KEYWORDS π
blocking, channel, end of line, end of file, nonblocking, read, translation
okay bestie, that's the whole page!! this one's from the Tcl 8.0 era of the fcopy(n) page, so go forth and copy some channels, you absolute icon πβ¨π
slay, periodt, and always close your channels π«Ά
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- Copyright (c) 1993 The Regents of the University of California
- Copyright (c) 1994-1997 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms