]>
git.proxmox.com Git - wasi-libc.git/blob - libc-bottom-half/sources/__wasilibc_initialize_environ.c
4a8bb1a3e824eeb78936e1bd8e260205aa1ab3d2
6 #include <wasi/libc-environ.h>
8 /// If the program doesn't use `environ`, it'll get this version of
9 /// `__wasilibc_environ`, which isn't initialized with a constructor function.
10 /// `getenv` etc. call `__wasilibc_ensure_environ()` before accessing it.
11 /// Statically-initialize it to an invalid pointer value so that we can
12 /// detect if it's been explicitly initialized (we can't use `NULL` because
13 /// `clearenv` sets it to NULL.
14 char **__wasilibc_environ weak
= (char **)-1;
16 // See the comments in libc-environ.h.
17 void __wasilibc_ensure_environ(void) {
18 if (__wasilibc_environ
== (char **)-1) {
19 __wasilibc_initialize_environ();
23 /// Avoid dynamic allocation for the case where there are no environment
24 /// variables, but we still need a non-NULL pointer to an (empty) array.
25 static char *empty_environ
[1] = { NULL
};
27 // See the comments in libc-environ.h.
28 void __wasilibc_initialize_environ(void) {
29 // Get the sizes of the arrays we'll have to create to copy in the environment.
31 size_t environ_buf_size
;
32 __wasi_errno_t err
= __wasi_environ_sizes_get(&environ_count
, &environ_buf_size
);
33 if (err
!= __WASI_ERRNO_SUCCESS
) {
36 if (environ_count
== 0) {
37 __wasilibc_environ
= empty_environ
;
41 // Add 1 for the NULL pointer to mark the end, and check for overflow.
42 size_t num_ptrs
= environ_count
+ 1;
47 // Allocate memory for storing the environment chars.
48 char *environ_buf
= malloc(environ_buf_size
);
49 if (environ_buf
== NULL
) {
53 // Allocate memory for the array of pointers. This uses `calloc` both to
54 // handle overflow and to initialize the NULL pointer at the end.
55 char **environ_ptrs
= calloc(num_ptrs
, sizeof(char *));
56 if (environ_ptrs
== NULL
) {
61 // Fill the environment chars, and the `__wasilibc_environ` array with
62 // pointers into those chars.
63 // TODO: Remove the casts on `environ_ptrs` and `environ_buf` once the witx is updated with char8 support.
64 err
= __wasi_environ_get((uint8_t **)environ_ptrs
, (uint8_t *)environ_buf
);
65 if (err
!= __WASI_ERRNO_SUCCESS
) {
71 __wasilibc_environ
= environ_ptrs
;
79 // See the comments in libc-environ.h.
80 void __wasilibc_deinitialize_environ(void) {
81 if (__wasilibc_environ
!= (char **)-1) {
82 // Let libc-top-half clear the old environment-variable strings.
84 // Set the pointer to the special init value.
85 __wasilibc_environ
= (char **)-1;
89 // See the comments in libc-environ.h.
91 void __wasilibc_maybe_reinitialize_environ_eagerly(void) {
92 // This version does nothing. It may be overridden by a version which does
93 // something if `environ` is used.