@@ -21,11 +21,24 @@ pub unsafe fn env_lock() -> impl Any {
2121}
2222
2323pub fn errno ( ) -> i32 {
24- 0
24+ extern {
25+ #[ thread_local]
26+ static errno: libc:: c_int ;
27+ }
28+
29+ unsafe { errno as i32 }
2530}
2631
2732pub fn error_string ( errno : i32 ) -> String {
28- wasi:: error_string ( errno)
33+ let mut buf = [ 0 as libc:: c_char ; 1024 ] ;
34+
35+ let p = buf. as_mut_ptr ( ) ;
36+ unsafe {
37+ if libc:: strerror_r ( errno as libc:: c_int , p, buf. len ( ) ) < 0 {
38+ panic ! ( "strerror_r failure" ) ;
39+ }
40+ str:: from_utf8 ( CStr :: from_ptr ( p) . to_bytes ( ) ) . unwrap ( ) . to_owned ( )
41+ }
2942}
3043
3144pub fn getcwd ( ) -> io:: Result < PathBuf > {
@@ -73,35 +86,45 @@ impl StdError for JoinPathsError {
7386pub fn current_exe ( ) -> io:: Result < PathBuf > {
7487 unsupported ( )
7588}
76-
7789pub struct Env {
78- iter : vec:: IntoIter < Vec < u8 > > ,
90+ iter : vec:: IntoIter < ( OsString , OsString ) > ,
7991 _dont_send_or_sync_me : PhantomData < * mut ( ) > ,
8092}
8193
8294impl Iterator for Env {
8395 type Item = ( OsString , OsString ) ;
84- fn next ( & mut self ) -> Option < ( OsString , OsString ) > {
85- self . iter . next ( ) . and_then ( |input| {
86- // See src/libstd/sys/unix/os.rs, same as that
87- if input. is_empty ( ) {
88- return None ;
89- }
90- let pos = memchr:: memchr ( b'=' , & input[ 1 ..] ) . map ( |p| p + 1 ) ;
91- pos. map ( |p| (
92- OsStringExt :: from_vec ( input[ ..p] . to_vec ( ) ) ,
93- OsStringExt :: from_vec ( input[ p+1 ..] . to_vec ( ) ) ,
94- ) )
95- } )
96- }
96+ fn next ( & mut self ) -> Option < ( OsString , OsString ) > { self . iter . next ( ) }
9797 fn size_hint ( & self ) -> ( usize , Option < usize > ) { self . iter . size_hint ( ) }
9898}
9999
100100
101101pub fn env ( ) -> Env {
102- Env {
103- iter : wasi:: get_environ ( ) . unwrap_or ( Vec :: new ( ) ) . into_iter ( ) ,
104- _dont_send_or_sync_me : PhantomData ,
102+ unsafe {
103+ let _guard = env_lock ( ) ;
104+ let mut environ = libc:: environ;
105+ let mut result = Vec :: new ( ) ;
106+ while environ != ptr:: null_mut ( ) && * environ != ptr:: null_mut ( ) {
107+ if let Some ( key_value) = parse ( CStr :: from_ptr ( * environ) . to_bytes ( ) ) {
108+ result. push ( key_value) ;
109+ }
110+ environ = environ. offset ( 1 ) ;
111+ }
112+ return Env {
113+ iter : result. into_iter ( ) ,
114+ _dont_send_or_sync_me : PhantomData ,
115+ }
116+ }
117+
118+ // See src/libstd/sys/unix/os.rs, same as that
119+ fn parse ( input : & [ u8 ] ) -> Option < ( OsString , OsString ) > {
120+ if input. is_empty ( ) {
121+ return None ;
122+ }
123+ let pos = memchr:: memchr ( b'=' , & input[ 1 ..] ) . map ( |p| p + 1 ) ;
124+ pos. map ( |p| (
125+ OsStringExt :: from_vec ( input[ ..p] . to_vec ( ) ) ,
126+ OsStringExt :: from_vec ( input[ p+1 ..] . to_vec ( ) ) ,
127+ ) )
105128 }
106129}
107130
@@ -147,7 +170,9 @@ pub fn home_dir() -> Option<PathBuf> {
147170}
148171
149172pub fn exit ( code : i32 ) -> ! {
150- unsafe { wasi:: proc_exit ( code as u32 ) }
173+ unsafe {
174+ libc:: exit ( code)
175+ }
151176}
152177
153178pub fn getpid ( ) -> u32 {
0 commit comments