Re: Strange pager (less, more) behavior



On Wed, 29 Mar 2006 11:56:34 +0200,
Kasper Dupont <31026663590054698937@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
wrote:
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.

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.

As far as I recall, my code never closes stdout or stderr, but I do
call fflush(stdout) in a number of situations (generally to synchronize
stderr with stdout).

I've seen this happen on occasion as well. It's rare enough that I
haven't tried to figure out why it happens, and I've not been able to
spot any pattern. AFAIK, I've never run any of the OP's programs.

I think I have only seen it happen if I accidentially ran less
in the background.

In that case, you'll see it happen every time. :-)

Dave
--
Dave Ulrick
Email: d-ulrick@xxxxxxx
Web: http://www.niu.edu/~ulrick/
.



Relevant Pages

  • 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)
  • Get exit status, STDOUT and STDERR of a command
    ... I need some way to run a OS command and get ... I could get STDOUT And STDERR, but how to get exit status. ...
    (perl.beginners)
  • Bourne Shell: stdout and stderr to same file ?
    ... In a Bourne shell script, is there a way to determine if stdout and ... will send the message to both stdout and stderr, ...
    (comp.unix.shell)
  • how to limit system() execution time?
    ... I'm shelling out to a unreliable 3rd party utility that ... is supposed to spit some stuff out to STDOUT and exit. ...
    (comp.lang.perl.misc)
  • Re: Strange pager (less, more) behavior
    ... Closing stdout is fine, and the correct thing to do in this case. ... I would worry about what is going to happen when stdio is going ... to access stdout on exit. ... disallowing fcloseon the stdio streams. ...
    (comp.os.linux.development.apps)