Abort if the device doesn't exist.
[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.3 2005/08/30 22:42:15 liamfoy Exp $
27  */
28
29 #include <sys/stat.h>
30 #include <sys/param.h>
31
32 #include <err.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37
38 static void     usage(void);
39 static int      print_matches(char *, char *);
40
41 int    silent;
42 int    allpaths;
43
44 int
45 main(int argc, char **argv)
46 {
47        char *p, *path;
48        ssize_t pathlen;
49        int opt, status;
50
51        status = EXIT_SUCCESS;
52
53        while ((opt = getopt(argc, argv, "as")) != -1) {
54                switch (opt) {
55                case 'a':
56                        allpaths = 1;
57                        break;
58                case 's':
59                        silent = 1;
60                        break;
61                default:
62                        usage();
63                        break;
64                }
65        }
66
67        argv += optind;
68        argc -= optind;
69
70        if (argc == 0)
71              usage();
72         
73        if ((p = getenv("PATH")) == NULL)
74                exit(EXIT_FAILURE);
75        pathlen = strlen(p) + 1;
76        path = malloc(pathlen);
77        if (path == NULL)
78                err(EXIT_FAILURE, "malloc failed");
79
80        while (argc > 0) {
81                memcpy(path, p, pathlen);
82
83                if (strlen(*argv) >= FILENAME_MAX ||
84                    print_matches(path, *argv) == -1)
85                        status = EXIT_FAILURE;
86
87                argv++;
88                argc--;
89        }
90
91        exit(status);
92 }
93
94 static void
95 usage(void)
96 {
97        fprintf(stderr, "usage: which [-as] program ...\n");
98        exit(EXIT_FAILURE);
99 }
100
101 static int
102 is_there(const char *candidate)
103 {
104        struct stat fin;
105
106        /* XXX work around access(2) false positives for superuser */
107        if (access(candidate, X_OK) == 0 &&
108            stat(candidate, &fin) == 0 &&
109            S_ISREG(fin.st_mode) &&
110            (getuid() != 0 ||
111            (fin.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0)) {
112                if (!silent)
113                        printf("%s\n", candidate);
114                return (1);
115        }
116        return (0);
117 }
118
119 static int
120 print_matches(char *path, char *filename)
121 {
122        char candidate[PATH_MAX];
123        const char *d;
124        int found;
125
126        if (strchr(filename, '/') != NULL)
127                return (is_there(filename) ? 0 : -1);
128        found = 0;
129        while ((d = strsep(&path, ":")) != NULL) {
130                if (*d == '\0')
131                        d = ".";
132                if (snprintf(candidate, sizeof(candidate), "%s/%s", d,
133                    filename) >= (int)sizeof(candidate))
134                        continue;
135                if (is_there(candidate)) {
136                        found = 1;
137                        if (!allpaths)
138                                break;
139                }
140        }
141        return (found ? 0 : -1);
142 }
143