Document that which() skips empty PATH components

An empty component in PATH, from a leading, trailing, or doubled colon,
means the current directory to execvp() and the shell.  which() has
always skipped them, but nothing said so, or kept it that way.

Say so in the API description and add tests for it.  A caller that
wants the current directory searched must list it, e.g., as ".".

Closes #27
This commit is contained in:
omrzk authored and Joachim Wiberg committed 2026-11-16 14:22:40 +01:00
1 parent fac54a5001
commit dc90e93812
2 files changed
+39 -1

No files matched your search

+36 -1
View File
@@ -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();
}
/**