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

From: Michael Forney <mforney_AT_mforney.org>
Date: Tue, 10 Oct 2017 20:06:28 -0700

On 10/10/17, 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:

I think the right thing to do here is to try to stat the file if it
contains a slash, otherwise search through PATH.

> $ 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 - 05:06:28 CEST

This archive was generated by hypermail 2.3.0 : Wed Oct 11 2017 - 05:12:35 CEST