@@ -9,57 +9,21 @@ extern void __wasm_call_ctors(void);
99extern int __original_main (void );
1010extern void __prepare_for_exit (void );
1111void _Exit (int ) __attribute__((noreturn ));
12-
13- static __wasi_errno_t populate_libpreopen (void ) {
14- // Skip stdin, stdout, and stderr, and count up until we reach an invalid
15- // file descriptor.
16- for (__wasi_fd_t fd = 3 ; fd != 0 ; ++ fd ) {
17- __wasi_prestat_t prestat ;
18- __wasi_errno_t ret = __wasi_fd_prestat_get (fd , & prestat );
19- if (ret == __WASI_EBADF )
20- break ;
21- if (ret != __WASI_ESUCCESS )
22- return ret ;
23- switch (prestat .pr_type ) {
24- case __WASI_PREOPENTYPE_DIR : {
25- char * path = malloc (prestat .u .dir .pr_name_len + 1 );
26- if (path == NULL )
27- return __WASI_ENOMEM ;
28-
29- ret = __wasi_fd_prestat_dir_name (fd , path , prestat .u .dir .pr_name_len );
30- if (ret != __WASI_ESUCCESS ) {
31- free (path );
32- return ret ;
33- }
34- path [prestat .u .dir .pr_name_len ] = '\0' ;
35-
36- if (__wasilibc_register_preopened_fd (fd , path ) != 0 ) {
37- free (path );
38- return __WASI_ENOMEM ;
39- }
40-
41- free (path );
42- break ;
43- }
44- default :
45- break ;
46- }
47- }
48-
49- return __WASI_ESUCCESS ;
50- }
12+ __wasi_errno_t __wasilibc_populate_libpreopen (void ) __attribute__((weak ));
5113
5214void _start (void ) {
53- // Record the preopened resources.
54- if (populate_libpreopen () != __WASI_ESUCCESS ) {
15+ // Record the preopened resources, if needed.
16+ if (& __wasilibc_populate_libpreopen != NULL &&
17+ __wasilibc_populate_libpreopen () != __WASI_ESUCCESS )
18+ {
5519 _Exit (EX_OSERR );
5620 }
5721
5822 // Fill in the environment from WASI syscalls, if needed.
59- if (& __wasilibc_populate_environ != NULL ) {
60- if ( __wasilibc_populate_environ () != __WASI_ESUCCESS ) {
61- _Exit ( EX_OSERR );
62- }
23+ if (& __wasilibc_populate_environ != NULL &&
24+ __wasilibc_populate_environ () != __WASI_ESUCCESS )
25+ {
26+ _Exit ( EX_OSERR );
6327 }
6428
6529 // The linker synthesizes this to call constructors.
0 commit comments