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