Finding where an executable was run from -- a proposal.

David Goodenough dg at lakart.UUCP
Sat May 14 00:43:16 AEST 1988


In article <4527 at hoptoad.uucp> gnu at hoptoad.uucp (John Gilmore) writes:
>If exec() would pass this value to the executed program, say as
>argv[-1], then a program could reliably know its own name, and apply a
>simple transformation to it to find its data files (e.g. for program
>"XXXXXX/foo", its data files are found in "XXXXXX/lib/foo/whatever").
>This works for all values of XXXXXX, whether absolute or relative.
>For a subsystem like uucp, you would turn e.g. XXXXXX/uucico into
>XXXXXX/lib/uucp/whatever (replace program name with subsystem name).

Wait just a minute.

If the information is REALLY important, argv[0] is the FULL PATH NAME
that the program was invoked with:

Script started on Fri May 13 10:32:21 1988
lakart!dg(~)[1]-> cat eco.c
main(argc, argv)
char **argv;
 {
    printf("%s\n", argv[0]);
 }
lakart!dg(~)[2]-> eco
eco
lakart!dg(~)[3]-> ./eco
./eco
lakart!dg(~)[4]-> cd ..
lakart!dg(/u2)[5]-> dg/eco
dg/eco
lakart!dg(/u2)[6]-> cd dg/src
lakart!dg(src)[7]-> ../eco
../eco
lakart!dg(src)[8]-> echo ~dg/eco
/u2/dg/eco
lakart!dg(src)[9]-> ~dg/eco
/u2/dg/eco
lakart!dg(src)[10]-> ^D
script done on Fri May 13 10:33:05 1988

Now, if argv[0][0] is a '/' everything is OK, else just do a

popen("pwd", "r");

suck it all up, and prepend it to argv[0], with an intervening '/'. You
may not have an optimal path, BUT IT WILL BE CORRECT, and ABSOLUTE.
Now you can go to work.
-- 
	dg at lakart.UUCP - David Goodenough		+---+
							| +-+-+
	....... !harvard!adelie!cfisun!lakart!dg	+-+-+ |
						  	  +---+



More information about the Comp.unix.wizards mailing list