grep: Remove trailing newline before trying to match RE
This unbreaks the "$" operator and some uses of grep with other REs (e.g. 'grep . TODO' to remove empty lines).
This commit is contained in:
parent
8ed28e50e4
commit
e6238dda31
7
grep.c
7
grep.c
|
@ -3,6 +3,7 @@
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "text.h"
|
#include "text.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
@ -70,9 +71,11 @@ grep(FILE *fp, const char *str, regex_t *preg)
|
||||||
{
|
{
|
||||||
char *buf = NULL;
|
char *buf = NULL;
|
||||||
long n, c = 0;
|
long n, c = 0;
|
||||||
size_t size = 0;
|
size_t size = 0, len;
|
||||||
|
|
||||||
for(n = 1; afgets(&buf, &size, fp); n++) {
|
for(n = 1; afgets(&buf, &size, fp); n++) {
|
||||||
|
if(buf[(len = strlen(buf))-1] == '\n')
|
||||||
|
buf[--len] = '\0';
|
||||||
if(regexec(preg, buf, 0, NULL, 0) ^ vflag)
|
if(regexec(preg, buf, 0, NULL, 0) ^ vflag)
|
||||||
continue;
|
continue;
|
||||||
switch(mode) {
|
switch(mode) {
|
||||||
|
@ -89,7 +92,7 @@ grep(FILE *fp, const char *str, regex_t *preg)
|
||||||
printf("%s:", str);
|
printf("%s:", str);
|
||||||
if(mode == 'n')
|
if(mode == 'n')
|
||||||
printf("%ld:", n);
|
printf("%ld:", n);
|
||||||
fputs(buf, stdout);
|
printf("%s\n", buf);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
match = true;
|
match = true;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user