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