help w/userinit() changing environ
Hello,
Larry Wall is there a way that userinit() can add/modify/delete the
environment, without causing it to get clobbered in perl's main()?
My userinit() calls putenv() which enlarges the environment (eg. the global
variable environ gets realloc'd which moves it.) Perl then notices that it
has changed, and clobbers it (see code below.)
The main for perl (with my comments added) is declared as follows:
char rcsid[] = "$RCSfile: perl.c,v $$Revision: 4.0.1.7 $$Date: 92/06/08 14:50:39 $\nPatch level: ###\n";
main(argc,argv,env)
register int argc;
register char **argv;
register char **env;
{
...
userinit(); /* putenv() ( eg. environ= realloc( environ, ... ) ) */
...
if (env != environ) /* Of course they are now different */
environ[0] = Nullch; /* !clobber the environment! (WHY?) */
/* env= environ; /* THIS IS WHAT I WANT IT TO DO */
for (; *env; env++) { /* build %ENV array from INVALID env */
if (!(s = index(*env,'=')))
continue;
*s++ = '\0';
str = str_make(s--,0);
str_magic(str, envstab, 'E', *env, s - *env);
(void)hstore(stab_hash(envstab), *env, s - *env, str, 0);
*s = '=';
}
...
Quote:
}
int
userinit()
{
char buf[80], *myfil;
char **oldenv= environ;
char *getenv(), *strdup();
strcpy( buf, "MYVAR=/tmp/myfile" );
if( putenv( strdup(buf) ) || NULL == (myfil= getenv("MYVAR")) )
fprintf( stderr, "Insufficient memory to putenv %s", buf );
else if( oldenv != environ )
fprintf( stderr, "putenv() enlarged the environment and moved it" );
Quote:
}
Thanks