Annotation of src/usr.bin/which/which.c, Revision 1.24
1.24 ! millert 1: /* $OpenBSD: which.c,v 1.23 2016/01/14 21:54:24 millert Exp $ */
1.1 millert 2:
3: /*
4: * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
5: *
1.9 millert 6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
1.1 millert 9: *
1.11 millert 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 millert 17: */
18:
19: #include <sys/stat.h>
1.2 millert 20: #include <sys/sysctl.h>
1.1 millert 21:
22: #include <err.h>
23: #include <errno.h>
24: #include <locale.h>
1.18 guenther 25: #include <paths.h>
1.1 millert 26: #include <stdio.h>
27: #include <stdlib.h>
28: #include <string.h>
29: #include <unistd.h>
1.20 deraadt 30: #include <limits.h>
1.1 millert 31:
1.2 millert 32: #define PROG_WHICH 1
33: #define PROG_WHEREIS 2
34:
1.1 millert 35: extern char *__progname;
36:
1.6 millert 37: int findprog(char *, char *, int, int);
1.12 millert 38: __dead void usage(void);
1.1 millert 39:
40: /*
41: * which(1) -- find an executable(s) in the user's path
1.2 millert 42: * whereis(1) -- find an executable(s) in the default user path
1.1 millert 43: *
44: * Return values:
45: * 0 - all executables found
46: * 1 - some found, some not
47: * 2 - none found
48: */
49:
50: int
1.10 deraadt 51: main(int argc, char *argv[])
1.1 millert 52: {
53: char *path;
1.2 millert 54: size_t n;
1.3 millert 55: int ch, allmatches = 0, notfound = 0, progmode = PROG_WHICH;
1.1 millert 56:
57: (void)setlocale(LC_ALL, "");
58:
1.17 guenther 59: while ((ch = getopt(argc, argv, "a")) != -1)
1.2 millert 60: switch (ch) {
1.3 millert 61: case 'a':
62: allmatches = 1;
63: break;
1.2 millert 64: default:
65: usage();
66: }
1.17 guenther 67: argc -= optind;
68: argv += optind;
69:
70: if (argc == 0)
71: usage();
1.2 millert 72:
73: if (strcmp(__progname, "whereis") == 0) {
74: progmode = PROG_WHEREIS;
1.18 guenther 75: path = _PATH_STDPATH;
1.2 millert 76: } else {
1.23 millert 77: if ((path = getenv("PATH")) == NULL || *path == '\0')
78: path = _PATH_DEFPATH;
1.2 millert 79: }
1.1 millert 80:
81: /* To make access(2) do what we want */
82: if (setgid(getegid()))
1.4 deraadt 83: err(1, "Can't set gid to %u", getegid());
1.1 millert 84: if (setuid(geteuid()))
1.4 deraadt 85: err(1, "Can't set uid to %u", geteuid());
1.21 deraadt 86:
87: if (pledge("stdio rpath", NULL) == -1)
1.22 gsoares 88: err(2, "pledge");
1.1 millert 89:
1.17 guenther 90: for (n = 0; n < argc; n++)
1.3 millert 91: if (findprog(argv[n], path, progmode, allmatches) == 0)
1.1 millert 92: notfound++;
93:
1.17 guenther 94: exit((notfound == 0) ? 0 : ((notfound == argc) ? 2 : 1));
1.1 millert 95: }
96:
97: int
1.8 millert 98: findprog(char *prog, char *path, int progmode, int allmatches)
1.1 millert 99: {
1.20 deraadt 100: char *p, filename[PATH_MAX];
1.3 millert 101: int proglen, plen, rval = 0;
1.1 millert 102: struct stat sbuf;
1.13 fgsch 103: char *pathcpy;
1.1 millert 104:
105: /* Special case if prog contains '/' */
106: if (strchr(prog, '/')) {
107: if ((stat(prog, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
108: access(prog, X_OK) == 0) {
109: (void)puts(prog);
1.12 millert 110: return (1);
1.1 millert 111: } else {
1.16 sobrado 112: warnx("%s: Command not found.", prog);
1.12 millert 113: return (0);
1.1 millert 114: }
115: }
116:
117: if ((path = strdup(path)) == NULL)
1.16 sobrado 118: err(1, "strdup");
1.13 fgsch 119: pathcpy = path;
1.1 millert 120:
121: proglen = strlen(prog);
1.13 fgsch 122: while ((p = strsep(&pathcpy, ":")) != NULL) {
1.1 millert 123: if (*p == '\0')
124: p = ".";
125:
126: plen = strlen(p);
1.24 ! millert 127: while (plen > 0 && p[plen-1] == '/')
1.1 millert 128: p[--plen] = '\0'; /* strip trailing '/' */
129:
130: if (plen + 1 + proglen >= sizeof(filename)) {
1.19 guenther 131: warnc(ENAMETOOLONG, "%s/%s", p, prog);
1.12 millert 132: free(path);
133: return (0);
1.1 millert 134: }
135:
1.8 millert 136: snprintf(filename, sizeof(filename), "%s/%s", p, prog);
1.1 millert 137: if ((stat(filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
138: access(filename, X_OK) == 0) {
139: (void)puts(filename);
1.3 millert 140: rval = 1;
1.12 millert 141: if (!allmatches) {
142: free(path);
143: return (rval);
144: }
1.1 millert 145: }
146: }
147: (void)free(path);
148:
1.2 millert 149: /* whereis(1) is silent on failure. */
1.3 millert 150: if (!rval && progmode != PROG_WHEREIS)
1.16 sobrado 151: warnx("%s: Command not found.", prog);
1.12 millert 152: return (rval);
1.1 millert 153: }
154:
1.12 millert 155: __dead void
1.8 millert 156: usage(void)
1.1 millert 157: {
1.14 sobrado 158: (void)fprintf(stderr, "usage: %s [-a] name ...\n", __progname);
1.1 millert 159: exit(1);
160: }