Add setenv to standard library

This commit is contained in:
Brian Anderson 2011-07-17 17:11:40 -07:00
parent c18127b913
commit 6fb168b3db
5 changed files with 113 additions and 2 deletions

View file

@ -1,8 +1,53 @@
#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
fn getenv(str n) -> option::t[str] {
auto s = os::libc::getenv(str::buf(n));
ret if (s as int == 0) {
option::none[str]
} else { option::some[str](str::str_from_cstr(s)) };
}
}
#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
fn setenv(str n, str v) {
auto nbuf = str::buf(n);
auto vbuf = str::buf(v);
os::libc::setenv(nbuf, vbuf, 1);
}
#[cfg(target_os = "win32")]
fn getenv(str n) -> option::t[str]{
auto nbuf = str::buf(n);
auto nsize = 256u;
while (true) {
auto vstr = str::alloc(nsize - 1u);
auto vbuf = str::buf(vstr);
auto res = os::rustrt::rust_GetEnvironmentVariable(nbuf, vbuf, nsize);
if (res == 0u) {
ret option::none;
} else if (res < nsize) {
ret option::some(str::str_from_cstr(vbuf));
} else {
nsize = res;
}
}
fail;
}
#[cfg(target_os = "win32")]
fn setenv(str n, str v) {
auto nbuf = str::buf(n);
auto vbuf = str::buf(v);
os::rustrt::rust_SetEnvironmentVariable(nbuf, vbuf);
}
// 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:

View file

@ -18,7 +18,6 @@ native "cdecl" mod libc = "" {
fn fwrite(vbuf buf, uint size, uint n, FILE f) -> uint;
fn fseek(FILE f, int offset, int whence) -> int;
fn ftell(FILE f) -> int;
fn getenv(sbuf n) -> sbuf;
fn _pipe(*mutable int fds, uint size, int mode) -> int;
}
@ -67,6 +66,8 @@ fn fd_FILE(int fd) -> libc::FILE { ret libc::_fdopen(fd, str::buf("r")); }
native "rust" mod rustrt {
fn rust_process_wait(int handle) -> int;
fn rust_getcwd() -> str;
fn rust_SetEnvironmentVariable(sbuf n, sbuf v) -> int;
fn rust_GetEnvironmentVariable(sbuf n, sbuf v, uint nsize) -> uint;
}
fn waitpid(int pid) -> int { ret rustrt::rust_process_wait(pid); }