bug-gnu-utils
[Top][All Lists]
Advanced

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

Re: sed bug on seekable stdin


From: Eric Blake
Subject: Re: sed bug on seekable stdin
Date: Sun, 17 Dec 2006 15:26:07 -0700
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.8) Gecko/20061025 Thunderbird/1.5.0.8 Mnenhy/0.7.4.666

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

According to Bob Proulx on 12/17/2006 12:13 PM:
> 
> AFAIK fflush() is not needed in input streams as its purpose is to
> flush pending output buffers and input streams have none.

Wrong.  According to POSIX, fflush followed by fseek is required to have
the side effect of setting the byte offset of the underlying fd, even on
input streams.  Some implementations (such as newlib) perform the lseek
unconditionally as part of the fflush; but others optimize and use fflush
to set some state, so that fseek(...,0,SEEK_CUR) can be a no-op except
when immediately following fflush, where it must do an lseek.

> 
>> +      fseek(stdin, 0, SEEK_CUR);
>> +      fclose(stdin);

fclose is required to flush the input stream, although POSIX is not clear
on whether the fflush/fseek sequence is redundant with fclose or must be
manually done first to guarantee the fseek results.  Many implementations,
such as Solaris, newlib, or HP-UX, appear to implicitly do the equivalent
flushing, seeking, and closing of all FILE* on exit(), but glibc does not.

> 
> How are I/O errors on stdin handled in this case?

If you want them handled, you have to check return values.

- --
Life is short - so eat dessert first!

Eric Blake             address@hidden
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFhcP+84KuGfSFAYARAoxVAJ0SC/bIepWaH6vGxAfrHNqAWala8ACfU1Ed
DyDiyqD/i8MzGzdmVNCRSyA=
=3I8k
-----END PGP SIGNATURE-----




reply via email to

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