From d8c22c9ce6a3619fe7a5d1a15b20cb3147a3dcf8 Mon Sep 17 00:00:00 2001 From: Pieter Kockx Date: Wed, 11 Oct 2017 03:12:31 +0200 Subject: [PATCH] which: handle relative/absolute pathnames --- which.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/which.c b/which.c index 1975710..e3ab0d8 100644 --- a/which.c +++ b/which.c @@ -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++) { @@ -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; } } -- 2.13.5