Tightened things up a bit, saved 20 bytes, and made the code a bit clearer in
the process.
This commit is contained in:
parent
065c7e779a
commit
b5c29850e3
@ -48,19 +48,6 @@ static int matched; /* keeps track of whether we ever matched */
|
|||||||
static char *cur_file = NULL; /* the current file we are reading */
|
static char *cur_file = NULL; /* the current file we are reading */
|
||||||
|
|
||||||
|
|
||||||
static void print_matched_line(char *line, int linenum)
|
|
||||||
{
|
|
||||||
if (print_count_only)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (print_filename)
|
|
||||||
printf("%s:", cur_file);
|
|
||||||
if (print_line_num)
|
|
||||||
printf("%i:", linenum);
|
|
||||||
|
|
||||||
puts(line);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void grep_file(FILE *file)
|
static void grep_file(FILE *file)
|
||||||
{
|
{
|
||||||
char *line = NULL;
|
char *line = NULL;
|
||||||
@ -72,7 +59,11 @@ static void grep_file(FILE *file)
|
|||||||
chomp(line);
|
chomp(line);
|
||||||
linenum++;
|
linenum++;
|
||||||
ret = regexec(®ex, line, 0, NULL, 0);
|
ret = regexec(®ex, line, 0, NULL, 0);
|
||||||
if (ret == 0 && !invert_search) { /* match */
|
|
||||||
|
/* test for a postitive-assertion match (regexec returned success (0)
|
||||||
|
* and the user did not specify invert search), or a negative-assertion
|
||||||
|
* match (vice versa) */
|
||||||
|
if ((ret == 0 && !invert_search) || (ret == REG_NOMATCH && invert_search)) {
|
||||||
|
|
||||||
/* if we found a match but were told to be quiet, stop here and
|
/* if we found a match but were told to be quiet, stop here and
|
||||||
* return success */
|
* return success */
|
||||||
@ -81,20 +72,17 @@ static void grep_file(FILE *file)
|
|||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* otherwise, keep track of matches, print the matched line, and
|
||||||
|
* whatever else the user wanted */
|
||||||
nmatches++;
|
nmatches++;
|
||||||
print_matched_line(line, linenum);
|
if (!print_count_only) {
|
||||||
|
if (print_filename)
|
||||||
}
|
printf("%s:", cur_file);
|
||||||
else if (ret == REG_NOMATCH && invert_search) {
|
if (print_line_num)
|
||||||
if (be_quiet) {
|
printf("%i:", linenum);
|
||||||
regfree(®ex);
|
puts(line);
|
||||||
exit(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
nmatches++;
|
|
||||||
print_matched_line(line, linenum);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free(line);
|
free(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -186,8 +174,5 @@ extern int grep_main(int argc, char **argv)
|
|||||||
|
|
||||||
regfree(®ex);
|
regfree(®ex);
|
||||||
|
|
||||||
if (!matched)
|
return !matched; /* invert return value 0 = success, 1 = failed */
|
||||||
return 1;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
43
grep.c
43
grep.c
@ -48,19 +48,6 @@ static int matched; /* keeps track of whether we ever matched */
|
|||||||
static char *cur_file = NULL; /* the current file we are reading */
|
static char *cur_file = NULL; /* the current file we are reading */
|
||||||
|
|
||||||
|
|
||||||
static void print_matched_line(char *line, int linenum)
|
|
||||||
{
|
|
||||||
if (print_count_only)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (print_filename)
|
|
||||||
printf("%s:", cur_file);
|
|
||||||
if (print_line_num)
|
|
||||||
printf("%i:", linenum);
|
|
||||||
|
|
||||||
puts(line);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void grep_file(FILE *file)
|
static void grep_file(FILE *file)
|
||||||
{
|
{
|
||||||
char *line = NULL;
|
char *line = NULL;
|
||||||
@ -72,7 +59,11 @@ static void grep_file(FILE *file)
|
|||||||
chomp(line);
|
chomp(line);
|
||||||
linenum++;
|
linenum++;
|
||||||
ret = regexec(®ex, line, 0, NULL, 0);
|
ret = regexec(®ex, line, 0, NULL, 0);
|
||||||
if (ret == 0 && !invert_search) { /* match */
|
|
||||||
|
/* test for a postitive-assertion match (regexec returned success (0)
|
||||||
|
* and the user did not specify invert search), or a negative-assertion
|
||||||
|
* match (vice versa) */
|
||||||
|
if ((ret == 0 && !invert_search) || (ret == REG_NOMATCH && invert_search)) {
|
||||||
|
|
||||||
/* if we found a match but were told to be quiet, stop here and
|
/* if we found a match but were told to be quiet, stop here and
|
||||||
* return success */
|
* return success */
|
||||||
@ -81,20 +72,17 @@ static void grep_file(FILE *file)
|
|||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* otherwise, keep track of matches, print the matched line, and
|
||||||
|
* whatever else the user wanted */
|
||||||
nmatches++;
|
nmatches++;
|
||||||
print_matched_line(line, linenum);
|
if (!print_count_only) {
|
||||||
|
if (print_filename)
|
||||||
}
|
printf("%s:", cur_file);
|
||||||
else if (ret == REG_NOMATCH && invert_search) {
|
if (print_line_num)
|
||||||
if (be_quiet) {
|
printf("%i:", linenum);
|
||||||
regfree(®ex);
|
puts(line);
|
||||||
exit(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
nmatches++;
|
|
||||||
print_matched_line(line, linenum);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free(line);
|
free(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -186,8 +174,5 @@ extern int grep_main(int argc, char **argv)
|
|||||||
|
|
||||||
regfree(®ex);
|
regfree(®ex);
|
||||||
|
|
||||||
if (!matched)
|
return !matched; /* invert return value 0 = success, 1 = failed */
|
||||||
return 1;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user