LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Question about execv() (https://www.linuxquestions.org/questions/programming-9/question-about-execv-625325/)

oddabe83 03-03-2008 04:24 AM

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!

SciYro 03-03-2008 04:40 AM

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.

Hko 03-03-2008 04:53 AM

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:

The first argument, by convention, should point to the filename associated with the file being executed.
So when you want to execv, "/usr/local/bin/quatsh" you should (by convention) pass "quatsch" as the name of the process (it argv[0])

osor 03-03-2008 05:15 PM

Quote:

Originally Posted by SciYro (Post 3076462)
Some programs like to know how they where invoked

Just an example of this: if bash is invoked with its argv[0] as “-bash” (rather than “bash”), it will start as a login shell.

ta0kira 03-03-2008 06:39 PM

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

ararus 03-04-2008 12:22 PM

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.

oddabe83 03-06-2008 12:16 PM

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!

ta0kira 03-06-2008 12:35 PM

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.