On Wed, 6 Nov 2002, Peter Rundle wrote: > Coding a 'C' routine I need to know at execute time, the > full path that the program is being executed from. > > In shell this is in $0 and is expanded at runtime even if > the program is found via the $PATH variable, I.e > > echo $0 will always contain the full path to the program.
This behaviour is shell specific and not portable. If you have ash or bsh (or probably ksh) installed you can test it for yourself... they give identical behaviour to what you are experiencing in C. > Any suggestions / cluesticks? To replicate the behaviour you are expecting, you will need to replicate the shell's behaviour in finding the executable. First see if it's an absolute pathname and check if it exists, then try each of the pieces of the PATH environment variable until you find the directory it exists in. I assume you have a good reason for needing this. just about everything else sets up filesystem locations for data files, etc at compile-time... not just because it's marginally easier, but because it's a lot cleaner and more predictable. You do sometimes get the option of over-riding that default with an environment variable, as well. To read environment variables in C use the getenv() function. man 3 getenv -- Jess (Everything with a grin :) -- SLUG - Sydney Linux User's Group - http://slug.org.au/ More Info: http://lists.slug.org.au/listinfo/slug
