2019-06-23 04:21:30 +02:00
|
|
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
module os
|
|
|
|
|
2019-06-24 15:53:29 +02:00
|
|
|
//#include <execinfo.h> // for backtrace_symbols_fd
|
2019-06-22 20:20:28 +02:00
|
|
|
#include <signal.h>
|
2019-06-24 15:53:29 +02:00
|
|
|
#include <unistd.h>
|
2019-06-22 20:20:28 +02:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
// import darwin
|
|
|
|
fn log(s string) {
|
|
|
|
}
|
|
|
|
|
2019-06-27 19:02:47 +02:00
|
|
|
|
|
|
|
pub fn is_dir(path string) bool {
|
|
|
|
statbuf := C.stat{}
|
2019-06-22 20:20:28 +02:00
|
|
|
cstr := path.cstr()
|
2019-06-27 19:02:47 +02:00
|
|
|
if C.stat(cstr, &statbuf) != 0 {
|
2019-06-22 20:20:28 +02:00
|
|
|
return false
|
|
|
|
}
|
2019-06-27 19:02:47 +02:00
|
|
|
return statbuf.st_mode & S_IFMT == S_IFDIR
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn chdir(path string) {
|
|
|
|
C.chdir(path.cstr())
|
|
|
|
}
|
|
|
|
|
2019-06-26 12:56:49 +02:00
|
|
|
pub fn getwd() string {
|
2019-06-27 19:02:47 +02:00
|
|
|
cwd := malloc(512)
|
|
|
|
if C.getcwd(cwd, 512) == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
return string(cwd)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-06-26 12:56:49 +02:00
|
|
|
pub fn ls(path string) []string {
|
2019-06-22 20:20:28 +02:00
|
|
|
mut res := []string
|
2019-06-27 19:02:47 +02:00
|
|
|
dir := C.opendir(path.str)
|
|
|
|
if isnil(dir) {
|
2019-06-22 20:20:28 +02:00
|
|
|
println('ls() couldnt open dir "$path"')
|
|
|
|
print_c_errno()
|
|
|
|
return res
|
|
|
|
}
|
2019-06-27 19:02:47 +02:00
|
|
|
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
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-06-27 19:02:47 +02:00
|
|
|
C.closedir(dir)
|
2019-06-22 20:20:28 +02:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_backtrace() {
|
2019-06-24 15:53:29 +02:00
|
|
|
/*
|
2019-06-22 20:20:28 +02:00
|
|
|
# void *buffer[100];
|
|
|
|
nptrs := 0
|
|
|
|
# nptrs = backtrace(buffer, 100);
|
|
|
|
# printf("%d!!\n", nptrs);
|
|
|
|
# backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO) ;
|
2019-06-24 15:53:29 +02:00
|
|
|
*/
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|