home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.programmer
- Path: sparky!uunet!scorn!brianm
- From: brianm@sco.com (Brian Moffet)
- Subject: Re: How can a program know where it was invoked?
- Organization: The Santa Cruz Operation, Inc.
- Date: Fri, 14 Aug 1992 18:35:21 GMT
- Message-ID: <1992Aug14.183521.1704@sco.com>
- References: <1992Aug9.025101.8559@ccsun7.csie.nctu.edu.tw> <bmckeon.713727455@unix1.tcd.ie> <1992Aug13.182509.10985@nic.umass.edu>
- Sender: news@sco.com (News admin)
- Lines: 35
-
- beowulf@titan.ucc.umass.edu (Peter Munson) writes:
-
- > Tacking the name of the executable onto the directory from which it
- >was _invoked_ will give you garbage unless you happen to be sitting in
- >the same directory as the executable.
- > The correct answer is 'no'.
- >P.
-
- Not necessarily. Under the Unix OS, you can do an attempt at figuring
- out where the binary is:
-
- 1: check to see if the argv[0] is an absolute/relative path name.
- 2: check to see if your psargs have any good information (may not be doable)
- 3: get the environment variable PATH and go through it in order
- checking to see whether the program in question exists in that directory.
- 4: Check to see if there is your program name in the current directory.
-
- If you find that the file you found was actually a symbolic link,
- you have to trace that link to see if it ends with a valid executable.
-
- If all of these fail, you have a program which someone changed the name on
- via execl() or something similar. In this case, you're not going to have
- much luck.
-
- My question would be, "why would you want to know where the program is in the
- first place?" Most cases of having some startup script can be solved by
- placing the startup script (or whatever) someplace sane, not where the
- directory is...
-
- brian moffet
- --
- "Face it, Chuck. Taken as an aggregate, the human race is a stupid, sorry lot."
- -- Jonathan Quist
-
- brianm@sco.com or uunet!sco!brianm Speaking for Myself
-