xref: /freebsd/usr.bin/what/what.c (revision 4b2eaea43fec8e8792be611dea204071a10b655a)
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 
36 __FBSDID("$FreeBSD$");
37 
38 #ifndef lint
39 static const char copyright[] =
40 "@(#) Copyright (c) 1980, 1988, 1993\n\
41 	The Regents of the University of California.  All rights reserved.\n";
42 #endif
43 
44 #ifndef lint
45 static const char sccsid[] = "@(#)what.c	8.1 (Berkeley) 6/6/93";
46 #endif
47 
48 #include <err.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <unistd.h>
52 
53 static int sflag;
54 static int found;
55 
56 void search(void);
57 static void usage(void);
58 
59 /*
60  * what
61  */
62 int
63 main(int argc, char **argv)
64 {
65 	int c;
66 
67 	while ((c = getopt(argc, argv, "s")) != -1)
68 		switch (c) {
69 		case 's':
70 			sflag = 1;
71 			break;
72 		default:
73 			usage();
74 		}
75 	argv += optind;
76 
77 	if (!*argv)
78 		search();
79 	else do {
80 		if (!freopen(*argv, "r", stdin))
81 			warn("%s", *argv);
82 		else {
83 			printf("%s:\n", *argv);
84 			search();
85 		}
86 	} while(*++argv);
87 	exit(!found);
88 }
89 
90 static void
91 usage(void)
92 {
93 	(void)fprintf(stderr, "usage: what [-s] [file ...]\n");
94 	exit(1);
95 }
96 
97 void
98 search(void)
99 {
100 	int c;
101 
102 	while ((c = getchar()) != EOF) {
103 loop:		if (c != '@')
104 			continue;
105 		if ((c = getchar()) != '(')
106 			goto loop;
107 		if ((c = getchar()) != '#')
108 			goto loop;
109 		if ((c = getchar()) != ')')
110 			goto loop;
111 		putchar('\t');
112 		while ((c = getchar()) != EOF && c && c != '"' &&
113 		    c != '>' && c != '\\' && c != '\n')
114 			putchar(c);
115 		putchar('\n');
116 		found = 1;
117 		if (sflag)
118 			return;
119 	}
120 }
121