home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / perl / 5923 < prev    next >
Encoding:
Text File  |  1992-09-14  |  1.9 KB  |  61 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!spillman!kevin
  3. From: kevin@spillman.uucp (Kevin Hoffman)
  4. Subject: help w/userinit() changing environ
  5. Message-ID: <1992Sep14.201519.6216@spillman.uucp>
  6. Date: Mon, 14 Sep 1992 20:15:19 GMT
  7. Organization: Spillman Data Systems
  8. Lines: 51
  9.  
  10. Hello,
  11.  
  12. Larry Wall is there a way that userinit() can add/modify/delete the
  13. environment, without causing it to get clobbered in perl's main()?
  14.  
  15. My userinit() calls putenv() which enlarges the environment (eg. the global
  16. variable environ gets realloc'd which moves it.)  Perl then notices that it
  17. has changed, and clobbers it (see code below.)
  18.  
  19. The main for perl (with my comments added) is declared as follows:
  20.  
  21. char rcsid[] = "$RCSfile: perl.c,v $$Revision: 4.0.1.7 $$Date: 92/06/08 14:50:39 $\nPatch level: ###\n";
  22.  
  23. main(argc,argv,env)
  24. register int argc;
  25. register char **argv;
  26. register char **env;
  27. {
  28.     ...
  29.     userinit(); /* putenv() ( eg. environ= realloc( environ, ... ) ) */
  30.     ...
  31.     if (env != environ)         /* Of course they are now different */
  32.         environ[0] = Nullch;    /* !clobber the environment! (WHY?) */
  33.         /* env= environ;        /* THIS IS WHAT I WANT IT TO DO */
  34.     for (; *env; env++) {       /* build %ENV array from INVALID env */
  35.         if (!(s = index(*env,'=')))
  36.         continue;
  37.         *s++ = '\0';
  38.         str = str_make(s--,0);
  39.         str_magic(str, envstab, 'E', *env, s - *env);
  40.         (void)hstore(stab_hash(envstab), *env, s - *env, str, 0);
  41.         *s = '=';
  42.     }
  43.     ...
  44. }
  45.  
  46. int
  47. userinit()
  48. {
  49.   char buf[80], *myfil;
  50.   char **oldenv= environ;
  51.   char *getenv(), *strdup();
  52.     strcpy( buf, "MYVAR=/tmp/myfile" );
  53.     if( putenv( strdup(buf) ) || NULL == (myfil= getenv("MYVAR")) )
  54.         fprintf( stderr, "Insufficient memory to putenv %s", buf );
  55.     else if( oldenv != environ )
  56.         fprintf( stderr, "putenv() enlarged the environment and moved it" );
  57. }
  58.  
  59. Thanks
  60. Kevin Hoffman   kevin@spillman.com   -OR-  ...!uunet!spillman!kevin
  61.