[hackers] [sbase] grep: Remove newlines before matching a line || Michael Forney

From: <git_AT_suckless.org>
Date: Sun, 02 Nov 2014 10:46:51 +0100

commit 1ca8a314f80274d06c887aa347ecb142487a946d
Author: Michael Forney <mforney_AT_mforney.org>
Date: Sun Nov 2 03:08:12 2014 +0000

    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>"

diff --git a/grep.c b/grep.c
index 715f864..1376513 100644
--- a/grep.c
+++ b/grep.c
_AT_@ -129,6 +129,9 @@ grep(FILE *fp, const char *str)
         int match = NoMatch;
 
         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) {
                         if(regexec(&pnode->preg, buf, 0, NULL, 0) ^ vflag)
                                 continue;
_AT_@ -146,9 +149,7 @@ grep(FILE *fp, const char *str)
                                         printf("%s:", str);
                                 if(mode == 'n')
                                         printf("%ld:", n);
- printf("%s", buf);
- if(len && buf[len - 1] != '\n')
- putchar('\n');
+ puts(buf);
                                 break;
                         }
                         match = Match;
Received on Sun Nov 02 2014 - 10:46:51 CET

This archive was generated by hypermail 2.3.0 : Sun Nov 02 2014 - 10:48:13 CET