From: Frans Klaver Date: Thu, 29 Mar 2012 11:31:09 GMT Subject: Re: [PATCH 2/2] git: continue alias lookup on EACCES errors Message-ID: In-Reply-To: <20120328215704.GB10795@sigill.intra.peff.net> On Wed, Mar 28, 2012 at 11:57 PM, Jeff King wrote: > +static int exists_in_PATH(const char *file) > +{ > +       const char *p = getenv("PATH"); > +       struct strbuf buf = STRBUF_INIT; > + > +       if (!p || !*p) > +               return 0; > + > +       while (1) { > +               const char *end = strchrnul(p, ':'); > + > +               strbuf_reset(&buf); > + > +               /* POSIX specifies an empty entry as the current directory. */ > +               if (end != p) { > +                       strbuf_add(&buf, p, end - p); > +                       strbuf_addch(&buf, '/'); > +               } > +               strbuf_addstr(&buf, file); > + > +               if (!access(buf.buf, F_OK)) { > +                       strbuf_release(&buf); > +                       return 1; > +               } > + > +               if (!*end) > +                       break; > +               p = end + 1; > +       } > + > +       strbuf_release(&buf); > +       return 0; > +} I expect that if more post-mortem checking is done, this function is going to need a sibling that provides you with the first found entry in PATH, so you can do more checks on it. > + > +int sane_execvp(const char *file, char * const argv[]) > +{ > +       if (!execvp(file, argv)) > +               return 0; > +       if (errno == EACCES && !strchr(file, '/')) > +               errno = exists_in_PATH(file) ? EACCES : ENOENT; > +       return -1; > +} One of the things I ran into while working on [1] is that quite some errors that are produced can also be caused by the interpreter. This does cover most of the itch I had earlier. I will still want to have the interpreter check [2] in though; errno can for example also be set to ENOENT if the interpreter or a required library isn't available. In that case you wouldn't want to continue to the aliases, right?