2011-10-24 17:25:41 -05:00
|
|
|
/*
|
|
|
|
Module: generic_os
|
|
|
|
|
|
|
|
Some miscellaneous platform functions.
|
|
|
|
|
|
|
|
These should be rolled into another module.
|
|
|
|
*/
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
|
2011-10-24 17:25:41 -05:00
|
|
|
// Wow, this is an ugly way to write doc comments
|
|
|
|
|
|
|
|
#[cfg(bogus)]
|
|
|
|
/*
|
|
|
|
Function: getenv
|
|
|
|
|
|
|
|
Get the value of an environment variable
|
|
|
|
*/
|
|
|
|
fn getenv(n: str) -> option::t<str> { }
|
|
|
|
|
|
|
|
#[cfg(bogus)]
|
|
|
|
/*
|
|
|
|
Function: setenv
|
|
|
|
|
|
|
|
Set the value of an environment variable
|
|
|
|
*/
|
|
|
|
fn setenv(n: str, v: str) { }
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-07-17 19:11:40 -05:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
#[cfg(target_os = "macos")]
|
2011-10-12 15:31:41 -05:00
|
|
|
fn getenv(n: str) -> option::t<str> unsafe {
|
2011-09-02 17:34:58 -05:00
|
|
|
let s = str::as_buf(n, {|buf| os::libc::getenv(buf) });
|
2011-08-31 19:34:50 -05:00
|
|
|
ret if unsafe::reinterpret_cast(s) == 0 {
|
2011-09-02 17:34:58 -05:00
|
|
|
option::none::<str>
|
|
|
|
} else {
|
|
|
|
let s = unsafe::reinterpret_cast(s);
|
|
|
|
option::some::<str>(str::str_from_cstr(s))
|
|
|
|
};
|
2011-07-17 19:11:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
#[cfg(target_os = "macos")]
|
2011-09-12 04:27:30 -05:00
|
|
|
fn setenv(n: str, v: str) {
|
2011-08-31 19:34:50 -05:00
|
|
|
// FIXME (868)
|
2011-11-15 10:06:10 -06:00
|
|
|
str::as_buf(
|
|
|
|
n,
|
|
|
|
// FIXME (868)
|
|
|
|
{|nbuf|
|
|
|
|
str::as_buf(
|
|
|
|
v,
|
|
|
|
{|vbuf|
|
|
|
|
os::libc::setenv(nbuf, vbuf, 1i32)})});
|
2011-07-17 19:11:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "win32")]
|
2011-09-12 04:27:30 -05:00
|
|
|
fn getenv(n: str) -> option::t<str> {
|
2011-07-27 07:19:39 -05:00
|
|
|
let nsize = 256u;
|
|
|
|
while true {
|
2011-08-31 19:34:50 -05:00
|
|
|
let v: [u8] = [];
|
|
|
|
vec::reserve(v, nsize);
|
2011-09-02 17:34:58 -05:00
|
|
|
let res =
|
|
|
|
str::as_buf(n,
|
|
|
|
{|nbuf|
|
2011-10-12 18:42:13 -05:00
|
|
|
unsafe {
|
2011-09-02 17:34:58 -05:00
|
|
|
let vbuf = vec::to_ptr(v);
|
|
|
|
os::kernel32::GetEnvironmentVariableA(nbuf, vbuf,
|
|
|
|
nsize)
|
2011-10-12 18:42:13 -05:00
|
|
|
}
|
2011-09-02 17:34:58 -05:00
|
|
|
});
|
2011-07-27 07:19:39 -05:00
|
|
|
if res == 0u {
|
2011-07-17 19:11:40 -05:00
|
|
|
ret option::none;
|
2011-08-19 17:16:48 -05:00
|
|
|
} else if res < nsize {
|
2011-10-12 18:42:13 -05:00
|
|
|
unsafe {
|
|
|
|
vec::unsafe::set_len(v, res);
|
|
|
|
}
|
2011-09-01 19:27:58 -05:00
|
|
|
ret option::some(str::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")]
|
2011-09-12 04:27:30 -05:00
|
|
|
fn setenv(n: str, v: str) {
|
2011-08-31 19:34:50 -05:00
|
|
|
// FIXME (868)
|
2011-09-12 04:27:30 -05:00
|
|
|
let _: () =
|
2011-09-12 05:39:38 -05:00
|
|
|
str::as_buf(n, {|nbuf|
|
|
|
|
let _: () =
|
|
|
|
str::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
|
|
|
|
// End:
|