home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / c / 12218 < prev    next >
Encoding:
Text File  |  1992-08-12  |  1.3 KB  |  43 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!mnemosyne.cs.du.edu!nyx!ebergman
  3. From: ebergman@nyx.cs.du.edu (Eric Bergman-Terrell)
  4. Subject: Re: How can a program know where it was invoked?
  5. Message-ID: <1992Aug12.141025.778@mnemosyne.cs.du.edu>
  6. X-Disclaimer: Nyx is a public access Unix system run by the University
  7.     of Denver for the Denver community.  The University has neither
  8.     control over nor responsibility for the opinions of users.
  9. Sender: usenet@mnemosyne.cs.du.edu (netnews admin account)
  10. Organization: Nyx, Public Access Unix at U. of Denver Math/CS dept.
  11. References: <1992Aug9.024844.6230@debbie.cc.nctu.edu.tw> <31460023@hpcuhe.cup.hp.com>
  12. Date: Wed, 12 Aug 92 14:10:25 GMT
  13. Lines: 28
  14.  
  15. In article <31460023@hpcuhe.cup.hp.com> jbc@hpcuhe.cup.hp.com (Jeff Caldwell) writes:
  16. >>    I would like to know how does a program knows where it was invoked?
  17. >>    For example, 
  18. >>    can foo detect where its binary execution file is?
  19. >
  20. >One rudimentary method is to use "system".  Compile the following and run it:
  21. >
  22. >     #include <stdlib.h>
  23. >     
  24. >     main(argc, argv)
  25. >      int argc;
  26. >      char     *argv[];
  27. >     {
  28. >        char string[20];
  29. >        sprintf(&string, "which %s", argv[0]);
  30. >        system(string);
  31. >     }
  32. >
  33. >            -Jeff Caldwell
  34. >
  35.  
  36.  
  37. What about looking at argv[0].  Isn't this a reasonably portable way of
  38. ascertaining the pathname of the executable? 
  39.  
  40. Terrell
  41.  
  42.  
  43.