[hackers] [sbase] which: Move executable check to helper function || Michael Forney
commit 44265b29ce2da27a525c72e3e9b48f3233535ec1
Author: Michael Forney <mforney_AT_mforney.org>
AuthorDate: Fri Oct 20 12:30:20 2017 -0700
Commit: Michael Forney <mforney_AT_mforney.org>
CommitDate: Sat Oct 21 12:44:09 2017 -0700
which: Move executable check to helper function
diff --git a/which.c b/which.c
index dfc1551..e47aa63 100644
--- a/which.c
+++ b/which.c
_AT_@ -14,17 +14,24 @@
static int aflag;
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;
+ return faccessat(fd, name, X_OK, 0) == 0;
+}
+
+static int
which(const char *path, const char *name)
{
char *ptr, *p;
size_t i, len;
- struct stat st;
int dirfd, found = 0;
if (strchr(name, '/')) {
- if (!fstatat(AT_FDCWD, name, &st, 0) &&
- S_ISREG(st.st_mode) &&
- !access(name, X_OK)) {
+ if (canexec(AT_FDCWD, name)) {
puts(name);
return 1;
}
_AT_@ -37,9 +44,7 @@ which(const char *path, const char *name)
continue;
ptr[i] = '\0';
if ((dirfd = open(p, O_RDONLY, 0)) >= 0) {
- if (!fstatat(dirfd, name, &st, 0) &&
- S_ISREG(st.st_mode) &&
- !faccessat(dirfd, name, X_OK, 0)) {
+ if (canexec(dirfd, name)) {
found = 1;
fputs(p, stdout);
if (i && ptr[i - 1] != '/')
Received on Sat Oct 21 2017 - 21:57:39 CEST
This archive was generated by hypermail 2.3.0
: Sat Oct 21 2017 - 22:00:36 CEST