From 1336ea5512afb81bd9de75a3062ccca40c8b9dbb Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Mon, 10 Sep 2018 15:22:02 +0200 Subject: magrep: add -h, which is like -p but doesn't print the file name. --- magrep.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'magrep.c') diff --git a/magrep.c b/magrep.c index 83e1c49..44ef548 100644 --- a/magrep.c +++ b/magrep.c @@ -14,6 +14,7 @@ static int aflag; static int cflag; static int dflag; +static int hflag; static int iflag; static int lflag; static int oflag; @@ -42,6 +43,8 @@ match(char *file, char *hdr, char *s) } if (pflag) printf("%s: %s: ", file, hdr); + else if (hflag) + printf("%s: ", hdr); printf("%.*s\n", len, s); s += len; matched++; @@ -52,9 +55,12 @@ match(char *file, char *hdr, char *s) exit(0); matches++; if (!cflag) { - printf("%s", file); + if (vflag || !hflag) + printf("%s", file); if (pflag && !vflag) printf(": %s: %s", hdr, s); + else if (hflag && !vflag) + printf("%s: %s", hdr, s); putchar('\n'); } if (mflag && matches >= mflag) @@ -82,7 +88,7 @@ match_part(int depth, struct message *msg, char *body, size_t bodylen) strcasecmp(charset, "utf-8") == 0 || strcasecmp(charset, "utf8") == 0 || strcasecmp(charset, "us-ascii") == 0) { - if (pflag && !cflag && !oflag && !vflag) { + if ((hflag || pflag) && !cflag && !oflag && !vflag) { char *s, *p; for (p = s = body; p < body+bodylen+1; p++) { if (*p == '\r' || *p == '\n') { @@ -183,11 +189,12 @@ int main(int argc, char *argv[]) { int c; - while ((c = getopt(argc, argv, "acdilm:opqv")) != -1) + while ((c = getopt(argc, argv, "acdhilm:opqv")) != -1) switch (c) { case 'a': aflag = 1; break; case 'c': cflag = 1; break; case 'd': dflag = 1; break; + case 'h': hflag = 1; break; case 'i': iflag = REG_ICASE; break; case 'l': lflag = 1; break; case 'm': mflag = atol(optarg); break; @@ -198,7 +205,7 @@ main(int argc, char *argv[]) default: usage: fprintf(stderr, -"Usage: magrep [-c|-o|-p|-q|-m max] [-v] [-i] [-l] [-a|-d] header:regex [msgs...]\n"); +"Usage: magrep [-c|-h|-o|-p|-q|-m max] [-v] [-i] [-l] [-a|-d] header:regex [msgs...]\n"); exit(2); } -- cgit 1.4.1