commit: 6536efccc9f5401955645024fc2142e5edb08b05
parent 00344d108e893926e2aeb7a72fcc0d027081785c
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Tue, 7 May 2024 21:59:20 +0200
grep-stub.c: Return non-zero status when nothing was found
Diffstat:
1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/grep-stub.c b/grep-stub.c
@@ -33,6 +33,8 @@ static bool multiple_files = false;
static char *(*find_substring)(const char *, const char *) = &strstr;
+static bool found_any = false;
+
static int
do_grep(FILE *stream, char *filename)
{
@@ -76,6 +78,7 @@ do_grep(FILE *stream, char *filename)
if(found != invert)
{
+ found_any = true;
count++;
if(!quiet)
{
@@ -216,7 +219,7 @@ main(int argc, char *argv[])
{
fprintf(stderr, "%s: No pattern given\n", argv0);
usage();
- return 1;
+ return 2;
}
patterns = &(struct char_list){
@@ -238,7 +241,7 @@ main(int argc, char *argv[])
if(patterns_r == NULL)
{
fprintf(stderr, "%s: Failed allocating memory for patterns: %s\n", argv0, strerror(errno));
- return 1;
+ return 2;
}
for(struct char_list *pattern = patterns; pattern != NULL; pattern = pattern->next)
@@ -260,7 +263,7 @@ main(int argc, char *argv[])
if(argc == 0)
{
- if(do_grep(stdin, "(standard input)") < 0) return 1;
+ if(do_grep(stdin, "(standard input)") < 0) return 2;
goto cleanup;
}
@@ -276,10 +279,12 @@ main(int argc, char *argv[])
goto cleanup;
}
- if(do_grep(stream, argv[i]) < 0) return 1;
+ if(do_grep(stream, argv[i]) < 0) return 2;
}
cleanup:
+ if(!found_any) err++;
+
for(size_t i = 0; i < patterns_r_len; i++)
{
regfree(&patterns_r[i]);