diff --git a/usr.bin/what/what.1 b/usr.bin/what/what.1 index 59cb2815cf66..fff062ab1a33 100644 --- a/usr.bin/what/what.1 +++ b/usr.bin/what/what.1 @@ -41,7 +41,7 @@ .Nd "show what versions of object modules were used to construct a file" .Sh SYNOPSIS .Nm -.Op Fl s +.Op Fl qs .Op Ar .Sh DESCRIPTION The @@ -59,6 +59,8 @@ character, or backslash. .Pp The following option is available: .Bl -tag -width Ds +.It Fl q +Only output the match text, rather than formatting it. .It Fl s Stop searching each file after the first match. .El @@ -72,6 +74,11 @@ The .Nm utility conforms to .St -p1003.1-2001 . +The +.Fl q +option is a non-standard +.Fx +extension which may not be available on other operating systems. .Sh HISTORY The .Nm diff --git a/usr.bin/what/what.c b/usr.bin/what/what.c index c22671564110..8e994e9a650f 100644 --- a/usr.bin/what/what.c +++ b/usr.bin/what/what.c @@ -46,75 +46,96 @@ static const char sccsid[] = "@(#)what.c 8.1 (Berkeley) 6/6/93"; #endif #include +#include #include #include #include -static int sflag; -static int found; - -void search(void); static void usage(void); +static bool search(bool, bool, FILE *); -/* - * what - */ int -main(int argc, char **argv) +main(int argc, char *argv[]) { + const char *file; + FILE *in; + bool found, qflag, sflag; int c; - while ((c = getopt(argc, argv, "s")) != -1) + qflag = sflag = false; + + while ((c = getopt(argc, argv, "qs")) != -1) { switch (c) { + case 'q': + qflag = true; + break; case 's': - sflag = 1; + sflag = true; break; default: usage(); } + } + argc -= optind; argv += optind; - if (!*argv) - search(); - else do { - if (!freopen(*argv, "r", stdin)) - warn("%s", *argv); - else { - printf("%s:\n", *argv); - search(); + found = false; + + if (argc == 0) { + if (search(sflag, qflag, stdin)) + found = true; + } else { + while (argc--) { + file = *argv++; + in = fopen(file, "r"); + if (in == NULL) { + if (!qflag) + warn("%s", file); + continue; + } + if (!qflag) + printf("%s:\n", file); + if (search(sflag, qflag, in)) + found = true; + fclose(in); } - } while(*++argv); - exit(!found); + } + exit(found ? 0 : 1); } static void usage(void) { - (void)fprintf(stderr, "usage: what [-s] [file ...]\n"); + fprintf(stderr, "usage: what [-qs] [file ...]\n"); exit(1); } -void -search(void) +bool +search(bool one, bool quiet, FILE *in) { + bool found; int c; - while ((c = getchar()) != EOF) { + found = false; + + while ((c = getc(in)) != EOF) { loop: if (c != '@') continue; - if ((c = getchar()) != '(') + if ((c = getc(in)) != '(') goto loop; - if ((c = getchar()) != '#') + if ((c = getc(in)) != '#') goto loop; - if ((c = getchar()) != ')') + if ((c = getc(in)) != ')') goto loop; - putchar('\t'); - while ((c = getchar()) != EOF && c && c != '"' && + if (!quiet) + putchar('\t'); + while ((c = getc(in)) != EOF && c && c != '"' && c != '>' && c != '\\' && c != '\n') putchar(c); putchar('\n'); - found = 1; - if (sflag) - return; + found = true; + if (one) + break; } + return (found); }