stdlib: added getcwd and a convenience function to make relative paths absolute. This will be helpful for #441.

This commit is contained in:
Eric Holk 2011-06-17 11:12:51 -07:00
parent 175fd8ee73
commit a40116b398
7 changed files with 69 additions and 2 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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

View File

@ -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;

View File

@ -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);