When can argv[0] have null?

With the exec class of calls, you specify the program name and program executable separately so you can set it to NULL then.

But that quote is actually from the ISO standard (possibly paraphrased) and that standard covers a awfully large range of execution environments from the smallest micro-controller to the latest z10 Enterprise-class mainframe.

Many of those embedded systems would be in the situation where an executable name makes little sense.

From the latest c1x draft:

The value of argc shall be nonnegative.

The value argv[argc] shall be a null pointer.

If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program start up.

This means that, if argc is zero (and it can be), argv[0] is NULL.

But, even when argc is not 0, you may not get the program name, since the standard also states:

If the value of argc is greater than zero, 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. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters.

So, there is no requirement under the standard that a program name be provided. I’ve seen programs use a wide selection of options for this value:

  • no value at all (for supposed security).
  • a blatant lie (such as sleep for a malicious piece of code).
  • the actual program name (such as sleep).
  • a slightly modified one (such as -ksh for the login shell).
  • a descriptive name (e.g., progname - a program for something).

Leave a Comment