[clug] stdio library, feof(3) can't detect EOF in a script [was Re: using 'dd' in a pipeline.]

John Mills johnmi at effect.net.au
Mon Nov 24 17:10:32 MST 2014


On Mon, Nov 24, 2014 at 09:30:56PM +1100, steve jenkin wrote:
> 
> John Mills solution is correct & complete and just proves how far ahead of 
> the game Ousterhout’s TCL was :(

Well, my solution is not correct. Nor complete so far...:-)

And it's naive Tcl.
Currently it seems to flag eof at 102397 bytes in the first file.
(Clue? or just Tcl's buffering niceties I haven't accounted for?)
And both 25-block hashes are wrong.


# for f in brown.jpg white.jpg; do cat $f|./pipetestj2.tcl;done

# ==================== pipetestj2.tcl ==========================
#!/usr/bin/tclsh
set bs 4096
set blocks 1
set data {}

proc GetData {chan} {
    upvar data data bs bs blocks blocks
    append data [read $chan $bs]
    if {$data ne {}} {
 	puts -nonewline "$blocks "
	puts "[exec md5sum << $data]"
    }
    if {[eof $chan]} {
	puts "eof"
	puts "length was [string length $data]"
	exit
    }
    incr blocks
}

fconfigure stdin -blocking 0 -encoding binary
fileevent stdin readable [list GetData stdin]

vwait forever
# =======================================================





More information about the linux mailing list