[hackers] [sbase] grep: Remove trailing newline before trying to match RE || Robert Ransom

From: <hg_AT_suckless.org>
Date: Tue, 22 May 2012 13:08:55 +0200 (CEST)

changeset: 135:c69bda5d1bc2
user: Robert Ransom <rransom.8774_AT_gmail.com>
date: Tue May 22 00:05:03 2012 +0000
files: grep.c
description:
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).


diff -r f0e37e63335c -r c69bda5d1bc2 grep.c
--- a/grep.c Mon May 21 23:46:22 2012 +0000
+++ b/grep.c Tue May 22 00:05:03 2012 +0000
_AT_@ -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"
_AT_@ -70,9 +71,11 @@
 {
         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) {
_AT_@ -89,7 +92,7 @@
                                 printf("%s:", str);
                         if(mode == 'n')
                                 printf("%ld:", n);
- fputs(buf, stdout);
+ printf("%s\n", buf);
                         break;
                 }
                 match = true;
Received on Tue May 22 2012 - 13:08:55 CEST

This archive was generated by hypermail 2.3.0 : Tue May 22 2012 - 13:12:19 CEST