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
30#include <sys/param.h>
31#include <sys/stat.h>
32#include <err.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <unistd.h>
37
38static void	 usage(void);
39static int	 print_matches(char *, char *);
40
41static int 	 silent;
42static int 	 allpaths;
43
44int
45main(int argc, char **argv)
46{
47	char *p, *path;
48	size_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, NULL);
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
94static void
95usage(void)
96{
97
98	(void)fprintf(stderr, "usage: which [-as] program ...\n");
99	exit(EXIT_FAILURE);
100}
101
102static int
103is_there(char *candidate)
104{
105	struct stat fin;
106
107	/* XXX work around access(2) false positives for superuser */
108	if (access(candidate, X_OK) == 0 &&
109	    stat(candidate, &fin) == 0 &&
110	    S_ISREG(fin.st_mode) &&
111	    (getuid() != 0 ||
112	    (fin.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0)) {
113		if (!silent)
114			printf("%s\n", candidate);
115		return (1);
116	}
117	return (0);
118}
119
120static int
121print_matches(char *path, char *filename)
122{
123	char candidate[PATH_MAX];
124	const char *d;
125	int found;
126
127	if (strchr(filename, '/') != NULL)
128		return (is_there(filename) ? 0 : -1);
129	found = 0;
130	while ((d = strsep(&path, ":")) != NULL) {
131		if (*d == '\0')
132			d = ".";
133		if (snprintf(candidate, sizeof(candidate), "%s/%s", d,
134		    filename) >= (int)sizeof(candidate))
135			continue;
136		if (is_there(candidate)) {
137			found = 1;
138			if (!allpaths)
139				break;
140		}
141	}
142	return (found ? 0 : -1);
143}
144
145