Skip to content

Commit 685baed

Browse files
doygraydon
authored andcommitted
add a higher level glob() function to os
this could probably use expansion - it just uses all of the default options, which is usually what we want, but not always. maybe add a separate function that takes more options?
1 parent 0583462 commit 685baed

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

src/libcore/os.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use ptr;
3838
use str;
3939
use task;
4040
use uint;
41+
use unstable::finally::Finally;
4142
use vec;
4243

4344
pub use libc::fclose;
@@ -1183,6 +1184,88 @@ pub fn set_args(new_args: ~[~str]) {
11831184
}
11841185
}
11851186
1187+
// FIXME #6100 we should really use an internal implementation of this - using
1188+
// the POSIX glob functions isn't portable to windows, probably has slight
1189+
// inconsistencies even where it is implemented, and makes extending
1190+
// functionality a lot more difficult
1191+
// FIXME #6101 also provide a non-allocating version - each_glob or so?
1192+
/// Returns a vector of Path objects that match the given glob pattern
1193+
#[cfg(target_os = "linux")]
1194+
#[cfg(target_os = "android")]
1195+
#[cfg(target_os = "freebsd")]
1196+
#[cfg(target_os = "macos")]
1197+
pub fn glob(pattern: &str) -> ~[Path] {
1198+
#[cfg(target_os = "linux")]
1199+
#[cfg(target_os = "android")]
1200+
fn default_glob_t () -> libc::glob_t {
1201+
libc::glob_t {
1202+
gl_pathc: 0,
1203+
gl_pathv: ptr::null(),
1204+
gl_offs: 0,
1205+
__unused1: ptr::null(),
1206+
__unused2: ptr::null(),
1207+
__unused3: ptr::null(),
1208+
__unused4: ptr::null(),
1209+
__unused5: ptr::null(),
1210+
}
1211+
}
1212+
1213+
#[cfg(target_os = "freebsd")]
1214+
fn default_glob_t () -> libc::glob_t {
1215+
libc::glob_t {
1216+
gl_pathc: 0,
1217+
__unused1: 0,
1218+
gl_offs: 0,
1219+
__unused2: 0,
1220+
gl_pathv: ptr::null(),
1221+
__unused3: ptr::null(),
1222+
__unused4: ptr::null(),
1223+
__unused5: ptr::null(),
1224+
__unused6: ptr::null(),
1225+
__unused7: ptr::null(),
1226+
__unused8: ptr::null(),
1227+
}
1228+
}
1229+
1230+
#[cfg(target_os = "macos")]
1231+
fn default_glob_t () -> libc::glob_t {
1232+
libc::glob_t {
1233+
gl_pathc: 0,
1234+
__unused1: 0,
1235+
gl_offs: 0,
1236+
__unused2: 0,
1237+
gl_pathv: ptr::null(),
1238+
__unused3: ptr::null(),
1239+
__unused4: ptr::null(),
1240+
__unused5: ptr::null(),
1241+
__unused6: ptr::null(),
1242+
__unused7: ptr::null(),
1243+
__unused8: ptr::null(),
1244+
}
1245+
}
1246+
1247+
let mut g = default_glob_t();
1248+
do str::as_c_str(pattern) |c_pattern| {
1249+
unsafe { libc::glob(c_pattern, 0, ptr::null(), &mut g) }
1250+
};
1251+
do(|| {
1252+
let paths = unsafe {
1253+
vec::raw::from_buf_raw(g.gl_pathv, g.gl_pathc as uint)
1254+
};
1255+
do paths.map |&c_str| {
1256+
Path(unsafe { str::raw::from_c_str(c_str) })
1257+
}
1258+
}).finally {
1259+
unsafe { libc::globfree(&mut g) };
1260+
}
1261+
}
1262+
1263+
/// Returns a vector of Path objects that match the given glob pattern
1264+
#[cfg(target_os = "win32")]
1265+
pub fn glob(pattern: &str) -> ~[Path] {
1266+
fail!(~"glob() is unimplemented on Windows")
1267+
}
1268+
11861269
#[cfg(target_os = "macos")]
11871270
extern {
11881271
// These functions are in crt_externs.h.

0 commit comments

Comments
 (0)