summaryrefslogtreecommitdiffstats
path: root/gl/setenv.c
diff options
context:
space:
mode:
Diffstat (limited to 'gl/setenv.c')
-rw-r--r--gl/setenv.c22
1 files changed, 7 insertions, 15 deletions
diff --git a/gl/setenv.c b/gl/setenv.c
index ef301d41..ae53146f 100644
--- a/gl/setenv.c
+++ b/gl/setenv.c
@@ -1,4 +1,4 @@
1/* Copyright (C) 1992, 1995-2003, 2005-2025 Free Software Foundation, Inc. 1/* Copyright (C) 1992, 1995-2003, 2005-2026 Free Software Foundation, Inc.
2 This file is part of the GNU C Library. 2 This file is part of the GNU C Library.
3 3
4 This file is free software: you can redistribute it and/or modify 4 This file is free software: you can redistribute it and/or modify
@@ -126,8 +126,6 @@ int
126__add_to_environ (const char *name, const char *value, const char *combined, 126__add_to_environ (const char *name, const char *value, const char *combined,
127 int replace) 127 int replace)
128{ 128{
129 char **ep;
130 size_t size;
131 const size_t namelen = strlen (name); 129 const size_t namelen = strlen (name);
132 const size_t vallen = value != NULL ? strlen (value) + 1 : 0; 130 const size_t vallen = value != NULL ? strlen (value) + 1 : 0;
133 131
@@ -135,9 +133,9 @@ __add_to_environ (const char *name, const char *value, const char *combined,
135 133
136 /* We have to get the pointer now that we have the lock and not earlier 134 /* We have to get the pointer now that we have the lock and not earlier
137 since another thread might have created a new environment. */ 135 since another thread might have created a new environment. */
138 ep = __environ; 136 char **ep = __environ;
139 137
140 size = 0; 138 size_t size = 0;
141 if (ep != NULL) 139 if (ep != NULL)
142 { 140 {
143 for (; *ep != NULL; ++ep) 141 for (; *ep != NULL; ++ep)
@@ -149,13 +147,8 @@ __add_to_environ (const char *name, const char *value, const char *combined,
149 147
150 if (ep == NULL || *ep == NULL) 148 if (ep == NULL || *ep == NULL)
151 { 149 {
152 char **new_environ;
153#ifdef USE_TSEARCH
154 char *new_value;
155#endif
156
157 /* We allocated this space; we can extend it. */ 150 /* We allocated this space; we can extend it. */
158 new_environ = 151 char **new_environ =
159 (char **) (last_environ == NULL 152 (char **) (last_environ == NULL
160 ? malloc ((size + 2) * sizeof (char *)) 153 ? malloc ((size + 2) * sizeof (char *))
161 : realloc (last_environ, (size + 2) * sizeof (char *))); 154 : realloc (last_environ, (size + 2) * sizeof (char *)));
@@ -177,6 +170,7 @@ __add_to_environ (const char *name, const char *value, const char *combined,
177 { 170 {
178 /* See whether the value is already known. */ 171 /* See whether the value is already known. */
179#ifdef USE_TSEARCH 172#ifdef USE_TSEARCH
173 char *new_value;
180# ifdef _LIBC 174# ifdef _LIBC
181 new_value = (char *) alloca (namelen + 1 + vallen); 175 new_value = (char *) alloca (namelen + 1 + vallen);
182 __mempcpy (__mempcpy (__mempcpy (new_value, name, namelen), "=", 1), 176 __mempcpy (__mempcpy (__mempcpy (new_value, name, namelen), "=", 1),
@@ -448,7 +442,6 @@ extern int setenv (const char *, const char *, int);
448int 442int
449rpl_setenv (const char *name, const char *value, int replace) 443rpl_setenv (const char *name, const char *value, int replace)
450{ 444{
451 int result;
452 if (name == NULL || *name == '\0' || strchr (name, '=') != NULL) 445 if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
453 { 446 {
454 errno = EINVAL; 447 errno = EINVAL;
@@ -456,13 +449,12 @@ rpl_setenv (const char *name, const char *value, int replace)
456 } 449 }
457 /* Call the real setenv even if replace is 0, in case implementation 450 /* Call the real setenv even if replace is 0, in case implementation
458 has underlying data to update, such as when environ changes. */ 451 has underlying data to update, such as when environ changes. */
459 result = setenv (name, value, replace); 452 int result = setenv (name, value, replace);
460 if (result == 0 && replace && *value == '=') 453 if (result == 0 && replace && *value == '=')
461 { 454 {
462 char *tmp = getenv (name); 455 char *tmp = getenv (name);
463 if (!STREQ (tmp, value)) 456 if (!STREQ (tmp, value))
464 { 457 {
465 int saved_errno;
466 size_t len = strlen (value); 458 size_t len = strlen (value);
467 tmp = malloca (len + 2); 459 tmp = malloca (len + 2);
468 if (tmp == NULL) 460 if (tmp == NULL)
@@ -474,7 +466,7 @@ rpl_setenv (const char *name, const char *value, int replace)
474 *tmp = '='; 466 *tmp = '=';
475 memcpy (tmp + 1, value, len + 1); 467 memcpy (tmp + 1, value, len + 1);
476 result = setenv (name, tmp, replace); 468 result = setenv (name, tmp, replace);
477 saved_errno = errno; 469 int saved_errno = errno;
478 freea (tmp); 470 freea (tmp);
479 errno = saved_errno; 471 errno = saved_errno;
480 } 472 }