Contents of argv[0]

Bill Poser poser at csli.Stanford.EDU
Sun Aug 20 07:59:57 AEST 1989


I don't know of a reliable way of finding out what directory an
executable resides in from within a C program, but there is a
reasonably simple way around this, which is to call the C program
from a shell script that first records the directory in a file.

I have a program that consists of three executables that do a sequence
of overlays. (Don't ask why.) Rather than compile in the path name,
I use the shell script trick. Here is the shell script:

	which $0 > .L3_loc
	$0_top $0 $*

The "which" gets the full path name of the shell script and
writes it into a temp file. The second line executes the top
level C program and passes the arguments to it. The top level
C program then reads the path name from the file. You could also
pass the result of "which" as a command line argument.



More information about the Comp.lang.c mailing list