From c9ec058657f9f8b3fd39a16f1a9e993b4a1e982e Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Tue, 28 May 2024 15:50:03 -0400 Subject: abstract out "forced" implementations of functions from future specifications --- src/stdlib/getenv.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src/stdlib/getenv.c') diff --git a/src/stdlib/getenv.c b/src/stdlib/getenv.c index 989a5f84..c85c1922 100644 --- a/src/stdlib/getenv.c +++ b/src/stdlib/getenv.c @@ -1,6 +1,7 @@ #include #include #include "_stdlib.h" +#include "_readonly.h" /** get an environment variable **/ @@ -8,13 +9,20 @@ char * getenv(const char * name) { size_t len = 0; size_t i = 0; + static char *variable = NULL; SIGNAL_SAFE(0); + if (variable == NULL) { + variable = __readonly(RO_ALLOC, "getenv"); + } len = strlen(name); for (i = 0; environ[i] != NULL; i++) { if (!strncmp(environ[i], name, len) && environ[i][len] == '=') { - return environ[i] + len + 1; + __readonly(RO_UNLOCK, variable); + strcpy(variable, environ[i] + len + 1); + __readonly(RO_LOCK, variable); + return variable; } } -- cgit v1.2.1