xref: /freebsd/usr.bin/what/what.c (revision 7899f917b1c0ea178f1d2be0cfb452086d079d23)
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 #include <err.h>
33 #include <stdbool.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 
38 static void usage(void) __dead2;
39 static bool search(bool, bool, FILE *);
40 
41 int
42 main(int argc, char *argv[])
43 {
44 	const char *file;
45 	FILE *in;
46 	bool found, qflag, sflag;
47 	int c;
48 
49 	qflag = sflag = false;
50 
51 	while ((c = getopt(argc, argv, "qs")) != -1) {
52 		switch (c) {
53 		case 'q':
54 			qflag = true;
55 			break;
56 		case 's':
57 			sflag = true;
58 			break;
59 		default:
60 			usage();
61 		}
62 	}
63 	argc -= optind;
64 	argv += optind;
65 
66 	found = false;
67 
68 	if (argc == 0) {
69 		if (search(sflag, qflag, stdin))
70 			found = true;
71 	} else {
72 		while (argc--) {
73 			file = *argv++;
74 			in = fopen(file, "r");
75 			if (in == NULL) {
76 				if (!qflag)
77 					warn("%s", file);
78 				continue;
79 			}
80 			if (!qflag)
81 				printf("%s:\n", file);
82 			if (search(sflag, qflag, in))
83 				found = true;
84 			fclose(in);
85 		}
86 	}
87 	exit(found ? 0 : 1);
88 }
89 
90 static void
91 usage(void)
92 {
93 	fprintf(stderr, "usage: what [-qs] [file ...]\n");
94 	exit(1);
95 }
96 
97 bool
98 search(bool one, bool quiet, FILE *in)
99 {
100 	bool found;
101 	int c;
102 
103 	found = false;
104 
105 	while ((c = getc(in)) != EOF) {
106 loop:		if (c != '@')
107 			continue;
108 		if ((c = getc(in)) != '(')
109 			goto loop;
110 		if ((c = getc(in)) != '#')
111 			goto loop;
112 		if ((c = getc(in)) != ')')
113 			goto loop;
114 		if (!quiet)
115 			putchar('\t');
116 		while ((c = getc(in)) != EOF && c && c != '"' &&
117 		    c != '>' && c != '\\' && c != '\n')
118 			putchar(c);
119 		putchar('\n');
120 		found = true;
121 		if (one)
122 			break;
123 	}
124 	return (found);
125 }
126