bdaebd
diff --git a/server/util_script.c b/server/util_script.c
bdaebd
index 4121ae0..b7f8674 100644
bdaebd
--- a/server/util_script.c
bdaebd
+++ b/server/util_script.c
bdaebd
@@ -92,9 +92,21 @@ static void add_unless_null(apr_table_t *table, const char *name, const char *va
bdaebd
     }
bdaebd
 }
bdaebd
 
bdaebd
-static void env2env(apr_table_t *table, const char *name)
bdaebd
+/* Sets variable @name in table @dest from r->subprocess_env if
bdaebd
+ * available, else from the environment, else from @fallback if
bdaebd
+ * non-NULL. */
bdaebd
+static void env2env(apr_table_t *dest, request_rec *r,
bdaebd
+                    const char *name, const char *fallback)
bdaebd
 {
bdaebd
-    add_unless_null(table, name, getenv(name));
bdaebd
+    const char *val;
bdaebd
+
bdaebd
+    val = apr_table_get(r->subprocess_env, name);
bdaebd
+    if (!val)
bdaebd
+        val = apr_pstrdup(r->pool, getenv(name));
bdaebd
+    if (!val)
bdaebd
+        val = apr_pstrdup(r->pool, fallback);
bdaebd
+    if (val)
bdaebd
+        apr_table_addn(dest, name, val);
bdaebd
 }
bdaebd
 
bdaebd
 AP_DECLARE(char **) ap_create_environment(apr_pool_t *p, apr_table_t *t)
bdaebd
@@ -211,37 +223,29 @@ AP_DECLARE(void) ap_add_common_vars(request_rec *r)
bdaebd
             add_unless_null(e, http2env(r, hdrs[i].key), hdrs[i].val);
bdaebd
     }
bdaebd
 
bdaebd
-    env_temp = apr_table_get(r->subprocess_env, "PATH");
bdaebd
-    if (env_temp == NULL) {
bdaebd
-        env_temp = getenv("PATH");
bdaebd
-    }
bdaebd
-    if (env_temp == NULL) {
bdaebd
-        env_temp = DEFAULT_PATH;
bdaebd
-    }
bdaebd
-    apr_table_addn(e, "PATH", apr_pstrdup(r->pool, env_temp));
bdaebd
-
bdaebd
+    env2env(e, r, "PATH", DEFAULT_PATH);
bdaebd
 #if defined(WIN32)
bdaebd
-    env2env(e, "SystemRoot");
bdaebd
-    env2env(e, "COMSPEC");
bdaebd
-    env2env(e, "PATHEXT");
bdaebd
-    env2env(e, "WINDIR");
bdaebd
+    env2env(e, r, "SystemRoot", NULL);
bdaebd
+    env2env(e, r, "COMSPEC", NULL);
bdaebd
+    env2env(e, r, "PATHEXT", NULL);
bdaebd
+    env2env(e, r, "WINDIR", NULL);
bdaebd
 #elif defined(OS2)
bdaebd
-    env2env(e, "COMSPEC");
bdaebd
-    env2env(e, "ETC");
bdaebd
-    env2env(e, "DPATH");
bdaebd
-    env2env(e, "PERLLIB_PREFIX");
bdaebd
+    env2env(e, r, "COMSPEC", NULL);
bdaebd
+    env2env(e, r, "ETC", NULL);
bdaebd
+    env2env(e, r, "DPATH", NULL);
bdaebd
+    env2env(e, r, "PERLLIB_PREFIX", NULL);
bdaebd
 #elif defined(BEOS)
bdaebd
-    env2env(e, "LIBRARY_PATH");
bdaebd
+    env2env(e, r, "LIBRARY_PATH", NULL);
bdaebd
 #elif defined(DARWIN)
bdaebd
-    env2env(e, "DYLD_LIBRARY_PATH");
bdaebd
+    env2env(e, r, "DYLD_LIBRARY_PATH", NULL);
bdaebd
 #elif defined(_AIX)
bdaebd
-    env2env(e, "LIBPATH");
bdaebd
+    env2env(e, r, "LIBPATH", NULL);
bdaebd
 #elif defined(__HPUX__)
bdaebd
     /* HPUX PARISC 2.0W knows both, otherwise redundancy is harmless */
bdaebd
-    env2env(e, "SHLIB_PATH");
bdaebd
-    env2env(e, "LD_LIBRARY_PATH");
bdaebd
+    env2env(e, r, "SHLIB_PATH", NULL);
bdaebd
+    env2env(e, r, "LD_LIBRARY_PATH", NULL);
bdaebd
 #else /* Some Unix */
bdaebd
-    env2env(e, "LD_LIBRARY_PATH");
bdaebd
+    env2env(e, r, "LD_LIBRARY_PATH", NULL);
bdaebd
 #endif
bdaebd
 
bdaebd
     apr_table_addn(e, "SERVER_SIGNATURE", ap_psignature("", r));