[hackers] [sbase] Properly handle multiline patterns in grep(1) || sin

From: <git_AT_suckless.org>
Date: Thu, 20 Nov 2014 20:56:49 +0100

commit 8767e4b32060e59432199cefa0e0b626ce9d7adb
Author: sin <sin_AT_2f30.org>
Date: Thu Nov 20 19:56:18 2014 +0000

    Properly handle multiline patterns in grep(1)
    
    We should be POSIX compliant now.

diff --git a/grep.c b/grep.c
index c6dcbf6..1b89242 100644
--- a/grep.c
+++ b/grep.c
_AT_@ -12,7 +12,7 @@
 enum { Match = 0, NoMatch = 1, Error = 2 };
 
 static void addpattern(const char *);
-static void addpatternfile(const char *);
+static void addpatternfile(FILE *);
 static int grep(FILE *, const char *);
 
 static int Fflag;
_AT_@ -47,6 +47,7 @@ main(int argc, char *argv[])
         struct pattern *pnode;
         int i, m, flags = REG_NOSUB, match = NoMatch;
         FILE *fp;
+ char *arg;
 
         SLIST_INIT(&phead);
 
_AT_@ -61,11 +62,19 @@ main(int argc, char *argv[])
                 Hflag = 1;
                 break;
         case 'e':
- addpattern(EARGF(usage()));
+ arg = EARGF(usage());
+ fp = fmemopen(arg, strlen(arg) + 1, "r");
+ addpatternfile(fp);
+ fclose(fp);
                 eflag = 1;
                 break;
         case 'f':
- addpatternfile(EARGF(usage()));
+ arg = EARGF(usage());
+ fp = fopen(arg, "r");
+ if (!fp)
+ enprintf(Error, "fopen %s:", arg);
+ addpatternfile(fp);
+ fclose(fp);
                 fflag = 1;
                 break;
         case 'h':
_AT_@ -99,7 +108,9 @@ main(int argc, char *argv[])
 
         /* just add literal pattern to list */
         if (!eflag && !fflag) {
- addpattern(argv[0]);
+ fp = fmemopen(argv[0], strlen(argv[0]) + 1, "r");
+ addpatternfile(fp);
+ fclose(fp);
                 argc--;
                 argv++;
         }
_AT_@ -163,22 +174,17 @@ addpattern(const char *pattern)
 }
 
 static void
-addpatternfile(const char *file)
+addpatternfile(FILE *fp)
 {
- FILE *fp;
         char *buf = NULL;
         size_t len = 0, size = 0;
 
- fp = fopen(file, "r");
- if (!fp)
- enprintf(Error, "fopen %s:", file);
         while ((len = getline(&buf, &size, fp)) != -1) {
                 if (len && buf[len - 1] == '\n')
                         buf[len - 1] = '\0';
                 addpattern(buf);
         }
         free(buf);
- fclose(fp);
 }
 
 static int
Received on Thu Nov 20 2014 - 20:56:49 CET

This archive was generated by hypermail 2.3.0 : Thu Nov 20 2014 - 21:00:11 CET