Skip to content

Commit a40116b

Browse files
author
Eric Holk
committed
stdlib: added getcwd and a convenience function to make relative paths absolute. This will be helpful for rust-lang#441.
1 parent 175fd8e commit a40116b

File tree

7 files changed

+69
-2
lines changed

7 files changed

+69
-2
lines changed

src/lib/fs.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
import os::getcwd;
23

34
native "rust" mod rustrt {
45
fn rust_file_is_dir(str path) -> int;
@@ -56,6 +57,23 @@ fn list_dir(path p) -> vec[str] {
5657
}
5758
ret full_paths;
5859
}
60+
61+
// FIXME: Windows absolute paths can start with \ for C:\ or
62+
// whatever... However, we're under MinGW32 so we have the same rules and
63+
// posix has.
64+
fn path_is_absolute(path p) -> bool {
65+
ret p.(0) == '/';
66+
}
67+
68+
fn make_absolute(path p) -> path {
69+
if(path_is_absolute(p)) {
70+
ret p;
71+
}
72+
else {
73+
ret connect(getcwd(), p);
74+
}
75+
}
76+
5977
// Local Variables:
6078
// mode: rust;
6179
// fill-column: 78;

src/lib/linux_os.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ fn waitpid(int pid) -> int {
7272
assert (os::libc::waitpid(pid, vec::buf(status), 0) != -1);
7373
ret status.(0);
7474
}
75+
76+
native "rust" mod rustrt {
77+
fn rust_getcwd() -> str;
78+
}
79+
80+
fn getcwd() -> str { ret rustrt::rust_getcwd(); }
81+
82+
7583
// Local Variables:
7684
// mode: rust;
7785
// fill-column: 78;

src/lib/macos_os.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ fn waitpid(int pid) -> int {
6969
assert (os::libc::waitpid(pid, vec::buf(status), 0) != -1);
7070
ret status.(0);
7171
}
72+
73+
native "rust" mod rustrt {
74+
fn rust_getcwd() -> str;
75+
}
76+
77+
fn getcwd() -> str { ret rustrt::rust_getcwd(); }
78+
79+
7280
// Local Variables:
7381
// mode: rust;
7482
// fill-column: 78;

src/lib/posix_fs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

2-
32
native "rust" mod rustrt {
43
fn rust_list_files(str path) -> vec[str];
54
fn rust_dirent_filename(os::libc::dirent ent) -> str;
@@ -36,6 +35,7 @@ fn list_dir(str path) -> vec[str] {
3635
const char path_sep = '/';
3736

3837
const char alt_path_sep = '/';
38+
3939
// Local Variables:
4040
// mode: rust;
4141
// fill-column: 78;

src/lib/win32_fs.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ native "rust" mod rustrt {
77

88
fn list_dir(str path) -> vec[str] { ret rustrt::rust_list_files(path + "*"); }
99

10-
1110
/* FIXME: win32 path handling actually accepts '/' or '\' and has subtly
1211
* different semantics for each. Since we build on mingw, we are usually
1312
* dealing with /-separated paths. But the whole interface to splitting and

src/lib/win32_os.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,13 @@ fn fd_FILE(int fd) -> libc::FILE { ret libc::_fdopen(fd, str::buf("r")); }
5959

6060
native "rust" mod rustrt {
6161
fn rust_process_wait(int handle) -> int;
62+
fn rust_getcwd() -> str;
6263
}
6364

6465
fn waitpid(int pid) -> int { ret rustrt::rust_process_wait(pid); }
66+
67+
fn getcwd() -> str { ret rustrt::rust_getcwd(); }
68+
6569
// Local Variables:
6670
// mode: rust;
6771
// fill-column: 78;

src/rt/rust_builtin.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,36 @@ last_os_error(rust_task *task) {
5555
return st;
5656
}
5757

58+
extern "C" CDECL rust_str *
59+
rust_getcwd(rust_task *task) {
60+
rust_dom *dom = task->dom;
61+
LOG(task, task, "rust_getcwd()");
62+
63+
char cbuf[BUF_BYTES];
64+
65+
#if defined(__WIN32__)
66+
if (!_getcwd(cbuf, sizeof(cbuf))) {
67+
#else
68+
if (!getcwd(cbuf, sizeof(cbuf))) {
69+
#endif
70+
task->fail(1);
71+
return NULL;
72+
}
73+
74+
size_t fill = strlen(cbuf) + 1;
75+
size_t alloc = next_power_of_two(sizeof(rust_str) + fill);
76+
void *mem = dom->malloc(alloc, memory_region::LOCAL);
77+
if (!mem) {
78+
task->fail(1);
79+
return NULL;
80+
}
81+
82+
rust_str *st;
83+
st = new (mem) rust_str(dom, alloc, fill, (const uint8_t *)cbuf);
84+
85+
return st;
86+
}
87+
5888
extern "C" CDECL
5989
void squareroot(rust_task *task, double *input, double *output) {
6090
*output = sqrt(*input);

0 commit comments

Comments
 (0)