Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!rutgers!labrea!decwrl!sun!gorodish!guy From: guy%gorodish@Sun.COM (Guy Harris) Newsgroups: comp.lang.c Subject: Re: Accessing argc & argv from a functi Message-ID: <24333@sun.uucp> Date: Tue, 28-Jul-87 14:38:53 EDT Article-I.D.: sun.24333 Posted: Tue Jul 28 14:38:53 1987 Date-Received: Thu, 30-Jul-87 02:34:29 EDT References: <420@sugar.UUCP> <39@flmis06.ATT.COM> Sender: news@sun.uucp Lines: 40 > >> In standard C you cannot get access to argv/argc/envp without doing work > >> in main(). > > > > getenv()? putenv()? execl()? execv()? execlp()? execvp()? > > Sure, all of the above access the enviorment in one way or another. > > However I am still looking for a way to get at "argc" and "argv" from > a function when I don't have access to "main". getenv() and putenv() > gets it from somewhere... No, "getenv" and "putenv" get at *envp*; they don't look at "argv" or "argc" at all. In UNIX C implementations, at least, there is a global variable "environ", to which the value of "envp" is assigned by the C startup code. Note, however, that if we're truly talking about "standard" C, there isn't even a guarantee that "argv", "argc", or "envp" even *exist*! According to the current ANSI C draft, in a "freestanding environment" (where the C program may be run "without any benefit of an operating system"), "the name and type of the function called at program startup are implementation-defined". In a "hosted environment", the function in question is called "main"; I infer from the description that the function is guaranteed to be called with two arguments, "argc" and "argv" (it says the function can be defined with no parameters, or with the usual two parameters, which I assume means either is valid in any hosted environment). The "envp" argument is listed only in Appendix A, under "Common extensions". > As far as the rest of the above mentioned functions, you will need to > have the args (or at least create your own) first, before you can pass > them on... Arguments yes, environment no. Those routines get the environment from "environ". Guy Harris {ihnp4, decvax, seismo, decwrl, ...}!sun!guy guy@sun.com