Re: view growbuf read until() suggestion

Pavel Tsekov wrote:
Of course you might still be right in assuming that fread() may return less
bytes than requested. I can imagine that it could happen with a fdopen()-ed
FILE which has a file descriptor in non-blocking mode. This is of coure just
speculation. Of course, calls to feof() and ferror() could be added to
detect the situation where the read didn't fail but still returned less

ISO C99, 7.19.3p3:
[...] characters may be accumulated to or from the host environment as a block. [...] characters are intended to be transmitted [...] when a block is filled. [...]

The relevant part is the "may" and "are intended to". These are not strict requirements, so we cannot rely on it.

My patch has the side effect that mcview reads one byte more than strictly necessary in some situations. As it already reads data in blocks of 8k, this should not harm.

Index: view.c
RCS file: /cvsroot/mc/mc/src/view.c,v
retrieving revision 1.313
diff -u -p -r1.313 view.c
--- view.c	31 Jul 2005 20:29:35 -0000	1.313
+++ view.c	6 Aug 2005 01:49:16 -0000
@@ -376,13 +376,15 @@ view_growbuf_read_until (WView *view, of
     ssize_t nread;
     byte *p;
     size_t bytesfree;
+    gboolean short_read;
     assert (view->growbuf_in_use);
     if (view->growbuf_finished)
-    while (view_growbuf_filesize (view) < ofs) {
+    short_read = FALSE;
+    while (view_growbuf_filesize (view) < ofs || short_read) {
 	if (view->growbuf_blocks == 0 || view->growbuf_lastindex == VIEW_PAGE_SIZE) {
 	    /* Append a new block to the growing buffer */
 	    byte *newblock = g_try_malloc (VIEW_PAGE_SIZE);
@@ -421,6 +423,7 @@ view_growbuf_read_until (WView *view, of
+	short_read = (nread < bytesfree);
 	view->growbuf_lastindex += nread;

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]