grep: Remove newlines before matching a line
Otherwise, a pattern with a '$' anchor will never match and POSIX says that "By default, an input line shall be selected if any pattern ... matches any part of the line excluding the terminating <newline>"
This commit is contained in:
parent
7ed4866556
commit
1ca8a314f8
7
grep.c
7
grep.c
|
@ -129,6 +129,9 @@ grep(FILE *fp, const char *str)
|
||||||
int match = NoMatch;
|
int match = NoMatch;
|
||||||
|
|
||||||
for(n = 1; (len = agetline(&buf, &size, fp)) != -1; n++) {
|
for(n = 1; (len = agetline(&buf, &size, fp)) != -1; n++) {
|
||||||
|
/* Remove the trailing newline if one is present. */
|
||||||
|
if (len && buf[len - 1] == '\n')
|
||||||
|
buf[len - 1] = '\0';
|
||||||
for(pnode = phead; pnode; pnode = pnode->next) {
|
for(pnode = phead; pnode; pnode = pnode->next) {
|
||||||
if(regexec(&pnode->preg, buf, 0, NULL, 0) ^ vflag)
|
if(regexec(&pnode->preg, buf, 0, NULL, 0) ^ vflag)
|
||||||
continue;
|
continue;
|
||||||
|
@ -146,9 +149,7 @@ grep(FILE *fp, const char *str)
|
||||||
printf("%s:", str);
|
printf("%s:", str);
|
||||||
if(mode == 'n')
|
if(mode == 'n')
|
||||||
printf("%ld:", n);
|
printf("%ld:", n);
|
||||||
printf("%s", buf);
|
puts(buf);
|
||||||
if(len && buf[len - 1] != '\n')
|
|
||||||
putchar('\n');
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
match = Match;
|
match = Match;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user