xref: /freebsd/usr.bin/what/what.c (revision 42c159fe388a3765f69860c84183700af37aca8a)
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(argc, argv)
64 	int argc;
65 	char **argv;
66 {
67 	int c;
68 
69 	while ((c = getopt(argc, argv, "s")) != -1)
70 		switch (c) {
71 		case 's':
72 			sflag = 1;
73 			break;
74 		default:
75 			usage();
76 		}
77 	argv += optind;
78 
79 	if (!*argv)
80 		search();
81 	else do {
82 		if (!freopen(*argv, "r", stdin))
83 			warn("%s", *argv);
84 		else {
85 			printf("%s:\n", *argv);
86 			search();
87 		}
88 	} while(*++argv);
89 	exit(!found);
90 }
91 
92 static void
93 usage()
94 {
95 	(void)fprintf(stderr, "usage: what [-s] file ...\n");
96 	exit(1);
97 }
98 
99 void
100 search()
101 {
102 	register int c;
103 
104 	while ((c = getchar()) != EOF) {
105 loop:		if (c != '@')
106 			continue;
107 		if ((c = getchar()) != '(')
108 			goto loop;
109 		if ((c = getchar()) != '#')
110 			goto loop;
111 		if ((c = getchar()) != ')')
112 			goto loop;
113 		putchar('\t');
114 		while ((c = getchar()) != EOF && c && c != '"' &&
115 		    c != '>' && c != '\\' && c != '\n')
116 			putchar(c);
117 		putchar('\n');
118 		found = 1;
119 		if (sflag)
120 			return;
121 	}
122 }
123