You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/09/11 18:05:56 UTC

svn commit: r813896 - in /commons/sandbox/runtime/trunk/src/main/native: include/acr_env.h os/darwin/env.c os/hpux/env.c os/linux/env.c os/solaris/env.c os/unix/temps.c os/win32/env.c shared/ini.c test/testsuite.c

Author: mturk
Date: Fri Sep 11 16:05:56 2009
New Revision: 813896

URL: http://svn.apache.org/viewvc?rev=813896&view=rev
Log:
Strdup the resulting environment var

Modified:
    commons/sandbox/runtime/trunk/src/main/native/include/acr_env.h
    commons/sandbox/runtime/trunk/src/main/native/os/darwin/env.c
    commons/sandbox/runtime/trunk/src/main/native/os/hpux/env.c
    commons/sandbox/runtime/trunk/src/main/native/os/linux/env.c
    commons/sandbox/runtime/trunk/src/main/native/os/solaris/env.c
    commons/sandbox/runtime/trunk/src/main/native/os/unix/temps.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/env.c
    commons/sandbox/runtime/trunk/src/main/native/shared/ini.c
    commons/sandbox/runtime/trunk/src/main/native/test/testsuite.c

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_env.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_env.h?rev=813896&r1=813895&r2=813896&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_env.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_env.h Fri Sep 11 16:05:56 2009
@@ -33,11 +33,19 @@
 
 /**
  * Get the value of an environment variable
- * @param envvar the name of the environment variable
+ * @param envvar the name of the environment variable.
+ * @note The resulting value must be freed when no longer needed.
  */
 ACR_DECLARE(char *) ACR_EnvGet(const char *envvar);
 
 /**
+ * Check the presence of an environment variable
+ * @param envvar the name of the environment variable
+ * @return non zero if variable is present.
+ */
+ACR_DECLARE(int) ACR_EnvHas(const char *var);
+
+/**
  * Set the value of an environment variable
  * @param envvar the name of the environment variable
  * @param value the value to set

Modified: commons/sandbox/runtime/trunk/src/main/native/os/darwin/env.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/darwin/env.c?rev=813896&r1=813895&r2=813896&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/darwin/env.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/darwin/env.c Fri Sep 11 16:05:56 2009
@@ -19,12 +19,18 @@
 #include "acr_arch.h"
 #include "acr_error.h"
 #include "acr_memory.h"
+#include "acr_string.h"
 #include "acr_env.h"
 
 
 ACR_DECLARE(char *) ACR_EnvGet(const char *var)
 {
-    return getenv(var);
+    return ACR_StrdupA(INVALID_HANDLE_VALUE, THROW_NMARK, getenv(var));
+}
+
+ACR_DECLARE(int) ACR_EnvHas(const char *var)
+{
+    return getenv(var) == NULL ? 0 : 1;
 }
 
 ACR_DECLARE(int) ACR_EnvSet(const char *var, const char *val)
@@ -36,4 +42,3 @@
 {
     return unsetenv(var);
 }
-

Modified: commons/sandbox/runtime/trunk/src/main/native/os/hpux/env.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/hpux/env.c?rev=813896&r1=813895&r2=813896&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/hpux/env.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/hpux/env.c Fri Sep 11 16:05:56 2009
@@ -19,11 +19,17 @@
 #include "acr_arch.h"
 #include "acr_error.h"
 #include "acr_memory.h"
+#include "acr_string.h"
 #include "acr_env.h"
 
 ACR_DECLARE(char *) ACR_EnvGet(const char *var)
 {
-    return getenv(var);
+    return ACR_StrdupA(INVALID_HANDLE_VALUE, THROW_NMARK, getenv(var));
+}
+
+ACR_DECLARE(int) ACR_EnvHas(const char *var)
+{
+    return getenv(var) == NULL ? 0 : 1;
 }
 
 ACR_DECLARE(int) ACR_EnvSet(const char *var, const char *val)

Modified: commons/sandbox/runtime/trunk/src/main/native/os/linux/env.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/linux/env.c?rev=813896&r1=813895&r2=813896&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/linux/env.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/linux/env.c Fri Sep 11 16:05:56 2009
@@ -19,12 +19,18 @@
 #include "acr_arch.h"
 #include "acr_error.h"
 #include "acr_memory.h"
+#include "acr_string.h"
 #include "acr_env.h"
 
 
 ACR_DECLARE(char *) ACR_EnvGet(const char *var)
 {
-    return getenv(var);
+    return ACR_StrdupA(INVALID_HANDLE_VALUE, THROW_NMARK, getenv(var));
+}
+
+ACR_DECLARE(int) ACR_EnvHas(const char *var)
+{
+    return getenv(var) == NULL ? 0 : 1;
 }
 
 ACR_DECLARE(int) ACR_EnvSet(const char *var, const char *val)

Modified: commons/sandbox/runtime/trunk/src/main/native/os/solaris/env.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/solaris/env.c?rev=813896&r1=813895&r2=813896&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/solaris/env.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/solaris/env.c Fri Sep 11 16:05:56 2009
@@ -19,11 +19,17 @@
 #include "acr_arch.h"
 #include "acr_error.h"
 #include "acr_memory.h"
+#include "acr_string.h"
 #include "acr_env.h"
 
 ACR_DECLARE(char *) ACR_EnvGet(const char *var)
 {
-    return getenv(var);
+    return ACR_StrdupA(INVALID_HANDLE_VALUE, THROW_NMARK, getenv(var));
+}
+
+ACR_DECLARE(int) ACR_EnvHas(const char *var)
+{
+    return getenv(var) == NULL ? 0 : 1;
 }
 
 ACR_DECLARE(int) ACR_EnvSet(const char *var, const char *val)

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/temps.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/temps.c?rev=813896&r1=813895&r2=813896&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/temps.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/temps.c Fri Sep 11 16:05:56 2009
@@ -235,10 +235,12 @@
             if (strlen(val) < TMP_PATH_MAX) {
                 if (_temp_test(val)) {
                     strlcpy(_temp_path, val, TMP_PATH_MAX);
+                    x_free(val);
                     break;
                 }
             }
         }
+        x_free(val);
         i++;
     }
     if (_temp_path[0]) {
@@ -268,6 +270,7 @@
                     strlcpy(_temp_path, val, TMP_PATH_MAX);
             }
         }
+        x_free(val);
     }
     if (_temp_path[0]) {
         ACR_NO_END_SLASHA(_temp_path);

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/env.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/env.c?rev=813896&r1=813895&r2=813896&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/env.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/env.c Fri Sep 11 16:05:56 2009
@@ -20,6 +20,7 @@
 #include "acr_arch.h"
 #include "acr_error.h"
 #include "acr_memory.h"
+#include "acr_string.h"
 #include "acr_env.h"
 
 static int _msvcrt_putenv(const char *envstring)
@@ -129,7 +130,32 @@
 
 ACR_DECLARE(char *) ACR_EnvGet(const char *var)
 {
-    return getenv(var);
+    char *env = getenv(var);
+    if (env && strchr(env, '%')) {
+        char *buf;
+        DWORD siz;
+        siz = ExpandEnvironmentStringsA(env, NULL, 0);
+        if (siz) {
+            buf = x_malloc(siz);
+            if (buf == NULL)
+                return NULL;
+            siz = ExpandEnvironmentStringsA(env, buf, siz);
+            if (!siz) {
+                /* Shouldn't ever happen */
+                int rc = GetLastError();
+                x_free(buf);
+                SetLastError(rc);
+                return NULL;
+            }
+            return buf;
+        }
+    }
+    return ACR_StrdupA(INVALID_HANDLE_VALUE, THROW_NMARK, env);
+}
+
+ACR_DECLARE(int) ACR_EnvHas(const char *var)
+{
+    return getenv(var) == NULL ? 0 : 1;
 }
 
 ACR_DECLARE(int) ACR_EnvSet(const char *var, const char *val)
