diff --git a/src/lib/fs.rs b/src/lib/fs.rs index 8d5cd3a4c7bc..99c823381226 100644 --- a/src/lib/fs.rs +++ b/src/lib/fs.rs @@ -1,4 +1,5 @@ +import os::getcwd; native "rust" mod rustrt { fn rust_file_is_dir(str path) -> int; @@ -56,6 +57,23 @@ fn list_dir(path p) -> vec[str] { } ret full_paths; } + +// FIXME: Windows absolute paths can start with \ for C:\ or +// whatever... However, we're under MinGW32 so we have the same rules and +// posix has. +fn path_is_absolute(path p) -> bool { + ret p.(0) == '/'; +} + +fn make_absolute(path p) -> path { + if(path_is_absolute(p)) { + ret p; + } + else { + ret connect(getcwd(), p); + } +} + // Local Variables: // mode: rust; // fill-column: 78; diff --git a/src/lib/linux_os.rs b/src/lib/linux_os.rs index 228fc2d4d1ae..b3d5cea3ac3b 100644 --- a/src/lib/linux_os.rs +++ b/src/lib/linux_os.rs @@ -72,6 +72,14 @@ fn waitpid(int pid) -> int { assert (os::libc::waitpid(pid, vec::buf(status), 0) != -1); ret status.(0); } + +native "rust" mod rustrt { + fn rust_getcwd() -> str; +} + +fn getcwd() -> str { ret rustrt::rust_getcwd(); } + + // Local Variables: // mode: rust; // fill-column: 78; diff --git a/src/lib/macos_os.rs b/src/lib/macos_os.rs index 2741ca64aed1..2ab7e7b748a5 100644 --- a/src/lib/macos_os.rs +++ b/src/lib/macos_os.rs @@ -69,6 +69,14 @@ fn waitpid(int pid) -> int { assert (os::libc::waitpid(pid, vec::buf(status), 0) != -1); ret status.(0); } + +native "rust" mod rustrt { + fn rust_getcwd() -> str; +} + +fn getcwd() -> str { ret rustrt::rust_getcwd(); } + + // Local Variables: // mode: rust; // fill-column: 78; diff --git a/src/lib/posix_fs.rs b/src/lib/posix_fs.rs index 1835b92c2b0e..c962fa7250f6 100644 --- a/src/lib/posix_fs.rs +++ b/src/lib/posix_fs.rs @@ -1,5 +1,4 @@ - native "rust" mod rustrt { fn rust_list_files(str path) -> vec[str]; fn rust_dirent_filename(os::libc::dirent ent) -> str; @@ -36,6 +35,7 @@ fn list_dir(str path) -> vec[str] { const char path_sep = '/'; const char alt_path_sep = '/'; + // Local Variables: // mode: rust; // fill-column: 78; diff --git a/src/lib/win32_fs.rs b/src/lib/win32_fs.rs index 6a9c521a598d..4b3d6ba10bc7 100644 --- a/src/lib/win32_fs.rs +++ b/src/lib/win32_fs.rs @@ -7,7 +7,6 @@ native "rust" mod rustrt { fn list_dir(str path) -> vec[str] { ret rustrt::rust_list_files(path + "*"); } - /* FIXME: win32 path handling actually accepts '/' or '\' and has subtly * different semantics for each. Since we build on mingw, we are usually * dealing with /-separated paths. But the whole interface to splitting and diff --git a/src/lib/win32_os.rs b/src/lib/win32_os.rs index 38e53fa2ff2a..1a8d2071d2e4 100644 --- a/src/lib/win32_os.rs +++ b/src/lib/win32_os.rs @@ -59,9 +59,13 @@ 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 waitpid(int pid) -> int { ret rustrt::rust_process_wait(pid); } + +fn getcwd() -> str { ret rustrt::rust_getcwd(); } + // Local Variables: // mode: rust; // fill-column: 78; diff --git a/src/rt/rust_builtin.cpp b/src/rt/rust_builtin.cpp index c713a923d460..afa022fe533a 100644 --- a/src/rt/rust_builtin.cpp +++ b/src/rt/rust_builtin.cpp @@ -55,6 +55,36 @@ last_os_error(rust_task *task) { return st; } +extern "C" CDECL rust_str * +rust_getcwd(rust_task *task) { + rust_dom *dom = task->dom; + LOG(task, task, "rust_getcwd()"); + + char cbuf[BUF_BYTES]; + +#if defined(__WIN32__) + if (!_getcwd(cbuf, sizeof(cbuf))) { +#else + if (!getcwd(cbuf, sizeof(cbuf))) { +#endif + task->fail(1); + return NULL; + } + + size_t fill = strlen(cbuf) + 1; + size_t alloc = next_power_of_two(sizeof(rust_str) + fill); + void *mem = dom->malloc(alloc, memory_region::LOCAL); + if (!mem) { + task->fail(1); + return NULL; + } + + rust_str *st; + st = new (mem) rust_str(dom, alloc, fill, (const uint8_t *)cbuf); + + return st; +} + extern "C" CDECL void squareroot(rust_task *task, double *input, double *output) { *output = sqrt(*input);