22#include <sysexits.h>
33#include <wasi/core.h>
44
5- extern char * * environ ;
5+ extern char * * __environ ;
66extern void __wasm_call_ctors (void );
77extern int main (int , char * []);
88extern void __prepare_for_exit (void );
99void _Exit (int ) __attribute__((noreturn ));
1010
11- static __wasi_errno_t populate_args (size_t * argc , char * * argv ) {
11+ static __wasi_errno_t populate_args (size_t * argc , char * * * argv ) {
1212 __wasi_errno_t err ;
1313
1414 /* Get the sizes of the arrays we'll have to create to copy in the args. */
15- size_t argv_size ;
16- err = __wasi_arg_sizes_get (argc , & argv_size );
15+ size_t argv_buf_size ;
16+ err = __wasi_args_sizes_get (argc , & argv_buf_size );
1717 if (err != __WASI_ESUCCESS ) {
1818 return err ;
1919 }
@@ -22,43 +22,46 @@ static __wasi_errno_t populate_args(size_t *argc, char **argv) {
2222 }
2323
2424 /* Allocate memory for the array of pointers. */
25- argv = malloc (sizeof (char * ) * * argc );
25+ * argv = malloc (sizeof (char * ) * * argc );
2626 /* Allocate memory for storing the argument chars. */
27- char * argv_buf = malloc (sizeof (char ) * argv_size );
28- if (argv == NULL || argv_buf == NULL ) {
27+ char * argv_buf = malloc (sizeof (char ) * argv_buf_size );
28+ if (* argv == NULL || argv_buf == NULL ) {
2929 return __WASI_ENOMEM ;
3030 }
3131
3232 /* Fill the argument chars, and the argv array with pointers into those chars. */
33- return __wasi_argv_get ( argv , argv_buf );
33+ return __wasi_args_get ( * argv , argv_buf );
3434}
3535
3636static __wasi_errno_t populate_environ () {
3737 __wasi_errno_t err ;
3838
3939 /* Get the sizes of the arrays we'll have to create to copy in the environment. */
4040 size_t environ_count ;
41- size_t environ_size ;
42- err = __wasi_environ_sizes_get (& environ_count , & environ_size );
41+ size_t environ_buf_size ;
42+ err = __wasi_environ_sizes_get (& environ_count , & environ_buf_size );
4343 if (err != __WASI_ESUCCESS ) {
4444 return err ;
4545 }
4646 /* If there's no environment pairs, make sure environ is null and return. */
4747 if (environ_count == 0 ) {
48- environ = NULL ;
48+ __environ = NULL ;
4949 return __WASI_ESUCCESS ;
5050 }
5151
5252 /* Allocate memory for the array of pointers, plus one terminating null pointer. */
53- environ = malloc (sizeof (char * ) * (environ_count + 1 ));
53+ __environ = malloc (sizeof (char * ) * (environ_count + 1 ));
5454 /* Allocate memory for storing the environment chars. */
55- char * environ_buf = malloc (sizeof (char ) * environ_size );
56- if (environ == NULL || environ_buf == NULL ) {
55+ char * environ_buf = malloc (sizeof (char ) * environ_buf_size );
56+ if (__environ == NULL || environ_buf == NULL ) {
5757 return __WASI_ENOMEM ;
5858 }
5959
60+ /* Make sure the last pointer in the array is NULL. */
61+ __environ [environ_count ] = NULL ;
62+
6063 /* Fill the environment chars, and the __environ array with pointers into those chars. */
61- return __wasi_environ_get (environ , environ_buf );
64+ return __wasi_environ_get (__environ , environ_buf );
6265}
6366
6467void _start (void ) {
@@ -73,7 +76,7 @@ void _start(void) {
7376 /* Fill in the arguments from WASI syscalls. */
7477 size_t argc ;
7578 char * * argv ;
76- if (populate_args (& argc , argv ) != __WASI_ESUCCESS ) {
79+ if (populate_args (& argc , & argv ) != __WASI_ESUCCESS ) {
7780 _Exit (EX_OSERR );
7881 }
7982
0 commit comments