summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2016-11-18 05:11:30 (GMT)
committerPaul Eggert <eggert@cs.ucla.edu>2016-11-19 09:18:31 (GMT)
commite25ea1223d0fb50e759907cdd88e8096b354cbab (patch)
tree08e4fe9dc64fcd8db051b54a75347f0f0c10beb6
parentfc6fce9a16bc52aca11cd8f1ad2632943fe94201 (diff)
downloadgrep-e25ea1223d0fb50e759907cdd88e8096b354cbab.zip
grep-e25ea1223d0fb50e759907cdd88e8096b354cbab.tar.gz
grep-e25ea1223d0fb50e759907cdd88e8096b354cbab.tar.bz2
grep: tune -f /dev/null
* src/grep.c (main): Do the -f /dev/null early-exit checks before more-expensive tests that involve syscalls.
-rw-r--r--src/grep.c80
1 files changed, 40 insertions, 40 deletions
diff --git a/src/grep.c b/src/grep.c
index 317a0d5..066df8c 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -2761,6 +2761,28 @@ main (int argc, char **argv)
if (show_help)
usage (EXIT_SUCCESS);
+ if (keys)
+ {
+ if (keycc == 0)
+ {
+ /* No keys were specified (e.g. -f /dev/null). Match nothing. */
+ out_invert ^= true;
+ match_lines = match_words = false;
+ }
+ else
+ /* Strip trailing newline. */
+ --keycc;
+ }
+ else if (optind < argc)
+ {
+ /* Make a copy so that it can be reallocated or freed later. */
+ keycc = strlen (argv[optind]);
+ keys = xmemdup (argv[optind++], keycc + 1);
+ fl_add (keys, 0, keycc, "");
+ }
+ else
+ usage (EXIT_TROUBLE);
+
bool possibly_tty = false;
struct stat tmp_stat;
if (! exit_on_match && fstat (STDOUT_FILENO, &tmp_stat) == 0)
@@ -2778,21 +2800,6 @@ main (int argc, char **argv)
}
}
- if (color_option == 2)
- color_option = possibly_tty && should_colorize () && isatty (STDOUT_FILENO);
- init_colorize ();
-
- if (color_option)
- {
- /* Legacy. */
- char *userval = getenv ("GREP_COLOR");
- if (userval != NULL && *userval != '\0')
- selected_match_color = context_match_color = userval;
-
- /* New GREP_COLORS has priority. */
- parse_grep_colors ();
- }
-
/* POSIX says -c, -l and -q are mutually exclusive. In this
implementation, -q overrides -l and -L, which in turn override -c. */
if (exit_on_match | dev_null_output)
@@ -2809,27 +2816,26 @@ main (int argc, char **argv)
if (out_before < 0)
out_before = default_context;
- if (keys)
- {
- if (keycc == 0)
- {
- /* No keys were specified (e.g. -f /dev/null). Match nothing. */
- out_invert ^= true;
- match_lines = match_words = false;
- }
- else
- /* Strip trailing newline. */
- --keycc;
- }
- else if (optind < argc)
+ /* If it is easy to see that matching cannot succeed (e.g., 'grep -f
+ /dev/null'), fail without reading the input. */
+ if (max_count == 0
+ || (keycc == 0 && out_invert && !match_lines && !match_words))
+ return EXIT_FAILURE;
+
+ if (color_option == 2)
+ color_option = possibly_tty && should_colorize () && isatty (STDOUT_FILENO);
+ init_colorize ();
+
+ if (color_option)
{
- /* Make a copy so that it can be reallocated or freed later. */
- keycc = strlen (argv[optind]);
- keys = xmemdup (argv[optind++], keycc + 1);
- fl_add (keys, 0, keycc, "");
+ /* Legacy. */
+ char *userval = getenv ("GREP_COLOR");
+ if (userval != NULL && *userval != '\0')
+ selected_match_color = context_match_color = userval;
+
+ /* New GREP_COLORS has priority. */
+ parse_grep_colors ();
}
- else
- usage (EXIT_TROUBLE);
initialize_unibyte_mask ();
@@ -2866,12 +2872,6 @@ main (int argc, char **argv)
if (O_BINARY && !isatty (STDOUT_FILENO))
set_binary_mode (STDOUT_FILENO, O_BINARY);
- /* If it is easy to see that matching cannot succeed (e.g., 'grep -f
- /dev/null'), fail without reading the input. */
- if (max_count == 0
- || (keycc == 0 && out_invert && !match_lines && !match_words))
- return EXIT_FAILURE;
-
/* Prefer sysconf for page size, as getpagesize typically returns int. */
#ifdef _SC_PAGESIZE
long psize = sysconf (_SC_PAGESIZE);