Consolodated some common code into print_matched_line to make 'grep_file' a

little more terse & clean. Also made a few minor formatting changes.
This commit is contained in:
Mark Whitley 2000-07-18 21:02:06 +00:00
parent a9d69fb568
commit 2ef880b236
2 changed files with 22 additions and 20 deletions

View File

@ -48,6 +48,9 @@ static char *cur_file = NULL; /* the current file we are reading */
static void print_matched_line(char *line, int linenum) static void print_matched_line(char *line, int linenum)
{ {
if (print_count_only)
return;
if (print_filename) if (print_filename)
printf("%s:", cur_file); printf("%s:", cur_file);
if (print_line_num) if (print_line_num)
@ -75,16 +78,12 @@ static void grep_file(FILE *file)
} }
nmatches++; nmatches++;
print_matched_line(line, linenum);
if (!print_count_only) }
print_matched_line(line, linenum); else if (ret == REG_NOMATCH && invert_search) {
} else if (ret == REG_NOMATCH && invert_search) {
nmatches++; nmatches++;
print_matched_line(line, linenum);
if (!print_count_only)
print_matched_line(line, linenum);
} }
free(line); free(line);
@ -161,7 +160,8 @@ extern int grep_main(int argc, char **argv)
* stdin. Otherwise, we grep through all the files specified. */ * stdin. Otherwise, we grep through all the files specified. */
if (argv[optind+1] == NULL || (strcmp(argv[optind+1], "-") == 0)) { if (argv[optind+1] == NULL || (strcmp(argv[optind+1], "-") == 0)) {
grep_file(stdin); grep_file(stdin);
} else { }
else {
int i; int i;
FILE *file; FILE *file;
for (i = optind + 1; i < argc; i++) { for (i = optind + 1; i < argc; i++) {
@ -170,7 +170,8 @@ extern int grep_main(int argc, char **argv)
if (file == NULL) { if (file == NULL) {
if (!suppress_err_msgs) if (!suppress_err_msgs)
errorMsg("%s: %s\n", cur_file, strerror(errno)); errorMsg("%s: %s\n", cur_file, strerror(errno));
} else { }
else {
grep_file(file); grep_file(file);
fclose(file); fclose(file);
} }

21
grep.c
View File

@ -48,6 +48,9 @@ static char *cur_file = NULL; /* the current file we are reading */
static void print_matched_line(char *line, int linenum) static void print_matched_line(char *line, int linenum)
{ {
if (print_count_only)
return;
if (print_filename) if (print_filename)
printf("%s:", cur_file); printf("%s:", cur_file);
if (print_line_num) if (print_line_num)
@ -75,16 +78,12 @@ static void grep_file(FILE *file)
} }
nmatches++; nmatches++;
print_matched_line(line, linenum);
if (!print_count_only) }
print_matched_line(line, linenum); else if (ret == REG_NOMATCH && invert_search) {
} else if (ret == REG_NOMATCH && invert_search) {
nmatches++; nmatches++;
print_matched_line(line, linenum);
if (!print_count_only)
print_matched_line(line, linenum);
} }
free(line); free(line);
@ -161,7 +160,8 @@ extern int grep_main(int argc, char **argv)
* stdin. Otherwise, we grep through all the files specified. */ * stdin. Otherwise, we grep through all the files specified. */
if (argv[optind+1] == NULL || (strcmp(argv[optind+1], "-") == 0)) { if (argv[optind+1] == NULL || (strcmp(argv[optind+1], "-") == 0)) {
grep_file(stdin); grep_file(stdin);
} else { }
else {
int i; int i;
FILE *file; FILE *file;
for (i = optind + 1; i < argc; i++) { for (i = optind + 1; i < argc; i++) {
@ -170,7 +170,8 @@ extern int grep_main(int argc, char **argv)
if (file == NULL) { if (file == NULL) {
if (!suppress_err_msgs) if (!suppress_err_msgs)
errorMsg("%s: %s\n", cur_file, strerror(errno)); errorMsg("%s: %s\n", cur_file, strerror(errno));
} else { }
else {
grep_file(file); grep_file(file);
fclose(file); fclose(file);
} }