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 ebl...@redhat.com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature