Handling `argv` in a C command-line program

I read that the first member of the argv array will always be the name of the program.

Do you like hanging it? I am studying, so please forgive me if this is a stupid question.

Do people never change the first term because it is useless (and reset argv less than one?), Or leaves the best practice there because people expect it to always be there (and work out of the box with argc )?

+7
c command-line-arguments
source share
6 answers

I read that the first member of the argv array will always be the program name.

It must be. C and C ++ require that if argc greater than zero, argv[0] must be either a program name or an empty string.

Some systems do not necessarily follow this convention under any circumstances (on Windows, for example, you can use CreateProcess to create a new process and not pass the program name in the command line arguments that are used to populate argc and argv ).

Do you like hanging it?

Of course. If you want, for example, to create another instance of yourself or print the name of your program (for example, in the instructions for use).

Do people never change the first term, because it is useless, or leaves him the best practice?

Do not modify the actual arguments; the next person who comes, probably expects them to be in their original form.

+7
source share

To be precise, argv[0] is all that is passed to exec (2), which is usually the name of a program, but can be anything.

Do not reinstall it, because too many hidden dependencies are calculated on it.

+4
source share

It is expected that argv [0] will always contain the name of the executable. Because of this, it should never be deleted.

As for whether this is useful, one use for argv [0] is if you want your application to change behavior depending on how it was called.

Busybox , for example, uses this on embedded Linux systems. As a rule, you will have one copy of the executable file and create symbolic links to the executable file with different names. eg. cp, rm, ls etc. Busybox then determines which function to perform based on the contents of argv [0].

+3
source share

The first argument is not always the name of the program - it depends on what it is called (see here ).

Often, argv[0] used in the help text:

 fprintf(stderr, "Usage: %s <foo> [bar]\n", argv[0]); 

Array offset is not very useful or efficient. You can just start using argv from index 1.

+3
source share

The contents of argv [0] are not defined by ANSI. argv may be listed:

 main(int argc,char**argv) { while( argc-- ) puts(*argv++); return 0; } 
0
source share

From ISO C99, ยง5.1.2.2.1,

If argc is greater than zero, the line pointed to by argv [0] is the name of the program; argv [0] [0] must be a null character if the program name is not available in the host environment.

Thus, according to implementations, you can rely on argv[0] as the name of the program if you check for its existence earlier (that is, if you can argue that argc > 0 ).

0
source share

All Articles