xref: /openbsd/usr.bin/what/what.c (revision 5b133f3f)
1*5b133f3fSguenther /*	$OpenBSD: what.c,v 1.17 2023/03/08 04:43:13 guenther Exp $	*/
2df930be7Sderaadt /*	$NetBSD: what.c,v 1.4 1994/12/20 16:01:03 jtc Exp $	*/
3df930be7Sderaadt 
4df930be7Sderaadt /*
5df930be7Sderaadt  * Copyright (c) 1980, 1988, 1993
6df930be7Sderaadt  *	The Regents of the University of California.  All rights reserved.
7df930be7Sderaadt  *
8df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
9df930be7Sderaadt  * modification, are permitted provided that the following conditions
10df930be7Sderaadt  * are met:
11df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
12df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
13df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
14df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
15df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
16f75387cbSmillert  * 3. Neither the name of the University nor the names of its contributors
17df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
18df930be7Sderaadt  *    without specific prior written permission.
19df930be7Sderaadt  *
20df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df930be7Sderaadt  * SUCH DAMAGE.
31df930be7Sderaadt  */
32df930be7Sderaadt 
3329a915ceSderaadt #include <sys/types.h>
3429a915ceSderaadt #include <sys/utsname.h>
35df930be7Sderaadt #include <stdio.h>
3629a915ceSderaadt #include <ctype.h>
37f1beaa94Sart #include <err.h>
3870ef01f6Sdavid #include <stdlib.h>
39f1beaa94Sart #include <string.h>
4073119f56Snino #include <unistd.h>
41df930be7Sderaadt 
4273119f56Snino static void search(char *);
4373119f56Snino static void usage(void);
4473119f56Snino 
4573119f56Snino static int matches;
4673119f56Snino static int sflag;
4773119f56Snino 
4873119f56Snino extern char *__progname;
49df930be7Sderaadt 
50df930be7Sderaadt /*
51df930be7Sderaadt  * what
52df930be7Sderaadt  */
53df930be7Sderaadt int
main(int argc,char * argv[])541837a5caSderaadt main(int argc, char *argv[])
55df930be7Sderaadt {
5629a915ceSderaadt 	struct utsname utsn;
5729a915ceSderaadt 	char match[256];
5873119f56Snino 	int c;
5973119f56Snino 
600bd1216cSderaadt 	if (pledge("stdio rpath", NULL) == -1)
610bd1216cSderaadt 		err(1, "pledge");
62ff9adc31Sderaadt 
6373119f56Snino 	matches = sflag = 0;
6473119f56Snino 	while ((c = getopt(argc, argv, "s")) != -1) {
6573119f56Snino 		switch (c) {
6673119f56Snino 		case 's':
6773119f56Snino 			sflag = 1;
6873119f56Snino 			break;
6973119f56Snino 		default:
7073119f56Snino 			usage();
7173119f56Snino 		}
7273119f56Snino 	}
7373119f56Snino 	argc -= optind;
7473119f56Snino 	argv += optind;
7529a915ceSderaadt 
7629a915ceSderaadt 	if (uname(&utsn) == -1)
7729a915ceSderaadt 		err(1, "uname");
7873119f56Snino 	strlcpy(match, utsn.sysname, sizeof match);
7929a915ceSderaadt 
8073119f56Snino 	if (argc < 1) {
8173119f56Snino 		usage();
8273119f56Snino 	} else do {
83df930be7Sderaadt 		if (!freopen(*argv, "r", stdin)) {
84df930be7Sderaadt 			perror(*argv);
8573119f56Snino 			exit(matches ? 0 : 1);
86df930be7Sderaadt 		}
87a7763ee0Smartijn 		printf("%s:\n", *argv);
8829a915ceSderaadt 		search(match);
89df930be7Sderaadt 	} while(*++argv);
9073119f56Snino 	exit(matches ? 0 : 1);
91df930be7Sderaadt }
92df930be7Sderaadt 
9373119f56Snino static void
search(char * match)941837a5caSderaadt search(char *match)
95df930be7Sderaadt {
96c0932ef1Smpech 	int c;
9729a915ceSderaadt 	int i;
98df930be7Sderaadt 
99df930be7Sderaadt 	while ((c = getchar()) != EOF) {
10029a915ceSderaadt loop:		if (c == '$') {
10129a915ceSderaadt 			for (i = 0; match[i]; i++)
10229a915ceSderaadt 				if ((c = getchar()) != match[i])
10329a915ceSderaadt 					goto loop;
10474b69f87Sderaadt 			printf("\t$%s", match);
105afa72ff4Skrw 			while (isprint(c = getchar())) {
10629a915ceSderaadt 				putchar(c);
107afa72ff4Skrw 				if (c == '$')
108afa72ff4Skrw 					break;
109afa72ff4Skrw 			}
11029a915ceSderaadt 			putchar('\n');
11173119f56Snino 			matches++;
11273119f56Snino 			if (sflag)
11373119f56Snino 				break;
11429a915ceSderaadt 			goto loop;
11529a915ceSderaadt 		}
11629a915ceSderaadt 		if (c != '@')
117df930be7Sderaadt 			continue;
118df930be7Sderaadt 		if ((c = getchar()) != '(')
119df930be7Sderaadt 			goto loop;
120df930be7Sderaadt 		if ((c = getchar()) != '#')
121df930be7Sderaadt 			goto loop;
122df930be7Sderaadt 		if ((c = getchar()) != ')')
123df930be7Sderaadt 			goto loop;
124df930be7Sderaadt 		putchar('\t');
125df930be7Sderaadt 		while ((c = getchar()) != EOF && c && c != '"' &&
12673119f56Snino 		    c != '>' && c != '\n' && c != '\\')
127df930be7Sderaadt 			putchar(c);
128df930be7Sderaadt 		putchar('\n');
12973119f56Snino 		matches++;
13073119f56Snino 		if (sflag)
13173119f56Snino 			break;
132df930be7Sderaadt 	}
133df930be7Sderaadt }
13473119f56Snino 
13573119f56Snino static void
usage(void)1361837a5caSderaadt usage(void)
13773119f56Snino {
13873119f56Snino 
13973119f56Snino 	(void)fprintf(stderr, "usage: %s [-s] file ...\n", __progname);
14073119f56Snino 	exit(1);
14173119f56Snino }
142