Annotation of src/usr.bin/which/which.c, Revision 1.6
1.6 ! millert 1: /* $OpenBSD: which.c,v 1.5 1998/06/21 22:14:05 millert Exp $ */
1.1 millert 2:
3: /*
4: * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
1.5 millert 15: * 3. The name of the author may not be used to endorse or promote products
1.1 millert 16: * derived from this software without specific prior written permission.
17: *
18: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28: */
29:
30: #ifndef lint
1.6 ! millert 31: static char rcsid[] = "$OpenBSD: which.c,v 1.5 1998/06/21 22:14:05 millert Exp $";
1.1 millert 32: #endif /* not lint */
33:
34: #include <sys/param.h>
35: #include <sys/stat.h>
1.2 millert 36: #include <sys/sysctl.h>
1.1 millert 37:
38: #include <err.h>
39: #include <errno.h>
40: #include <locale.h>
41: #include <stdio.h>
42: #include <stdlib.h>
43: #include <string.h>
44: #include <unistd.h>
45:
1.2 millert 46: #define PROG_WHICH 1
47: #define PROG_WHEREIS 2
48:
1.1 millert 49: extern char *__progname;
50:
1.6 ! millert 51: int findprog(char *, char *, int, int);
! 52: void usage(void);
1.1 millert 53:
54: /*
55: * which(1) -- find an executable(s) in the user's path
1.2 millert 56: * whereis(1) -- find an executable(s) in the default user path
1.1 millert 57: *
58: * Return values:
59: * 0 - all executables found
60: * 1 - some found, some not
61: * 2 - none found
62: */
63:
64: int
65: main(argc, argv)
66: int argc;
67: char **argv;
68: {
69: char *path;
1.2 millert 70: size_t n;
1.3 millert 71: int ch, allmatches = 0, notfound = 0, progmode = PROG_WHICH;
1.1 millert 72:
73: (void)setlocale(LC_ALL, "");
74:
75: if (argc == 1)
76: usage();
77:
1.2 millert 78: /* Don't accept command args but check since old whereis(1) used to */
1.3 millert 79: while ((ch = getopt(argc, argv, "a")) != -1) {
1.2 millert 80: switch (ch) {
1.3 millert 81: case 'a':
82: allmatches = 1;
83: break;
1.2 millert 84: default:
85: usage();
86: }
87: }
88:
89: /*
90: * which(1) uses user's $PATH.
91: * whereis(1) uses user.cs_path from sysctl(3).
92: */
93: if (strcmp(__progname, "whereis") == 0) {
94: int mib[2];
95:
96: progmode = PROG_WHEREIS;
97: mib[0] = CTL_USER;
98: mib[1] = USER_CS_PATH;
99: if (sysctl(mib, 2, NULL, &n, NULL, 0) == -1)
1.4 deraadt 100: err(1, "unable to get length of user.cs_path");
1.2 millert 101: if (n == 0)
1.4 deraadt 102: errx(1, "user.cs_path was zero length!");
1.2 millert 103: if ((path = (char *)malloc(n)) == NULL)
1.4 deraadt 104: errx(1, "can't allocate memory.");
1.2 millert 105: if (sysctl(mib, 2, path, &n, NULL, 0) == -1)
1.4 deraadt 106: err(1, "unable to get user.cs_path");
1.2 millert 107: } else {
108: if ((path = getenv("PATH")) == NULL)
1.4 deraadt 109: err(1, "can't get $PATH from environment");
1.2 millert 110: }
1.1 millert 111:
112: /* To make access(2) do what we want */
113: if (setgid(getegid()))
1.4 deraadt 114: err(1, "Can't set gid to %u", getegid());
1.1 millert 115: if (setuid(geteuid()))
1.4 deraadt 116: err(1, "Can't set uid to %u", geteuid());
1.1 millert 117:
1.3 millert 118: for (n = optind; n < argc; n++)
119: if (findprog(argv[n], path, progmode, allmatches) == 0)
1.1 millert 120: notfound++;
121:
122: exit((notfound == 0) ? 0 : ((notfound == argc - 1) ? 2 : 1));
123: }
124:
125: int
1.3 millert 126: findprog(prog, path, progmode, allmatches)
1.1 millert 127: char *prog;
128: char *path;
1.2 millert 129: int progmode;
1.3 millert 130: int allmatches;
1.1 millert 131: {
132: char *p, filename[MAXPATHLEN];
1.3 millert 133: int proglen, plen, rval = 0;
1.1 millert 134: struct stat sbuf;
135:
136: /* Special case if prog contains '/' */
137: if (strchr(prog, '/')) {
138: if ((stat(prog, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
139: access(prog, X_OK) == 0) {
140: (void)puts(prog);
141: return(1);
142: } else {
143: (void)printf("%s: Command not found.\n", prog);
144: return(0);
145: }
146: }
147:
148: if ((path = strdup(path)) == NULL)
1.4 deraadt 149: errx(1, "Can't allocate memory.");
1.1 millert 150:
151: proglen = strlen(prog);
152: while ((p = strsep(&path, ":")) != NULL) {
153: if (*p == '\0')
154: p = ".";
155:
156: plen = strlen(p);
157: while (p[plen-1] == '/')
158: p[--plen] = '\0'; /* strip trailing '/' */
159:
160: if (plen + 1 + proglen >= sizeof(filename)) {
161: warnx("%s/%s: %s", p, prog, strerror(ENAMETOOLONG));
162: return(0);
163: }
164:
165: (void)strcpy(filename, p);
166: filename[plen] = '/';
167: (void)strcpy(filename + plen + 1, prog);
168: if ((stat(filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
169: access(filename, X_OK) == 0) {
170: (void)puts(filename);
1.3 millert 171: rval = 1;
172: if (!allmatches)
173: return(rval);
1.1 millert 174: }
175: }
176: (void)free(path);
177:
1.2 millert 178: /* whereis(1) is silent on failure. */
1.3 millert 179: if (!rval && progmode != PROG_WHEREIS)
1.2 millert 180: (void)printf("%s: Command not found.\n", prog);
1.3 millert 181: return(rval);
1.1 millert 182: }
183:
184: void
185: usage()
186: {
1.3 millert 187: (void) fprintf(stderr, "Usage: %s [-a] name [...]\n", __progname);
1.1 millert 188: exit(1);
189: }