Paul Eggert wrote:
> On 06/12/2012 04:21 AM, Paolo Bonzini wrote:
> > perhaps we can follow the suggestion and
> > replace "if (freadahead (f))" with "if (freading(f) && !feof(f))" in
> > closein.c.
> 
> Yes, thanks, I like this idea the best of those suggested so far.
> Here's a proposed patch to gnulib.

Unfortunately, this idea will cause extra system calls for programs which
terminate without having read from stdin.

This is because initially,
   (freadahead (f))   is false, whereas
   (freading(f) && !feof(f))   is true.

Test program:
========================================================
#include <config.h>
#include <stdio.h>
#include "freading.h"
#include "freadahead.h"
int main ()
{
  printf ("%d %d\n", freadahead (stdin) > 0, freading (stdin) && !feof (stdin));
}
========================================================
Output:
0 1

Another test program:
========================================================
#include <config.h>
#include <stdio.h>
#include "closein.h"
int main ()
{
  close_stdin ();
}
========================================================

Before the change:
$ strace ./a.out
...
munmap(0x7f7bdf62e000, 184169)          = 0
close(0)                                = 0
close(1)                                = 0
close(2)                                = 0
exit_group(0)                           = ?

After the change:
$ strace ./a.out
...
munmap(0x7fc84bf74000, 184169)          = 0
lseek(0, 0, SEEK_CUR)                   = -1 ESPIPE (Illegal seek)
close(0)                                = 0
close(1)                                = 0
close(2)                                = 0
exit_group(0)                           = ?

I think the idea of gnulib is to provide extra source code to many
programs, but not extra system calls at runtime on sane glibc platforms.

Bruno


Reply via email to