Re: [hackers] [sbase] [PATCH] which: absolute path not handled

From: Evan Gates <evan.gates_AT_gmail.com>
Date: Tue, 10 Oct 2017 17:56:35 -0700

I dislike that which(1) is even part of sbase as it's not POSIX. The
sh builtin 'command' can and should be used in its place as it is
standardized.

That being said you are correct, that output is terrible and a patch
would be appreciated.


On Tue, Oct 10, 2017 at 3:56 PM Pieter Kockx <pieterkockx_AT_gmail.com> wrote:
>
> Hello hackers
>
> $ which /usr/bin/env
> /usr/bin//usr/bin/env
>
> which should probably be idempotent when applied to its own output as in which $(which $(which env)).
>
> The underlying reason is that fstatat ignores dirfd if the the given
> filename is an absolute path.
> The following fix is possible:
>
> if ((dirfd = open(p, O_RDONLY, 0)) >= 0) {
> if (!fstatat(dirfd, name, &st, 0) &&
> S_ISREG(st.st_mode) &&
> !faccessat(dirfd, name, X_OK, 0)) {
> found = 1;
> + if (name[0] == '/') {
> + puts(name);
> + close(dirfd);
> + break;
> + }
> fputs(p, stdout);
> if (i && ptr[i - 1] != '/')
> fputc('/', stdout);
> puts(name);
> if (!aflag) {
> close(dirfd);
> break;
> }
> }
> }
>
> This fix is not perfect though, since which will (still) return
> a different result if PATH contains no valid directories:
>
> $ PATH=/foo which /usr/bin/env
> Error
> $ PATH=/ which /usr/bin/env
> /usr/bin/env
>
> Patch incoming.
>
> Something else, utilities like which are often used used to check for the existence of a file, so printing an error message seems undesirable.
>
> Thoughts?
>
> -- Pieter
Received on Wed Oct 11 2017 - 02:56:35 CEST

This archive was generated by hypermail 2.3.0 : Wed Oct 11 2017 - 03:00:41 CEST