Re: head.c usage function

2006-05-30 Thread matthew . garman
On Fri, May 26, 2006 at 08:47:15PM +0100, Nick Guenther wrote:
 least, it should be) in usage() because the proper form is
 fprintf(stderr, usage: %s [-ks]\n, __progname); where __progname
 gets filled in automatically with the name of the program. I don't
 know the details of how it works though, does anyone have a link
 to an explanation?

I'm not sure about this, but isn't __progname a compiler-specific
extension?

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).

MG



Re: head.c usage function

2006-05-30 Thread Peter Blair

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.

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).




Re: head.c usage function

2006-05-30 Thread Peter Blair

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).




head.c usage function

2006-05-30 Thread Arnaud Bergeron

On 5/30/06, Peter Blair [EMAIL PROTECTED] wrote:

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.


Wrong, argv[0] will be whatever was passed to execvp as the first
element of the second argument.  (If that is not clear remember that
the second argument is a NULL-terminated array of char pointers.)
Although in most cases it will be set to the name of the application
or a path to it.


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.


Wrong again, __progname is derived from argv[0].  That's what enable
things like crunchgen to work by altering the function of the program
based on its name.



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).



__progname is filled in at program initialisation time (in ___start)
with everything after the last / in argv[0].  It has nothing to do
with the compiler.

If you don't trust me, trust the sources:
http://www.openbsd.org/cgi-bin/cvsweb/src/lib/csu/i386/crt0.c?rev=1.13content-type=text/x-cvsweb-markup

--
i think we should rewrite the kernel in java since it has good
support for threads. - Ted Unangst



head.c usage function

2006-05-26 Thread Will H. Backman

Looking at /bin/head source code.
The usage function uses:
  fputs(usage: head [-n line_count] [file ...]\n, stderr);
While many other programs use:
 fprintf(stderr, usage: arch [-ks]\n);

Is there a difference?  Is one preferred?
Yes, I know.  I should take a C programming course.



Re: head.c usage function

2006-05-26 Thread Nick Guenther

On 5/26/06, Will H. Backman [EMAIL PROTECTED] wrote:

Looking at /bin/head source code.
The usage function uses:
   fputs(usage: head [-n line_count] [file ...]\n, stderr);
While many other programs use:
  fprintf(stderr, usage: arch [-ks]\n);

Is there a difference?  Is one preferred?
Yes, I know.  I should take a C programming course.



all the printf functions are formatted printing. If you aren't
formatting then there is no difference. fprintf is more often used (at
least, it should be) in usage() because the proper form is
fprintf(stderr, usage: %s [-ks]\n, __progname);
where __progname gets filled in automatically with the name of the
program. I don't know the details of how it works though, does anyone
have a link to an explanation?

-Nick