home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / programm / 4307 < prev    next >
Encoding:
Text File  |  1992-08-14  |  1.9 KB  |  47 lines

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