]>
Commit | Line | Data |
---|---|---|
1f2f436a A |
1 | --- putenv.c.orig 2011-04-12 22:08:20.000000000 -0700 |
2 | +++ putenv.c 2011-04-13 14:33:50.000000000 -0700 | |
3 | @@ -35,22 +35,81 @@ __FBSDID("$FreeBSD: src/lib/libc/stdlib/ | |
224c7076 | 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> |
1f2f436a | 12 | + |
224c7076 | 13 | +extern malloc_zone_t *__zone0; |
1f2f436a A |
14 | +#ifdef LEGACY_CRT1_ENVIRON |
15 | +extern char **_saved_environ; | |
16 | +#endif /* LEGACY_CRT1_ENVIRON */ | |
224c7076 | 17 | + |
1f2f436a | 18 | +extern void __malloc_check_env_name(const char *); |
224c7076 | 19 | +__private_extern__ int __setenv(const char *, const char *, int, int, char ***, malloc_zone_t *); |
3d9156a7 | 20 | + |
224c7076 A |
21 | +#ifndef BUILDING_VARIANT |
22 | +/* | |
23 | + * _putenvp -- SPI using an arbitrary pointer to string array (the array must | |
24 | + * have been created with malloc) and an env state, created by _allocenvstate(). | |
25 | + * Returns ptr to value associated with name, if any, else NULL. | |
26 | + */ | |
1f2f436a | 27 | +int |
224c7076 | 28 | +_putenvp(char *str, char ***envp, void *state) |
1f2f436a | 29 | +{ |
224c7076 A |
30 | + /* insure __zone0 is set up */ |
31 | + if (!__zone0) { | |
32 | + __zone0 = malloc_create_zone(0, 0); | |
33 | + if (!__zone0) { | |
34 | + errno = ENOMEM; | |
35 | + return (-1); | |
36 | + } | |
37 | + } | |
38 | + return (__setenv(str, NULL, 1, 0, envp, (state ? (malloc_zone_t *)state : __zone0))); | |
39 | +} | |
40 | +#endif /* BUILDING_VARIANT */ | |
41 | ||
1f2f436a A |
42 | int |
43 | putenv(str) | |
44 | - const char *str; | |
224c7076 | 45 | + char *str; |
1f2f436a A |
46 | { |
47 | - char *p, *equal; | |
48 | - int rval; | |
49 | +#ifdef LEGACY_CRT1_ENVIRON | |
50 | + int ret; | |
51 | +#endif /* LEGACY_CRT1_ENVIRON */ | |
52 | ||
53 | - if ((p = strdup(str)) == NULL) | |
3d9156a7 A |
54 | +#if __DARWIN_UNIX03 |
55 | + if (str == NULL || *str == 0 || index(str, '=') == NULL) { | |
56 | + errno = EINVAL; | |
224c7076 A |
57 | return (-1); |
58 | - if ((equal = index(p, '=')) == NULL) { | |
59 | - (void)free(p); | |
3d9156a7 | 60 | + } |
3d9156a7 | 61 | +#else /* !__DARWIN_UNIX03 */ |
224c7076 A |
62 | + if (index(str, '=') == NULL) |
63 | return (-1); | |
64 | +#endif /* __DARWIN_UNIX03 */ | |
65 | + /* insure __zone0 is set up before calling __malloc_check_env_name */ | |
66 | + if (!__zone0) { | |
67 | + __zone0 = malloc_create_zone(0, 0); | |
68 | + if (!__zone0) { | |
69 | + errno = ENOMEM; | |
70 | + return (-1); | |
71 | + } | |
72 | } | |
73 | - *equal = '\0'; | |
74 | - rval = setenv(p, equal + 1, 1); | |
75 | - (void)free(p); | |
76 | - return (rval); | |
77 | + __malloc_check_env_name(str); /* see if we are changing a malloc environment variable */ | |
1f2f436a A |
78 | +#ifdef LEGACY_CRT1_ENVIRON |
79 | + ret = | |
80 | +#else /* !LEGACY_CRT1_ENVIRON */ | |
81 | + return | |
82 | +#endif /* !LEGACY_CRT1_ENVIRON */ | |
83 | + __setenv(str, NULL, 1, | |
224c7076 A |
84 | +#if __DARWIN_UNIX03 |
85 | + 0, | |
86 | +#else /* !__DARWIN_UNIX03 */ | |
87 | + -1, | |
3d9156a7 | 88 | +#endif /* __DARWIN_UNIX03 */ |
1f2f436a A |
89 | + _NSGetEnviron(), __zone0); |
90 | +#ifdef LEGACY_CRT1_ENVIRON | |
91 | + _saved_environ = *_NSGetEnviron(); | |
92 | + return ret; | |
93 | +#endif /* LEGACY_CRT1_ENVIRON */ | |
3d9156a7 | 94 | } |