Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!brutus.cs.uiuc.edu!apple!agate!shelby!csli!gandalf From: gandalf@csli.Stanford.EDU (Juergen Wagner) Newsgroups: comp.lang.c Subject: Re: Contents of argv[0] Message-ID: <10154@csli.Stanford.EDU> Date: 21 Aug 89 06:01:40 GMT References: <9002@attctc.Dallas.TX.US> <1017@virtech.UUCP> <10094@csli.Stanford.EDU> <1935@ifi.uio.no> <1989Aug20.023248.28923@utzoo.uucp> Sender: gandalf@csli.Stanford.EDU (Juergen Wagner) Reply-To: gandalf@csli.stanford.edu (Juergen Wagner) Organization: Center for the Study of Language and Information, Stanford U. Lines: 42 In some article I wrote a long time ago: > This effectively means that in general, argv[0] cannot > be treated as a reliable source for the path name! [RTFM] Referring to this, gisle@ifi.uio.no (Gisle Hannemyr) wrote: >Is there anything that can be relied upon for this? In a response, henry@utzoo.uucp (Henry Spencer) writes: >No. There are partial solutions to the problem, which are too complex for most cases. In most cases, the simplest thing to do is to rely on the fact that certain conventions are observed, and depending on the environment, the path name can be obtained in one of several ways: [1] Directly from argv[0] (which could be the correct path name). [2] From argv[0] and getenv("PATH") by simulating the shell's interpretation of the search path. [3] In an operating system dependent way. E.g. by examining the inode associated with the text segment of the current process, ... Maybe it is would be a good idea to associate with each process a description of the file the executable image came from... This shouldn't be too difficult, in fact, in most cases, one could do something like that by modifying the exec*() routines in the C library such that they record their first argument in an environment variable "EXEC_PATH" which is passed to the new process. Usually, [1] and [2] cover the cases one is interested in. Other pathological cases might be very interesting but not necessarily important. Happy hacking, Juergen Wagner gandalf@csli.stanford.edu wagner@arisia.xerox.com PS: Also note that the most famous example of argv[0] not being the program name is the way 'login' calls login shells as "-sh" or "-csh"... PPS: It is important to hang on to the idea of conventions (same theme can be found in that ongoing discussion about main() or not main()). They can make like much easier (and reduce our problem to cases [1] and [2]).