[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.2

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