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/08/12 18:02:54 UTC

svn commit: r803576 - in /commons/sandbox/runtime/trunk/src/main/native: Makefile.in Makefile.msc.in include/arch/unix/acr_arch.h include/arch/windows/acr_arch.h os/linux/env.c os/solaris/env.c os/win32/env.c shared/getopt.c

Author: mturk
Date: Wed Aug 12 16:02:53 2009
New Revision: 803576

URL: http://svn.apache.org/viewvc?rev=803576&view=rev
Log:
Add environment variable API

Added:
    commons/sandbox/runtime/trunk/src/main/native/os/linux/env.c   (with props)
    commons/sandbox/runtime/trunk/src/main/native/os/solaris/env.c   (with props)
    commons/sandbox/runtime/trunk/src/main/native/os/win32/env.c   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/native/Makefile.in
    commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
    commons/sandbox/runtime/trunk/src/main/native/include/arch/unix/acr_arch.h
    commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch.h
    commons/sandbox/runtime/trunk/src/main/native/shared/getopt.c

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.in?rev=803576&r1=803575&r2=803576&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.in Wed Aug 12 16:02:53 2009
@@ -113,6 +113,7 @@
 	$(SRCDIR)/os/unix/time.$(OBJ) \
 	$(SRCDIR)/os/unix/uuid.$(OBJ) \
 	$(SRCDIR)/os/unix/uutils.$(OBJ) \
+	$(SRCDIR)/os/linux/env.$(OBJ) \
 	$(SRCDIR)/os/linux/execmem.$(OBJ) \
 	$(SRCDIR)/os/linux/platform.$(OBJ) \
 	$(SRCDIR)/os/linux/pgroup.$(OBJ) \
@@ -134,6 +135,7 @@
 	$(SRCDIR)/os/unix/time.$(OBJ) \
 	$(SRCDIR)/os/unix/uuid.$(OBJ) \
 	$(SRCDIR)/os/unix/uutils.$(OBJ) \
+	$(SRCDIR)/os/solaris/env.$(OBJ) \
 	$(SRCDIR)/os/solaris/platform.$(OBJ) \
 	$(SRCDIR)/os/solaris/pgroup.$(OBJ) \
 	$(SRCDIR)/os/solaris/puser.$(OBJ) \

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in?rev=803576&r1=803575&r2=803576&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in Wed Aug 12 16:02:53 2009
@@ -94,6 +94,7 @@
 
 WINDOWS_OBJS= \
 	$(SRCDIR)/os/win32/dirent.$(OBJ) \
+	$(SRCDIR)/os/win32/env.$(OBJ) \
 	$(SRCDIR)/os/win32/execmem.$(OBJ) \
 	$(SRCDIR)/os/win32/file.$(OBJ) \
 	$(SRCDIR)/os/win32/main.$(OBJ) \

