(bind-func fill_buffer_with_file
"fill buffer with up to maxlen bytes from filename"
(lambda (buf filename maxlen)
(let ((fp (fopen filename "rb")))
(if (null? fp)
(begin (println "Error reading file.")
-1)
(let ((filesize (file_size fp))
(bytes_read (fread buf 1 (if (> filesize maxlen) maxlen filesize) fp)))
(if (<> bytes_read filesize)
(println "Warning: filesize is" filesize "but only read" bytes_read "bytes.\nPerhaps you need a bigger buffer?")
(begin (printf "Read %lld bytes from %s\n" bytes_read filename)
void))
(fclose fp)
bytes_read)))))