It was pretty much impossible to understand what Perl_my_setenv (util.c) was doing due the mix of code and macro conditionals and lack of proper indentation. I've polished that function a bit simplifying and indenting things. Please consider committing it. Here it is (and attached as a diff).

void
Perl_my_setenv(pTHX_ char *nam, char *val)
{
#ifdef USE_ITHREADS
    /* only parent thread can modify process environment */
    if (PL_curinterp != aTHX) {
        return;
    }
#endif

#ifndef PERL_USE_SAFE_PUTENV
    if (!PL_use_safe_putenv) {
        /* most putenv()s leak, so we manipulate environ directly */
        register I32 i=setenv_getix(nam);    /* where does it go? */
        int nlen, vlen;

        if (environ == PL_origenviron) { /* need we copy environment? */
            I32 j;
            I32 max;
            char **tmpenv;

            /*SUPPRESS 530*/
            for (max = i; environ[max]; max++) ;
            tmpenv = (char**)safesysmalloc((max+2) * sizeof(char*));
            for (j=0; j<max; j++) {          /* copy environment */
                int len = strlen(environ[j]);
                tmpenv[j] = (char*)safesysmalloc((len+1)*sizeof(char));
                Copy(environ[j], tmpenv[j], len+1, char);
            }
            tmpenv[max] = Nullch;
            environ = tmpenv;           /* tell exec where it is now */
        }
        if (!val) {
            safesysfree(environ[i]);
            while (environ[i]) {
                environ[i] = environ[i+1];
                i++;
            }
            return;
        }
        if (!environ[i]) {                      /* does not exist yet */
            environ = (char**)safesysrealloc(environ, (i+2) * sizeof(char*));
            environ[i+1] = Nullch;      /* make sure it's null terminated */
        }
        else {
            safesysfree(environ[i]);
        }

        nlen = strlen(nam);
        vlen = strlen(val);

        environ[i] = (char*)safesysmalloc((nlen+vlen+2) * sizeof(char));
        /* all that work just for this */
        my_setenv_format(environ[i], nam, nlen, val, vlen);
    } else {
# endif /* PERL_USE_SAFE_PUTENV */

#   if defined(__CYGWIN__) || defined(EPOC)
        setenv(nam, val, 1);
#   else
        char *new_env;
        int nlen = strlen(nam), vlen;
        if (!val) {
            val = "";
        }
        vlen = strlen(val);
        new_env = (char*)safesysmalloc((nlen + vlen + 2) * sizeof(char));
        /* all that work just for this */
        my_setenv_format(new_env, nam, nlen, val, vlen);
        (void)putenv(new_env);
#   endif /* __CYGWIN__ */

#ifndef PERL_USE_SAFE_PUTENV
    }
#endif
}


