xref: /freebsd/usr.bin/what/what.c (revision 5e3190f700637fcfc1a52daeaa4a031fdd2557c7)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1988, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 
33 #ifndef lint
34 static const char copyright[] =
35 "@(#) Copyright (c) 1980, 1988, 1993\n\
36 	The Regents of the University of California.  All rights reserved.\n";
37 #endif
38 
39 #ifndef lint
40 static const char sccsid[] = "@(#)what.c	8.1 (Berkeley) 6/6/93";
41 #endif
42 
43 #include <err.h>
44 #include <stdbool.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 
49 static void usage(void) __dead2;
50 static bool search(bool, bool, FILE *);
51 
52 int
53 main(int argc, char *argv[])
54 {
55 	const char *file;
56 	FILE *in;
57 	bool found, qflag, sflag;
58 	int c;
59 
60 	qflag = sflag = false;
61 
62 	while ((c = getopt(argc, argv, "qs")) != -1) {
63 		switch (c) {
64 		case 'q':
65 			qflag = true;
66 			break;
67 		case 's':
68 			sflag = true;
69 			break;
70 		default:
71 			usage();
72 		}
73 	}
74 	argc -= optind;
75 	argv += optind;
76 
77 	found = false;
78 
79 	if (argc == 0) {
80 		if (search(sflag, qflag, stdin))
81 			found = true;
82 	} else {
83 		while (argc--) {
84 			file = *argv++;
85 			in = fopen(file, "r");
86 			if (in == NULL) {
87 				if (!qflag)
88 					warn("%s", file);
89 				continue;
90 			}
91 			if (!qflag)
92 				printf("%s:\n", file);
93 			if (search(sflag, qflag, in))
94 				found = true;
95 			fclose(in);
96 		}
97 	}
98 	exit(found ? 0 : 1);
99 }
100 
101 static void
102 usage(void)
103 {
104 	fprintf(stderr, "usage: what [-qs] [file ...]\n");
105 	exit(1);
106 }
107 
108 bool
109 search(bool one, bool quiet, FILE *in)
110 {
111 	bool found;
112 	int c;
113 
114 	found = false;
115 
116 	while ((c = getc(in)) != EOF) {
117 loop:		if (c != '@')
118 			continue;
119 		if ((c = getc(in)) != '(')
120 			goto loop;
121 		if ((c = getc(in)) != '#')
122 			goto loop;
123 		if ((c = getc(in)) != ')')
124 			goto loop;
125 		if (!quiet)
126 			putchar('\t');
127 		while ((c = getc(in)) != EOF && c && c != '"' &&
128 		    c != '>' && c != '\\' && c != '\n')
129 			putchar(c);
130 		putchar('\n');
131 		found = true;
132 		if (one)
133 			break;
134 	}
135 	return (found);
136 }
137