buggrep - Bugs: bug #25805, fgrep or "grep -F" ...

 
 

You are not allowed to post comments on this tracker with your current authentication level.

bug #25805: fgrep or "grep -F" fails to match patterns on Windows/DOS

Submitter:  Harold Bamford <hbamford>
Submitted:  Tue 10 Mar 2009 12:57:06 AM UTC
   
 
Category:  None Severity:  3 - Normal
Item Group:  None Status:  None
Privacy:  Public Assigned to:  None
Open/Closed:  Closed

Fri 04 Dec 2009 12:54:31 PM UTC, comment #2: 

Thank you for the bug report. This bug has been fixed in the
development sources, which can be downloaded using git from
git://git.sv.gnu.org/grep.git

Paolo Bonzini <bonzini>
Tue 10 Mar 2009 01:00:57 AM UTC, comment #1: 

I attached my modified file since the verbatim comment seems to have wrapped the code to something almost unreadable.

Sorry about that.

Harold Bamford <hbamford>
Tue 10 Mar 2009 12:57:06 AM UTC, original submission:  

On a Windows/DOS box, the newline is actually \r\n rather than the normal Unix \n. The function Fcompile() (in src/search.c around line 500) doesn't account for this and so the \r is treated as part of the pattern. This is almost never what was intended. I checked the latest version, 2.5.4

As a temporary kludgey patch, I have this alternate version of that function:


COMPILE_FCT(Fcompile)
{
  char const *beg, *lim, *err;

  kwsinit ();
  beg = pattern;
  do
  {
    for (lim = beg; lim < pattern + size && *lim != '\n'; ++lim)
      ;

#if HAVE_DOS_FILE_CONTENTS        /* heb */
    //-----------------------------------------------------------------------------------
    // Try to handle Windows/DOS \r\n pairs as just a \n.
    //
    // This function (Fcompile) is called as:
    //
    //                compile(keys, keycc);
    //
    // which becomes:
    //
    //                Fcompile(pattern,size);
    //
    // if the -F option was given or if the program is called 'fgrep' rather than 'grep'.
    // This means that the patterns are in a file, seperated by newlines. And that is
    // the problem; newlines are different on different machines (like Windows and Mac).
    // This "fix" only addresses the case of Windows/DOS machines using ASCII pattern
    // files.
    //
    // This is a ghastly kludge, but for straight ASCII input on a Windows machine
    // it seems to work.  The idea is that if we have just encountered a '\n' char
    // (as the above for() loop has terminated), then see if it was preceded by a '\r'
    // char. If so, tell kwsincr() that the string is shorter by 1 than actually found.
    //
    // This won't work for multibyte chars and it probably doesn't handle lots of other
    // cases, but it lets me get by on my Windows box running Cygwin.
    //-----------------------------------------------------------------------------------
    if(((lim - beg) >= 1) &&        /* must have at least 2 chars to look at */
        (*lim == '\n')    &&        /* last one is a normal Unix-style newline char */
        (*(lim - 1) == '\r'))        /* next-to-last one is a carriage-return char */
    {
      // Indicate string is shorter by 1
      if ((err = kwsincr (kwset, beg, (lim - beg) - 1)) != 0)
        error (2, 0, err);
    }
    else
    {
      // Normal, non-Windows operation
      if ((err = kwsincr (kwset, beg, lim - beg)) != 0)
        error (2, 0, err);
    }
#else
    // This does not handle Windows/DOS newlines (\r\n instead of \n)
    if ((err = kwsincr (kwset, beg, lim - beg)) != 0)
      error (2, 0, err);
#endif

    if (lim < pattern + size)
      ++lim;
    beg = lim;
  }
  while (beg < pattern + size);

  if ((err = kwsprep (kwset)) != 0)
    error (2, 0, err);
}


Harold Bamford <hbamford>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attached Files
file #17633:  search.c added by hbamford (21KiB - text/plain)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by bonzini (Posted a comment)
  • -email is unavailable- added by hbamford (Submitted the item)
  • -email is unavailable- added by hbamford
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

     

    Follow 3 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2009-12-04 bonzini Open/ClosedOpen Closed
    2009-03-10 hbamford Attached File- Added search.c, #17633
    2009-03-10 hbamford Carbon-Copy- Added -email is unavailable-

    Back to the top

    Powered by Savane 3.13-758e.
    Corresponding source code