Email List: Xaustin-group-lX
[All Lists]

Re: fgets/strtok and LINE_MAX

To: austin-group-l@xxxxxxxxxxxxx
Subject: Re: fgets/strtok and LINE_MAX
From: Vincent Lefevre <vincent-opgr@xxxxxxxxxx>
Date: Wed, 4 Nov 2009 03:27:51 +0100
Mail-followup-to: austin-group-l@xxxxxx
References: <20090920230313.GV657@xxxxxx><20091103025454.GA27699@xxxxxx><8CC2A78725B6067-87B8-1D3ED@webmail-d013.sysops.aol.com>
On 2009-11-03 03:40:35 -0500, shwaresyst@xxxxxx wrote:
> Could be less a bug in the example than the wording of the
> function's behavior. Taking the function description literally, you
> do have a case that there's a discrepancy. Taking the example as an
> expression of the intent of the description, though, it could be
> interpreted that up to n-1 characters will be read that aren't
> newline chars by an implementation and if it gets that far the nth
> char is to be assumed to be a newline, as the function is intended
> for use with well formed text streams that would have honored the
> LINE_MAX length when created. [...]

But in that particular case (newline at nth byte), at the next fgets
call, the user will get the newline character that remained in the
stream, thus a spurious empty line. This is incorrect. Of course, it
is possible to test this particular case in the loop and fix it, but
the example does not mention that, making it incorrect anyway.

Moreover the example is meant to be simple, and using LINE_MAX+1 would
be much simpler and more efficient than testing the particular case.

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / Arénaire project (LIP, ENS-Lyon)

<Prev in Thread] Current Thread [Next in Thread>