问题
In the C / Unix environment I work in, I see some developers using __progname
instead of argv[0]
for usage messages. Is there some advantage to this? What's the difference between __progname
and argv[0]
. Is it portable?
回答1:
__progname
isn't standard and therefore not portable, prefer argv[0]
. I suppose __progname
could lookup a string resource to get the name which isn't dependent on the filename you ran it as. But argv[0]
will give you the name they actually ran it as which I would find more useful.
回答2:
Using __progname
allows you to alter the contents of the argv[]
array while still maintaining the program name. Some of the common tools such as getopt()
modify argv[]
as they process the arguments.
For portability, you can strcopy argv[0]
into your own progname
buffer when your program starts.
回答3:
There is also a GNU extension for this, so that one can access the program invocation name from outside of main() without saving it manually. One might be better off doing it manually, however; thus making it portable as opposed to relying on the GNU extension. Nevertheless, I here provide an excerpt from the available documentation.
From the on-line GNU C Library manual (accessed today):
"Many programs that don't read input from the terminal are designed to exit if any system call fails. By convention, the error message from such a program should start with the program's name, sans directories. You can find that name in the variable program_invocation_short_name
; the full file name is stored the variable program_invocation_name
.
Variable: char * program_invocation_name This variable's value is the name that was used to invoke the program running in the current process. It is the same as
argv[0]
. Note that this is not necessarily a useful file name; often it contains no directory names.Variable: char * program_invocation_short_name This variable's value is the name that was used to invoke the program running in the current process, with directory names removed. (That is to say, it is the same as
program_invocation_name
minus everything up to the last slash, if any.)
The library initialization code sets up both of these variables before calling main.
Portability Note: These two variables are GNU extensions. If you want your program to work with non-GNU libraries, you must save the value of argv[0]
in main, and then strip off the directory names yourself. We added these extensions to make it possible to write self-contained error-reporting subroutines that require no explicit cooperation from main."
回答4:
I see at least two potential problems with argv[0].
First, argv[0] or argv itself may be NULL if execve() caller was evil or careless enough. Calling execve("foobar", NULL, NULL) is usually an easy and fun way to prove an over confident programmer his code is not sig11-proof.
It must also be noted that argv will not be defined outside of main() while __progname is usually defined as a global variable you can use from within your usage() function or even before main() is called (like non standard GCC constructors).
回答5:
It's a BSDism, and definitely not portable.
回答6:
__progname is just argv[0], and examples in other replies here show the weaknesses of using it. Although not portable either, I'm using readlink on /proc/self/exe (Linux, Android), and reading the contents of /proc/self/exefile (QNX).
回答7:
If your program was run using, for instance, a symbolic link, argv[0] will contain the name of that link.
I'm guessing that __progname will contain the name of the actual program file.
In any case, argv[0] is defined by the C standard. __progname is not.
来源:https://stackoverflow.com/questions/273691/using-progname-instead-of-argv0