BUG: alternatives

Gary R. Van Sickle g.r.vansickle@worldnet.att.net
Sat Nov 5 20:09:00 GMT 2005


> From:  Charles Wilson
> Sent: Saturday, November 05, 2005 12:12 PM
> To: Mailing List: CygWin
> Subject: Re: BUG: alternatives
> 
> Gary R. Van Sickle wrote:
> > More investigation reveals that it's textmode mounts that 
> alternatives 
> > doesn't like, not necessarily (although almost certainly as 
> well as) 
> > \r\n line endings.
> > 
> 
> I'm pretty sure the problem is here:
> 
>      if (read(fd, buf, sb.st_size) != sb.st_size) {
>          close(fd);
>          fprintf(stderr, _("failed to read %s: %s\n"), path,
>                  strerror(errno));
>          return 1;
>      }
> 
> Since one effect of 'textmode' conversions is that the 
> bytecount "read" 
> differs from the size reported by the filesystem, textmode 
> files will always fail this test.
>

Ok, yep.

> (1) I could remove this test -- but then you lose the error 
> checking it's attempting to do: make sure the entire file was 
> read into buf, and nothing was lost.
> 

I'm not sure the test is actually valid anyway.  IIRC (and I may not, POSIX
may prove me wrong here), there's no guarantee that read() will return the
number of bytes you requested, so at a minium to make this 100% correct
you'd have to do something like this:

// Read entire file
while(read(...) != 0)
{
}

// Check if there was a read() failure or if it just ran out of bytes to
read.
[...]

> (2) I could always open the file in binmode, and change the 
> parseLine routine to ignore trailing '\r' on each line. 
> (We'll assume that no alternatives config file will ever 
> contain a vertical-feed character).
> 
> Suggestions?

Something like (2) is the only way to do things correctly under any and all
circumstances.  What I usually do is accept either an '\r' or an '\n' as an
EOL character, and then ignore blank lines.  That way, whichever combination
of '\r's and '\n's you have, even abberations like '\r\r\n' which
occaisionally show up, you can read it with no problem.

-- 
Gary R. Van Sickle
 


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/



More information about the Cygwin mailing list