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