LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-03-2008, 04:24 AM   #1
oddabe83
LQ Newbie
 
Registered: Feb 2008
Posts: 2

Rep: Reputation: 0
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!
 
Old 03-03-2008, 04:40 AM   #2
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
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.

Last edited by SciYro; 03-03-2008 at 04:43 AM.
 
Old 03-03-2008, 04:53 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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])
 
Old 03-03-2008, 05:15 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by SciYro View Post
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.

Last edited by osor; 03-03-2008 at 06:51 PM. Reason: typo
 
Old 03-03-2008, 06:39 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 03-04-2008, 12:22 PM   #6
ararus
Member
 
Registered: Mar 2008
Location: UK
Distribution: Slackware
Posts: 56

Rep: Reputation: 15
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.
 
Old 03-06-2008, 12:16 PM   #7
oddabe83
LQ Newbie
 
Registered: Feb 2008
Posts: 2

Original Poster
Rep: Reputation: 0
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!
 
Old 03-06-2008, 12:35 PM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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

Last edited by ta0kira; 03-06-2008 at 12:36 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing groupadd to execv() returned error msg: "file already locked." walwali Programming 9 11-22-2010 06:36 AM
Unrecoverable Error: execv() error attempting to run powah Red Hat 0 09-06-2006 01:15 PM
Question, Apples Contribution to Open Source + MacOs file structure question Higgy3k Other *NIX 5 07-25-2005 04:23 AM
Not your regular GRUB question - just a short question for a fried MBR!! ziphem Linux - General 3 01-31-2005 01:51 PM
login prompt question & kde scheme question JustinCoyan Slackware 2 06-09-2004 02:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration