]>
git.proxmox.com Git - wasi-libc.git/blob - libc-bottom-half/sources/__main_void.c
5 // The user's `main` function, expecting arguments.
6 int __main_argc_argv(int argc
, char *argv
[]);
8 // If the user's `main` function expects arguments, the compiler will rename
9 // it to `__main_argc_argv`, and this version will get linked in, which
10 // initializes the argument data and calls `__main_argc_argv`.
11 __attribute__((__weak__
, nodebug
))
12 int __main_void(void) {
15 // Get the sizes of the arrays we'll have to create to copy in the args.
18 err
= __wasi_args_sizes_get(&argc
, &argv_buf_size
);
19 if (err
!= __WASI_ERRNO_SUCCESS
) {
23 // Add 1 for the NULL pointer to mark the end, and check for overflow.
24 size_t num_ptrs
= argc
+ 1;
29 // Allocate memory for storing the argument chars.
30 char *argv_buf
= malloc(argv_buf_size
);
31 if (argv_buf
== NULL
) {
35 // Allocate memory for the array of pointers. This uses `calloc` both to
36 // handle overflow and to initialize the NULL pointer at the end.
37 char **argv
= calloc(num_ptrs
, sizeof(char *));
43 // Fill the argument chars, and the argv array with pointers into those chars.
44 // TODO: Remove the casts on `argv_ptrs` and `argv_buf` once the witx is updated with char8 support.
45 err
= __wasi_args_get((uint8_t **)argv
, (uint8_t *)argv_buf
);
46 if (err
!= __WASI_ERRNO_SUCCESS
) {
52 // Call `__main_argc_argv` with the arguments!
53 return __main_argc_argv(argc
, argv
);