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