summaryrefslogtreecommitdiffstats
path: root/usr.bin/what
diff options
context:
space:
mode:
authorjmallett <jmallett@FreeBSD.org>2005-05-12 18:57:37 +0000
committerjmallett <jmallett@FreeBSD.org>2005-05-12 18:57:37 +0000
commit85f5689fb5328f41073d4f5ceb653a4779633672 (patch)
treef43a8ddf9a98f0db0060fc9331a7ce46ec6b8311 /usr.bin/what
parent2b0b1f00308c182524fcae43aad2f9ef0823a7c2 (diff)
downloadFreeBSD-src-85f5689fb5328f41073d4f5ceb653a4779633672.zip
FreeBSD-src-85f5689fb5328f41073d4f5ceb653a4779633672.tar.gz
Add a flag (-q) which prints no header for the file and which doesn't indent the
match text; it also doesn't warn() for files which can't be opened. Remove global variables. Use bool. fopen(3) the files instead of freopen(3)ing stdin.
Diffstat (limited to 'usr.bin/what')
-rw-r--r--usr.bin/what/what.19
-rw-r--r--usr.bin/what/what.c85
2 files changed, 61 insertions, 33 deletions
diff --git a/usr.bin/what/what.1 b/usr.bin/what/what.1
index 59cb281..fff062a 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 c226715..8e994e9 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 <err.h>
+#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
-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);
}
OpenPOWER on IntegriCloud