James said:
Martin York wrote:
Most OS's will give you the command used to execute your
program in argv[0] This will usually include the path
Actually it's rather usual that argv[0] is simply what you
wrote in the command line as the program's name, without any
additions.
That's more or less what the standard requires. Under Unix, of
course, this only works if the shell used to start the command
collaborates (most do). And it leaves open the question as to
what should be in argv[0] if the command is not started from the
command line, but from some other program.
Also, if I'm not mistaken, the C standard doesn't actually
guarantee that argv[0] will contain anything at all (although
I don't remember if that means that argv[0] could actually be
a null pointer, or if it simply means that it points to an
empty string).
I assume the C++ standard inherits the same specification.
It does, more or less. First, argc may be 0, in which case,
argv[0] is guaranteed to be a null pointer (and accessing any
other argv is undefined behavior). If argc is greater than 0,
then "the string pointed to by argv[0] represents the program
name; argv[0][0] shall be the null character if the program name
is not available from the host environment." Note that this
does NOT say that the implementation may unconditionally place
an empty string in argv; it says that it may put an empty string
there IF "the program name is not available from the host
environment" (from the C standard), or ``argv[0] shall be the
pointer to the initial character of a NTMBS that represents the
name used to invoke the program or ""'' (from the C++ standard).
Note that the C standard does not define what it means by
"program name": is it the basename, the name as it appears in
the command line, or the full pathname? The C++ standard is
univocal: it must be the name as it appears in the command line.
On the other hand, the C standard requires the name, if it is
available; the C++ standard leaves it up to the implementation.
(The reason that I bring up the C standard here is that I
imagine most implementations will use common code for this,
whether main is in C or in C++, and so should be conform to both
standards.)
Of course, this requirement is ignored more often than it is
met; Unix doesn't make the program name available in any shape,
form or fashion, and I don't think Windows does either, so a
conforming C or C++ implementation must, strictly speaking,
always put an empty string in argv[0]. None do, at least that I
know of, and Posix has a conflicting requirement that argv[0]
actually contain a string specified by the program which starts
your program (which calls the system function execv). All of
the Unix shells I have handy under Solaris (sh, ksh and bash)
*do* pass the name from the command line as argv[0], when
starting a program, as does the default command interpreter
under Windows. The bash in CygWin doesn't, however, and of
course, if your program is started by some other tool, you're at
the mercy of that tool. (I have no idea, for example, what
you'll get if your program is started by clicking an icon on the
desktop. Hopefully, whatever is present in the "Target:" field
of the properties, but I have no way of verifying this.)