Modified: commons/sandbox/runtime/trunk/src/main/native/include/arch/unix/acr_arch.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/arch/unix/acr_arch.h?rev=803576&r1=803575&r2=803576&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/arch/unix/acr_arch.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/arch/unix/acr_arch.h Wed Aug 12 16:02:53 2009
@@ -75,12 +75,22 @@
 
 static ACR_INLINE void *x_malloc(size_t size)
 {
-    return calloc(1, size);
+    if (size > 0 && size < INT_MAX)
+        return calloc(1, size);
+    else {
+        errno = ENOMEM;
+        return NULL;
+    }
 }
 
 static ACR_INLINE void *x_calloc(size_t size)
 {
-    return calloc(1, size);
+    if (size > 0 && size < INT_MAX)
+        return calloc(1, size);
+    else {
+        errno = ENOMEM;
+        return NULL;
+    }
 }
 
 /**

Modified: commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch.h?rev=803576&r1=803575&r2=803576&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch.h Wed Aug 12 16:02:53 2009
@@ -309,12 +309,22 @@
 
 static ACR_INLINE void *x_malloc(size_t size)
 {
-    return calloc(1, size);
+    if (size > 0 && size < INT_MAX)
+        return calloc(1, size);
+    else {
+        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+        return NULL;
+    }
 }
 
 static ACR_INLINE void *x_calloc(size_t size)
 {
-    return calloc(1, size);
+    if (size > 0 && size < INT_MAX)
+        return calloc(1, size);
+    else {
+        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+        return NULL;
+    }
 }
 
 static ACR_INLINE void FileTimeToAprTime(acr_time_t *result, LPFILETIME input)
@@ -377,18 +387,19 @@
 #define LOG_MSG_DOMAIN            "ApacheCommonsRuntime"
 
 typedef enum {
-    SYSDLL_KERNEL32     = 0,    /* kernel32 From WinBase.h              */
-    SYSDLL_NTDLL        = 1,    /* ntdll    From our real kernel        */
-    SYSDLL_USER32       = 2,    /* user32   From WinUser.h              */
-    SYSDLL_IPHLPAPI     = 3,    /* iphlpapi From Iphlpapi.h             */
-    SYSDLL_MSWSOCK      = 4,    /* mswsock  From WinSock.h              */
-    SYSDLL_WS2_32       = 5,    /* ws2_32   From WinSock2.h             */
-    SYSDLL_SHELL32      = 6,    /* shell32  From ShellAPI.h             */
-    SYSDLL_ADVAPI32     = 7,    /* advapi32 From WinBase.h              */
-    SYSDLL_JVM          = 8,    /* jvm      From our own jvm.dll        */
-    SYSDLL_KTMW32       = 9,    /* ktmw32   From Ktmw32.h               */
+    SYSDLL_KERNEL32     =  0,    /* kernel32 From WinBase.h              */
+    SYSDLL_NTDLL        =  1,    /* ntdll    From our real kernel        */
+    SYSDLL_USER32       =  2,    /* user32   From WinUser.h              */
+    SYSDLL_IPHLPAPI     =  3,    /* iphlpapi From Iphlpapi.h             */
+    SYSDLL_MSWSOCK      =  4,    /* mswsock  From WinSock.h              */
+    SYSDLL_WS2_32       =  5,    /* ws2_32   From WinSock2.h             */
+    SYSDLL_SHELL32      =  6,    /* shell32  From ShellAPI.h             */
+    SYSDLL_ADVAPI32     =  7,    /* advapi32 From WinBase.h              */
+    SYSDLL_JVM          =  8,    /* jvm      From our own jvm.dll        */
+    SYSDLL_KTMW32       =  9,    /* ktmw32   From Ktmw32.h               */
+    SYSDLL_MSVCRT       = 10,    /* ktmw32   From Ktmw32.h               */
 
-    SYSDLL_defined      = 10    /* must define as last idx_ + 1         */
+    SYSDLL_defined      = 11     /* must define as last idx_ + 1         */
 } acr_dlltoken_e;
 
 extern LPSYSTEM_INFO           acr_osinf;
@@ -447,6 +458,20 @@
 #define ldExitProcess acr_winapi_ld_ExitProcess
 #define fnExitProcess acr_winapi_pfn_ExitProcess
 
