Use the speedup patches, too
[dragonfly.git] / usr.bin / which / which.c
1 /**
2  * Copyright (c) 2000 Dan Papasian.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  * $FreeBSD: src/usr.bin/which/which.c,v 1.5 2002/06/30 06:02:39 tjr Exp $");
26  * $DragonFly: src/usr.bin/which/which.c,v 1.1 2004/03/25 16:18:40 drhodus Exp $
27  */
28
29 #include <sys/cdefs.h>
30
31
32 #include <sys/stat.h>
33 #include <sys/param.h>
34
35 #include <err.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 static void     usage(void);
42 static int      print_matches(char *, char *);
43
44 int    silent;
45 int    allpaths;
46
47 int
48 main(int argc, char **argv)
49 {
50        char *p, *path;
51        ssize_t pathlen;
52        int opt, status;
53
54        status = EXIT_SUCCESS;
55
56        /* If called without args, die silently to conform */
57        if (argc < 2)
58                exit(EXIT_FAILURE);
59
60        while ((opt = getopt(argc, argv, "as")) != -1) {
61                switch (opt) {
62                case 'a':
63                        allpaths = 1;
64                        break;
65                case 's':
66                        silent = 1;
67                        break;
68                default:
69                        usage();
70                        break;
71                }
72        }
73
74        argv += optind;
75        argc -= optind;
76
77        if ((p = getenv("PATH")) == NULL)
78                exit(EXIT_FAILURE);
79        pathlen = strlen(p) + 1;
80        path = malloc(pathlen);
81        if (path == NULL)
82                err(EXIT_FAILURE, NULL);
83
84        if (argc == 0)
85                status = EXIT_FAILURE;
86
87        while (argc > 0) {
88                memcpy(path, p, pathlen);
89
90                if (strlen(*argv) >= FILENAME_MAX ||
91                    print_matches(path, *argv) == -1)
92                        status = EXIT_FAILURE;
93
94                argv++;
95                argc--;
96        }
97
98        exit(status);
99 }
100
101 static void
102 usage(void)
103 {
104
105        errx(EXIT_FAILURE, "usage: which [-as] program ...");
106 }
107
108 static int
109 is_there(char *candidate)
110 {
111        struct stat fin;
112
113        /* XXX work around access(2) false positives for superuser */
114        if (access(candidate, X_OK) == 0 &&
115            stat(candidate, &fin) == 0 &&
116            S_ISREG(fin.st_mode) &&
117            (getuid() != 0 ||
118            (fin.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0)) {
119                if (!silent)
120                        printf("%s\n", candidate);
121                return (1);
122        }
123        return (0);
124 }
125
126 static int
127 print_matches(char *path, char *filename)
128 {
129        char candidate[PATH_MAX];
130        const char *d;
131        int found;
132
133        if (strchr(filename, '/') != NULL)
134                return (is_there(filename) ? 0 : -1);
135        found = 0;
136        while ((d = strsep(&path, ":")) != NULL) {
137                if (*d == '\0')
138                        d = ".";
139                if (snprintf(candidate, sizeof(candidate), "%s/%s", d,
140                    filename) >= (int)sizeof(candidate))
141                        continue;
142                if (is_there(candidate)) {
143                        found = 1;
144                        if (!allpaths)
145                                break;
146                }
147        }
148        return (found ? 0 : -1);
149 }
150