[BACK]Return to which.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / which

Annotation of src/usr.bin/which/which.c, Revision 1.8

1.7       deraadt     1: /*     $OpenBSD: which.c,v 1.6 2002/02/16 21:27:59 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.8     ! millert    31: static const char rcsid[] = "$OpenBSD: which.c,v 1.6 2002/02/16 21:27:59 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
1.8     ! millert    65: main(int argc, char **argv)
1.1       millert    66: {
                     67:        char *path;
1.2       millert    68:        size_t n;
1.3       millert    69:        int ch, allmatches = 0, notfound = 0, progmode = PROG_WHICH;
1.1       millert    70:
                     71:        (void)setlocale(LC_ALL, "");
                     72:
                     73:        if (argc == 1)
                     74:                usage();
                     75:
1.2       millert    76:        /* Don't accept command args but check since old whereis(1) used to */
1.3       millert    77:        while ((ch = getopt(argc, argv, "a")) != -1) {
1.2       millert    78:                switch (ch) {
1.3       millert    79:                case 'a':
                     80:                        allmatches = 1;
                     81:                        break;
1.2       millert    82:                default:
                     83:                        usage();
                     84:                }
                     85:        }
                     86:
                     87:        /*
                     88:         * which(1) uses user's $PATH.
                     89:         * whereis(1) uses user.cs_path from sysctl(3).
                     90:         */
                     91:        if (strcmp(__progname, "whereis") == 0) {
                     92:                int mib[2];
                     93:
                     94:                progmode = PROG_WHEREIS;
                     95:                mib[0] = CTL_USER;
                     96:                mib[1] = USER_CS_PATH;
                     97:                if (sysctl(mib, 2, NULL, &n, NULL, 0) == -1)
1.4       deraadt    98:                        err(1, "unable to get length of user.cs_path");
1.2       millert    99:                if (n == 0)
1.4       deraadt   100:                        errx(1, "user.cs_path was zero length!");
1.2       millert   101:                if ((path = (char *)malloc(n)) == NULL)
1.4       deraadt   102:                        errx(1, "can't allocate memory.");
1.2       millert   103:                if (sysctl(mib, 2, path, &n, NULL, 0) == -1)
1.4       deraadt   104:                        err(1, "unable to get user.cs_path");
1.2       millert   105:        } else {
                    106:                if ((path = getenv("PATH")) == NULL)
1.4       deraadt   107:                        err(1, "can't get $PATH from environment");
1.2       millert   108:        }
1.1       millert   109:
                    110:        /* To make access(2) do what we want */
                    111:        if (setgid(getegid()))
1.4       deraadt   112:                err(1, "Can't set gid to %u", getegid());
1.1       millert   113:        if (setuid(geteuid()))
1.4       deraadt   114:                err(1, "Can't set uid to %u", geteuid());
1.1       millert   115:
1.3       millert   116:        for (n = optind; n < argc; n++)
                    117:                if (findprog(argv[n], path, progmode, allmatches) == 0)
1.1       millert   118:                        notfound++;
                    119:
                    120:        exit((notfound == 0) ? 0 : ((notfound == argc - 1) ? 2 : 1));
                    121: }
                    122:
                    123: int
1.8     ! millert   124: findprog(char *prog, char *path, int progmode, int allmatches)
1.1       millert   125: {
                    126:        char *p, filename[MAXPATHLEN];
1.3       millert   127:        int proglen, plen, rval = 0;
1.1       millert   128:        struct stat sbuf;
                    129:
                    130:        /* Special case if prog contains '/' */
                    131:        if (strchr(prog, '/')) {
                    132:                if ((stat(prog, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
                    133:                    access(prog, X_OK) == 0) {
                    134:                        (void)puts(prog);
                    135:                        return(1);
                    136:                } else {
                    137:                        (void)printf("%s: Command not found.\n", prog);
                    138:                        return(0);
                    139:                }
                    140:        }
                    141:
                    142:        if ((path = strdup(path)) == NULL)
1.4       deraadt   143:                errx(1, "Can't allocate memory.");
1.1       millert   144:
                    145:        proglen = strlen(prog);
                    146:        while ((p = strsep(&path, ":")) != NULL) {
                    147:                if (*p == '\0')
                    148:                        p = ".";
                    149:
                    150:                plen = strlen(p);
                    151:                while (p[plen-1] == '/')
                    152:                        p[--plen] = '\0';       /* strip trailing '/' */
                    153:
                    154:                if (plen + 1 + proglen >= sizeof(filename)) {
                    155:                        warnx("%s/%s: %s", p, prog, strerror(ENAMETOOLONG));
                    156:                        return(0);
                    157:                }
                    158:
1.8     ! millert   159:                snprintf(filename, sizeof(filename), "%s/%s", p, prog);
1.1       millert   160:                if ((stat(filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
                    161:                    access(filename, X_OK) == 0) {
                    162:                        (void)puts(filename);
1.3       millert   163:                        rval = 1;
                    164:                        if (!allmatches)
                    165:                                return(rval);
1.1       millert   166:                }
                    167:        }
                    168:        (void)free(path);
                    169:
1.2       millert   170:        /* whereis(1) is silent on failure. */
1.3       millert   171:        if (!rval && progmode != PROG_WHEREIS)
1.2       millert   172:                (void)printf("%s: Command not found.\n", prog);
1.3       millert   173:        return(rval);
1.1       millert   174: }
                    175:
                    176: void
1.8     ! millert   177: usage(void)
1.1       millert   178: {
1.3       millert   179:        (void) fprintf(stderr, "Usage: %s [-a] name [...]\n", __progname);
1.1       millert   180:        exit(1);
                    181: }