+ACR_DECLARE_LATE_DLL_FUNC(SYSDLL_MSVCRT, int, -1,
+                          _cdecl, _putenv, 0, (
+    const char *envstring),
+    (envstring));
+#undef  _crt_putenv
+#define _crt_putenv acr_winapi_putenv
+
+ACR_DECLARE_LATE_DLL_FUNC(SYSDLL_MSVCRT, int, -1,
+                          _cdecl, _wputenv, 0, (
+    const wchar_t *envstring),
+    (envstring));
+#undef  _crt_wputenv
+#define _crt_wputenv acr_winapi_wputenv
+
 ACR_DECLARE_LATE_DLL_FUNC(SYSDLL_KERNEL32, BOOL, FALSE,
                           WINAPI, GetSystemTimes, 0, (
     OUT LPFILETIME lpIdleTime,

Added: 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=803576&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/linux/env.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/linux/env.c Wed Aug 12 16:02:53 2009
@@ -0,0 +1,38 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "acr.h"
+#include "acr_private.h"
+#include "acr_arch.h"
+#include "acr_error.h"
+#include "acr_memory.h"
+
+
+ACR_DECLARE(char *) ACR_EnvGet(const char *var)
+{
+    return getenv(var);
+}
+
+ACR_DECLARE(int) ACR_EnvSet(const char *var, const char *val)
+{
+    return setenv(var, val, 1);
+}
+
+ACR_DECLARE(int) ACR_EnvDelete(const char *var)
+{
+    return unsetenv(var);
+}
+

Propchange: commons/sandbox/runtime/trunk/src/main/native/os/linux/env.c
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 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=803576&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/solaris/env.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/solaris/env.c Wed Aug 12 16:02:53 2009
@@ -0,0 +1,88 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "acr.h"
+#include "acr_private.h"
+#include "acr_arch.h"
+#include "acr_error.h"
+#include "acr_memory.h"
+
+ACR_DECLARE(char *) ACR_EnvGet(const char *var)
+{
+    return getenv(var);
+}
+
+ACR_DECLARE(int) ACR_EnvSet(const char *var, const char *val)
+{
+    int rc;
+    char *estr;
+
+    if (!var || !*var || !val) {
+        errno = EINVAL;
+        return -1;
+    }
+    estr = (char *)malloc(strlen(var) + strlen(val) + 2);
+    if (!estr)                /* not much we can do if no memory */
+        return -1;
+
+    /* Override the existing setting by forcibly defining the var */
+    sprintf(estr, "%s=%s", var, val);
+    if (putenv(estr)) {
+        int se = errno;
+        free(estr);
+        errno = se;
+        return -1;
+    }
+    free(estr);
+
+    return 0;
+}
+
+ACR_DECLARE(int) ACR_EnvDelete(const char *var)
+{
+    char *estr;
+
+    if (!var || !*var) {
+        errno = EINVAL;
+        return -1;
+    }
+    if (getenv(var) == NULL)
+        return 0;             /* no work */
+    estr = (char *)malloc(strlen(var) + 2);
+    if (!estr)                /* not much we can do if no memory */
+        return -1;
+
+    /* Override the existing setting by forcibly defining the var */
+    sprintf(estr, "%s=", var);
+    if (putenv(estr)) {
+        int se = errno;
+        free(estr);
+        errno = se;
+        return -1;
+    }
+
+    /* Now we can clobber the variable definition this way: */
+    strcpy(estr, "=");
+    /*
+     * This last putenv cleans up if we have multiple zero-length names as a
+     * result of unsetting multiple things.
+     */
+    putenv(estr);
+    free(estr);
+
+    return 0;
+}
+

Propchange: commons/sandbox/runtime/trunk/src/main/native/os/solaris/env.c
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 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=803576&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/env.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/env.c Wed Aug 12 16:02:53 2009
@@ -0,0 +1,179 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "acr.h"
+#include "acr_private.h"
+#define ACR_WANT_LATE_DLL
+#include "acr_arch.h"
+#include "acr_error.h"
+#include "acr_memory.h"
+
+static int _msvcrt_putenv(const char *envstring)
+{
+    char       *cpy;
+    char       *cp;
+
+    /*
+     * Each version of MSVCRT has its own _putenv() call in the runtime
+     * library.
+     *
+     * If we're in VC 7.0 or later (means != mingw), update in the 6.0
+     * MSVCRT.DLL environment as well, to work with third party libraries
+     * linked against it (such as gnuwin32 libraries).
+     */
+#if defined(_MSC_VER) && (_MSC_VER >= 1300)
+    ret = _crt_putenv(envstring);
+    if (ret != 0)
+        return ret;
+#endif   /* _MSC_VER >= 1300 */
+
+    /*
+     * Update the process environment - to make modifications visible to child
+     * processes.
+     *
+     * Need a copy of the string so we can modify it.
+     */
+    cpy = strdup(envstring);
+    if (!cpy)
+        return -1;
+    cp = strchr(cpy, '=');
+    if (cp == NULL) {
+        errno = EINVAL;
+        return -1;
+    }
+    *(cp++) = '\0';
+    if (strlen(cp)) {
+        /*
+         * Only call SetEnvironmentVariable() when we are adding a variable,
+         * not when removing it. Calling it on both crashes on at least
+         * certain versions of MingW.
+         */
+        if (!SetEnvironmentVariableA(cpy, cp)) {
+            free(cpy);
+            errno = ENOMEM;
+            return -1;
+        }
+    }
+    free(cpy);
+
+    /* Finally, update our "own" cache */
+    return _putenv(envstring);
+}
+
+static int _msvcrt_wputenv(const wchar_t *envstring)
+{
+    wchar_t    *cpy;
+    wchar_t    *cp;
+
+    /*
+     * Each version of MSVCRT has its own _putenv() call in the runtime
+     * library.
+     *
+     * If we're in VC 7.0 or later (means != mingw), update in the 6.0
+     * MSVCRT.DLL environment as well, to work with third party libraries
+     * linked against it (such as gnuwin32 libraries).
+     */
+#if defined(_MSC_VER) && (_MSC_VER >= 1300)
+    ret = _crt_wputenv(envstring);
+    if (ret != 0)
+        return ret;
+#endif   /* _MSC_VER >= 1300 */
+
+    /*
+     * Update the process environment - to make modifications visible to child
+     * processes.
+     *
+     * Need a copy of the string so we can modify it.
+     */
+    cpy = wcsdup(envstring);
+    if (!cpy)
+        return -1;
+    cp = wcschr(cpy, L'=');
+    if (cp == NULL) {
+        errno = EINVAL;
+        return -1;
+    }
+    *(cp++) = L'\0';
+    if (!SetEnvironmentVariableW(cpy, *cp ? cp : NULL)) {
+        free(cpy);
+        errno = ENOMEM;
+        return -1;
+    }
+    free(cpy);
+
+    /* Finally, update our "own" cache */
+    return _wputenv(envstring);
+}
+
+
+ACR_DECLARE(char *) ACR_EnvGet(const char *var)
+{
+    return getenv(var);
+}
+
+ACR_DECLARE(int) ACR_EnvSet(const char *var, const char *val)
+{
+    int rc;
+    char *estr;
+
+    if (!var || !*var || !val) {
+        errno = EINVAL;
+        return -1;
+    }
+    estr = (char *)malloc(strlen(var) + strlen(val) + 2);
+    if (!estr)                /* not much we can do if no memory */
+        return -1;
+
+    /* Override the existing setting by forcibly defining the var */
+    sprintf(estr, "%s=%s", var, val);
+    if (msvcrt_putenv(estr)) {
+        int se = errno;
+        free(estr);
+        errno = se;
+        return -1;
+    }
+
+    free(estr);
+    return 0;
+}
+
+ACR_DECLARE(int) ACR_EnvDelete(const char *var)
+{
+    char *estr;
+
+    if (!var || !*var) {
+        errno = EINVAL;
+        return -1;
+    }
+    if (getenv(var) == NULL)
+        return 0;             /* no work */
+    estr = (char *)malloc(strlen(var) + 2);
+    if (!estr)                /* not much we can do if no memory */
+        return -1;
+
+    /* Override the existing setting by forcibly defining the var */
+    sprintf(estr, "%s=", var);
+    if (msvcrt_putenv(estr)) {
+        int se = errno;
+        free(estr);
+        errno = se;
+        return -1;
+    }
+
+    free(estr);
+    return 0;
+}
+

Propchange: commons/sandbox/runtime/trunk/src/main/native/os/win32/env.c
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/getopt.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/getopt.c?rev=803576&r1=803575&r2=803576&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/getopt.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/getopt.c Wed Aug 12 16:02:53 2009
@@ -34,6 +34,7 @@
  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
  */
 #include "acr.h"
+#include "acr_arch.h"
 #include "acr_private.h"
 #include "acr_error.h"
 #include "acr_memory.h"