Implement grep -s

This commit is contained in:
sin 2014-11-20 14:14:26 +00:00
parent 5197affac5
commit 728f36aa77
2 changed files with 16 additions and 8 deletions

5
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 [ \-EHchilnqv ] .RB [ \-EHchilnqsv ]
.RB [ \-e .RB [ \-e
.I pattern ] .I pattern ]
.I pattern .I pattern
@ -53,6 +53,9 @@ prefixes each matching line with its line number in the input.
.B \-q .B \-q
prints nothing, only returns status. prints nothing, only returns status.
.TP .TP
.B \-s
Suppress the error messages ordinarily written for nonexistent or unreadable files.
.TP
.B \-v .B \-v
selects lines which do selects lines which do
.B not .B not

19
grep.c
View File

@ -14,12 +14,13 @@ enum { Match = 0, NoMatch = 1, Error = 2 };
static void addpattern(const char *); static void addpattern(const char *);
static int grep(FILE *, const char *); static int grep(FILE *, const char *);
static int eflag = 0; static int Hflag;
static int vflag = 0; static int eflag;
static int hflag = 0; static int hflag;
static int Hflag = 0; static int sflag;
static int vflag;
static int many; static int many;
static char mode = 0; static char mode;
struct pattern { struct pattern {
char *pattern; char *pattern;
@ -32,7 +33,7 @@ static SLIST_HEAD(phead, pattern) phead;
static void static void
usage(void) usage(void)
{ {
enprintf(Error, "usage: %s [-EHcilnqv] [-e pattern] pattern [files...]\n", argv0); enprintf(Error, "usage: %s [-EHcilnqsv] [-e pattern] pattern [files...]\n", argv0);
} }
int int
@ -67,6 +68,9 @@ main(int argc, char *argv[])
case 'i': case 'i':
flags |= REG_ICASE; flags |= REG_ICASE;
break; break;
case 's':
sflag = 1;
break;
case 'v': case 'v':
vflag = 1; vflag = 1;
break; break;
@ -93,7 +97,8 @@ main(int argc, char *argv[])
} else { } else {
for (i = 0; i < argc; i++) { for (i = 0; i < argc; i++) {
if (!(fp = fopen(argv[i], "r"))) { if (!(fp = fopen(argv[i], "r"))) {
weprintf("fopen %s:", argv[i]); if (!sflag)
weprintf("fopen %s:", argv[i]);
match = Error; match = Error;
continue; continue;
} }