sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit e6238dda31b4a82e3806c51f809c3d1c657bfcdc
parent 8ed28e50e44a5ccdbe263b5dbc0e415811e7d0f0
Author: Robert Ransom <rransom.8774@gmail.com>
Date:   Tue, 22 May 2012 00:05:03 +0000

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).
Diffstat:
Mgrep.c | 7+++++--
1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/grep.c b/grep.c @@ -3,6 +3,7 @@ #include <stdbool.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <unistd.h> #include "text.h" #include "util.h" @@ -70,9 +71,11 @@ grep(FILE *fp, const char *str, regex_t *preg) { char *buf = NULL; long n, c = 0; - size_t size = 0; + size_t size = 0, len; 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) continue; switch(mode) { @@ -89,7 +92,7 @@ grep(FILE *fp, const char *str, regex_t *preg) printf("%s:", str); if(mode == 'n') printf("%ld:", n); - fputs(buf, stdout); + printf("%s\n", buf); break; } match = true;