summaryrefslogtreecommitdiffstats
path: root/gl/unsetenv.c
diff options
context:
space:
mode:
Diffstat (limited to 'gl/unsetenv.c')
-rw-r--r--gl/unsetenv.c34
1 files changed, 31 insertions, 3 deletions
diff --git a/gl/unsetenv.c b/gl/unsetenv.c
index d8ada2aa..d38ed37a 100644
--- a/gl/unsetenv.c
+++ b/gl/unsetenv.c
@@ -1,4 +1,4 @@
1/* Copyright (C) 1992, 1995-2002, 2005-2024 Free Software Foundation, Inc. 1/* Copyright (C) 1992, 1995-2002, 2005-2025 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
@@ -57,7 +57,6 @@ int
57unsetenv (const char *name) 57unsetenv (const char *name)
58{ 58{
59 size_t len; 59 size_t len;
60 char **ep;
61 60
62 if (name == NULL || *name == '\0' || strchr (name, '=') != NULL) 61 if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
63 { 62 {
@@ -67,9 +66,37 @@ unsetenv (const char *name)
67 66
68 len = strlen (name); 67 len = strlen (name);
69 68
69#if HAVE_DECL__PUTENV /* native Windows */
70 /* The Microsoft documentation
71 <https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/putenv-wputenv>
72 says:
73 "Don't change an environment entry directly: instead,
74 use _putenv or _wputenv to change it."
75 Note: Microsoft's _putenv updates not only the contents of _environ but
76 also the contents of _wenviron, so that both are in kept in sync.
77
78 The way to remove an environment variable is to pass to _putenv a string
79 of the form "NAME=". (NB: This is a different convention than with glibc
80 putenv, which expects a string of the form "NAME"!) */
81 {
82 int putenv_result;
83 char *name_ = malloc (len + 2);
84 if (name_ == NULL)
85 return -1;
86 memcpy (name_, name, len);
87 name_[len] = '=';
88 name_[len + 1] = 0;
89 putenv_result = _putenv (name_);
90 /* In this particular case it is OK to free() the argument passed to
91 _putenv. */
92 free (name_);
93 return putenv_result;
94 }
95#else
96
70 LOCK; 97 LOCK;
71 98
72 ep = __environ; 99 char **ep = __environ;
73 while (*ep != NULL) 100 while (*ep != NULL)
74 if (!strncmp (*ep, name, len) && (*ep)[len] == '=') 101 if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
75 { 102 {
@@ -87,6 +114,7 @@ unsetenv (const char *name)
87 UNLOCK; 114 UNLOCK;
88 115
89 return 0; 116 return 0;
117#endif
90} 118}
91 119
92#ifdef _LIBC 120#ifdef _LIBC