Gak! The "should" was hedged with quotes because I couldn't verify
how it behaved. Apparently it's just a quick way to access argv[0].
Thanks!
On 30 May 2006 16:29:31 +0200, Artur Grabowski <[EMAIL PROTECTED]> wrote:
"Peter Blair" <[EMAIL PROTECTED]> writes:
> Be careful -- if you have an application "say /usr/local/whatever/foo"
> that is linked from "/usr/local/bin/bar" then when you call
> "/usr/local/bin/bar" it will populate "bar" as the argv[0] element.
> This may be what you want, but then again, perhaps you want to know
> that "foo" is the application being called.
>
> "__progname" *should* ensure that "foo" is is being used when doing
> things like opening app-name specific logs etc.
"should"? Says who? How do you handle hard links then? Why should anyone
even care what the file name was? The kernel isn't even aware that
a symbolic link was followed when performing the exec system call (it
could be made aware of the fact by horrible layering violations in the
filesystem code and/or best effort measures that would break down in
case of hard links).
$ grep usage: /usr/src/bin/rm/rm.c
(void)fprintf(stderr, "usage: %s [-dfiPRr] file ...\n", __progname);
$ rm -J
rm: unknown option -- J
usage: rm [-dfiPRr] file ...
$ ln -s /bin/rm /tmp/foo
$ /tmp/foo -J
foo: unknown option -- J
usage: foo [-dfiPRr] file ...
$
See: /usr/src/lib/csu/*/crt0.c that fills in __progname. Considering
that argv[0] can be set to anything by the caller, __progname can be
set to anything too. If you trust it, you're wrong.
//art
> On 5/30/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
> > I usually write a usage function to take a single argument, i.e.
> > usage(const char *progname). When I call usage(), which is always
> > from main(), I just pass in argv[0]. I'm under the impression that
> > argv[0] being set to the name of the program is standard (could be
> > wrong though).