Skip to content

Commit 52d2871

Browse files
committed
use wasi::get_environ
1 parent 7a4f0ae commit 52d2871

File tree

2 files changed

+17
-30
lines changed

2 files changed

+17
-30
lines changed

src/libstd/sys/wasi/args.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::ffi::CStr;
22
use crate::io;
3-
use crate::sys::cvt_wasi;
43
use crate::ffi::OsString;
54
use crate::marker::PhantomData;
65
use crate::os::wasi::ffi::OsStringExt;

src/libstd/sys/wasi/os.rs

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -75,45 +75,33 @@ pub fn current_exe() -> io::Result<PathBuf> {
7575
}
7676

7777
pub struct Env {
78-
iter: vec::IntoIter<(OsString, OsString)>,
78+
iter: Vec<Vec<u8>>,
7979
_dont_send_or_sync_me: PhantomData<*mut ()>,
8080
}
8181

8282
impl Iterator for Env {
8383
type Item = (OsString, OsString);
84-
fn next(&mut self) -> Option<(OsString, OsString)> { self.iter.next() }
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+
}
8597
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
8698
}
8799

88100

89101
pub fn env() -> Env {
90-
unsafe {
91-
let _guard = env_lock();
92-
// FIXME: replace with wasi::environ_get
93-
let mut environ = libc::environ;
94-
let mut result = Vec::new();
95-
while environ != ptr::null_mut() && *environ != ptr::null_mut() {
96-
if let Some(key_value) = parse(CStr::from_ptr(*environ).to_bytes()) {
97-
result.push(key_value);
98-
}
99-
environ = environ.offset(1);
100-
}
101-
return Env {
102-
iter: result.into_iter(),
103-
_dont_send_or_sync_me: PhantomData,
104-
}
105-
}
106-
107-
// See src/libstd/sys/unix/os.rs, same as that
108-
fn parse(input: &[u8]) -> Option<(OsString, OsString)> {
109-
if input.is_empty() {
110-
return None;
111-
}
112-
let pos = memchr::memchr(b'=', &input[1..]).map(|p| p + 1);
113-
pos.map(|p| (
114-
OsStringExt::from_vec(input[..p].to_vec()),
115-
OsStringExt::from_vec(input[p+1..].to_vec()),
116-
))
102+
Env {
103+
iter: wasi::get_environ().unwrap_or(Vec::new()),
104+
_dont_send_or_sync_me: PhantomData,
117105
}
118106
}
119107

0 commit comments

Comments
 (0)