Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!sundc!pitstop!sun!quintus!ok From: ok@quintus.UUCP (Richard A. O'Keefe) Newsgroups: comp.unix.questions Subject: Re: rsh, rcp and the message "Where are you?" Message-ID: <186@cresswell.quintus.UUCP> Date: Sun, 15-Nov-87 19:12:38 EST Article-I.D.: cresswel.186 Posted: Sun Nov 15 19:12:38 1987 Date-Received: Tue, 17-Nov-87 01:53:58 EST References: <173@tarski.quintus.UUCP> <9312@mimsy.UUCP> <365@minya.UUCP> Organization: Quintus Computer Systems, Mountain View, CA Lines: 58 Keywords: UNIX exec arguments Summary: finding argv[0] In article <365@minya.UUCP>, jc@minya.UUCP (John Chambers) writes: > a global environ[] so we can check the environment without permission > from main(), but I have never seen mention of a similar global pointer > that leads to the argv[] array. In every UNIX system I've used (that only includes ones where sizeof (int) == sizeof (char*), but it includes PDP-11s, VAXen, a couple of 68000s, and an 80386), there is a a chunk of memory that looks like this: argc : int | addresses argv[0] : char* | increase ... : going argv[argc-1] : char* | down the NULL : char* \|/ page *environ -----> envv[0] : char* ... envv[last] : char* NULL : char* I believe this was actually documented in one of the Version 7 manuals. If you can't get at the argument list any other way, the following function may be of use: /* arguments(int *pargc, char ***pargv) If this function can find the program's argv[] vector -- see execl(3) in the UNIX manual -- it sets *pargc to the number of arguments and *pargv to a pointer to the argv[] vector, and then returns 1. If it cannot find the argument vector, it returns 0 without changing either argument. The limit ARGMAX is a typical limit on the number of arguments which could have been passed. If either argument is NULL that argument is not set. For example, to find the name the program was invoked as, do char **argv; if (arguments(NULL, &argv)) { program_name = argv[0]; } */ #define ARGMAX 1024 int arguments(pargc, pargv) int *pargc; char ***pargv; { extern char **environ; char **p; int n; p = environ; if (!*--p) { for (n = 0; n < ARGMAX; n++) { if ((int)*--p != n) continue; if (pargc) *pargc = n; if (pargv) *pargv = p+1; return 1; } } return 0; }