~ chicken-core (chicken-5) 1b499f9022ca5650bea2510eb1f061e5997e98a1
commit 1b499f9022ca5650bea2510eb1f061e5997e98a1 Author: Peter Bex <peter.bex@xs4all.nl> AuthorDate: Sat Nov 3 21:47:11 2012 +0100 Commit: felix <felix@call-with-current-continuation.org> CommitDate: Mon Nov 5 20:45:58 2012 +0100 Simplify and fix fast_read_string_from_file so it will correctly keep reading when interrupted after having read 0 bytes (by always clearing errors) Signed-off-by: felix <felix@call-with-current-continuation.org> diff --git a/library.scm b/library.scm index 1711b84a..569c9f5c 100644 --- a/library.scm +++ b/library.scm @@ -117,16 +117,10 @@ fast_read_string_from_file(C_word dest, C_word port, C_word len, C_word pos) size_t m = fread (buf, sizeof (char), n, fp); if (m < n) { - if (feof (fp)) { - if (0 == m) - return C_SCHEME_END_OF_FILE; - } else if (ferror (fp)) { - if (0 == m) { - return C_SCHEME_FALSE; - } else { - clearerr (fp); - } - } + if (ferror(fp)) /* Report to Scheme, which may retry, so clear errors */ + clearerr(fp); + else if (feof(fp) && 0 == m) /* eof but m > 0? Return data first, below */ + return C_SCHEME_END_OF_FILE; /* Calling again will get us here */ } return C_fix (m); @@ -1796,9 +1790,8 @@ EOF (lambda (p n dest start) ; read-string! (let loop ([rem (or n (fx- (##sys#size dest) start))] [act 0] [start start]) (let ([len (##core#inline "fast_read_string_from_file" dest p rem start)]) - (cond [(or (not len) ; error returns EOF - (eof-object? len)) ; EOF returns 0 bytes read - act] + (cond ((eof-object? len) ; EOF returns 0 bytes read + act) ((fx< len 0) (##sys#update-errno) (if (eq? (errno) (foreign-value "EINTR" int)) @@ -1810,10 +1803,10 @@ EOF #:file-error 'read-string! (##sys#string-append "cannot read from port - " strerror) p n dest start))) - [(fx< len rem) - (loop (fx- rem len) (fx+ act len) (fx+ start len))] - [else - (fx+ act len) ] ) ))) + ((fx< len rem) + (loop (fx- rem len) (fx+ act len) (fx+ start len))) + (else + (fx+ act len) ) ) ))) (lambda (p rlimit) ; read-line (if rlimit (##sys#check-exact rlimit 'read-line)) (let ((sblen read-line-buffer-initial-size))Trap