putenv.c.patch   [plain text]


--- putenv.c.orig	2011-04-12 22:08:20.000000000 -0700
+++ putenv.c	2011-04-13 14:33:50.000000000 -0700
@@ -35,22 +35,81 @@ __FBSDID("$FreeBSD: src/lib/libc/stdlib/
 
 #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;
+#ifdef LEGACY_CRT1_ENVIRON
+extern char **_saved_environ;
+#endif /* LEGACY_CRT1_ENVIRON */
+
+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
+_putenvp(char *str, char ***envp, void *state)
+{
+	/* 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 */
 
 int
 putenv(str)
-	const char *str;
+	char *str;
 {
-	char *p, *equal;
-	int rval;
+#ifdef LEGACY_CRT1_ENVIRON
+	int ret;
+#endif /* LEGACY_CRT1_ENVIRON */
 
-	if ((p = strdup(str)) == NULL)
+#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 */
+#ifdef LEGACY_CRT1_ENVIRON
+	ret =
+#else /* !LEGACY_CRT1_ENVIRON */
+	return
+#endif /* !LEGACY_CRT1_ENVIRON */
+	    __setenv(str, NULL, 1,
+#if __DARWIN_UNIX03
+		0,
+#else /* !__DARWIN_UNIX03 */
+		-1,
+#endif /* __DARWIN_UNIX03 */
+		_NSGetEnviron(), __zone0);
+#ifdef LEGACY_CRT1_ENVIRON
+	_saved_environ = *_NSGetEnviron();
+	return ret;
+#endif /* LEGACY_CRT1_ENVIRON */
 }