OSDN Git Service

Merge remote-tracking branch 'toybox/master' into HEAD
[android-x86/external-toybox.git] / toys / posix / grep.c
index d02dd9e..9e50a7c 100644 (file)
@@ -74,7 +74,7 @@ static void outline(char *line, char dash, char *name, long lcount, long bcount,
   if (!line || (lcount && (toys.optflags&FLAG_n)))
     printf("%ld%c", lcount, line ? dash : TT.outdelim);
   if (bcount && (toys.optflags&FLAG_b)) printf("%ld%c", bcount-1, dash);
-  if (line) xprintf("%.*s%c", trim ? trim : INT_MAX, line, TT.outdelim);
+  if (line) xprintf("%.*s%c", trim, line, TT.outdelim);
 }
 
 // Show matches in one file
@@ -125,13 +125,8 @@ static void do_grep(int fd, char *name)
             fseek.arg = s = line;
             break;
           }
-          if (toys.optflags & FLAG_i) {
-            long ll = strlen(seek->arg);;
-
-            // Alas, posix hasn't got strcasestr()
-            for (s = line; *s; s++) if (!strncasecmp(s, seek->arg, ll)) break;
-            if (!*s) s = 0;
-          } else s = strstr(line, seek->arg);
+          if (toys.optflags & FLAG_i) s = strnstr(line, seek->arg);
+          else s = strstr(line, seek->arg);
           if (s) break;
         }
 
@@ -206,13 +201,13 @@ static void do_grep(int fd, char *name)
           while (dlb) {
             struct double_list *dl = dlist_pop(&dlb);
 
-            outline(dl->data, '-', name, lcount-before, 0, 0);
+            outline(dl->data, '-', name, lcount-before, 0, -1);
             free(dl->data);
             free(dl);
             before--;
           }
 
-          outline(line, ':', name, lcount, bcount, 0);
+          outline(line, ':', name, lcount, bcount, -1);
           if (TT.a) after = TT.a+1;
         } else outline(start+matches.rm_so, ':', name, lcount, bcount,
                        matches.rm_eo-matches.rm_so);
@@ -228,7 +223,7 @@ static void do_grep(int fd, char *name)
       int discard = (after || TT.b);
 
       if (after && --after) {
-        outline(line, '-', name, lcount, 0, 0);
+        outline(line, '-', name, lcount, 0, -1);
         discard = 0;
       }
       if (discard && TT.b) {
@@ -252,7 +247,7 @@ static void do_grep(int fd, char *name)
     if ((toys.optflags & FLAG_m) && mcount >= TT.m) break;
   }
 
-  if (toys.optflags & FLAG_c) outline(0, ':', name, mcount, 0, 0);
+  if (toys.optflags & FLAG_c) outline(0, ':', name, mcount, 0, -1);
 
   // loopfiles will also close the fd, but this frees an (opaque) struct.
   fclose(file);