]> git.saurik.com Git - apple/libc.git/blame - stdlib/FreeBSD/putenv.c.patch
Libc-498.1.5.tar.gz
[apple/libc.git] / stdlib / FreeBSD / putenv.c.patch
CommitLineData
224c7076
A
1--- putenv.c.orig 2006-10-05 11:57:06.000000000 -0700
2+++ putenv.c 2006-11-02 11:15:33.000000000 -0800
3@@ -39,22 +39,65 @@
4
3d9156a7
A
5 #include <stdlib.h>
6 #include <string.h>
224c7076
A
7+#include <sys/types.h>
8+#include <db.h>
9+#include <crt_externs.h>
10+#include <malloc/malloc.h>
3d9156a7 11+#include <errno.h>
224c7076
A
12
13+extern malloc_zone_t *__zone0;
14+extern void __malloc_check_env_name(const char *);
15+
16+__private_extern__ int __setenv(const char *, const char *, int, int, char ***, malloc_zone_t *);
3d9156a7 17+
224c7076
A
18+#ifndef BUILDING_VARIANT
19+/*
20+ * _putenvp -- SPI using an arbitrary pointer to string array (the array must
21+ * have been created with malloc) and an env state, created by _allocenvstate().
22+ * Returns ptr to value associated with name, if any, else NULL.
23+ */
3d9156a7 24 int
224c7076 25-putenv(str)
3d9156a7 26- const char *str;
224c7076 27+_putenvp(char *str, char ***envp, void *state)
3d9156a7 28 {
224c7076
A
29- char *p, *equal;
30- int rval;
31+ /* insure __zone0 is set up */
32+ if (!__zone0) {
33+ __zone0 = malloc_create_zone(0, 0);
34+ if (!__zone0) {
35+ errno = ENOMEM;
36+ return (-1);
37+ }
38+ }
39+ return (__setenv(str, NULL, 1, 0, envp, (state ? (malloc_zone_t *)state : __zone0)));
40+}
41+#endif /* BUILDING_VARIANT */
42
43- if ((p = strdup(str)) == NULL)
44+int
45+putenv(str)
46+ char *str;
47+{
3d9156a7
A
48+#if __DARWIN_UNIX03
49+ if (str == NULL || *str == 0 || index(str, '=') == NULL) {
50+ errno = EINVAL;
224c7076
A
51 return (-1);
52- if ((equal = index(p, '=')) == NULL) {
53- (void)free(p);
3d9156a7 54+ }
3d9156a7 55+#else /* !__DARWIN_UNIX03 */
224c7076
A
56+ if (index(str, '=') == NULL)
57 return (-1);
58+#endif /* __DARWIN_UNIX03 */
59+ /* insure __zone0 is set up before calling __malloc_check_env_name */
60+ if (!__zone0) {
61+ __zone0 = malloc_create_zone(0, 0);
62+ if (!__zone0) {
63+ errno = ENOMEM;
64+ return (-1);
65+ }
66 }
67- *equal = '\0';
68- rval = setenv(p, equal + 1, 1);
69- (void)free(p);
70- return (rval);
71+ __malloc_check_env_name(str); /* see if we are changing a malloc environment variable */
72+ return (__setenv(str, NULL, 1,
73+#if __DARWIN_UNIX03
74+ 0,
75+#else /* !__DARWIN_UNIX03 */
76+ -1,
3d9156a7 77+#endif /* __DARWIN_UNIX03 */
224c7076 78+ _NSGetEnviron(), __zone0));
3d9156a7 79 }