bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

read buffer length problem in grep on windows


From: Keenan Forbes
Subject: read buffer length problem in grep on windows
Date: Thu, 4 Mar 2004 14:04:31 -0800

Hi-
        I tracked down (and then discovered others had as well after doing a
quick google search) a bug in the Microsoft console code which renders grep
unable to read from stdin. I tested 2.5, but also read the current CVS
source code and the problem still appears to exist there as well.
        I'm pretty sure that the fix is trivial, though I'm not familiar
enough with the code to be sure there are no side effects:
if line 216 of grep.c is changed from:
        #define INITIAL_BUFSIZE 32768   /* Initial buffer size, not counting
slop. */
to something like
        #if defined (_WIN32)
        # define INITIAL_BUFSIZE 16384  /* Initial buffer size, not counting
slop. */        
        #else
        # define INITIAL_BUFSIZE 32768  /* Initial buffer size, not counting
slop. */
        #endif
Then it avoids the Microsoft bug. I'm not sure if it's better to use the
actual limit (30704) or to use a power of 2 (hence the 16384).

Without this fix, the command "grep PATTERN" results in:
        grep: (standard input): Not enough space
With the fix, it properly reads from stdin.

The following C program demonstrates the Microsoft bug:

#include <stdio.h>

int main()
{
    int n;
    char good_buf[30704] = {0};
    char bad_buf[30705] = {0};

    /* due to a bug in the Microsoft console, the following will fail, and
       print -1 w/o reading any input */
    n = read (0, bad_buf, sizeof(bad_buf));
    printf( "%d %s\n", n, bad_buf);

    /* this read request is small enough, so stdin will be read, then echoed
*/
    n = read (0, good_buf, sizeof(good_buf));
    printf( "%d %s\n", n, good_buf);

}

Sorry in advance to CC each of the developers on this, but it wasn't clear
to me who the best person to contact was. If there is a better person to
submit this fix to, please let me know.

Also, if you're interested, I managed to build a working grep for windows
w/o cygwin tools, but had to make two additions to the source package:
dirent.c (which implements opendir and friends using the windows API) and
getpagesize.c (which implements getpagesize). I also manually created a
config.h that works for both the Microsoft and GHS compilers. If anyone's
interested, I'm happy to provide the code (it's only a few dozen lines).

Thanx,

--Keenan Forbes





reply via email to

[Prev in Thread] Current Thread [Next in Thread]