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