grep: add -H flag

This commit is contained in:
Hiltjo Posthuma 2014-11-16 11:45:10 +01:00 committed by sin
parent e17b9cdd0a
commit 865869fb28
2 changed files with 11 additions and 3 deletions

6
grep.1
View File

@ -3,7 +3,7 @@
grep \- search files for a pattern grep \- search files for a pattern
.SH SYNOPSIS .SH SYNOPSIS
.B grep .B grep
.RB [ \-Ecilnqv ] .RB [ \-EHcilnqv ]
.RB [ \-e .RB [ \-e
.I pattern ] .I pattern ]
.I pattern .I pattern
@ -24,6 +24,10 @@ status code is 2.
.B \-E .B \-E
matches using extended regex. matches using extended regex.
.TP .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 .B \-c
prints only a count of matching lines. prints only a count of matching lines.
.TP .TP

8
grep.c
View File

@ -15,6 +15,7 @@ static int grep(FILE *, const char *);
static int eflag = 0; static int eflag = 0;
static int vflag = 0; static int vflag = 0;
static int Hflag = 0;
static int many; static int many;
static char mode = 0; static char mode = 0;
@ -27,7 +28,7 @@ static struct plist {
static void static void
usage(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 int
@ -42,6 +43,9 @@ main(int argc, char *argv[])
case 'E': case 'E':
flags |= REG_EXTENDED; flags |= REG_EXTENDED;
break; break;
case 'H':
Hflag = 1;
break;
case 'e': case 'e':
addpattern(EARGF(usage())); addpattern(EARGF(usage()));
eflag = 1; eflag = 1;
@ -143,7 +147,7 @@ grep(FILE *fp, const char *str)
case 'q': case 'q':
exit(Match); exit(Match);
default: default:
if (many) if (many || Hflag)
printf("%s:", str); printf("%s:", str);
if (mode == 'n') if (mode == 'n')
printf("%ld:", n); printf("%ld:", n);