Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.csd.uwm.edu!gem.mps.ohio-state.edu!ginosko!uunet!mcvax!unido!tub!net From: net@tub.UUCP (Oliver Laumann) Newsgroups: comp.lang.c Subject: Re: Contents of argv[0] Message-ID: <922@tub.UUCP> Date: 22 Aug 89 11:46:49 GMT References: <9002@attctc.Dallas.TX.US> <1017@virtech.UUCP> <10094@csli.Stanford.EDU> <1935@ifi.uio.no> Reply-To: net@tub.UUCP (Oliver Laumann) Organization: Technical University of Berlin, Germany Lines: 23 In article <1935@ifi.uio.no> gisle@ifi.uio.no (Gisle Hannemyr) writes: > gandalf@csli.Stanford.EDU (Juergen Wagner) wrote: > > This effectively means that in general, argv[0] cannot > > be treated as a reliable source for the path name! [RTFM] > > Is there anything that can be relied upon for this? > I want to know which directory the executable file resided in. To obtain the full path name of the executable you can do the following: If argv[0][0] is a '/', do a stat() on the string and, if a file with this name exists, test whether it is a regular file and whether it is executable. If argv[0][0] is not a '/', append a '/' and argv[0] to each component of $PATH (use getenv() to obtain it) and perform the above test until it succeeds. However, there is no guarantee that this procedure yields the correct path name (or anything useful at all). As I said in an earlier article, argv[0] may not even be there, i.e. it is not guaranteed that argc is non-zero. -- Oliver Laumann net@TUB.BITNET net@tub.UUCP