[dev] [sbase/grep] [PATCH]: add z flag to use NULL separators
Not sure which would be better choice of flag: 'z' or '0', so I
arbitrarily chose 'z'.
commit 474a73ae118e6791fc56e616233dd9ccb5c8e92f
Author: strake <strake888_AT_gmail.com>
Date: Thu Oct 4 19:50:23 2012 -0500
grep: add z flag to use NULL separators
diff --git a/grep.c b/grep.c
index 9716328..48a54c0 100644
--- a/grep.c
+++ b/grep.c
_AT_@ -18,6 +18,8 @@ static bool many;
static bool match = false;
static char mode = 0;
+static char delim = '\n';
+
int
main(int argc, char *argv[])
{
_AT_@ -41,6 +43,9 @@ main(int argc, char *argv[])
case 'v':
vflag = true;
break;
+ case 'z':
+ delim = '\0';
+ break;
default:
usage();
} ARGEND;
_AT_@ -73,7 +78,7 @@ grep(FILE *fp, const char *str, regex_t *preg)
long n, c = 0;
size_t size = 0, len;
- for(n = 1; afgets(&buf, &size, fp); n++) {
+ for(n = 1; getdelim(&buf, &size, delim, fp) >= 0; n++) {
if(buf[(len = strlen(buf))-1] == '\n')
buf[--len] = '\0';
if(regexec(preg, buf, 0, NULL, 0) ^ vflag)
_AT_@ -92,7 +97,7 @@ grep(FILE *fp, const char *str, regex_t *preg)
printf("%s:", str);
if(mode == 'n')
printf("%ld:", n);
- printf("%s\n", buf);
+ printf("%s%c", buf, delim);
break;
}
match = true;
Received on Sun Oct 07 2012 - 18:27:55 CEST
This archive was generated by hypermail 2.3.0
: Sun Oct 07 2012 - 18:36:05 CEST