bug-gnulib
[Top][All Lists]
Advanced

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

Re: gnulib portability issues


From: Eric Blake
Subject: Re: gnulib portability issues
Date: Mon, 11 Jun 2012 06:13:03 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20120430 Thunderbird/12.0.1

On 06/10/2012 06:43 AM, Rich Felker wrote:

>> Come to think of it, a variant might even work for seekable files.
>> Use dup2 to move the file descriptor somewhere else.  Close the
>> fd.  Keep reading until error, and count the bytes read.  Then
>> ungetc all the bytes that you read, in reverse order, and restore
>> the file descriptor.  Of course ISO C doesn't guarantee this, but
>> it should be fairly portable in practice.
> 
> No, ungetc normally can only unget one character. musl is fairly
> unique in allowing you to unget more,

Wrong.  Pretty much every libc out there lets you ungetc() more than one
byte.  It's just that no one exploits that fact, because ISO C99 doesn't
guarantee that it will work, and POSIX hasn't added any wording to
require it to work either.

In fact, most implementations of fscanf() use more than one ungetc()
when encountering multi-byte ambiguous inputs.  For example, when
parsing "%g" against the partial input "1.e+", whether you push back the
multiple character sequence "e+" or consume it in addition to the next
byte depends on whether the 5th byte is numeric.

-- 
Eric Blake   address@hidden    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]