summaryrefslogtreecommitdiff
path: root/grep.c
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2022-04-21 09:25:44 -0400
committerJakob Kaivo <jkk@ung.org>2022-04-21 09:25:44 -0400
commit7865aed19c65e11c98e6b8174c9e27dbeb8f67ab (patch)
treea807daacf52beb8ea2d7403d4f372dfffa1848da /grep.c
parentdb7aadfa489566529abd3e9d330494b246be3797 (diff)
implement -s
Diffstat (limited to 'grep.c')
-rw-r--r--grep.c27
1 files changed, 12 insertions, 15 deletions
diff --git a/grep.c b/grep.c
index 344921f..16f80eb 100644
--- a/grep.c
+++ b/grep.c
@@ -43,8 +43,8 @@ enum { NORMAL, COUNT, LIST, QUIET } grep_display = NORMAL;
static int grep_filenames = 0;
static int grep_linenumbers = 0;
static int grep_inverse = 0;
+static int grep_silent = 0;
-#define SUPPRESS 1 << 2
#define WHOLELINE 1 << 4
#define FILENAMES 1 << 5
@@ -66,18 +66,16 @@ static int grep_match(struct grep_list *head, const char *buf)
return grep_inverse;
}
-static uintmax_t grep(struct grep_list *head, const char *path, int flags)
+static uintmax_t grep(struct grep_list *head, const char *path)
{
- (void)flags;
-
FILE *f = stdin;
if (path && strcmp(path, "-")) {
f = fopen(path, "r");
if (f == NULL) {
- if (/*!suppress ||*/ (errno != ENOENT && errno != EPERM)) {
+ if (!grep_silent || (errno != ENOENT && errno != EPERM)) {
fprintf(stderr, "grep: %s: %s\n", path, strerror(errno));
}
- return 0;
+ return 2;
}
}
@@ -118,7 +116,7 @@ static uintmax_t grep(struct grep_list *head, const char *path, int flags)
printf("%s\n", path);
}
- return found;
+ return found == 0;
}
static struct grep_list * grep_add_list(struct grep_list *head, char *s)
@@ -216,7 +214,7 @@ int main(int argc, char *argv[])
break;
case 's':
- //flags |= SUPPRESS;
+ grep_silent = 1;
break;
case 'v':
@@ -258,14 +256,13 @@ int main(int argc, char *argv[])
grep_filenames = (argc > optind + 1);
- uintmax_t found = 0;
+ int ret = 1;
do {
- found += grep(head, argv[optind++], flags);
+ int r = grep(head, argv[optind++]);
+ if (r != 1) {
+ ret = r;
+ }
} while (optind < argc);
- if (found) {
- return 0;
- }
-
- return 1;
+ return ret;
}