Re: Strange pager (less, more) behavior



Kasper Dupont
<31026663590054698937@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> writes:

Måns Rullgård wrote:

Closing stdout is fine, and the correct thing to do in this case. If
you do not close it, you have no chance of detecting errors in the
final buffer flush.

I'd rather use fflush to check for write errors on stdout.

Network filesystems with aggressive caching may not report an error
(such as insufficient disk space) until the file is closed.

There is no reason for not closing stdin/out/err. They are all
closed automatically on program exit anyway, so there is no
externally visible difference.

I would worry about what is going to happen when stdio is going
to access stdout on exit. Usually I consider whoever opens a file
responsible to close it again. But if any part of a program copy
a pointer to stdout and eventually close it, you may easilly have
many attempts to close it, and somebody may try to access it after
it has been closed.

If you use strace on the program, there is a visible difference
between calling fclose(stdout) and letting stdio deal with it on
exit. In the first case there is a close(1) system call, there is
none in the second case.

I can't find anything in the C99 standard explicitly allowing or
disallowing fclose() on the stdio streams. The closest I get is this
sentence: The value of a pointer to a FILE object is indeterminate
after the associated file is closed (including the standard text
streams). I suppose this could be taken as an implicit permission to
close these streams. I've never seen or heard of any ill effects from
doing so either.

--
Måns Rullgård
mru@xxxxxxxxxxxxx
.



Relevant Pages

  • Re: Any way to capture stdcout output to memory?
    ... implementations store pointers to the underlying i/o functions in the ... This came from my stdio by way of 4.4BSD and then ... stdout need not be a modifiable lvalue, ... check for failures ... ...
    (comp.lang.c)
  • Re: GOTOXY() in C/400
    ... You can write to the screen using C/C++ stdio functions. ... stdout and stderr default to STDIN, STDOUT and STDERR, but you do not ...
    (comp.sys.ibm.as400.misc)
  • Re: two-way communication with pipes
    ... But if I run the program, it seems to not communicate at all. ... If gnuchess uses stdio and doesn't unbuffer its stdout, ...
    (comp.unix.programmer)
  • Re: two-way communication with pipes
    ... If gnuchess uses stdio and doesn't unbuffer its stdout, ...
    (comp.unix.programmer)
  • self-tailing output checker
    ... certain user-defined patterns in addition to watching exit status. ... control where output goes via standard redirection, ... it would cause the program which uses it to create two pipes and fork. ... One side of the fork would connect stdout and stderr to the write ends ...
    (comp.unix.programmer)