How do I get full path of argv[0]?

How do I get full path of argv[0]?

Post by Richard C. Gai » Fri, 09 Jun 1995 04:00:00



I guess the subject says it all.  I'd like to be able to find the full
path of the executable.  I want to do this so I can have a program be
required to be in a certain location.  Any help appreciated.

rick

 
 
 

How do I get full path of argv[0]?

Post by Dave Haverka » Wed, 14 Jun 1995 04:00:00



|> I guess the subject says it all.  I'd like to be able to find the full
|> path of the executable.  I want to do this so I can have a program be
|> required to be in a certain location.  Any help appreciated.

Yucko!! requiring the program be in a specific place...  It is prefered by
many users to be able to add a path their environment, then the program
figures out where it was executed from.  Then if it has additional things
that must be executed they can be installed in a subdirectory in the
directory that users add to their path.
:-)

If it will only be in one specific place, why bother checking to see where
it was executed from.  If you try to execute other things based on this
knowlege... Things break.

|>
|> rick

For a real solution to this you should let it execute from where the
user installed the program.  The program should determine this by doing
getenv("PATH") then parse and search it.   I've done it, and the code
is pretty easy to do.  If you haven't ever done a file search before,
Steven's book on Advanced Programming in the Unix Environment has an
example on finding a file in a directory.

Just remember you need to allow for someone using a "/" or "." as the
first character in the program name to see if they explicitly referenced
the program before you search the environment PATH.  i.e. check argv[0]

Dave

--
David Haverkamp


 
 
 

How do I get full path of argv[0]?

Post by Gordon Burdi » Sun, 18 Jun 1995 04:00:00


Quote:>|> I guess the subject says it all.  I'd like to be able to find the full
>|> path of the executable.  I want to do this so I can have a program be
>|> required to be in a certain location.  Any help appreciated.

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This suggests to me that users may have an interest in subverting
the program.  

Quote:>For a real solution to this you should let it execute from where the
>user installed the program.  The program should determine this by doing
>getenv("PATH") then parse and search it.   I've done it, and the code
>is pretty easy to do.  

...

Quote:>Just remember you need to allow for someone using a "/" or "." as the
>first character in the program name to see if they explicitly referenced
>the program before you search the environment PATH.  i.e. check argv[0]

If someone wants to have the program find the wrong executable,
possibly to bypass security, then be aware that neither argv[0] nor
$PATH need have anything to do with the pathname of the executable
program.  It is easy to execute any program with values like:

argv[0]="Fascist system administrators should go take a flying leap!!"
PATH="`rm -rf /`:`shutdown`:`cp /dev/null /unix`

Do not depend on these being accurate, or even reasonable, in any
situation where users might wish to subvert the program.

                                        Gordon L. Burditt
                                        sneaky.lonestar.org!gordon

 
 
 

1. should argv[0] = "/full/path/to/pgm" or "pgm"?

Should the value of argv[0] as passed to a binary executeable program
be the full path to it or just the ending (tail) part?

Should the concept be different if the binary executeable program is
an interpreter for a script language when invoked via the #! feature
of script execution?  I'm referring to the path of the interpreter as
passed to the interpreter, not the script file as seen by the script.

This issue was raised on comp.os.linux.development.system because of
the fact that Linux 2.2.X has code to intentionally remove the leading
path from the program only when it is the interpreter, but otherwise.
A couple people suggested that this was a good idea but I never saw
any substantial argument in favor (especially that focused on why an
interpreter should be treated differently).  By comparison, Solaris
does what I expect (and currently think is right).

Does anyone here have any useful feedback on this?

Are there any examples of when argv[0] being the full path is useful?

Are there any good reasons why it should not be the full path when
the program is a script interpreter via the #! line?

Are there any security issues involved?

--
| Phil Howard - KA9WGN | My current websites: linuxhomepage.com, ham.org
| phil  (at)  ipal.net +----------------------------------------------------

2. Any application can be kerberized ?

3. How to expand paths in $PATH like "~/bin" to full path?

4. 0% cpu utilization? Bug?

5. Getting the full path name in an 'ls'

6. Help with Twm bug

7. getting ls -l to give full path names

8. SMTP to WFW 3.11...

9. Extracting full path name from ~user/path

10. Full pathname of argv[0]?

11. How to convert a dos path to unix path on Sys V?

12. Is */argv[i] = argv[i + 1];/* safe?

13. printf("*++*argv=%d\t%c\n",c=*++*argv,c); [ c no change ]