]>
git.proxmox.com Git - wasi-libc.git/blob - libc-bottom-half/sources/__original_main.c
31ce665e9da8f350e2ff2d7a2218141db2a151ef
6 // The user's `main` function, expecting arguments.
7 int main(int argc
, char *argv
[]);
9 // If the user's `main` function expects arguments, the compiler won't emit
10 // an `__original_main` function so this version will get linked in, which
11 // initializes the argument data and calls `main`.
13 int __original_main(void) {
16 // Get the sizes of the arrays we'll have to create to copy in the args.
19 err
= __wasi_args_sizes_get(&argc
, &argv_buf_size
);
20 if (err
!= __WASI_ESUCCESS
) {
24 // Add 1 for the NULL pointer to mark the end, and check for overflow.
25 size_t num_ptrs
= argc
+ 1;
30 // Allocate memory for storing the argument chars.
31 char *argv_buf
= malloc(argv_buf_size
);
32 if (argv_buf
== NULL
) {
36 // Allocate memory for the array of pointers. This uses `calloc` both to
37 // handle overflow and to initialize the NULL pointer at the end.
38 char **argv
= calloc(num_ptrs
, sizeof(char *));
44 // Fill the argument chars, and the argv array with pointers into those chars.
45 err
= __wasi_args_get(argv
, argv_buf
);
46 if (err
!= __WASI_ESUCCESS
) {
52 // Call main with the arguments!
53 return main(argc
, argv
);