rust/src/lib/generic_os.rs

68 lines
1.7 KiB
Rust
Raw Normal View History

import istr::sbuf;
2011-07-17 19:11:40 -05:00
#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
fn getenv(n: &istr) -> option::t<istr> {
let s = istr::as_buf(n, { |buf|
os::libc::getenv(buf)
});
ret if unsafe::reinterpret_cast(s) == 0 {
option::none::<istr>
} else {
let s = unsafe::reinterpret_cast(s);
option::some::<istr>(istr::str_from_cstr(s))
};
2011-07-17 19:11:40 -05:00
}
#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
fn setenv(n: &istr, v: &istr) {
// FIXME (868)
let _: () = istr::as_buf(n, { |nbuf|
// FIXME (868)
let _: () = istr::as_buf(v, { |vbuf|
os::libc::setenv(nbuf, vbuf, 1);
});
});
2011-07-17 19:11:40 -05:00
}
#[cfg(target_os = "win32")]
fn getenv(n: &istr) -> option::t<istr> {
2011-07-27 07:19:39 -05:00
let nsize = 256u;
while true {
let v: [u8] = [];
vec::reserve(v, nsize);
let res = istr::as_buf(n, { |nbuf|
let vbuf = vec::to_ptr(v);
os::kernel32::GetEnvironmentVariableA(nbuf, vbuf, nsize)
});
2011-07-27 07:19:39 -05:00
if res == 0u {
2011-07-17 19:11:40 -05:00
ret option::none;
} else if res < nsize {
vec::unsafe::set_len(v, res);
ret option::some(istr::unsafe_from_bytes(v));
2011-07-27 07:19:39 -05:00
} else { nsize = res; }
2011-07-17 19:11:40 -05:00
}
fail;
}
#[cfg(target_os = "win32")]
fn setenv(n: &istr, v: &istr) {
// FIXME (868)
let _: () = istr::as_buf(n, { |nbuf|
let _: () = istr::as_buf(v, { |vbuf|
os::kernel32::SetEnvironmentVariableA(nbuf, vbuf);
});
});
2011-07-17 19:11:40 -05:00
}
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End: