Path: utzoo!mnetor!uunet!lll-winken!lll-lcc!ames!ll-xn!mit-eddie!killer!pollux!ti-csl!tifsie!kent From: kent@tifsie.UUCP (Russell Kent) Newsgroups: comp.unix.questions Subject: Re: How does a program get its path name? Message-ID: <297@tifsie.UUCP> Date: 24 Feb 88 00:24:02 GMT References: <10106@ulysses.homer.nj.att.com> Organization: TI Process Automation Center, Dallas Texas Lines: 50 in article <10106@ulysses.homer.nj.att.com>, cjc@ulysses.homer.nj.att.com (Chris Calabrese[rs]) says: > > In article <2933@sdsu.UUCP>, turtle@sdsu.UUCP writes: >> >> 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. >> Is this what you were looking for? > > This is true only if the program was not found in the > current working directory, in which case argv[0] will contain > only the name of the program, not the full path. Of course, > you can test to see if argv[0][0] != '/' and getcwd() > to find the full path name. > > Christopher Calabrese Unfortunately, neither of these statements is entirely correct. That argv[0] contains any portion of the pathname of the process's executable file is _merely_ convention on the part of _most_ shells (sh, rsh, csh, ksh, and tcsh). It is perfectly ok to put your mother's maiden name in argv[0] of a process you are about to exec, although this is obviously not very useful. The convention that _most_ shells follows is that argv[0] contains the same text as the first blank-separated field of the command line after any aliasing takes place. This means that: Command Argv[0] cc cc /bin/cc /bin/cc alias rm /bin/rm -i rm jack /bin/rm \rm jill rm The astute read may counter: "But if I do a /bin/cp with no parameters, the computer comes back with "Usage: cp f1 f2" not "Usage: /bin/cp f1 f2". This is because some programs intentionally use only the last portion of the path given in argv[0] (using "argv0 = strrbrk (argv[0], '/');"). This convention is actually implemented in the shell through the use of exec*p(); if you use execl() you can bypass it. -- Russell Kent Phone: +1 214 995 3501 Texas Instruments UUCP address: P.O. Box 655012 MS 3635 ...!convex!smu!tifsie!kent Dallas, TX 75265 ...!ut-sally!im4u!ti-csl!tifsie!kent