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