Re: Strange pager (less, more) behavior



Kasper Dupont
<97619472400731189722@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx> writes:

Dave Ulrick wrote:

Hi,

Presently, I'm developing a set of C programs that run via the command
line and exclusively use standard C library stream I/O: fopen, fputs,
fprintf, fclose, etc.. As a convenience to the user, I allow many of
the output files to be defaulted to stdout or stderr. For instance,
a message log file might default to stderr. My file open logic for
such files amounts to something like this:

FILE *f;
char *filename;

if (!filename || strcmp (filename, "-") == 0)
f = stdout;
else
f = fopen (filename, "w");

In such a manner, my programs frequently assign stdout and stderr
to program-defined FILE pointers, but I _never_ assign values to
stdout or stderr, and I never use freopen. As far as I've been able
to tell from C programming books and the Internet, my use of
stdout and stderr is legitimate.

That is a perfectly valid way to use stdout. But then you'd
have to look on filename again to figure out if you need to
call fclose. Or you could just compare the pointers:
if (f != stdout) fclose(f);

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. 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.

However, I've noticed on my Linux 2.6.9 (and above) PCs that at
least some of my programs sometimes gets involved in a rather strange
bit of (mis)behavior when I pipe its output to a pager such as 'less'
or 'more'. When I do this, my command line looks something like
this:

$ ./myprogram 2>&1 | less

99.44% of the time, this works perfectly, but every once in a while,
the pager doesn't appear. Instead, 'bash' gives me these messages:

[1]+ Stopped ./myprogram 2>&1 | less

as though I'd pressed Ctrl-Z to stop the pipeline even though I've not
pressed any such key. 'ps auxw' reveals that my program has completed

This sounds a bit strange. When you say it happens with
grep do you mean something like
./myprogram 2>&1 | grep foobar | less
or is it a command line not even involving your own program?

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.

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



Relevant Pages

  • Re: STDOUT and STDERR redirection fails for forked process
    ... Simply, fork a command using the OPEN instruction, ... redirect STDERR to STDOUT and capture the returncode of the executed ... and the returncode is not captured. ...
    (comp.lang.perl.misc)
  • Re: getting stdout and stderr for system calls on windows
    ... It doesn't separate stderr and stdout, but for what I want it, it's more than enough. ... #You can then access the output and the return value for the command. ... def initialize cmd ...
    (comp.lang.ruby)
  • Re: How can i let shell program within TCL show run log?
    ... You'll find there that>@ means "redirect child's stdout to a Tcl ... and 2>@ the same for child's stderr. ... could be directly ran to display a command output on a text widget (a ...
    (comp.lang.tcl)
  • Re: Copying silently.
    ... so as to ReDirect both StdIn & StdErr for a Command, ... to think that "I've done it" is fit for stdout, ... do it" is fit for stderr. ... for reports on both Failure & Success? ...
    (uk.people.silversurfers)
  • Re: Getting both PID and output from a command
    ... > I'm calling a command from within a python script and I need to be ... > able to both catch the output (stdout and stderr) from it and also ... > I've tried doing the following to grab stderr (I only need stderr, ... I wouldn't trust using a pipe. ...
    (comp.lang.python)