Fix handling of ^$ by removing the newline from input lines and by not

compiling with REG_NEWLINE.
This commit is contained in:
Matt Kraai 2000-10-13 18:55:06 +00:00
parent 9a6e67c960
commit 567cdd1d51
2 changed files with 8 additions and 4 deletions

View File

@ -56,7 +56,7 @@ static void print_matched_line(char *line, int linenum)
if (print_line_num) if (print_line_num)
printf("%i:", linenum); printf("%i:", linenum);
printf("%s", line); puts(line);
} }
static void grep_file(FILE *file) static void grep_file(FILE *file)
@ -67,6 +67,8 @@ static void grep_file(FILE *file)
int nmatches = 0; int nmatches = 0;
while ((line = get_line_from_file(file)) != NULL) { while ((line = get_line_from_file(file)) != NULL) {
if (line[strlen(line)-1] == '\n')
line[strlen(line)-1] = '\0';
linenum++; linenum++;
ret = regexec(&regex, line, 0, NULL, 0); ret = regexec(&regex, line, 0, NULL, 0);
if (ret == 0 && !invert_search) { /* match */ if (ret == 0 && !invert_search) { /* match */
@ -144,7 +146,7 @@ extern int grep_main(int argc, char **argv)
/* compile the regular expression /* compile the regular expression
* we're not going to mess with sub-expressions, and we need to * we're not going to mess with sub-expressions, and we need to
* treat newlines right. */ * treat newlines right. */
reflags = REG_NOSUB | REG_NEWLINE; reflags = REG_NOSUB;
if (ignore_case) if (ignore_case)
reflags |= REG_ICASE; reflags |= REG_ICASE;
xregcomp(&regex, argv[optind], reflags); xregcomp(&regex, argv[optind], reflags);

6
grep.c
View File

@ -56,7 +56,7 @@ static void print_matched_line(char *line, int linenum)
if (print_line_num) if (print_line_num)
printf("%i:", linenum); printf("%i:", linenum);
printf("%s", line); puts(line);
} }
static void grep_file(FILE *file) static void grep_file(FILE *file)
@ -67,6 +67,8 @@ static void grep_file(FILE *file)
int nmatches = 0; int nmatches = 0;
while ((line = get_line_from_file(file)) != NULL) { while ((line = get_line_from_file(file)) != NULL) {
if (line[strlen(line)-1] == '\n')
line[strlen(line)-1] = '\0';
linenum++; linenum++;
ret = regexec(&regex, line, 0, NULL, 0); ret = regexec(&regex, line, 0, NULL, 0);
if (ret == 0 && !invert_search) { /* match */ if (ret == 0 && !invert_search) { /* match */
@ -144,7 +146,7 @@ extern int grep_main(int argc, char **argv)
/* compile the regular expression /* compile the regular expression
* we're not going to mess with sub-expressions, and we need to * we're not going to mess with sub-expressions, and we need to
* treat newlines right. */ * treat newlines right. */
reflags = REG_NOSUB | REG_NEWLINE; reflags = REG_NOSUB;
if (ignore_case) if (ignore_case)
reflags |= REG_ICASE; reflags |= REG_ICASE;
xregcomp(&regex, argv[optind], reflags); xregcomp(&regex, argv[optind], reflags);