Skip to content

Commit 9b3f8bf

Browse files
author
Elly Jones
committed
libstd: add fs::change_dir()
1 parent 1974cf9 commit 9b3f8bf

File tree

4 files changed

+18
-0
lines changed

4 files changed

+18
-0
lines changed

src/lib/fs.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,21 @@ fn remove_dir(p: path) -> bool {
178178
}
179179
}
180180

181+
fn change_dir(p: path) -> bool {
182+
ret chdir(p);
183+
184+
#[cfg(target_os = "win32")]
185+
fn chdir(_p: path) -> bool {
186+
ret str::as_buf(_p, {|buf| os::kernel32::SetCurrentDirectory(buf)});
187+
}
188+
189+
#[cfg(target_os = "linux")]
190+
#[cfg(target_os = "macos")]
191+
fn chdir(_p: path) -> bool {
192+
ret str::as_buf(_p, {|buf| os::libc::chdir(buf) == 0i32 });
193+
}
194+
}
195+
181196
/*
182197
Function: path_is_absolute
183198

src/lib/linux_os.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ native mod libc {
5353
fn readlink(path: str::sbuf, buf: str::sbuf, bufsize: size_t) -> ssize_t;
5454
fn mkdir(path: str::sbuf, mode: c_int) -> c_int;
5555
fn rmdir(path: str::sbuf) -> c_int;
56+
fn chdir(path: str::sbuf) -> c_int;
5657
}
5758

5859
mod libc_constants {

src/lib/macos_os.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ native mod libc {
4646
fn waitpid(pid: pid_t, &status: c_int, options: c_int) -> c_int;
4747
fn mkdir(s: str::sbuf, mode: c_int) -> c_int;
4848
fn rmdir(s: str::sbuf) -> c_int;
49+
fn chdir(s: str::sbuf) -> c_int;
4950
}
5051

5152
mod libc_constants {

src/lib/win32_os.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ native mod kernel32 {
5555
fn CreateDirectoryA(lpPathName: LPCTSTR,
5656
lpSecurityAttributes: LPSECURITY_ATTRIBUTES) -> bool;
5757
fn RemoveDirectoryA(lpPathName: LPCTSTR) -> bool;
58+
fn SetCurrentDirectory(lpPathName: LPCTSTR) -> bool;
5859
}
5960

6061
// FIXME turn into constants

0 commit comments

Comments
 (0)