@@ -191,9 +217,34 @@
     return 0;
 }
 
+wchar_t *acr_EnvHas(const wchar_t *var)
+{
+    return _wgetenv(var) == NULL ? 0 : 1;
+}
+
 wchar_t *acr_EnvGetW(const wchar_t *var)
 {
-    return _wgetenv(var);
+    wchar_t *env = _wgetenv(var);
+    if (env && wcschr(env, L'%')) {
+        wchar_t *buf;
+        DWORD siz;
+        siz = ExpandEnvironmentStringsW(env, NULL, 0);
+        if (siz) {
+            buf = x_malloc(siz * sizeof(wchar_t));
+            if (buf == NULL)
+                return NULL;
+            siz = ExpandEnvironmentStringsW(env, buf, siz);
+            if (!siz) {
+                /* Shouldn't ever happen */
+                int rc = GetLastError();
+                x_free(buf);
+                SetLastError(rc);
+                return NULL;
+            }
+            return buf;
+        }
+    }
+    return ACR_StrdupW(INVALID_HANDLE_VALUE, THROW_NMARK, env);
 }
 
 int acr_EnvSetW(const wchar_t *var, const wchar_t *val)

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/ini.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/ini.c?rev=813896&r1=813895&r2=813896&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/ini.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/ini.c Fri Sep 11 16:05:56 2009
@@ -531,6 +531,7 @@
                 var_rep = ACR_EnvGet(var_pos);
             if (var_rep) {
                 acr_sbuf_cat(&sbuf, var_rep);
+                x_free(var_rep);
             }
             else {
                 acr_sbuf_putc(&sbuf, '$');

Modified: commons/sandbox/runtime/trunk/src/main/native/test/testsuite.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/test/testsuite.c?rev=813896&r1=813895&r2=813896&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/test/testsuite.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/test/testsuite.c Fri Sep 11 16:05:56 2009
@@ -73,18 +73,22 @@
     envval = ACR_EnvGet("PATH");
     if (!envval)
         failed++;
+    x_free(envval);
     ACR_EnvSet("ACR_TEST_SUITE_VAR", "");
     envval = ACR_EnvGet("ACR_TEST_SUITE_VAR");
     if (envval && *envval)
         failed++;
+    x_free(envval);
     ACR_EnvSet("ACR_TEST_SUITE_VAR", "1");
     envval = ACR_EnvGet("ACR_TEST_SUITE_VAR");
     if (!envval)
         failed++;
+    x_free(envval);
     ACR_EnvDelete("ACR_TEST_SUITE_VAR");
     envval = ACR_EnvGet("ACR_TEST_SUITE_VAR");
     if (envval)
         failed++;
+    x_free(envval);
     tests_failed += failed;
     if (failed)
         fprintf(stderr, "getenv:        Failed (%d)\n", failed);
@@ -528,7 +532,7 @@
     }
     argc -= os->ind;
     argv += os->ind;
-    if (ACR_EnvGet("ACR_TEST")) {
+    if (ACR_EnvHas("ACR_TEST")) {
         run_test = ACR_EnvGet("ACR_TEST");
     }
     if (run_test) {
@@ -569,6 +573,8 @@
         else if (!strcasecmp(run_test, "pmatch")) {
             rv = test_pmatch(argc, argv);
         }
+
+        x_free(run_test);
     }
 
 cleanup: