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