bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: psmith@gnu.org
Cc: bug-gnulib@gnu.org
Subject: Re: [PATCH] findprog: Support searching in a specified path string
Date: Sun, 08 Sep 2019 16:59:45 +0200	[thread overview]
Message-ID: <2257421.7xAeYRz9PB@omega> (raw)
In-Reply-To: <377844c9fe46960a77d6e0ef0076a386bfa52a3a.camel@gnu.org>

Hi Paul,

> > >     prog = find_in_path_str ("myprog", lookup_path ());
> > > 
> > > and if lookup_path() returns NULL it defaults to the environment
> > > PATH,
> > 
> > I don't think NULL should be interpreted as "look in the default
> > PATH".  Rather, the convention is that an empty or null PATH means
> > the current directory.
> 
> I find that VERY odd, and possibly a security risk.  According to
> POSIX, the behavior if PATH is not present or is empty is
> implementation-defined and my preference/expectation would be to behave
> as if the program is not found.  However, that's a different
> discussion.

Your preference does not match what systems do.

Things are most robust if - the other differences set aside -
  find_in_path (prog)
is equivalent to
  find_in_path_str (prog, getenv ("PATH")).

I wouldn't want the two to make different interpretations of the
empty or absent PATH.

> > Note also that what you need is not merely an *attempt* to determine
> > the filename of the executable, but you need it always - since you
> > _don't_ want the program to be looked up in the parent's PATH. Thus
> > you need also a return value that indicates that the program was not
> > found in PATH.
> 
> There is already a return value to that effect in find_prog_in_path():
> it returns the same pointer that was passed in.

No, the returned pointer is the same as the argument not only in the
"not found" case, but also in the "already contains a slash" case.

> > Otherwise,
> > assume
> >   parent PATH = "/bin:/usr/bin"
> >   child PATH = "/tmp"
> >   program = "cat"
> > the find_in_path_str search would do a lookup in the child PATH, not
> > find it, return "cat", and the posix_spawnp would then find "cat" in
> > the parent PATH and invoke /bin/cat.
> 
> Sorry, I should have called it out more clearly, but in my email I said
> that after this lookup we would invoke posix_spawn() (no "p").

Makes sense.

On the other hand, if the function already determined that the program
cannot be found, there's no point in calling posix_spawn() or execl().
To allow the caller to do this optimization, a NULL return value is useful.

> > This, in turn, means that we need to provide also an implementation
> > for Windows, Cygwin, EMX, DOS.
> 
> Yes, clearly that would be ideal

OK, I just spent 1 hour determining the suffixes that the different
platforms use in their search.

> it's not needed for my use-case since
> I use it with posix_spawn() which obviously doesn't exist on these
> other platforms

I still hope someone will write a posix_spawn for native Windows...

> > And this means that it can't really share much with the existing
> > findprog.c.  So the implementation should go into a different .c
> > file.
> 
> I don't see why.

The original findprog.c does not need platform-specific knowledge,
since by definition it's merely an optimization. The code with
platform-specific knowledge will be significantly different.

Bruno



  reply	other threads:[~2019-09-08 15:00 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-06 23:10 [PATCH] findprog: Support searching in a specified path string Paul Smith
2019-09-06 23:21 ` Paul Smith
2019-09-07 10:42 ` Bruno Haible
2019-09-07 13:17   ` Paul Smith
2019-09-08 11:38     ` Bruno Haible
2019-09-08 14:03       ` Paul Smith
2019-09-08 14:59         ` Bruno Haible [this message]
2019-09-08 16:25           ` Bruno Haible
2019-09-08 17:34           ` Paul Smith
2019-09-08 17:48             ` Bruno Haible
2019-09-08 17:59               ` Paul Smith
2019-09-09 18:54                 ` Bruno Haible
2019-09-10 13:18                   ` Paul Smith
2019-09-14 11:20                     ` Bruno Haible

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2257421.7xAeYRz9PB@omega \
    --to=bruno@clisp.org \
    --cc=bug-gnulib@gnu.org \
    --cc=psmith@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).