Skip to content

Commit 4cbcf54

Browse files
committed
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 00ce4a4 commit 4cbcf54

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;
@@ -1151,6 +1152,88 @@ pub fn set_args(new_args: ~[~str]) {
11511152
}
11521153
}
11531154
1155+
// FIXME #6100 we should really use an internal implementation of this - using
1156+
// the POSIX glob functions isn't portable to windows, probably has slight
1157+
// inconsistencies even where it is implemented, and makes extending
1158+
// functionality a lot more difficult
1159+
// FIXME #6101 also provide a non-allocating version - each_glob or so?
1160+
/// Returns a vector of Path objects that match the given glob pattern
1161+
#[cfg(target_os = "linux")]
1162+
#[cfg(target_os = "android")]
1163+
#[cfg(target_os = "freebsd")]
1164+
#[cfg(target_os = "macos")]
1165+
pub fn glob(pattern: &str) -> ~[Path] {
1166+
#[cfg(target_os = "linux")]
1167+
#[cfg(target_os = "android")]
1168+
fn default_glob_t () -> libc::glob_t {
1169+
libc::glob_t {
1170+
gl_pathc: 0,
1171+
gl_pathv: ptr::null(),
1172+
gl_offs: 0,
1173+
__unused1: ptr::null(),
1174+
__unused2: ptr::null(),
1175+
__unused3: ptr::null(),
1176+
__unused4: ptr::null(),
1177+
__unused5: ptr::null(),
1178+
}
1179+
}
1180+
1181+
#[cfg(target_os = "freebsd")]
1182+
fn default_glob_t () -> libc::glob_t {
1183+
libc::glob_t {
1184+
gl_pathc: 0,
1185+
__unused1: 0,
1186+
gl_offs: 0,
1187+
__unused2: 0,
1188+
gl_pathv: ptr::null(),
1189+
__unused3: ptr::null(),
1190+
__unused4: ptr::null(),
1191+
__unused5: ptr::null(),
1192+
__unused6: ptr::null(),
1193+
__unused7: ptr::null(),
1194+
__unused8: ptr::null(),
1195+
}
1196+
}
1197+
1198+
#[cfg(target_os = "macos")]
1199+
fn default_glob_t () -> libc::glob_t {
1200+
libc::glob_t {
1201+
gl_pathc: 0,
1202+
__unused1: 0,
1203+
gl_offs: 0,
1204+
__unused2: 0,
1205+
gl_pathv: ptr::null(),
1206+
__unused3: ptr::null(),
1207+
__unused4: ptr::null(),
1208+
__unused5: ptr::null(),
1209+
__unused6: ptr::null(),
1210+
__unused7: ptr::null(),
1211+
__unused8: ptr::null(),
1212+
}
1213+
}
1214+
1215+
let mut g = default_glob_t();
1216+
do str::as_c_str(pattern) |c_pattern| {
1217+
unsafe { libc::glob(c_pattern, 0, ptr::null(), &mut g) }
1218+
};
1219+
do(|| {
1220+
let paths = unsafe {
1221+
vec::raw::from_buf_raw(g.gl_pathv, g.gl_pathc as uint)
1222+
};
1223+
do paths.map |&c_str| {
1224+
Path(unsafe { str::raw::from_c_str(c_str) })
1225+
}
1226+
}).finally {
1227+
unsafe { libc::globfree(&mut g) };
1228+
}
1229+
}
1230+
1231+
/// Returns a vector of Path objects that match the given glob pattern
1232+
#[cfg(target_os = "win32")]
1233+
pub fn glob(pattern: &str) -> ~[Path] {
1234+
fail!(~"glob() is unimplemented on Windows")
1235+
}
1236+
11541237
#[cfg(target_os = "macos")]
11551238
extern {
11561239
// These functions are in crt_externs.h.

0 commit comments

Comments
 (0)