trace add execution open leave tracer_open
trace add execution close leave tracer_close

proc handle2file {handle} {
	if {![info exists ::openfiles($handle)]} {
 		error "File handle not found"
	} else { return $::openfiles($handle) }
}
	
proc tracer_open {cmdstr rescode resstr op} {
	if {$rescode} { return }
	if {[string match file* $resstr]} {
		set ::openfiles($resstr) [lindex [split $cmdstr] 1]
	}
}   
	
proc tracer_close {cmdstr rescode resstr op} {
	set handle [lindex [split $cmdstr] 1]
	if {$rescode || ![info exists ::openfiles($handle)]} { return }
	unset ::openfiles($handle)
}
proc tracefiles {} {
	foreach fs [file channels] {
		if {[string match file* $fs]} { putcmdlog "($fs) [handle2file $fs]" }
	}
}
