which: handle relative/absolute pathnames
This commit is contained in:
parent
d15f49e715
commit
5b66e55a83
11
which.c
11
which.c
|
@ -21,6 +21,15 @@ which(const char *path, const char *name)
|
||||||
struct stat st;
|
struct stat st;
|
||||||
int dirfd, found = 0;
|
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);
|
ptr = p = enstrdup(3, path);
|
||||||
len = strlen(p);
|
len = strlen(p);
|
||||||
for (i = 0; i < len + 1; i++) {
|
for (i = 0; i < len + 1; i++) {
|
||||||
|
@ -80,7 +89,7 @@ main(int argc, char *argv[])
|
||||||
if (which(path, *argv)) {
|
if (which(path, *argv)) {
|
||||||
found = 1;
|
found = 1;
|
||||||
} else {
|
} else {
|
||||||
weprintf("%s: command not found.\n", *argv);
|
weprintf("%s: not an external command\n", *argv);
|
||||||
foundall = 0;
|
foundall = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user