fcopy(3tcl)
π fcopy π
okay bestie, welcome to fcopy β it's a Tcl Built-In Command, she lives in the 3tcl manual section, and she is genuinely SO good at her one job. her one job? moving data from one channel to another. that's it. that's the girl. π¦
β¨ NAME β¨
fcopy β copy data from one channel to another
β¨ SYNOPSIS β¨
fcopy inputChan outputChan ?-size size? ?-command callback?
β¨ DESCRIPTION β¨
so like. the fcopy command copies data from one I/O channel, inchan, to another I/O channel, outchan. and the way it does it?? it leverages the buffering in the Tcl I/O system to avoid extra copies and to avoid buffering too much data in main memory when you're copying large files to destinations like network sockets. iconic behavior, no notes. π
πΈ DATA QUANTITY πΈ
All data until EOF is copied. plus you can specify how much data gets copied with the -size option. the given size is in bytes if the input channel is in binary mode. otherwise? it's in characters.
Depreciated feature: 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. also, an eventually given size is in bytes in this case. (yes she said "depreciated", we're all just living with it π)
πΈ 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 patience. π§
πΈ BACKGROUND OPERATION MODE πΈ
The -command argument makes fcopy work in the background. in this case it 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 handles it. however!! it is necessary to enter the event loop by using the vwait command or by using Tk. don't forget the vibe check on that one. β
You are not allowed to do other input operations with inchan, or output operations with outchan, during a background fcopy. the converse is entirely legitimate though, as exhibited by the bidirectional fcopy example below. π«Ά
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.
heads up:inchancan become readable during a background copy. you should turn off anyfileeventhandlers during a background copy so those handlers do not interfere with the copy. any wrong-sided I/O attempted (by afileeventhandler 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. these 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. π
πΈ CHANNEL ENCODING OPTIONS πΈ
Fcopy obeys the encodings, profiles and character translations configured for the channels. this means that the incoming characters are converted from the encoding configured for the input channel and then encoded as per the encoding of the output channel. 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. β¨
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. ngl that's a little scary but okay, we move.
β¨ EXAMPLES β¨
example one β 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
example two β this one 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
example three β copies in chunks and tests for end of file in the command callback. chunked queen behavior fr fr. π
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
example four β 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). not me thinking about two sockets gossiping π«’
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(3tcl), fblocked(3tcl), fconfigure(3tcl), file(3tcl)
β¨ KEYWORDS β¨
blocking, channel, end of line, end of file, nonblocking, read, translation
and that's the whole page bestie!! this one came from Tcl 8.0, dated with the fcopy(3tcl) footer energy, and she ate. go copy some channels. periodt. πβ¨
π¦ where this page comes from
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- 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