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.
This commit is contained in:
parent
3b7d275a3d
commit
36f45138fb
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=146163
@ -41,7 +41,7 @@
|
|||||||
.Nd "show what versions of object modules were used to construct a file"
|
.Nd "show what versions of object modules were used to construct a file"
|
||||||
.Sh SYNOPSIS
|
.Sh SYNOPSIS
|
||||||
.Nm
|
.Nm
|
||||||
.Op Fl s
|
.Op Fl qs
|
||||||
.Op Ar
|
.Op Ar
|
||||||
.Sh DESCRIPTION
|
.Sh DESCRIPTION
|
||||||
The
|
The
|
||||||
@ -59,6 +59,8 @@ character, or backslash.
|
|||||||
.Pp
|
.Pp
|
||||||
The following option is available:
|
The following option is available:
|
||||||
.Bl -tag -width Ds
|
.Bl -tag -width Ds
|
||||||
|
.It Fl q
|
||||||
|
Only output the match text, rather than formatting it.
|
||||||
.It Fl s
|
.It Fl s
|
||||||
Stop searching each file after the first match.
|
Stop searching each file after the first match.
|
||||||
.El
|
.El
|
||||||
@ -72,6 +74,11 @@ The
|
|||||||
.Nm
|
.Nm
|
||||||
utility conforms to
|
utility conforms to
|
||||||
.St -p1003.1-2001 .
|
.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
|
.Sh HISTORY
|
||||||
The
|
The
|
||||||
.Nm
|
.Nm
|
||||||
|
@ -46,75 +46,96 @@ static const char sccsid[] = "@(#)what.c 8.1 (Berkeley) 6/6/93";
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <err.h>
|
#include <err.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
static int sflag;
|
|
||||||
static int found;
|
|
||||||
|
|
||||||
void search(void);
|
|
||||||
static void usage(void);
|
static void usage(void);
|
||||||
|
static bool search(bool, bool, FILE *);
|
||||||
|
|
||||||
/*
|
|
||||||
* what
|
|
||||||
*/
|
|
||||||
int
|
int
|
||||||
main(int argc, char **argv)
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
const char *file;
|
||||||
|
FILE *in;
|
||||||
|
bool found, qflag, sflag;
|
||||||
int c;
|
int c;
|
||||||
|
|
||||||
while ((c = getopt(argc, argv, "s")) != -1)
|
qflag = sflag = false;
|
||||||
|
|
||||||
|
while ((c = getopt(argc, argv, "qs")) != -1) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
|
case 'q':
|
||||||
|
qflag = true;
|
||||||
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
sflag = 1;
|
sflag = true;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
usage();
|
usage();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
argc -= optind;
|
||||||
argv += optind;
|
argv += optind;
|
||||||
|
|
||||||
if (!*argv)
|
found = false;
|
||||||
search();
|
|
||||||
else do {
|
if (argc == 0) {
|
||||||
if (!freopen(*argv, "r", stdin))
|
if (search(sflag, qflag, stdin))
|
||||||
warn("%s", *argv);
|
found = true;
|
||||||
else {
|
} else {
|
||||||
printf("%s:\n", *argv);
|
while (argc--) {
|
||||||
search();
|
file = *argv++;
|
||||||
|
in = fopen(file, "r");
|
||||||
|
if (in == NULL) {
|
||||||
|
if (!qflag)
|
||||||
|
warn("%s", file);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
} while(*++argv);
|
if (!qflag)
|
||||||
exit(!found);
|
printf("%s:\n", file);
|
||||||
|
if (search(sflag, qflag, in))
|
||||||
|
found = true;
|
||||||
|
fclose(in);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exit(found ? 0 : 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
usage(void)
|
usage(void)
|
||||||
{
|
{
|
||||||
(void)fprintf(stderr, "usage: what [-s] [file ...]\n");
|
fprintf(stderr, "usage: what [-qs] [file ...]\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
bool
|
||||||
search(void)
|
search(bool one, bool quiet, FILE *in)
|
||||||
{
|
{
|
||||||
|
bool found;
|
||||||
int c;
|
int c;
|
||||||
|
|
||||||
while ((c = getchar()) != EOF) {
|
found = false;
|
||||||
|
|
||||||
|
while ((c = getc(in)) != EOF) {
|
||||||
loop: if (c != '@')
|
loop: if (c != '@')
|
||||||
continue;
|
continue;
|
||||||
if ((c = getchar()) != '(')
|
if ((c = getc(in)) != '(')
|
||||||
goto loop;
|
goto loop;
|
||||||
if ((c = getchar()) != '#')
|
if ((c = getc(in)) != '#')
|
||||||
goto loop;
|
goto loop;
|
||||||
if ((c = getchar()) != ')')
|
if ((c = getc(in)) != ')')
|
||||||
goto loop;
|
goto loop;
|
||||||
|
if (!quiet)
|
||||||
putchar('\t');
|
putchar('\t');
|
||||||
while ((c = getchar()) != EOF && c && c != '"' &&
|
while ((c = getc(in)) != EOF && c && c != '"' &&
|
||||||
c != '>' && c != '\\' && c != '\n')
|
c != '>' && c != '\\' && c != '\n')
|
||||||
putchar(c);
|
putchar(c);
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
found = 1;
|
found = true;
|
||||||
if (sflag)
|
if (one)
|
||||||
return;
|
break;
|
||||||
}
|
}
|
||||||
|
return (found);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user