commit: 88628095c1c462fa7423e527cfde3b2579ffdf90
parent 0d667ad53ea808e6eb097abdec95b65c2fa1c517
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 3 May 2024 05:47:28 +0200
grep-stub.c: proper output
Diffstat:
1 file changed, 14 insertions(+), 7 deletions(-)
diff --git a/grep-stub.c b/grep-stub.c
@@ -21,25 +21,28 @@ struct char_list
struct char_list *next;
};
-struct char_list *patterns;
-struct char_list *files;
+static struct char_list *patterns;
+static struct char_list *files;
-size_t patterns_r_len = 0;
-regex_t *patterns_r = NULL;
+static size_t patterns_r_len = 0;
+static regex_t *patterns_r = NULL;
-bool fixed_str = false, invert = false, count_only = false;
+static bool fixed_str = false, invert = false, count_only = false, opt_n = false;
+static bool multiple_files = false;
static int
do_grep(FILE *stream, char *filename)
{
char *line = NULL;
size_t len = 0;
+ size_t ln = 0;
int count = 0;
ssize_t nread = 0;
while((nread = getline(&line, &len, stream)) != -1)
{
+ ln++;
if(line[nread-1] == '\n') line[nread-1] = 0;
bool found = false;
@@ -72,6 +75,8 @@ do_grep(FILE *stream, char *filename)
if(found != invert)
{
count++;
+ if(multiple_files) printf("%s:", filename);
+ if(opt_n) printf("%zd:", ln);
if(!count_only) printf("%s\n", line);
}
}
@@ -101,7 +106,7 @@ int
main(int argc, char *argv[])
{
argv0 = argv[0];
- bool opt_l = false, nl = false, quiet = false;
+ bool opt_l = false, quiet = false;
int regcomp_flags = REG_NOSUB;
struct char_list *patterns_last = NULL;
@@ -179,7 +184,7 @@ main(int argc, char *argv[])
opt_l = true;
break;
case 'n':
- nl = true;
+ opt_n = true;
break;
case 'q':
quiet = true;
@@ -253,6 +258,8 @@ main(int argc, char *argv[])
goto cleanup;
}
+ if(argc > 1) multiple_files = true;
+
for(int i = 0; i < argc; i++)
{
FILE *stream = fopen(argv[i], "r");