[hackers] [sbase] which: handle relative/absolute pathnames || Pieter Kockx

From: <git_AT_suckless.org>
Date: Sat, 21 Oct 2017 21:57:39 +0200 (CEST)

commit 5b66e55a83fd9b7bceabbc043e50f77cfe048c35
Author: Pieter Kockx <pieterkockx_AT_gmail.com>
AuthorDate: Wed Oct 11 03:12:31 2017 +0200
Commit: Michael Forney <mforney_AT_mforney.org>
CommitDate: Sat Oct 21 12:44:09 2017 -0700

    which: handle relative/absolute pathnames

diff --git a/which.c b/which.c
index ce312ae..dfc1551 100644
--- a/which.c
+++ b/which.c
_AT_@ -21,6 +21,15 @@ which(const char *path, const char *name)
         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)) {
+ puts(name);
+ return 1;
+ }
+ }
+
         ptr = p = enstrdup(3, path);
         len = strlen(p);
         for (i = 0; i < len + 1; i++) {
_AT_@ -80,7 +89,7 @@ main(int argc, char *argv[])
                 if (which(path, *argv)) {
                         found = 1;
                 } else {
- weprintf("%s: command not found.\n", *argv);
+ weprintf("%s: not an external command\n", *argv);
                         foundall = 0;
                 }
         }
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:31 CEST