Annotation of src/usr.bin/which/which.c, Revision 1.20
1.20 ! deraadt 1: /* $OpenBSD: which.c,v 1.19 2014/05/20 01:25:23 guenther 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 {
77: if ((path = getenv("PATH")) == NULL)
1.4 deraadt 78: err(1, "can't get $PATH from environment");
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.1 millert 86:
1.17 guenther 87: for (n = 0; n < argc; n++)
1.3 millert 88: if (findprog(argv[n], path, progmode, allmatches) == 0)
1.1 millert 89: notfound++;
90:
1.17 guenther 91: exit((notfound == 0) ? 0 : ((notfound == argc) ? 2 : 1));
1.1 millert 92: }
93:
94: int
1.8 millert 95: findprog(char *prog, char *path, int progmode, int allmatches)
1.1 millert 96: {
1.20 ! deraadt 97: char *p, filename[PATH_MAX];
1.3 millert 98: int proglen, plen, rval = 0;
1.1 millert 99: struct stat sbuf;
1.13 fgsch 100: char *pathcpy;
1.1 millert 101:
102: /* Special case if prog contains '/' */
103: if (strchr(prog, '/')) {
104: if ((stat(prog, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
105: access(prog, X_OK) == 0) {
106: (void)puts(prog);
1.12 millert 107: return (1);
1.1 millert 108: } else {
1.16 sobrado 109: warnx("%s: Command not found.", prog);
1.12 millert 110: return (0);
1.1 millert 111: }
112: }
113:
114: if ((path = strdup(path)) == NULL)
1.16 sobrado 115: err(1, "strdup");
1.13 fgsch 116: pathcpy = path;
1.1 millert 117:
118: proglen = strlen(prog);
1.13 fgsch 119: while ((p = strsep(&pathcpy, ":")) != NULL) {
1.1 millert 120: if (*p == '\0')
121: p = ".";
122:
123: plen = strlen(p);
124: while (p[plen-1] == '/')
125: p[--plen] = '\0'; /* strip trailing '/' */
126:
127: if (plen + 1 + proglen >= sizeof(filename)) {
1.19 guenther 128: warnc(ENAMETOOLONG, "%s/%s", p, prog);
1.12 millert 129: free(path);
130: return (0);
1.1 millert 131: }
132:
1.8 millert 133: snprintf(filename, sizeof(filename), "%s/%s", p, prog);
1.1 millert 134: if ((stat(filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
135: access(filename, X_OK) == 0) {
136: (void)puts(filename);
1.3 millert 137: rval = 1;
1.12 millert 138: if (!allmatches) {
139: free(path);
140: return (rval);
141: }
1.1 millert 142: }
143: }
144: (void)free(path);
145:
1.2 millert 146: /* whereis(1) is silent on failure. */
1.3 millert 147: if (!rval && progmode != PROG_WHEREIS)
1.16 sobrado 148: warnx("%s: Command not found.", prog);
1.12 millert 149: return (rval);
1.1 millert 150: }
151:
1.12 millert 152: __dead void
1.8 millert 153: usage(void)
1.1 millert 154: {
1.14 sobrado 155: (void)fprintf(stderr, "usage: %s [-a] name ...\n", __progname);
1.1 millert 156: exit(1);
157: }