Question about execv()
Hi people!
When we use the execv() system call, the very first argument is the path of the program to be exec. What bothers me is why is there a need to include the name of the program as another argument? Please advice! |
Some programs like to know how they where invoked, which is why argv[0] is the program name. As to why its required that you handle it: so you have another opportunity to shoot yourself in the foot, which is a ability unix and C take very seriously. Really, how else could you implement it? Its a low level API, which means the programmer probably needs a very high amount of control, so try to use default and assumptions will just get in the way of letting a programmer shoot themselfs in the foot.
|
Yes. In UNIX-like OS's you can specify a name different from the filename of the executable. This filename would appear as the process name in the output from utils like 'top' or 'ps'.
Your advice is in the man page: Quote:
|
Quote:
|
Also, notice how binaries in your $PATH generally have a "basename" argv[0], but the shell still needs to execute it by its full path name in order to use the current working directory for that program.
ta0kira |
Some commands behave differently depending on the name they are called by. For example, bunzip2 is just a symlink to bzip2. The program checks argv[0] and behaves appropriately (compress or uncompress) depending on how it was called. A lot of programs do this actually.
|
Thank you ppl for the answers!
Pardon me but I do not understand the metaphor "shoot yourself in your foot" -- is it trying to say something nasty about having too much freedom in the way you code? So am I right to say that actually arg[0] is in fact another argument passed to the program, that is directed in path, but for most of the time, arg[0] is simply the same name as the final program that's inside the given file name? TIA! |
If you call a program that's in your path using only its name, for example using "ls" to call /bin/ls, the first argument of execv will be /bin/ls and argv[0] will be ls. If the shell doesn't resolve the name using $PATH then both the first argument of execv and argv[0] will likely be the same. The second argument of execv becomes the array argv in the executed program.
ta0kira |
All times are GMT -5. The time now is 11:36 AM. |