diff options
| author | Mark Whitley <markw@lineo.com> | 2001-02-01 21:02:41 +0000 |
|---|---|---|
| committer | Mark Whitley <markw@lineo.com> | 2001-02-01 21:02:41 +0000 |
| commit | b5c29850e31ca41ee643cfe62eb8bc5f8682deb1 (patch) | |
| tree | d65b0d24aaba3ee86371b7a04c9c0e5b6eaf0a89 | |
| parent | 065c7e779a0ab49267ef18f2f851d9886719151c (diff) | |
| download | busybox-w32-b5c29850e31ca41ee643cfe62eb8bc5f8682deb1.tar.gz busybox-w32-b5c29850e31ca41ee643cfe62eb8bc5f8682deb1.tar.bz2 busybox-w32-b5c29850e31ca41ee643cfe62eb8bc5f8682deb1.zip | |
Tightened things up a bit, saved 20 bytes, and made the code a bit clearer in
the process.
| -rw-r--r-- | findutils/grep.c | 43 | ||||
| -rw-r--r-- | grep.c | 43 |
2 files changed, 28 insertions, 58 deletions
diff --git a/findutils/grep.c b/findutils/grep.c index fec8d0913..ce2990f64 100644 --- a/findutils/grep.c +++ b/findutils/grep.c | |||
| @@ -48,19 +48,6 @@ static int matched; /* keeps track of whether we ever matched */ | |||
| 48 | static char *cur_file = NULL; /* the current file we are reading */ | 48 | static char *cur_file = NULL; /* the current file we are reading */ |
| 49 | 49 | ||
| 50 | 50 | ||
| 51 | static void print_matched_line(char *line, int linenum) | ||
| 52 | { | ||
| 53 | if (print_count_only) | ||
| 54 | return; | ||
| 55 | |||
| 56 | if (print_filename) | ||
| 57 | printf("%s:", cur_file); | ||
| 58 | if (print_line_num) | ||
| 59 | printf("%i:", linenum); | ||
| 60 | |||
| 61 | puts(line); | ||
| 62 | } | ||
| 63 | |||
| 64 | static void grep_file(FILE *file) | 51 | static void grep_file(FILE *file) |
| 65 | { | 52 | { |
| 66 | char *line = NULL; | 53 | char *line = NULL; |
| @@ -72,7 +59,11 @@ static void grep_file(FILE *file) | |||
| 72 | chomp(line); | 59 | chomp(line); |
| 73 | linenum++; | 60 | linenum++; |
| 74 | ret = regexec(®ex, line, 0, NULL, 0); | 61 | ret = regexec(®ex, line, 0, NULL, 0); |
| 75 | if (ret == 0 && !invert_search) { /* match */ | 62 | |
| 63 | /* test for a postitive-assertion match (regexec returned success (0) | ||
| 64 | * and the user did not specify invert search), or a negative-assertion | ||
| 65 | * match (vice versa) */ | ||
| 66 | if ((ret == 0 && !invert_search) || (ret == REG_NOMATCH && invert_search)) { | ||
| 76 | 67 | ||
| 77 | /* if we found a match but were told to be quiet, stop here and | 68 | /* if we found a match but were told to be quiet, stop here and |
| 78 | * return success */ | 69 | * return success */ |
| @@ -81,20 +72,17 @@ static void grep_file(FILE *file) | |||
| 81 | exit(0); | 72 | exit(0); |
| 82 | } | 73 | } |
| 83 | 74 | ||
| 75 | /* otherwise, keep track of matches, print the matched line, and | ||
| 76 | * whatever else the user wanted */ | ||
| 84 | nmatches++; | 77 | nmatches++; |
| 85 | print_matched_line(line, linenum); | 78 | if (!print_count_only) { |
| 86 | 79 | if (print_filename) | |
| 87 | } | 80 | printf("%s:", cur_file); |
| 88 | else if (ret == REG_NOMATCH && invert_search) { | 81 | if (print_line_num) |
| 89 | if (be_quiet) { | 82 | printf("%i:", linenum); |
| 90 | regfree(®ex); | 83 | puts(line); |
| 91 | exit(0); | ||
| 92 | } | 84 | } |
| 93 | |||
| 94 | nmatches++; | ||
| 95 | print_matched_line(line, linenum); | ||
| 96 | } | 85 | } |
| 97 | |||
| 98 | free(line); | 86 | free(line); |
| 99 | } | 87 | } |
| 100 | 88 | ||
| @@ -186,8 +174,5 @@ extern int grep_main(int argc, char **argv) | |||
| 186 | 174 | ||
| 187 | regfree(®ex); | 175 | regfree(®ex); |
| 188 | 176 | ||
| 189 | if (!matched) | 177 | return !matched; /* invert return value 0 = success, 1 = failed */ |
| 190 | return 1; | ||
| 191 | |||
| 192 | return 0; | ||
| 193 | } | 178 | } |
| @@ -48,19 +48,6 @@ static int matched; /* keeps track of whether we ever matched */ | |||
| 48 | static char *cur_file = NULL; /* the current file we are reading */ | 48 | static char *cur_file = NULL; /* the current file we are reading */ |
| 49 | 49 | ||
| 50 | 50 | ||
| 51 | static void print_matched_line(char *line, int linenum) | ||
| 52 | { | ||
| 53 | if (print_count_only) | ||
| 54 | return; | ||
| 55 | |||
| 56 | if (print_filename) | ||
| 57 | printf("%s:", cur_file); | ||
| 58 | if (print_line_num) | ||
| 59 | printf("%i:", linenum); | ||
| 60 | |||
| 61 | puts(line); | ||
| 62 | } | ||
| 63 | |||
| 64 | static void grep_file(FILE *file) | 51 | static void grep_file(FILE *file) |
| 65 | { | 52 | { |
| 66 | char *line = NULL; | 53 | char *line = NULL; |
| @@ -72,7 +59,11 @@ static void grep_file(FILE *file) | |||
| 72 | chomp(line); | 59 | chomp(line); |
| 73 | linenum++; | 60 | linenum++; |
| 74 | ret = regexec(®ex, line, 0, NULL, 0); | 61 | ret = regexec(®ex, line, 0, NULL, 0); |
| 75 | if (ret == 0 && !invert_search) { /* match */ | 62 | |
| 63 | /* test for a postitive-assertion match (regexec returned success (0) | ||
| 64 | * and the user did not specify invert search), or a negative-assertion | ||
| 65 | * match (vice versa) */ | ||
| 66 | if ((ret == 0 && !invert_search) || (ret == REG_NOMATCH && invert_search)) { | ||
| 76 | 67 | ||
| 77 | /* if we found a match but were told to be quiet, stop here and | 68 | /* if we found a match but were told to be quiet, stop here and |
| 78 | * return success */ | 69 | * return success */ |
| @@ -81,20 +72,17 @@ static void grep_file(FILE *file) | |||
| 81 | exit(0); | 72 | exit(0); |
| 82 | } | 73 | } |
| 83 | 74 | ||
| 75 | /* otherwise, keep track of matches, print the matched line, and | ||
| 76 | * whatever else the user wanted */ | ||
| 84 | nmatches++; | 77 | nmatches++; |
| 85 | print_matched_line(line, linenum); | 78 | if (!print_count_only) { |
| 86 | 79 | if (print_filename) | |
| 87 | } | 80 | printf("%s:", cur_file); |
| 88 | else if (ret == REG_NOMATCH && invert_search) { | 81 | if (print_line_num) |
| 89 | if (be_quiet) { | 82 | printf("%i:", linenum); |
| 90 | regfree(®ex); | 83 | puts(line); |
| 91 | exit(0); | ||
| 92 | } | 84 | } |
| 93 | |||
| 94 | nmatches++; | ||
| 95 | print_matched_line(line, linenum); | ||
| 96 | } | 85 | } |
| 97 | |||
| 98 | free(line); | 86 | free(line); |
| 99 | } | 87 | } |
| 100 | 88 | ||
| @@ -186,8 +174,5 @@ extern int grep_main(int argc, char **argv) | |||
| 186 | 174 | ||
| 187 | regfree(®ex); | 175 | regfree(®ex); |
| 188 | 176 | ||
| 189 | if (!matched) | 177 | return !matched; /* invert return value 0 = success, 1 = failed */ |
| 190 | return 1; | ||
| 191 | |||
| 192 | return 0; | ||
| 193 | } | 178 | } |
