home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / c / 12235 < prev    next >
Encoding:
Internet Message Format  |  1992-08-12  |  1.3 KB

  1. From: jbc@hpcuhe.cup.hp.com (Jeff Caldwell)
  2. Date: Wed, 12 Aug 1992 05:09:40 GMT
  3. Subject: Re: How can a program know where it was invoked?
  4. Message-ID: <31460025@hpcuhe.cup.hp.com>
  5. Organization: Hewlett Packard, Cupertino
  6. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!mips!sdd.hp.com!hpscdc!hplextra!hpcc05!hpcuhb!hpcuhe!jbc
  7. Newsgroups: comp.lang.c
  8. References: <1992Aug9.024844.6230@debbie.cc.nctu.edu.tw>
  9. Lines: 27
  10.  
  11. >
  12. >jbc@hpcuhe.cup.hp.com (Jeff Caldwell) writes:
  13. >:One rudimentary method is to use "system".  Compile the following and run it:
  14. >:
  15. >:     #include <stdlib.h>
  16. >:     
  17. >:     main(argc, argv)
  18. >:      int argc;
  19. >:      char     *argv[];
  20. >:     {
  21. >:        char string[20];
  22. >:        sprintf(&string, "which %s", argv[0]);
  23. >:        system(string);
  24. >:     }
  25. >
  26. >Unfortunately, this assumes that you're running the default version of a
  27. >binary.  As a simple example of how this won't work, there are two C
  28. >compilers on SINIX machines; the CES system, and the CCS system.  If my
  29. >PATH is /usr/ccs/bin and I call /usr/ces/bin/cc (which we'll pretend
  30. >uses the above code for some reason), funny things happen.
  31. >
  32.  
  33. Ahhh but notice that the string we are sending to "what" uses argv[0].
  34. "what /usr/ces/bin/cc" will return /usr/ces/bin/cc, not the path search 
  35. for cc.
  36.  
  37.             -Jeff Caldwell
  38.