Xref: utzoo comp.unix.wizards:8564 comp.sources.d:2124 Path: utzoo!utgpu!water!watmath!clyde!ima!cfisun!lakart!dg From: dg@lakart.UUCP (David Goodenough) Newsgroups: comp.unix.wizards,comp.sources.d Subject: Re: Finding where an executable was run from -- a proposal. Message-ID: <107@lakart.UUCP> Date: 13 May 88 14:43:16 GMT References: <52873@sun.uucp> Organization: Lake - The systems people Lines: 50 In article <4527@hoptoad.uucp> gnu@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@lakart.UUCP - David Goodenough +---+ | +-+-+ ....... !harvard!adelie!cfisun!lakart!dg +-+-+ | +---+