In <[EMAIL PROTECTED]>, Dag-Erling Smørgrav <[EMAIL PROTECTED]> typed:
> Mike Meyer <[EMAIL PROTECTED]> writes:
> > However, you can also make a best effort. Check argv[0] to see if you
> > have an absolute path. If not, do what the shell would do to find the
> > executable - look for it on $PATH. Once you've found it, call
> > realpath(3) to canonicalize the path.
> You got it the wrong way around.  First pass argv[0] to realpath(3),
> fall back to using $PATH only if realpath(3) fails (which it shouldn't
> unless you've called chdir(2), chroot(2) or jail(2) earlier in the
> process, or the executable was moved or removed)

No, I got it the right way 'round. If the shell walks the PATH, then
calling realpath(3) on argv[0] is the wrong thing to do, as it'll
resolve the path relative to the pwd. In particular, since realpath(3)
doesn't care if the last component of the path actually exists or not,
any argv[0] that doesn't have a '/' in it will be mapped to that name
in the current directory. That's assuming that the shell doesn't pass
an argv[0] that's the full path to the executable; if the shell does
that, then my version still works. Since there are shells that don't
do that, you gotta walk the PATH before calling realpath(3) if the
shell walked the PATH.

Testing for absolute paths is the wrong way test for "did the shell
walk PATH", though. You should test for any occurrence of '/' instead,
but being absolutely right about it may involve changing the test to
match the shell being used.

        <mike
-- 
Mike Meyer <[EMAIL PROTECTED]>          http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.
_______________________________________________
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "[EMAIL PROTECTED]"

Reply via email to