xref: /minix3/usr.bin/whereis/whereis.c (revision d2532d3d42d764c9ef9816851cdb17eda7e08d36)
1*d24fd34cSstux /*	$NetBSD: whereis.c,v 1.21 2008/10/17 10:53:26 apb Exp $	*/
2*d24fd34cSstux 
3*d24fd34cSstux /*-
4*d24fd34cSstux  * Copyright (c) 1993
5*d24fd34cSstux  *	The Regents of the University of California.  All rights reserved.
6*d24fd34cSstux  *
7*d24fd34cSstux  * Redistribution and use in source and binary forms, with or without
8*d24fd34cSstux  * modification, are permitted provided that the following conditions
9*d24fd34cSstux  * are met:
10*d24fd34cSstux  * 1. Redistributions of source code must retain the above copyright
11*d24fd34cSstux  *    notice, this list of conditions and the following disclaimer.
12*d24fd34cSstux  * 2. Redistributions in binary form must reproduce the above copyright
13*d24fd34cSstux  *    notice, this list of conditions and the following disclaimer in the
14*d24fd34cSstux  *    documentation and/or other materials provided with the distribution.
15*d24fd34cSstux  * 3. Neither the name of the University nor the names of its contributors
16*d24fd34cSstux  *    may be used to endorse or promote products derived from this software
17*d24fd34cSstux  *    without specific prior written permission.
18*d24fd34cSstux  *
19*d24fd34cSstux  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*d24fd34cSstux  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*d24fd34cSstux  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*d24fd34cSstux  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*d24fd34cSstux  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*d24fd34cSstux  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*d24fd34cSstux  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*d24fd34cSstux  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*d24fd34cSstux  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*d24fd34cSstux  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*d24fd34cSstux  * SUCH DAMAGE.
30*d24fd34cSstux  */
31*d24fd34cSstux 
32*d24fd34cSstux #include <sys/cdefs.h>
33*d24fd34cSstux #ifndef lint
34*d24fd34cSstux __COPYRIGHT("@(#) Copyright (c) 1993\
35*d24fd34cSstux  The Regents of the University of California.  All rights reserved.");
36*d24fd34cSstux #endif /* not lint */
37*d24fd34cSstux 
38*d24fd34cSstux #ifndef lint
39*d24fd34cSstux #if 0
40*d24fd34cSstux static char sccsid[] = "@(#)whereis.c	8.3 (Berkeley) 5/4/95";
41*d24fd34cSstux #endif
42*d24fd34cSstux __RCSID("$NetBSD: whereis.c,v 1.21 2008/10/17 10:53:26 apb Exp $");
43*d24fd34cSstux #endif /* not lint */
44*d24fd34cSstux 
45*d24fd34cSstux #include <sys/param.h>
46*d24fd34cSstux #include <sys/stat.h>
47*d24fd34cSstux #include <sys/sysctl.h>
48*d24fd34cSstux 
49*d24fd34cSstux #include <err.h>
50*d24fd34cSstux #include <errno.h>
51*d24fd34cSstux #include <stdio.h>
52*d24fd34cSstux #include <stdlib.h>
53*d24fd34cSstux #include <string.h>
54*d24fd34cSstux #include <unistd.h>
55*d24fd34cSstux 
56*d24fd34cSstux static void usage(void) __dead;
57*d24fd34cSstux 
58*d24fd34cSstux int
main(int argc,char * argv[])59*d24fd34cSstux main(int argc, char *argv[])
60*d24fd34cSstux {
61*d24fd34cSstux 	struct stat sb;
62*d24fd34cSstux 	size_t len;
63*d24fd34cSstux 	int ch, mib[2];
64*d24fd34cSstux 	char *p, *std, path[MAXPATHLEN];
65*d24fd34cSstux 	const char *t;
66*d24fd34cSstux 	int which = strcmp(getprogname(), "which") == 0;
67*d24fd34cSstux 	int useenvpath = which, found = 0;
68*d24fd34cSstux 	gid_t egid = getegid();
69*d24fd34cSstux 	uid_t euid = geteuid();
70*d24fd34cSstux 
71*d24fd34cSstux 	/* To make access(2) do what we want */
72*d24fd34cSstux 	if (setgid(egid) == -1)
73*d24fd34cSstux 		err(1, "Can't set gid to %lu", (unsigned long)egid);
74*d24fd34cSstux 	if (setuid(euid) == -1)
75*d24fd34cSstux 		err(1, "Can't set uid to %lu", (unsigned long)euid);
76*d24fd34cSstux 
77*d24fd34cSstux 	while ((ch = getopt(argc, argv, "ap")) != -1)
78*d24fd34cSstux 		switch (ch) {
79*d24fd34cSstux 		case 'a':
80*d24fd34cSstux 			which = 0;
81*d24fd34cSstux 			break;
82*d24fd34cSstux 		case 'p':
83*d24fd34cSstux 			useenvpath = 1;	/* use environment for PATH */
84*d24fd34cSstux 			break;
85*d24fd34cSstux 
86*d24fd34cSstux 		case '?':
87*d24fd34cSstux 		default:
88*d24fd34cSstux 			usage();
89*d24fd34cSstux 		}
90*d24fd34cSstux 	argc -= optind;
91*d24fd34cSstux 	argv += optind;
92*d24fd34cSstux 
93*d24fd34cSstux 	if (argc == 0)
94*d24fd34cSstux 		usage();
95*d24fd34cSstux 
96*d24fd34cSstux  	if (useenvpath) {
97*d24fd34cSstux  		if ((std = getenv("PATH")) == NULL)
98*d24fd34cSstux  			errx(1, "PATH environment variable is not set");
99*d24fd34cSstux 	} else {
100*d24fd34cSstux 		/* Retrieve the standard path. */
101*d24fd34cSstux 		mib[0] = CTL_USER;
102*d24fd34cSstux 		mib[1] = USER_CS_PATH;
103*d24fd34cSstux 		if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1)
104*d24fd34cSstux 			err(1, "sysctl: user.cs_path");
105*d24fd34cSstux 		if (len == 0)
106*d24fd34cSstux 			errx(1, "sysctl: user.cs_path (zero length)");
107*d24fd34cSstux 		if ((std = malloc(len)) == NULL)
108*d24fd34cSstux 			err(1, NULL);
109*d24fd34cSstux 		if (sysctl(mib, 2, std, &len, NULL, 0) == -1)
110*d24fd34cSstux 			err(1, "sysctl: user.cs_path");
111*d24fd34cSstux 	}
112*d24fd34cSstux 
113*d24fd34cSstux 	/* For each path, for each program... */
114*d24fd34cSstux 	for (; *argv; ++argv) {
115*d24fd34cSstux 		if (**argv == '/') {
116*d24fd34cSstux 			if (stat(*argv, &sb) == -1)
117*d24fd34cSstux 				continue; /* next argv */
118*d24fd34cSstux 			if (!S_ISREG(sb.st_mode))
119*d24fd34cSstux 				continue; /* next argv */
120*d24fd34cSstux 			if (access(*argv, X_OK) == -1)
121*d24fd34cSstux 				continue; /* next argv */
122*d24fd34cSstux 			(void)printf("%s\n", *argv);
123*d24fd34cSstux 			found++;
124*d24fd34cSstux 			if (which)
125*d24fd34cSstux 				continue; /* next argv */
126*d24fd34cSstux 		} else for (p = std; p; ) {
127*d24fd34cSstux 			t = p;
128*d24fd34cSstux 			if ((p = strchr(p, ':')) != NULL) {
129*d24fd34cSstux 				*p = '\0';
130*d24fd34cSstux 				if (t == p)
131*d24fd34cSstux 					t = ".";
132*d24fd34cSstux 			} else
133*d24fd34cSstux 				if (strlen(t) == 0)
134*d24fd34cSstux 					t = ".";
135*d24fd34cSstux 			(void)snprintf(path, sizeof(path), "%s/%s", t, *argv);
136*d24fd34cSstux 			len = snprintf(path, sizeof(path), "%s/%s", t, *argv);
137*d24fd34cSstux 			if (p)
138*d24fd34cSstux 				*p++ = ':';
139*d24fd34cSstux 			if (len >= sizeof(path))
140*d24fd34cSstux 				continue; /* next p */
141*d24fd34cSstux 			if (stat(path, &sb) == -1)
142*d24fd34cSstux 				continue; /* next p */
143*d24fd34cSstux 			if (!S_ISREG(sb.st_mode))
144*d24fd34cSstux 				continue; /* next p */
145*d24fd34cSstux 			if (access(path, X_OK) == -1)
146*d24fd34cSstux 				continue; /* next p */
147*d24fd34cSstux 			(void)printf("%s\n", path);
148*d24fd34cSstux 			found++;
149*d24fd34cSstux 			if (which)
150*d24fd34cSstux 				break; /* next argv */
151*d24fd34cSstux 		}
152*d24fd34cSstux 	}
153*d24fd34cSstux 
154*d24fd34cSstux 	return ((found == 0) ? 3 : ((found >= argc) ? 0 : 2));
155*d24fd34cSstux }
156*d24fd34cSstux 
157*d24fd34cSstux static void
usage(void)158*d24fd34cSstux usage(void)
159*d24fd34cSstux {
160*d24fd34cSstux 
161*d24fd34cSstux 	(void)fprintf(stderr, "Usage: %s [-ap] program [...]\n", getprogname());
162*d24fd34cSstux 	exit(1);
163*d24fd34cSstux }
164