How to get the pathname of the current executable?

Randal Schwartz merlyn at iwarp.intel.com
Wed Feb 14 04:39:13 AEST 1990


In article <5378 at buengc.BU.EDU>, bph at buengc (Blair P. Houghton) writes:
| Sum hecker.
| 
| Take argv[0], if it doesn't have the path, or the full path,
| cut it up to get the command name, say "prog", then strcat(3)
| it onto "/usr/ucb/which" and call system(3):
| 
|     foo = "/usr/ucb/which prog"
|     system(foo);
| 
| As long as you're still in the directory from which the
| program was run, and as long as your path was the same
| as the one set in your .cshrc (someone please tell me
| why which(1) reads the .cshrc...) then you'll come up
| with /usr/foo/bar/bletch/prog, barring surreptition.

But, this is exactly what I said was subject to spoofing and failure!

There is no general solution that works in all cases, although you can
get a useful answer under *many* typical circumstances.

In case this isn't *very* obvious... remember: argv[0] is ARBITRARY!
Just because the shells typically pass the name of the command (with
or without a leading path, depending on the shell) in argv[0]
*doesn't* mean you can depend on it!

Try reading a little closer next time, please.

Just another UNIX hacker,
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn at iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/



More information about the Comp.unix.questions mailing list