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: Tue, 3 Nov 2009 03:54:54 +0100
Mail-followup-to: austin-group-l@opengroup.org
References: <20090920230313.GV657@prunille.vinc17.org>
Hi,

I haven't seen any reply. Is this a bug or what?
In the LINE_MAX definition or in the example?

Note that the LINE_MAX definition says:

  Unless otherwise noted, the maximum length, in bytes, of the input
  line of a utility (either standard input or another file), when the
  utility is described as processing text files. The length includes
  room for the trailing <newline>.

i.e. it includes the trailing <newline>.

On 2009-09-21 01:03:13 +0200, Vincent Lefevre wrote:
> http://www.opengroup.org/onlinepubs/9699919799/functions/fgets.html
> says in the EXAMPLES section (page 852 in the PDF version):
> 
>   The following example uses fgets() to read each line of input.
>   {LINE_MAX}, which defines the maximum size of the input line,
>   is defined in the <limits.h> header.
> 
>   #include <stdio.h>
>   ...
>   char line[LINE_MAX];
>   ...
>   while (fgets(line, LINE_MAX, fp) != NULL) {
>   ...
>   }
>   ...
> 
> However for a line of length LINE_MAX, LINE_MAX-1 bytes will be read,
> i.e. the last byte (here, the newline character) won't be read! This
> is wrong, isn't it?
> 
> To be correct, the example should have LINE_MAX+1 instead of LINE_MAX
> (both occurrences). It would also assume that LINE_MAX < INT_MAX.
> 
> Similarly, the strtok example (page 2041) looks wrong too.

-- 
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>