diff options
| author | Paul Eggert | 2013-03-01 07:16:43 -0800 |
|---|---|---|
| committer | Paul Eggert | 2013-03-01 07:16:43 -0800 |
| commit | 89bd9d36859503e25b767f97395870190f272bc1 (patch) | |
| tree | b42053badeb8e2274cc9f3a43b67ac800272aa37 /lib | |
| parent | a4837536e2ef918dacf687c134e690cbbe693487 (diff) | |
| download | emacs-89bd9d36859503e25b767f97395870190f272bc1.tar.gz emacs-89bd9d36859503e25b767f97395870190f272bc1.zip | |
Merge from gnulib.
2013-02-21 putenv: port better to native Windows
2013-02-18 extern-inline: avoid compilation error with HP-UX cc
2013-02-14 putenv: fix heap corruption with mixed putenv/_putenv
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/putenv.c | 94 |
1 files changed, 76 insertions, 18 deletions
diff --git a/lib/putenv.c b/lib/putenv.c index 5f0fedaf9cc..ed666afc3bb 100644 --- a/lib/putenv.c +++ b/lib/putenv.c | |||
| @@ -34,6 +34,11 @@ | |||
| 34 | #include <string.h> | 34 | #include <string.h> |
| 35 | #include <unistd.h> | 35 | #include <unistd.h> |
| 36 | 36 | ||
| 37 | #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | ||
| 38 | # define WIN32_LEAN_AND_MEAN | ||
| 39 | # include <windows.h> | ||
| 40 | #endif | ||
| 41 | |||
| 37 | #if _LIBC | 42 | #if _LIBC |
| 38 | # if HAVE_GNU_LD | 43 | # if HAVE_GNU_LD |
| 39 | # define environ __environ | 44 | # define environ __environ |
| @@ -67,6 +72,21 @@ _unsetenv (const char *name) | |||
| 67 | 72 | ||
| 68 | len = strlen (name); | 73 | len = strlen (name); |
| 69 | 74 | ||
| 75 | #if HAVE__PUTENV | ||
| 76 | { | ||
| 77 | int putenv_result, putenv_errno; | ||
| 78 | char *name_ = malloc (len + 2); | ||
| 79 | memcpy (name_, name, len); | ||
| 80 | name_[len] = '='; | ||
| 81 | name_[len + 1] = 0; | ||
| 82 | putenv_result = _putenv (name_); | ||
| 83 | putenv_errno = errno; | ||
| 84 | free (name_); | ||
| 85 | __set_errno (putenv_errno); | ||
| 86 | return putenv_result; | ||
| 87 | } | ||
| 88 | #else | ||
| 89 | |||
| 70 | LOCK; | 90 | LOCK; |
| 71 | 91 | ||
| 72 | ep = environ; | 92 | ep = environ; |
| @@ -87,6 +107,7 @@ _unsetenv (const char *name) | |||
| 87 | UNLOCK; | 107 | UNLOCK; |
| 88 | 108 | ||
| 89 | return 0; | 109 | return 0; |
| 110 | #endif | ||
| 90 | } | 111 | } |
| 91 | 112 | ||
| 92 | 113 | ||
| @@ -95,9 +116,8 @@ _unsetenv (const char *name) | |||
| 95 | int | 116 | int |
| 96 | putenv (char *string) | 117 | putenv (char *string) |
| 97 | { | 118 | { |
| 98 | const char *const name_end = strchr (string, '='); | 119 | const char *name_end = strchr (string, '='); |
| 99 | register size_t size; | 120 | char **ep; |
| 100 | register char **ep; | ||
| 101 | 121 | ||
| 102 | if (name_end == NULL) | 122 | if (name_end == NULL) |
| 103 | { | 123 | { |
| @@ -105,30 +125,68 @@ putenv (char *string) | |||
| 105 | return _unsetenv (string); | 125 | return _unsetenv (string); |
| 106 | } | 126 | } |
| 107 | 127 | ||
| 108 | size = 0; | 128 | #if HAVE__PUTENV |
| 109 | for (ep = environ; *ep != NULL; ++ep) | 129 | /* Rely on _putenv to allocate the new environment. If other |
| 110 | if (!strncmp (*ep, string, name_end - string) && | 130 | parts of the application use _putenv, the !HAVE__PUTENV code |
| 111 | (*ep)[name_end - string] == '=') | 131 | would fight over who owns the environ vector, causing a crash. */ |
| 132 | if (name_end[1]) | ||
| 133 | return _putenv (string); | ||
| 134 | else | ||
| 135 | { | ||
| 136 | /* _putenv ("NAME=") unsets NAME, so invoke _putenv ("NAME= ") | ||
| 137 | to allocate the environ vector and then replace the new | ||
| 138 | entry with "NAME=". */ | ||
| 139 | int putenv_result, putenv_errno; | ||
| 140 | char *name_x = malloc (name_end - string + sizeof "= "); | ||
| 141 | if (!name_x) | ||
| 142 | return -1; | ||
| 143 | memcpy (name_x, string, name_end - string + 1); | ||
| 144 | name_x[name_end - string + 1] = ' '; | ||
| 145 | name_x[name_end - string + 2] = 0; | ||
| 146 | putenv_result = _putenv (name_x); | ||
| 147 | putenv_errno = errno; | ||
| 148 | for (ep = environ; *ep; ep++) | ||
| 149 | if (strcmp (*ep, name_x) == 0) | ||
| 150 | { | ||
| 151 | *ep = string; | ||
| 152 | break; | ||
| 153 | } | ||
| 154 | # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | ||
| 155 | if (putenv_result == 0) | ||
| 156 | { | ||
| 157 | /* _putenv propagated "NAME= " into the subprocess environment; | ||
| 158 | fix that by calling SetEnvironmentVariable directly. */ | ||
| 159 | name_x[name_end - string] = 0; | ||
| 160 | putenv_result = SetEnvironmentVariable (name_x, "") ? 0 : -1; | ||
| 161 | putenv_errno = ENOMEM; /* ENOMEM is the only way to fail. */ | ||
| 162 | } | ||
| 163 | # endif | ||
| 164 | free (name_x); | ||
| 165 | __set_errno (putenv_errno); | ||
| 166 | return putenv_result; | ||
| 167 | } | ||
| 168 | #else | ||
| 169 | for (ep = environ; *ep; ep++) | ||
| 170 | if (strncmp (*ep, string, name_end - string) == 0 | ||
| 171 | && (*ep)[name_end - string] == '=') | ||
| 112 | break; | 172 | break; |
| 113 | else | ||
| 114 | ++size; | ||
| 115 | 173 | ||
| 116 | if (*ep == NULL) | 174 | if (*ep) |
| 175 | *ep = string; | ||
| 176 | else | ||
| 117 | { | 177 | { |
| 118 | static char **last_environ = NULL; | 178 | static char **last_environ = NULL; |
| 119 | char **new_environ = (char **) malloc ((size + 2) * sizeof (char *)); | 179 | size_t size = ep - environ; |
| 120 | if (new_environ == NULL) | 180 | char **new_environ = malloc ((size + 2) * sizeof *new_environ); |
| 181 | if (! new_environ) | ||
| 121 | return -1; | 182 | return -1; |
| 122 | (void) memcpy ((void *) new_environ, (void *) environ, | 183 | new_environ[0] = string; |
| 123 | size * sizeof (char *)); | 184 | memcpy (new_environ + 1, environ, (size + 1) * sizeof *new_environ); |
| 124 | new_environ[size] = (char *) string; | ||
| 125 | new_environ[size + 1] = NULL; | ||
| 126 | free (last_environ); | 185 | free (last_environ); |
| 127 | last_environ = new_environ; | 186 | last_environ = new_environ; |
| 128 | environ = new_environ; | 187 | environ = new_environ; |
| 129 | } | 188 | } |
| 130 | else | 189 | #endif |
| 131 | *ep = string; | ||
| 132 | 190 | ||
| 133 | return 0; | 191 | return 0; |
| 134 | } | 192 | } |