commit 98d3c2df3d621e41173a06550316aac4a17c7fb1
parent 6a90fedd0ebd1ac7546c2e535035aba64e422da5
Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
Date: Thu, 30 Apr 2026 19:02:58 +0200
libc/posix: Add putenv()
While putenv() is not a c99 function, it is used in several
of the tests, but even with some fallback mechanisms there
are cases where we still need it. Adding it to the library
archive does not hurt and if it does not populate it in any
header then we don't disturb the public namespace.
Diffstat:
5 files changed, 44 insertions(+), 0 deletions(-)
diff --git a/src/libc/arch/posix/Makefile b/src/libc/arch/posix/Makefile
@@ -10,6 +10,7 @@ OBJS=\
_tzone.$O\
clock.$O\
getenv.$O\
+ putenv.$O\
raise.$O\
signal.$O\
system.$O\
diff --git a/src/libc/arch/posix/putenv.c b/src/libc/arch/posix/putenv.c
@@ -0,0 +1,40 @@
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+
+extern char **_environ;
+
+int
+putenv(char *name)
+{
+ char **p, *s;
+ size_t siz, len, cnt;
+ static char **lastenv;
+
+ if ((s = strchr(name, '=')) == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
+ len = s - name;
+
+ for (p = _environ; *p; ++p) {
+ if (!strncmp(name, *p, len) && (*p)[len] == '=') {
+ *p = name;
+ return 0;
+ }
+ }
+
+ cnt = p - _environ;
+ siz = (cnt + 2) * sizeof(char **);
+
+ if ((p = realloc(lastenv, siz)) == NULL)
+ return -1;
+ if (!lastenv)
+ memcpy(p, _environ, cnt * sizeof(char **));
+ lastenv = _environ = p;
+
+ p[cnt] = name;
+ p[cnt+1] = NULL;
+
+ return 0;
+}
diff --git a/src/libc/objs/amd64-linux.mk b/src/libc/objs/amd64-linux.mk
@@ -41,6 +41,7 @@ OBJS =\
arch/posix/_tzone.$O\
arch/posix/clock.$O\
arch/posix/getenv.$O\
+ arch/posix/putenv.$O\
arch/posix/raise.$O\
arch/posix/signal.$O\
arch/posix/system.$O\
diff --git a/src/libc/objs/amd64-netbsd.mk b/src/libc/objs/amd64-netbsd.mk
@@ -33,6 +33,7 @@ OBJS =\
arch/posix/_open.$O\
arch/posix/_tzone.$O\
arch/posix/clock.$O\
+ arch/posix/putenv.$O\
arch/posix/getenv.$O\
arch/posix/raise.$O\
arch/posix/signal.$O\
diff --git a/src/libc/objs/amd64-openbsd.mk b/src/libc/objs/amd64-openbsd.mk
@@ -37,6 +37,7 @@ OBJS =\
arch/posix/_open.$O\
arch/posix/_tzone.$O\
arch/posix/clock.$O\
+ arch/posix/putenv.$O\
arch/posix/getenv.$O\
arch/posix/raise.$O\
arch/posix/signal.$O\