view sources/patches/toybox-grep.patch @ 1786:0787ceb820bf draft 1.4.2

When x86-64 switched on NPTL in uClibc, distcc broke, and it turns out fully native compiles under qemu never worked due to qemu not quite emulating floating point right and confusing the perl build with zero not comparing equal to zero. As long as it's broken anyway, switch it over to musl and fix it up on that side. It's no longer worth trying to fix anything broken in uClibc, the project is dead. (I'm aware of uClibc-ng, and am treating it exactly the same way I treated the ecommstation reboot of OS/2.)
author Rob Landley <rob@landley.net>
date Fri, 11 Sep 2015 13:25:14 -0500
parents 0494012aca6d
children bbe71b3a271d
line wrap: on
line source

Toybox grep on a directory hits a uClibc bug and segfaults. This bug does not
happen in glibc, musl, or bionic, so it's not worth adding a workaround
upstream.

diff -r dbee4e656aa6 toys/posix/grep.c
--- a/toys/posix/grep.c	Thu Mar 26 13:25:20 2015 -0500
+++ b/toys/posix/grep.c	Fri Mar 27 20:10:08 2015 -0500
@@ -62,16 +62,21 @@
 
 static void do_grep(int fd, char *name)
 {
-  FILE *file = fdopen(fd, "r");
+  struct stat *st = (void *)(toybuf+sizeof(regex_t));
+  FILE *file = 0;
   long offset = 0;
   int lcount = 0, mcount = 0, which = toys.optflags & FLAG_w ? 2 : 0;
   char indelim = '\n' * !(toys.optflags&FLAG_z),
        outdelim = '\n' * !(toys.optflags&FLAG_Z);
 
   if (!fd) name = "(standard input)";
-
+  if (!fstat(fd, st)) {
+    // grep * hits directories, skip but don't treat them as errors.
+    if (S_ISDIR(st->st_mode)) errno = 0;
+    else file = fdopen(fd, "r");
+  }
   if (!file) {
-    perror_msg("%s", name);
+    if (errno) perror_msg("%s", name);
     return;
   }
 
@@ -88,8 +93,7 @@
 
     start = line;
 
-    for (;;)
-    {
+    for (;;) {
       int rc = 0, skip = 0;
 
       if (toys.optflags & FLAG_F) {