[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
cat bug on cygwin
From: |
Eric Blake |
Subject: |
cat bug on cygwin |
Date: |
Tue, 29 May 2007 16:40:52 +0000 (UTC) |
User-agent: |
Loom/3.14 (http://gmane.org/) |
This code in cat.c, line 669:
if (O_BINARY && ! isatty (STDOUT_FILENO))
freopen (NULL, "wb", stdout);
is buggy. If the stream was originally opened in append mode, this converts it
to non-append mode, such that output overwrites at the current file offset
instead of appending on the end in something like:
cat extra >> existing
It has not been discovered until now on cygwin, because cygwin also had a bug
that lseek(open("existing",O_WRONLY|O_APPEND),0,SEEK_CUR) returned the end of
file offset instead of 0, contrary to POSIX; and that bug was masking the bug
in cat since the non-append writes still happened to take place at the correct
offset to appear as though it were an append.
The code in cat.c needs to be something like:
freopen (NULL, (fcntl (STDOUT_FILENO, F_GETFL) & O_APPEND) ? "ab" : "wb",
stdout);
I'm wondering how many of the other coreutils that use this freopen trick are
affected, and whether we should use a wrapper function rather than duplicating
all the logic. At one point, we even had a wrapper SET_BINARY() that attempted
to do this (prior to commit 8770c00ef...), except that it used the non-standard
setmode() function from the non-standard header <io.h>, not to mention that it
interfered with proper stdio operation; so I am in favor of sticking with the
standardized freopen interface. I'm also wondering if such a wrapper belongs
in the binary-io gnulib module.
--
Eric Blake
- cat bug on cygwin,
Eric Blake <=