Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!uunet!seismo!mcnc!unc!steele
From: steele@unc.cs.unc.edu (Oliver Steele)
Newsgroups: comp.lang.c
Subject: Re: Accessing argc & argv from a function
Message-ID: <820@unc.cs.unc.edu>
Date: Fri, 17-Jul-87 09:36:24 EDT
Article-I.D.: unc.820
Posted: Fri Jul 17 09:36:24 1987
Date-Received: Sat, 18-Jul-87 13:39:19 EDT
References: <22@flmis06.ATT.COM>
Reply-To: steele@unc.UUCP (Oliver Steele)
Organization: University of North Carolina, Chapel Hill
Lines: 44

In article <22@flmis06.ATT.COM> mikel@flmis06.ATT.COM (Mikel Manitius) writes:
>I recently ran accross an interresting question.
>
>How does one get at argc and argv (and possibly envp) from a function?
>Without declaring it in main first, and then passing a pointer (global
>or not)! Assume you don't have control over what happens in main. Can
>you still get at the argument vector?

If your question is theoretic, this is probably cheating, but if it's
something that you really need to do here's how:

If you're under UN*X, include some code
    int     myArgc;
    char    **myArgv;
    char    *myEnvp;
    myMain(argc,argv,envp)
	int     argc;
	char    **argv;
	char    *envp;
    {
	myArgc = argc;
	myArgv = argv;
	return main(argc,argv,envp);
    }
and use the -e option of ld.  my{Arg[cv],Envp} have global scope.

If you're on some system without ld, you'll need to find out how to
set the loader to use a different entry point from main().

Alternatively, find out the stack-frame format on your machine, and
create a function that looks down the stack for argc etc. and saves them
for future reference by your other functions.  If main() does not do
stuff like argc--, and if you can guarantee that your function will
always be called from the same depth or that it can find the correct
stack frame in some other way, this will work.  (By the way, this is
non-portable :-).

Note that setenv() and getenv() are preferred for accessing envp.

------------------------------------------------------------------------------
Oliver Steele				  ...!{decvax,ihnp4}!mcnc!unc!steele
							steele%unc@mcnc.org

	"They're directly beneath us, Moriarty.  Release the piano!"