master
 1#include <stdlib.h>
 2#include <string.h>
 3#include <errno.h>
 4#include <unistd.h>
 5
 6static void dummy(char *old, char *new) {}
 7weak_alias(dummy, __env_rm_add);
 8
 9int unsetenv(const char *name)
10{
11	size_t l = __strchrnul(name, '=') - name;
12	if (!l || name[l]) {
13		errno = EINVAL;
14		return -1;
15	}
16#ifdef __wasilibc_unmodified_upstream // Lazy environment variable init.
17#else
18// This specialized header is included within the function body to arranges for
19// the environment variables to be lazily initialized. It redefined `__environ`,
20// so don't remove or reorder it with respect to other code.
21#include "wasi/libc-environ-compat.h"
22#endif
23	if (__environ) {
24		char **e = __environ, **eo = e;
25		for (; *e; e++)
26			if (!strncmp(name, *e, l) && l[*e] == '=')
27				__env_rm_add(*e, 0);
28			else if (eo != e)
29				*eo++ = *e;
30			else
31				eo++;
32		if (eo != e) *eo = 0;
33	}
34	return 0;
35}