changeset 982:0666d42df954

Found the fault. My method of -w fails sans -E, so I just disallow it. Kernel build never uses -w sans -E anyhow.
author M. Farkas-Dyck <strake888@gmail.com>
date Thu, 01 Aug 2013 15:46:45 -0500
parents a1a04ea38db2
children c38c25282b88
files toys/pending/grep.c
diffstat 1 files changed, 7 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/toys/pending/grep.c	Thu Aug 01 15:22:52 2013 -0500
+++ b/toys/pending/grep.c	Thu Aug 01 15:46:45 2013 -0500
@@ -46,7 +46,7 @@
 )
 
 static void do_grep (int fd, char *name) {
-  int n = 0, nMatch = 0;
+  int n = 0, nMatch = 0, which = toys.optflags & FLAG_w ? 2 : 0;
 
   for (;;) {
     char *x, *y;
@@ -75,11 +75,11 @@
         if (!(toys.optflags & FLAG_h)) printf ("%s:", name);
         if ( (toys.optflags & FLAG_n)) printf ("%d:", n);
         if ( (toys.optflags & FLAG_b)) printf ("%ld:", lseek (0, 0, SEEK_CUR) - strlen (y) +
-                                                       (toys.optflags & FLAG_o ? matches[2].rm_so : 0));
+                                                       (toys.optflags & FLAG_o ? matches[which].rm_so : 0));
         if (!(toys.optflags & FLAG_o)) fputs (x, stdout);
         else {
-          y += matches[2].rm_so;
-          printf ("%.*s\n", matches[2].rm_eo - matches[2].rm_so, y++);
+          y += matches[which].rm_so;
+          printf ("%.*s\n", matches[which].rm_eo - matches[which].rm_so, y++);
         }
       }
       if (!(toys.optflags & FLAG_o)) break;
@@ -145,7 +145,7 @@
     toys.optc--; toys.optargs++;
   }
 
-  TT.re_xs = xmsprintf (toys.optflags & FLAG_w ? "(^|[^_[:alnum:]])(%s)($|[^_[:alnum:]])" : "()(%s)()", TT.re_xs);
+  TT.re_xs = xmsprintf (toys.optflags & FLAG_w ? "(^|[^_[:alnum:]])(%s)($|[^_[:alnum:]])" : "%s", TT.re_xs);
 
   if (regcomp (&re, TT.re_xs,
                (toys.optflags & (FLAG_E | FLAG_F) ? REG_EXTENDED : 0) |
@@ -156,6 +156,8 @@
 }
 
 void grep_main (void) {
+  if (toys.optflags & FLAG_w && !(toys.optflags & FLAG_E || toys.optflags & FLAG_F)) error_exit ("must not use -w sans -E");
+
   buildRE ();
 
   if (toys.optflags & FLAG_c) TT.mode = 'c';