Re: Strange pager (less, more) behavior
- From: Måns Rullgård <mru@xxxxxxxxxxxxx>
- Date: Wed, 29 Mar 2006 08:55:47 +0100
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
.
- References:
- Strange pager (less, more) behavior
- From: Dave Ulrick
- Strange pager (less, more) behavior
- Prev by Date: Re: writing to freed memory--issues
- Next by Date: Re: feature tests for pthreads implementation and configuration?
- Previous by thread: Strange pager (less, more) behavior
- Next by thread: Re: Strange pager (less, more) behavior
- Index(es):
Relevant Pages
|