Path: utzoo!mnetor!uunet!husc6!ncar!gatech!mcnc!decvax!decwrl!pyramid!prls!philabs!micomvax!zap!iros1!mcgill-vision!mouse From: mouse@mcgill-vision.UUCP (der Mouse) Newsgroups: comp.unix.questions Subject: Re: How does a program get its path name? Message-ID: <994@mcgill-vision.UUCP> Date: 13 Mar 88 03:34:41 GMT References: <11923@brl-adm.ARPA> <2933@sdsu.UUCP> Organization: McGill University, Montreal Lines: 25 In article <2933@sdsu.UUCP>, turtle@sdsu.UUCP (Andrew Scherpbier) writes: > In article <11923@brl-adm.ARPA> Leisner.Henr@xerox.com (marty) writes: >> How does an exec program get the pathname it was execed from if it >> wants to find out this information? In general, it can't. > When a program executes, the full path to the executable file is kept > in the zero-th argument. If you have a declaration of main which > looks like this: > main(argc,argv) int argc; char *argv[]; > then argv[0] is a pointer to the full path. Not at all. argv[0] is merely whatever was passed to the exec-family routine that executed the program. The shell conventionally passes whatever name the program was called by in this field, but this (a) is not always a full pathname and (b) is not a universal convention in the first place. If a program wants, it can run your program and make argv[0] be "/bin/sh", or "Jan 23 1988, 11:44:82.35 GMT", or whatever it pleases. der Mouse uucp: mouse@mcgill-vision.uucp arpa: mouse@larry.mcrcim.mcgill.edu