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