@ page 441 line 14507 section freopen comment [gwc freopen NULL]
Problem:
Defect code : 2. Omission
It would be useful to add some guidance to application writers on the
freopen() page about the use of a NULL filename argument and the
problems associated with it.
Discussion of the problems petered out (a long time ago) after the
possibility was raised that a change to the C Standard might be made.
However, it looks like C99 TC3 will not include any such change.
Perhaps the new revision of the C Standard will fix the problem, but
that is no use for the POSIX revision, which will still reference C99.
While there is still a question mark over what will happen in C, I
don't think it is worth trying to make changes to the normative text
in POSIX, but it does make sense to mention the problem in
non-normative text.
Action:
Add to APPLICATION USAGE:
Since implementations are not required to support any stream mode
changes when the filename argument is NULL, portable applications
cannot rely on the use of freopen() to change the stream mode, and
use of this feature is discouraged. The feature was originally
added to the C Standard in order to facilitate changing stdin and
stdout to binary mode. Since a 'b' character in the mode has no
effect on POSIX systems, this use of the feature is unnecessary
in POSIX applications. However, even though the 'b' is ignored, a
successful call to freopen(NULL, "wb", stdout) does have an effect.
In particular for regular files it truncates the file and sets the
file-position indicator for the stream to the start of the file.
It is possible that these side-effects are an unintended
consequence of the way the feature is specified in C99, but
unless or until the C Standard is changed, applications which
successfully call freopen(NULL, "wb", stdout) will behave in
unexpected ways on conforming systems in situations such as:
{ appl file1; appl file2; } > file3
which will result in file3 containing only the output from the
second invocation of appl.
|