Subject: bug#32943: grep pattern < fifo fails on Cygwin (and MinGW), but not on Linux

From: Houder <[email-masked]>
Date: Mon, 08 Oct 2018 09:35:40 +0200

On 2018-10-06 22:12, Paul Eggert wrote:
[snip}

> My guess is that you've got an old version of grep on Cygwin, and need
> to upgrade to the current version. If you still have problems with the
> latest version, please investigate via GDB and/or other means exactly
> why grep is failing for you ...

Hi Paul,

I have found what causes grep to fail. (after building v3.1 of grep)

But in order not to mislead others reading this thread, I must correct 
what I
wrote in my initial post. There I wrote: "as far as I can tell, the code 
that
is executed on Cygwin is not the same as the one that is executed on 
Linux".

Wrong. My "strace-ing" misled me.

Returning to grep's failure on Gygwin:

grep terminates with an error on Cygwin because lseek() on Cygwin fails 
to
ascertain that it is applied to a FIFO.

As result Cygwin's executive returns EINVAL in errno, in stead of 
ESPIPE, as
required (my interpretation after reading "man 2 lseek").

ESPIPE is expected in reset() (src/grep.c), after lseek() has been 
applied to
stdin (which points to a FIFO here).

Until v2.26 of grep, a call to S_ISREG(st->st_mode) circumvented the 
call to
lseek() (and the call to suppressible_error()) and made reset() return 
true.

Version v2.27 of grep removed the call to S_ISREG(st->st_mode). As 
result of
that lseek() is invoked.

lseek() fails, but an exception is made for errno == ESPIPE. For this 
reason
grep does not fail on Linux (reset() returns true).

However on Cygwin, suppressible_error() is called and reset() returns 
false,
which makes grep fail on Cygwin.

You can see for yourself in the attached logfiles (GDB sessions).

-----
To what did I apply GDB?

  - downloaded the tarball (grep-3.1.tar.xz) from 
http://ftp.gnu.org/gnu/grep/
  - build grep using "make CFLAG='-O -g3'", both on Linux and Cygwin

Regards,
Henri