]> git.saurik.com Git - apple/libc.git/blob - stdlib/FreeBSD/putenv.c.patch
47a412ea055ffb06c37714302c11846a5b06a0b9
[apple/libc.git] / stdlib / FreeBSD / putenv.c.patch
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/
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 +#ifdef LEGACY_CRT1_ENVIRON
15 +extern char **_saved_environ;
16 +#endif /* LEGACY_CRT1_ENVIRON */
17 +
18 +extern void __malloc_check_env_name(const char *);
19 +__private_extern__ int __setenv(const char *, const char *, int, int, char ***, malloc_zone_t *);
20 +
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 + */
27 +int
28 +_putenvp(char *str, char ***envp, void *state)
29 +{
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
42 int
43 putenv(str)
44 - const char *str;
45 + char *str;
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)
54 +#if __DARWIN_UNIX03
55 + if (str == NULL || *str == 0 || index(str, '=') == NULL) {
56 + errno = EINVAL;
57 return (-1);
58 - if ((equal = index(p, '=')) == NULL) {
59 - (void)free(p);
60 + }
61 +#else /* !__DARWIN_UNIX03 */
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 */
78 +#ifdef LEGACY_CRT1_ENVIRON
79 + ret =
80 +#else /* !LEGACY_CRT1_ENVIRON */
81 + return
82 +#endif /* !LEGACY_CRT1_ENVIRON */
83 + __setenv(str, NULL, 1,
84 +#if __DARWIN_UNIX03
85 + 0,
86 +#else /* !__DARWIN_UNIX03 */
87 + -1,
88 +#endif /* __DARWIN_UNIX03 */
89 + _NSGetEnviron(), __zone0);
90 +#ifdef LEGACY_CRT1_ENVIRON
91 + _saved_environ = *_NSGetEnviron();
92 + return ret;
93 +#endif /* LEGACY_CRT1_ENVIRON */
94 }