-- __________________________________________________________________ Stas Bekman JAm_pH ------> Just Another mod_perl Hacker http://stason.org/ mod_perl Guide ---> http://perl.apache.org mailto:[EMAIL PROTECTED] http://use.perl.org http://apacheweek.com http://modperlbook.org http://apache.org http://ticketmaster.com
--- util.c.orig 2004-11-25 18:49:53.875370692 -0500
+++ util.c      2004-11-25 19:04:57.895074572 -0500
@@ -1374,72 +1374,77 @@
 Perl_my_setenv(pTHX_ char *nam, char *val)
 {
 #ifdef USE_ITHREADS
-  /* only parent thread can modify process environment */
-  if (PL_curinterp == aTHX)
+    /* only parent thread can modify process environment */
+    if (PL_curinterp != aTHX) {
+        return;
+    }
 #endif
-  {
+
 #ifndef PERL_USE_SAFE_PUTENV
     if (!PL_use_safe_putenv) {
-    /* most putenv()s leak, so we manipulate environ directly */
-    register I32 i=setenv_getix(nam);          /* where does it go? */
-    int nlen, vlen;
-
-    if (environ == PL_origenviron) {   /* need we copy environment? */
-       I32 j;
-       I32 max;
-       char **tmpenv;
-
-       /*SUPPRESS 530*/
-       for (max = i; environ[max]; max++) ;
-       tmpenv = (char**)safesysmalloc((max+2) * sizeof(char*));
-       for (j=0; j<max; j++) {         /* copy environment */
-           int len = strlen(environ[j]);
-           tmpenv[j] = (char*)safesysmalloc((len+1)*sizeof(char));
-           Copy(environ[j], tmpenv[j], len+1, char);
-       }
-       tmpenv[max] = Nullch;
-       environ = tmpenv;               /* tell exec where it is now */
-    }
-    if (!val) {
-       safesysfree(environ[i]);
-       while (environ[i]) {
-           environ[i] = environ[i+1];
-           i++;
-       }
-       return;
-    }
-    if (!environ[i]) {                 /* does not exist yet */
-       environ = (char**)safesysrealloc(environ, (i+2) * sizeof(char*));
-       environ[i+1] = Nullch;  /* make sure it's null terminated */
-    }
-    else
-       safesysfree(environ[i]);
-    nlen = strlen(nam);
-    vlen = strlen(val);
-
-    environ[i] = (char*)safesysmalloc((nlen+vlen+2) * sizeof(char));
-    /* all that work just for this */
-    my_setenv_format(environ[i], nam, nlen, val, vlen);
+        /* most putenv()s leak, so we manipulate environ directly */
+        register I32 i=setenv_getix(nam);    /* where does it go? */
+        int nlen, vlen;
+        
+        if (environ == PL_origenviron) { /* need we copy environment? */
+            I32 j;
+            I32 max;
+            char **tmpenv;
+            
+            /*SUPPRESS 530*/
+            for (max = i; environ[max]; max++) ;
+            tmpenv = (char**)safesysmalloc((max+2) * sizeof(char*));
+            for (j=0; j<max; j++) {            /* copy environment */
+                int len = strlen(environ[j]);
+                tmpenv[j] = (char*)safesysmalloc((len+1)*sizeof(char));
+                Copy(environ[j], tmpenv[j], len+1, char);
+            }
+            tmpenv[max] = Nullch;
+            environ = tmpenv;           /* tell exec where it is now */
+        }
+        if (!val) {
+            safesysfree(environ[i]);
+            while (environ[i]) {
+                environ[i] = environ[i+1];
+                i++;
+            }
+            return;
+        }
+        if (!environ[i]) {                     /* does not exist yet */
+            environ = (char**)safesysrealloc(environ, (i+2) * sizeof(char*));
+            environ[i+1] = Nullch;     /* make sure it's null terminated */
+        }
+        else {
+            safesysfree(environ[i]);
+        }
+        
+        nlen = strlen(nam);
+        vlen = strlen(val);
+        
+        environ[i] = (char*)safesysmalloc((nlen+vlen+2) * sizeof(char));
+        /* all that work just for this */
+        my_setenv_format(environ[i], nam, nlen, val, vlen);
     } else {
-# endif
-#   if defined(__CYGWIN__) || defined( EPOC)
-    setenv(nam, val, 1);
+# endif /* PERL_USE_SAFE_PUTENV */
+
+#   if defined(__CYGWIN__) || defined(EPOC)
+        setenv(nam, val, 1);
 #   else
-    char *new_env;
-    int nlen = strlen(nam), vlen;
-    if (!val) {
-       val = "";
-    }
-    vlen = strlen(val);
-    new_env = (char*)safesysmalloc((nlen + vlen + 2) * sizeof(char));
-    /* all that work just for this */
-    my_setenv_format(new_env, nam, nlen, val, vlen);
-    (void)putenv(new_env);
+        char *new_env;
+        int nlen = strlen(nam), vlen;
+        if (!val) {
+            val = "";
+        }
+        vlen = strlen(val);
+        new_env = (char*)safesysmalloc((nlen + vlen + 2) * sizeof(char));
+        /* all that work just for this */
+        my_setenv_format(new_env, nam, nlen, val, vlen);
+        (void)putenv(new_env);
 #   endif /* __CYGWIN__ */
+        
 #ifndef PERL_USE_SAFE_PUTENV
     }
 #endif
-  }
 }
 
 #else /* WIN32 || NETWARE */

Reply via email to