[hackers] [sbase] grep: add -H flag || Hiltjo Posthuma

From: <git_AT_suckless.org>
Date: Sun, 16 Nov 2014 12:23:23 +0100

commit 865869fb281e39acf38fa14f70284e8306ced894
Author: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Sun Nov 16 11:45:10 2014 +0100

    grep: add -H flag

diff --git a/grep.1 b/grep.1
index f6c4d54..8fad4e5 100644
--- a/grep.1
+++ b/grep.1
_AT_@ -3,7 +3,7 @@
 grep \- search files for a pattern
 .SH SYNOPSIS
 .B grep
-.RB [ \-Ecilnqv ]
+.RB [ \-EHcilnqv ]
 .RB [ \-e
 .I pattern ]
 .I pattern
_AT_@ -24,6 +24,10 @@ status code is 2.
 .B \-E
 matches using extended regex.
 .TP
+.B \-H
+prefixes each matching line with its filename in the output. This is the
+default when there is more than one file specified.
+.TP
 .B \-c
 prints only a count of matching lines.
 .TP
diff --git a/grep.c b/grep.c
index edc565a..c0f5856 100644
--- a/grep.c
+++ b/grep.c
_AT_@ -15,6 +15,7 @@ static int grep(FILE *, const char *);
 
 static int eflag = 0;
 static int vflag = 0;
+static int Hflag = 0;
 static int many;
 static char mode = 0;
 
_AT_@ -27,7 +28,7 @@ static struct plist {
 static void
 usage(void)
 {
- enprintf(Error, "usage: %s [-Ecilnqv] [-e pattern] pattern [files...]\n", argv0);
+ enprintf(Error, "usage: %s [-EHcilnqv] [-e pattern] pattern [files...]\n", argv0);
 }
 
 int
_AT_@ -42,6 +43,9 @@ main(int argc, char *argv[])
         case 'E':
                 flags |= REG_EXTENDED;
                 break;
+ case 'H':
+ Hflag = 1;
+ break;
         case 'e':
                 addpattern(EARGF(usage()));
                 eflag = 1;
_AT_@ -143,7 +147,7 @@ grep(FILE *fp, const char *str)
                         case 'q':
                                 exit(Match);
                         default:
- if (many)
+ if (many || Hflag)
                                         printf("%s:", str);
                                 if (mode == 'n')
                                         printf("%ld:", n);
Received on Sun Nov 16 2014 - 12:23:23 CET

This archive was generated by hypermail 2.3.0 : Sun Nov 16 2014 - 12:24:09 CET