putenv.c.patch   [plain text]


--- putenv.c.orig	2006-10-05 11:57:06.000000000 -0700
+++ putenv.c	2006-11-02 11:15:33.000000000 -0800
@@ -39,22 +39,65 @@
 
 #include <stdlib.h>
 #include <string.h>
+#include <sys/types.h>
+#include <db.h>
+#include <crt_externs.h>
+#include <malloc/malloc.h>
+#include <errno.h> 
 
+extern malloc_zone_t *__zone0;
+extern void __malloc_check_env_name(const char *);
+
+__private_extern__ int __setenv(const char *, const char *, int, int, char ***, malloc_zone_t *);
+
+#ifndef BUILDING_VARIANT
+/*
+ * _putenvp -- SPI using an arbitrary pointer to string array (the array must
+ * have been created with malloc) and an env state, created by _allocenvstate().
+ *	Returns ptr to value associated with name, if any, else NULL.
+ */
 int
-putenv(str)
-	const char *str;
+_putenvp(char *str, char ***envp, void *state)
 {
-	char *p, *equal;
-	int rval;
+	/* insure __zone0 is set up */
+	if (!__zone0) {
+	    __zone0 = malloc_create_zone(0, 0);
+	    if (!__zone0) {
+		    errno = ENOMEM;
+		    return (-1);
+	    }
+	}
+	return (__setenv(str, NULL, 1, 0, envp, (state ? (malloc_zone_t *)state : __zone0)));
+}
+#endif /* BUILDING_VARIANT */
 
-	if ((p = strdup(str)) == NULL)
+int
+putenv(str)
+	char *str;
+{
+#if __DARWIN_UNIX03
+	if (str == NULL || *str == 0 || index(str, '=') == NULL) {
+		errno = EINVAL;
 		return (-1);
-	if ((equal = index(p, '=')) == NULL) {
-		(void)free(p);
+	}
+#else /* !__DARWIN_UNIX03 */
+	if (index(str, '=') == NULL)
 		return (-1);
+#endif /* __DARWIN_UNIX03 */
+	/* insure __zone0 is set up before calling __malloc_check_env_name */
+	if (!__zone0) {
+	    __zone0 = malloc_create_zone(0, 0);
+	    if (!__zone0) {
+		    errno = ENOMEM;
+		    return (-1);
+	    }
 	}
-	*equal = '\0';
-	rval = setenv(p, equal + 1, 1);
-	(void)free(p);
-	return (rval);
+	__malloc_check_env_name(str); /* see if we are changing a malloc environment variable */
+	return (__setenv(str, NULL, 1,
+#if __DARWIN_UNIX03
+		0,
+#else /* !__DARWIN_UNIX03 */
+		-1,
+#endif /* __DARWIN_UNIX03 */
+	_NSGetEnviron(), __zone0));
 }