2019-07-01 18:04:09 +03:00
|
|
|
module os
|
|
|
|
|
|
|
|
#include <dirent.h>
|
2019-07-24 00:23:13 +03:00
|
|
|
#include <unistd.h>
|
2019-07-16 02:57:03 +03:00
|
|
|
|
|
|
|
const (
|
|
|
|
PathSeparator = '/'
|
2019-07-18 21:21:48 +03:00
|
|
|
)
|
|
|
|
|
2019-08-16 15:05:11 +03:00
|
|
|
struct C.dirent {
|
|
|
|
d_name byteptr
|
|
|
|
}
|
|
|
|
|
|
|
|
fn C.readdir(voidptr) C.dirent
|
|
|
|
|
|
|
|
|
2019-07-18 21:21:48 +03:00
|
|
|
// get_error_msg return error code representation in string.
|
|
|
|
pub fn get_error_msg(code int) string {
|
|
|
|
_ptr_text := C.strerror(code) // voidptr?
|
|
|
|
if _ptr_text == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
return tos(_ptr_text, C.strlen(_ptr_text))
|
2019-07-29 19:21:36 +03:00
|
|
|
}
|
2019-08-16 15:05:11 +03:00
|
|
|
|
|
|
|
pub fn ls(path string) []string {
|
|
|
|
mut res := []string
|
|
|
|
dir := C.opendir(path.str)
|
|
|
|
if isnil(dir) {
|
|
|
|
println('ls() couldnt open dir "$path"')
|
|
|
|
print_c_errno()
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
mut ent := &C.dirent{!}
|
|
|
|
for {
|
|
|
|
ent = C.readdir(dir)
|
|
|
|
if isnil(ent) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
name := tos_clone(ent.d_name)
|
|
|
|
if name != '.' && name != '..' && name != '' {
|
|
|
|
res << name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
C.closedir(dir)
|
|
|
|
return res
|
|
|
|
}
|