]> git.saurik.com Git - apple/libc.git/blob - stdlib/FreeBSD/putenv.c.patch
36017bc8bcff48db30d4ab1932ba1080b68e69f0
[apple/libc.git] / stdlib / FreeBSD / putenv.c.patch
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
5 #include <stdlib.h>
6 #include <string.h>
7 +#include <sys/types.h>
8 +#include <db.h>
9 +#include <crt_externs.h>
10 +#include <malloc/malloc.h>
11 +#include <errno.h>
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 *);
17 +
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 + */
24 int
25 -putenv(str)
26 - const char *str;
27 +_putenvp(char *str, char ***envp, void *state)
28 {
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 +{
48 +#if __DARWIN_UNIX03
49 + if (str == NULL || *str == 0 || index(str, '=') == NULL) {
50 + errno = EINVAL;
51 return (-1);
52 - if ((equal = index(p, '=')) == NULL) {
53 - (void)free(p);
54 + }
55 +#else /* !__DARWIN_UNIX03 */
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,
77 +#endif /* __DARWIN_UNIX03 */
78 + _NSGetEnviron(), __zone0));
79 }