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

1.1     ! millert     1: /*     $OpenBSD$       */
        !             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
        !            34: static char rcsid[] = "$OpenBSD: mktemp.c,v 1.2 1997/01/03 22:49:22 millert Exp $";
        !            35: #endif /* not lint */
        !            36:
        !            37: #include <sys/param.h>
        !            38: #include <sys/stat.h>
        !            39:
        !            40: #include <err.h>
        !            41: #include <errno.h>
        !            42: #include <locale.h>
        !            43: #include <stdio.h>
        !            44: #include <stdlib.h>
        !            45: #include <string.h>
        !            46: #include <unistd.h>
        !            47:
        !            48: extern char *__progname;
        !            49:
        !            50: int which __P((char *, char *));
        !            51: void usage __P((void));
        !            52:
        !            53: /*
        !            54:  * which(1) -- find an executable(s) in the user's path
        !            55:  *
        !            56:  * Return values:
        !            57:  *     0 - all executables found
        !            58:  *     1 - some found, some not
        !            59:  *     2 - none found
        !            60:  */
        !            61:
        !            62: int
        !            63: main(argc, argv)
        !            64:        int argc;
        !            65:        char **argv;
        !            66: {
        !            67:        char *path;
        !            68:        int n, notfound = 0;
        !            69:
        !            70:        (void)setlocale(LC_ALL, "");
        !            71:
        !            72:        if (argc == 1)
        !            73:                usage();
        !            74:
        !            75:        if ((path = getenv("PATH")) == NULL)
        !            76:                err(-1, "Can't get $PATH from environment");
        !            77:
        !            78:        /* To make access(2) do what we want */
        !            79:        if (setgid(getegid()))
        !            80:                err(-1, "Can't set gid to %u", getegid());
        !            81:        if (setuid(geteuid()))
        !            82:                err(-1, "Can't set uid to %u", geteuid());
        !            83:
        !            84:        for (n = 1; n < argc; n++)
        !            85:                if (which(argv[n], path) == 0)
        !            86:                        notfound++;
        !            87:
        !            88:        exit((notfound == 0) ? 0 : ((notfound == argc - 1) ? 2 : 1));
        !            89: }
        !            90:
        !            91: int
        !            92: which(prog, path)
        !            93:        char *prog;
        !            94:        char *path;
        !            95: {
        !            96:        char *p, filename[MAXPATHLEN];
        !            97:        int proglen, plen;
        !            98:        struct stat sbuf;
        !            99:
        !           100:        /* Special case if prog contains '/' */
        !           101:        if (strchr(prog, '/')) {
        !           102:                if ((stat(prog, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
        !           103:                    access(prog, X_OK) == 0) {
        !           104:                        (void)puts(prog);
        !           105:                        return(1);
        !           106:                } else {
        !           107:                        (void)printf("%s: Command not found.\n", prog);
        !           108:                        return(0);
        !           109:                }
        !           110:        }
        !           111:
        !           112:        if ((path = strdup(path)) == NULL)
        !           113:                errx(1, "Can't allocate memory.");
        !           114:
        !           115:        proglen = strlen(prog);
        !           116:        while ((p = strsep(&path, ":")) != NULL) {
        !           117:                if (*p == '\0')
        !           118:                        p = ".";
        !           119:
        !           120:                plen = strlen(p);
        !           121:                while (p[plen-1] == '/')
        !           122:                        p[--plen] = '\0';       /* strip trailing '/' */
        !           123:
        !           124:                if (plen + 1 + proglen >= sizeof(filename)) {
        !           125:                        warnx("%s/%s: %s", p, prog, strerror(ENAMETOOLONG));
        !           126:                        return(0);
        !           127:                }
        !           128:
        !           129:                (void)strcpy(filename, p);
        !           130:                filename[plen] = '/';
        !           131:                (void)strcpy(filename + plen + 1, prog);
        !           132:                if ((stat(filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
        !           133:                    access(filename, X_OK) == 0) {
        !           134:                        (void)puts(filename);
        !           135:                        return(1);
        !           136:                }
        !           137:        }
        !           138:        (void)free(path);
        !           139:
        !           140:        (void)printf("%s: Command not found.\n", prog);
        !           141:        return(0);
        !           142: }
        !           143:
        !           144: void
        !           145: usage()
        !           146: {
        !           147:        (void) fprintf(stderr, "Usage: %s [name ...]\n", __progname);
        !           148:        exit(1);
        !           149: }