ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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:
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])
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.
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?
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.