aboutsummaryrefslogtreecommitdiffstats
path: root/lib/putenv.c
diff options
context:
space:
mode:
authorTom Tromey2013-03-17 05:17:24 -0600
committerTom Tromey2013-03-17 05:17:24 -0600
commit6bd488cd8d05aa3983ca55f70ee384732d8c0085 (patch)
tree5645fc7b882638d6c0eb3f61fd55bde1a63fc190 /lib/putenv.c
parent71f91792e3013b397996905224f387da5cc539a9 (diff)
parent9c44569ea2a18099307e0571d523d8637000a153 (diff)
downloademacs-6bd488cd8d05aa3983ca55f70ee384732d8c0085.tar.gz
emacs-6bd488cd8d05aa3983ca55f70ee384732d8c0085.zip
merge from trunk
Diffstat (limited to 'lib/putenv.c')
-rw-r--r--lib/putenv.c82
1 files changed, 42 insertions, 40 deletions
diff --git a/lib/putenv.c b/lib/putenv.c
index ed666afc3bb..5461273084e 100644
--- a/lib/putenv.c
+++ b/lib/putenv.c
@@ -62,7 +62,9 @@ static int
62_unsetenv (const char *name) 62_unsetenv (const char *name)
63{ 63{
64 size_t len; 64 size_t len;
65#if !HAVE_DECL__PUTENV
65 char **ep; 66 char **ep;
67#endif
66 68
67 if (name == NULL || *name == '\0' || strchr (name, '=') != NULL) 69 if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
68 { 70 {
@@ -72,7 +74,7 @@ _unsetenv (const char *name)
72 74
73 len = strlen (name); 75 len = strlen (name);
74 76
75#if HAVE__PUTENV 77#if HAVE_DECL__PUTENV
76 { 78 {
77 int putenv_result, putenv_errno; 79 int putenv_result, putenv_errno;
78 char *name_ = malloc (len + 2); 80 char *name_ = malloc (len + 2);
@@ -125,46 +127,46 @@ putenv (char *string)
125 return _unsetenv (string); 127 return _unsetenv (string);
126 } 128 }
127 129
128#if HAVE__PUTENV 130#if HAVE_DECL__PUTENV
129 /* Rely on _putenv to allocate the new environment. If other 131 /* Rely on _putenv to allocate the new environment. If other
130 parts of the application use _putenv, the !HAVE__PUTENV code 132 parts of the application use _putenv, the !HAVE_DECL__PUTENV code
131 would fight over who owns the environ vector, causing a crash. */ 133 would fight over who owns the environ vector, causing a crash. */
132 if (name_end[1]) 134 if (name_end[1])
133 return _putenv (string); 135 return _putenv (string);
134 else 136 else
135 { 137 {
136 /* _putenv ("NAME=") unsets NAME, so invoke _putenv ("NAME= ") 138 /* _putenv ("NAME=") unsets NAME, so invoke _putenv ("NAME= ")
137 to allocate the environ vector and then replace the new 139 to allocate the environ vector and then replace the new
138 entry with "NAME=". */ 140 entry with "NAME=". */
139 int putenv_result, putenv_errno; 141 int putenv_result, putenv_errno;
140 char *name_x = malloc (name_end - string + sizeof "= "); 142 char *name_x = malloc (name_end - string + sizeof "= ");
141 if (!name_x) 143 if (!name_x)
142 return -1; 144 return -1;
143 memcpy (name_x, string, name_end - string + 1); 145 memcpy (name_x, string, name_end - string + 1);
144 name_x[name_end - string + 1] = ' '; 146 name_x[name_end - string + 1] = ' ';
145 name_x[name_end - string + 2] = 0; 147 name_x[name_end - string + 2] = 0;
146 putenv_result = _putenv (name_x); 148 putenv_result = _putenv (name_x);
147 putenv_errno = errno; 149 putenv_errno = errno;
148 for (ep = environ; *ep; ep++) 150 for (ep = environ; *ep; ep++)
149 if (strcmp (*ep, name_x) == 0) 151 if (strcmp (*ep, name_x) == 0)
150 { 152 {
151 *ep = string; 153 *ep = string;
152 break; 154 break;
153 } 155 }
154# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ 156# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
155 if (putenv_result == 0) 157 if (putenv_result == 0)
156 { 158 {
157 /* _putenv propagated "NAME= " into the subprocess environment; 159 /* _putenv propagated "NAME= " into the subprocess environment;
158 fix that by calling SetEnvironmentVariable directly. */ 160 fix that by calling SetEnvironmentVariable directly. */
159 name_x[name_end - string] = 0; 161 name_x[name_end - string] = 0;
160 putenv_result = SetEnvironmentVariable (name_x, "") ? 0 : -1; 162 putenv_result = SetEnvironmentVariable (name_x, "") ? 0 : -1;
161 putenv_errno = ENOMEM; /* ENOMEM is the only way to fail. */ 163 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 } 164 }
165# endif
166 free (name_x);
167 __set_errno (putenv_errno);
168 return putenv_result;
169 }
168#else 170#else
169 for (ep = environ; *ep; ep++) 171 for (ep = environ; *ep; ep++)
170 if (strncmp (*ep, string, name_end - string) == 0 172 if (strncmp (*ep, string, name_end - string) == 0
@@ -186,7 +188,7 @@ putenv (char *string)
186 last_environ = new_environ; 188 last_environ = new_environ;
187 environ = new_environ; 189 environ = new_environ;
188 } 190 }
189#endif
190 191
191 return 0; 192 return 0;
193#endif
192} 194}