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

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

Re: [Cooker] strange problem with "hotplug" package


From: Stepan Kasal
Subject: Re: [Cooker] strange problem with "hotplug" package
Date: Tue, 6 May 2003 17:54:35 +0200
User-agent: Mutt/1.2.5.1i

Hello to all people reading this,

On Tue, May 06, 2003 at 05:45:40PM +0400, Andrey Borzenkov wrote:
> address@hidden grep -q bor /etc/passwd <&- >&-
> grep: write error: Bad file descriptor
> address@hidden echo $?
> 1

the exit code is always correct.  The only problem is the bogus
error message.
So, we should not try to close(stdout) if -q was given.

Curiously enough, the problem doesn't appear with

        grep -q bor /etc/passwd >&-

because the file /etc/passwd is opened with file descriptor 1,
and it is closed on exit instead of stdout.
(With <&- >&-, /etc/passwd is fd 0 and the bug bites.)
Cause: if a match was found, we exit(0) immediately, without closing
the open file.  This optimization can be moved after the input file
is closed, without any loss.

The patch attached to the end of this mail should fix both problems.
(Apply to the 2.5.1 source tree.)  It will appear in the next release.

Regards,
        Stepan Kasal

Tue May  6 17:49:29 CEST 2003  Stepan Kasal  <address@hidden>

        * src/grep.c(main): Don't register atexit(close_stdout) if -q
          was given---no output will be written; there is also no need
          to use close_stdout_set_status().
        * src/grep.c(grepbuf): move exit(0) ...
          (grepfile): ... here, when the bufdesc is closed; this doesn't
          present any performance loss, done_on_match is 1 and ensures
          that we get out quickly.

--- grep-2.5.1.orig/src/grep.c  Tue Mar 26 16:54:12 2002
+++ grep-2.5.1/src/grep.c       Tue May  6 17:47:38 2003
@@ -722,8 +722,6 @@ grepbuf (char const *beg, char const *li
           outleft--;
          if (!outleft || done_on_match)
            {
-             if (exit_on_match)
-               exit (0);
              after_last_match = bufoffset - (buflim - endp);
              return nlines;
            }
@@ -978,6 +976,9 @@ grepfile (char const *file, struct stats
            }
     }
 
+  if (!status && exit_on_match)
+       exit (0);
+
   return status;
 }
 
@@ -1348,8 +1349,6 @@ main (int argc, char **argv)
   textdomain (PACKAGE);
 #endif
 
-  atexit (close_stdout);
-
   prepend_default_options (getenv ("GREP_OPTIONS"), &argc, &argv);
 
   while ((opt = get_nondigit_option (argc, argv, &default_context)) != -1)
@@ -1524,7 +1523,6 @@ main (int argc, char **argv)
 
       case 'q':
        exit_on_match = 1;
-       close_stdout_set_status(0);
        break;
 
       case 'R':
@@ -1630,6 +1628,10 @@ main (int argc, char **argv)
        break;
 
       }
+
+  /* don't close stdout for -q, consider ``grep -q pat <&- >&-'' */
+  if (!exit_on_match)
+    atexit(close_stdout);
 
   /* POSIX.2 says that -q overrides -l, which in turn overrides the
      other output options.  */




reply via email to

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