Re: view_growbuf_read_until() suggestion



Pavel Tsekov wrote:
I forgot to attach the patch in the original message.

On Fri, 5 Aug 2005, Pavel Tsekov wrote:


Hello Roland,

There is a small "bug" in the new viewer code. When the viewer is used in
growing buffer mode it incorrectly displays in the status bar ">= size"
even if the data source was exhausted and despite the fact the the
size of the whole buffer is known. Please, take a look at the attached
patch for a idea how to fix it. Of course you could find a better way to
solve this since you know the code better.

I confirm that the "bug" does not look nice. But your code has a little problem. It _might_ report a too small file size.

 	    nread = fread (p, 1, bytesfree, view->ds_stdio_pipe);
-	    if (nread == 0) {
+	    if (nread > 0)
+		view->growbuf_lastindex += nread;
+	    if (nread < bytesfree) {

Nothing in the C standard prevents fread from always returning only one element instead of ''bytesfree''. It's just a "quality of implementation" that most system's fread(3) function calls read(2) repeatedly until either EOF is reached or the necessary number of bytes is read.

I have run the two appended files on NetBSD, Linux, Solaris, IRIX and AIX, and all systems have a "greedy" fread(). But this is not mandated by the standard.

$ make reader writer
$ ./writer | ./reader
25

I would not be surprised if the output were

$ make reader writer
$ ./writer | ./reader
7
7
7
4

Nevertheless I will fix this "bug".

Roland
#include <stdio.h>

int main(void)
{
	unsigned char buf[100];
	size_t n;

	setvbuf(stdin, NULL, _IONBF, 0);
	while ((n = fread(buf, 1, sizeof(buf), stdin)) != 0) {
		printf("%d\n", (int) n);
	}
	return 0;
}
#include <unistd.h>

int main(void)
{
	write(STDOUT_FILENO, "Line 1\n", 7);
	sleep(1);
	write(STDOUT_FILENO, "Line 2\n", 7);
	sleep(1);
	write(STDOUT_FILENO, "Line 3\n", 7);
	sleep(1);
	write(STDOUT_FILENO, "EOF\n", 4);
	return 0;
}


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