Skip to content

Instantly share code, notes, and snippets.

@olsonjeffery
Last active December 14, 2015 09:49
Show Gist options
  • Save olsonjeffery/5067277 to your computer and use it in GitHub Desktop.
Save olsonjeffery/5067277 to your computer and use it in GitHub Desktop.
unsafe fn get_list(p: &Path) -> ~[~str] {
use libc::{DIR, dirent_t};
use libc::{opendir, readdir, closedir};
extern mod rustrt {
unsafe fn rust_list_dir_val(ptr: *dirent_t)
-> *libc::c_char;
}
let input = p.to_str();
let mut strings = ~[];
let input_ptr = ::cast::transmute(&input[0]);
log(debug, "os::list_dir -- BEFORE OPENDIR");
let dir_ptr = opendir(input_ptr);
if (dir_ptr as uint != 0) {
log(debug, "os::list_dir -- opendir() SUCCESS");
let mut entry_ptr = readdir(dir_ptr);
while (entry_ptr as uint != 0) {
strings.push(
str::raw::from_c_str(
rustrt::rust_list_dir_val(
entry_ptr)));
entry_ptr = readdir(dir_ptr);
}
closedir(dir_ptr);
}
else {
log(debug, "os::list_dir -- opendir() FAILURE");
}
log(debug, fmt!("os::list_dir -- AFTER ITERATION -- # of results: %?", strings.len()));
strings
}
unsafe fn main() {
let p = core::path::Path(~"./");
for get_list(&p).each |e| {
log(debug, *e);
};
}
mbp:osx_list_dir kerra$ ls -la
total 200
drwxr-xr-x 6 kerra staff 204 Mar 1 15:35 .
drwxr-xr-x 3 kerra staff 102 Mar 1 14:59 ..
drwxr-xr-x 12 kerra staff 408 Mar 1 14:59 .git
-rw-r--r-- 1 kerra staff 1116 Mar 1 14:59 osx_list_dir.rs
-rwxr-xr-x 1 kerra staff 96872 Mar 1 15:35 osx_list_dir~
drwxr-xr-x 3 kerra staff 102 Mar 1 15:35 osx_list_dir~.dSYM
mbp:osx_list_dir kerra$ RUST_LOG=osx_list_dir rust run osx_list_dir.rs
warning: no debug symbols in executable (-arch x86_64)
rust: "os::list_dir -- BEFORE OPENDIR"
rust: "os::list_dir -- opendir() SUCCESS"
rust: ~"os::list_dir -- AFTER ITERATION -- # of results: 6"
rust: ~"."
rust: ~"git"
rust: ~""
rust: ~"rs"
rust: ~""
rust: ~".dSYM"
mbp:osx_list_dir kerra$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment