Thu May 8 14:08:57 CEST 2003 Stepan Kasal * lib/closeout.c (default_exit_status): set to 2. * 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(). Move the atexit() call after all calls to usage(); usage() and --version call close_stdout() explicitely before exit. (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 of grepbuf() quickly. diff -urpN grep-2.5.1.orig/lib/closeout.c grep-2.5.1/lib/closeout.c --- grep-2.5.1.orig/lib/closeout.c Sun Mar 4 06:33:12 2001 +++ grep-2.5.1/lib/closeout.c Thu May 8 13:50:14 2003 @@ -47,7 +47,8 @@ extern int errno; #include "__fpending.h" #endif -static int default_exit_status = EXIT_FAILURE; +static int default_exit_status = /* EXIT_FAILURE */ + 2 /* error exit code for grep */; static const char *file_name; /* Set the value to be used for the exit status when close_stdout is called. diff -urpN grep-2.5.1.orig/src/grep.c grep-2.5.1/src/grep.c --- grep-2.5.1.orig/src/grep.c Tue Mar 26 16:54:12 2002 +++ grep-2.5.1/src/grep.c Thu May 8 14:05:27 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; } @@ -1125,6 +1126,7 @@ two FILEs given, assume -h. Exit status and 2 if trouble.\n")); printf (_("\nReport bugs to .\n")); } + close_stdout (); exit (status); } @@ -1348,8 +1350,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 +1524,6 @@ main (int argc, char **argv) case 'q': exit_on_match = 1; - close_stdout_set_status(0); break; case 'R': @@ -1579,7 +1578,7 @@ main (int argc, char **argv) !strcasecmp(optarg, "if-tty")) color_option = 2; else - show_help = 1; + error (2, 0, _("unrecognized colour option")); } else color_option = 2; if(color_option == 2) { @@ -1667,6 +1666,7 @@ Copyright 1988, 1992-1999, 2000, 2001 Fr This is free software; see the source for copying conditions. There is NO\n\ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n")); printf ("\n"); + close_stdout (); exit (0); } @@ -1693,6 +1693,10 @@ warranty; not even for MERCHANTABILITY o } else usage (2); + + /* don't close stdout for -q, consider ``grep -q pat <&- >&-'' */ + if (!exit_on_match) + atexit (close_stdout); if (!install_matcher (matcher) && !install_matcher ("default")) abort ();