2015-04-27 14:33:23 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-04-27 10:24:43 -04:00
|
|
|
#include <sys/stat.h>
|
2015-04-27 14:01:30 -04:00
|
|
|
#include <sys/types.h>
|
2015-04-27 10:24:43 -04:00
|
|
|
|
2015-04-27 14:01:30 -04:00
|
|
|
#include <fcntl.h>
|
2015-04-27 10:24:43 -04:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static int aflag;
|
|
|
|
|
2017-10-20 15:30:20 -04:00
|
|
|
static int
|
|
|
|
canexec(int fd, const char *name)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (fstatat(fd, name, &st, 0) < 0 || !S_ISREG(st.st_mode))
|
|
|
|
return 0;
|
2019-07-27 16:29:10 -04:00
|
|
|
return faccessat(fd, name, X_OK, AT_EACCESS) == 0;
|
2017-10-20 15:30:20 -04:00
|
|
|
}
|
|
|
|
|
2015-04-27 10:24:43 -04:00
|
|
|
static int
|
|
|
|
which(const char *path, const char *name)
|
|
|
|
{
|
2015-04-27 14:01:30 -04:00
|
|
|
char *ptr, *p;
|
|
|
|
size_t i, len;
|
|
|
|
int dirfd, found = 0;
|
2015-04-27 10:24:43 -04:00
|
|
|
|
2017-10-10 21:12:31 -04:00
|
|
|
if (strchr(name, '/')) {
|
2017-10-20 15:31:27 -04:00
|
|
|
found = canexec(AT_FDCWD, name);
|
|
|
|
if (found)
|
2017-10-10 21:12:31 -04:00
|
|
|
puts(name);
|
2017-10-20 15:31:27 -04:00
|
|
|
return found;
|
2017-10-10 21:12:31 -04:00
|
|
|
}
|
|
|
|
|
2015-04-27 14:01:30 -04:00
|
|
|
ptr = p = enstrdup(3, path);
|
|
|
|
len = strlen(p);
|
|
|
|
for (i = 0; i < len + 1; i++) {
|
|
|
|
if (ptr[i] != ':' && ptr[i] != '\0')
|
|
|
|
continue;
|
|
|
|
ptr[i] = '\0';
|
2019-07-27 16:29:11 -04:00
|
|
|
if ((dirfd = open(p, O_RDONLY)) >= 0) {
|
2017-10-20 15:30:20 -04:00
|
|
|
if (canexec(dirfd, name)) {
|
2015-04-27 14:01:30 -04:00
|
|
|
found = 1;
|
|
|
|
fputs(p, stdout);
|
|
|
|
if (i && ptr[i - 1] != '/')
|
|
|
|
fputc('/', stdout);
|
|
|
|
puts(name);
|
|
|
|
}
|
|
|
|
close(dirfd);
|
2017-10-21 01:26:30 -04:00
|
|
|
if (!aflag && found)
|
|
|
|
break;
|
2015-04-27 10:24:43 -04:00
|
|
|
}
|
2015-04-27 14:01:30 -04:00
|
|
|
p = ptr + i + 1;
|
2015-04-27 10:24:43 -04:00
|
|
|
}
|
|
|
|
free(ptr);
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-04-27 14:01:30 -04:00
|
|
|
eprintf("usage: %s [-a] name ...\n", argv0);
|
2015-04-27 10:24:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *path;
|
2015-04-27 14:01:30 -04:00
|
|
|
int found = 0, foundall = 1;
|
2015-04-27 10:24:43 -04:00
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'a':
|
|
|
|
aflag = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
2015-11-01 05:16:49 -05:00
|
|
|
} ARGEND
|
2015-04-27 10:24:43 -04:00
|
|
|
|
|
|
|
if (!argc)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
if (!(path = getenv("PATH")))
|
2015-04-27 14:01:30 -04:00
|
|
|
enprintf(3, "$PATH is not set\n");
|
2015-04-27 10:24:43 -04:00
|
|
|
|
2015-04-27 14:01:30 -04:00
|
|
|
for (; *argv; argc--, argv++) {
|
|
|
|
if (which(path, *argv)) {
|
|
|
|
found = 1;
|
|
|
|
} else {
|
2017-10-10 21:12:31 -04:00
|
|
|
weprintf("%s: not an external command\n", *argv);
|
2015-04-27 14:01:30 -04:00
|
|
|
foundall = 0;
|
|
|
|
}
|
2015-04-27 10:24:43 -04:00
|
|
|
}
|
2015-04-27 14:01:30 -04:00
|
|
|
|
|
|
|
return found ? foundall ? 0 : 1 : 2;
|
2015-04-27 10:24:43 -04:00
|
|
|
}
|