diff --git a/src/which.c b/src/which.c index 45e5a38..332c916 100644 --- a/src/which.c +++ b/src/which.c @@ -49,6 +49,9 @@ static char *strip_args(char *path) * Like which(1), or `command -v foo` * @param cmd Command to look for in $PATH * + * Empty components in $PATH are skipped, they do not mean the current + * directory. To search it, list it, e.g., as ".". + * * @returns A malloc()'ed path to @a cmd on success, or @c NULL. */ char *which(const char *cmd) diff --git a/test/which.c b/test/which.c index 8e8d8c8..653e528 100644 --- a/test/which.c +++ b/test/which.c @@ -6,6 +6,41 @@ struct tc { int res; }; +/* Empty PATH components are skipped, they do not mean current directory */ +static int empty(void) +{ + const char *cmd = "executable"; + char *path[] = { + "", + ":", + ":/nonexistent", + "/nonexistent:", + "/nonexistent::/nonexistent", + NULL + }; + int result = 0; + size_t i; + + if (chdir("data/which")) + return 1; + + for (i = 0; path[i]; i++) { + setenv("PATH", path[i], 1); + if (whichp(cmd)) { + fprintf(stderr, "%20s : found in cwd with PATH='%s'\n", cmd, path[i]); + result++; + } + } + + setenv("PATH", "/nonexistent:.", 1); + if (!whichp(cmd)) { + fprintf(stderr, "%20s : not found with . in PATH\n", cmd); + result++; + } + + return result; +} + int main(void) { int result = 0; @@ -38,7 +73,7 @@ int main(void) for (i = 0; test[i].cmd; i++) result += test[i].exp != test[i].res; - return result; + return result + empty(); } /**