2019-08-23 22:55:45 +02:00
|
|
|
module os
|
2019-07-01 17:04:09 +02:00
|
|
|
|
|
|
|
#include <dirent.h>
|
2019-07-23 23:23:13 +02:00
|
|
|
#include <unistd.h>
|
2019-07-16 01:57:03 +02:00
|
|
|
|
|
|
|
const (
|
2019-10-12 21:18:19 +02:00
|
|
|
path_separator = '/'
|
2019-07-18 20:21:48 +02:00
|
|
|
)
|
|
|
|
|
2019-09-14 22:48:30 +02:00
|
|
|
fn init_os_args(argc int, argv &byteptr) []string {
|
|
|
|
mut args := []string
|
|
|
|
for i := 0; i < argc; i++ {
|
|
|
|
args << string(argv[i])
|
|
|
|
}
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
2019-08-16 14:05:11 +02:00
|
|
|
|
2019-07-18 20:21:48 +02: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 ''
|
|
|
|
}
|
2019-09-15 14:36:05 +02:00
|
|
|
return tos(_ptr_text, vstrlen(_ptr_text))
|
2019-07-29 18:21:36 +02:00
|
|
|
}
|
2019-08-16 14:05:11 +02:00
|
|
|
|
|
|
|
pub fn ls(path string) []string {
|
|
|
|
mut res := []string
|
2019-08-23 22:55:45 +02:00
|
|
|
dir := C.opendir(path.str)
|
2019-08-16 14:05:11 +02:00
|
|
|
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
|
|
|
|
}
|
2019-09-15 18:07:40 +02:00
|
|
|
name := tos_clone(byteptr(ent.d_name))
|
2019-08-16 14:05:11 +02:00
|
|
|
if name != '.' && name != '..' && name != '' {
|
|
|
|
res << name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
C.closedir(dir)
|
|
|
|
return res
|
|
|
|
}
|
2019-08-17 15:17:43 +02:00
|
|
|
|
|
|
|
pub fn dir_exists(path string) bool {
|
|
|
|
dir := C.opendir(path.str)
|
|
|
|
res := !isnil(dir)
|
|
|
|
if res {
|
|
|
|
C.closedir(dir)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// mkdir creates a new directory with the specified path.
|
|
|
|
pub fn mkdir(path string) {
|
|
|
|
C.mkdir(path.str, 511)// S_IRWXU | S_IRWXG | S_IRWXO
|
|
|
|
}
|
|
|
|
|
|
|
|
|