Contents of argv[0]

Oliver Laumann net at tub.UUCP
Tue Aug 22 21:46:49 AEST 1989


In article <1935 at ifi.uio.no> gisle at ifi.uio.no (Gisle Hannemyr) writes:
> gandalf at 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 at TUB.BITNET              net at tub.UUCP



More information about the Comp.lang.c mailing list