2019-06-23 05:21:30 +03: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 21:20:28 +03:00
|
|
|
module os
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
2019-06-30 15:55:48 +03:00
|
|
|
#include <signal.h>
|
|
|
|
#include <errno.h>
|
2019-07-24 00:23:13 +03:00
|
|
|
|
2019-06-30 15:55:48 +03:00
|
|
|
//#include <execinfo.h> // for backtrace_symbols_fd
|
2019-06-29 03:06:09 +03:00
|
|
|
|
2019-07-01 12:01:48 +03:00
|
|
|
/*
|
|
|
|
struct dirent {
|
|
|
|
d_ino int
|
|
|
|
d_off int
|
|
|
|
d_reclen u16
|
|
|
|
d_type byte
|
|
|
|
d_name [256]byte
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct C.dirent {
|
|
|
|
d_name byteptr
|
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
const (
|
|
|
|
args = []string
|
2019-06-27 23:17:13 +03:00
|
|
|
MAX_PATH = 4096
|
2019-06-22 21:20:28 +03:00
|
|
|
)
|
|
|
|
|
2019-07-16 19:01:36 +03:00
|
|
|
// Windows
|
2019-07-01 12:01:48 +03:00
|
|
|
import const (
|
2019-07-16 19:01:36 +03:00
|
|
|
FILE_ATTRIBUTE_DIRECTORY
|
2019-07-01 12:01:48 +03:00
|
|
|
INVALID_FILE_ATTRIBUTES
|
2019-07-24 13:16:45 +03:00
|
|
|
)
|
2019-07-01 12:01:48 +03:00
|
|
|
|
2019-07-24 13:16:45 +03:00
|
|
|
struct C.FILE {
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct File {
|
|
|
|
cfile *FILE
|
|
|
|
}
|
|
|
|
|
2019-06-26 17:11:25 +03:00
|
|
|
struct FileInfo {
|
|
|
|
name string
|
|
|
|
size int
|
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
import const (
|
|
|
|
SEEK_SET
|
|
|
|
SEEK_END
|
2019-06-27 18:48:49 +03:00
|
|
|
SA_SIGINFO
|
2019-06-30 16:42:16 +03:00
|
|
|
S_IFMT
|
|
|
|
S_IFDIR
|
2019-07-02 23:13:23 +03:00
|
|
|
SIGABRT
|
|
|
|
SIGFPE
|
|
|
|
SIGILL
|
|
|
|
SIGINT
|
|
|
|
SIGSEGV
|
|
|
|
SIGTERM
|
2019-06-22 21:20:28 +03:00
|
|
|
)
|
|
|
|
|
2019-06-27 18:48:49 +03:00
|
|
|
struct C.stat {
|
|
|
|
st_size int
|
2019-06-27 20:02:47 +03:00
|
|
|
st_mode int
|
2019-07-07 22:46:21 +03:00
|
|
|
st_mtime int
|
2019-06-27 20:02:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct C.DIR {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-07-01 12:01:48 +03:00
|
|
|
//struct C.dirent {
|
|
|
|
//d_name byteptr
|
2019-06-27 20:02:47 +03:00
|
|
|
|
2019-07-01 12:01:48 +03:00
|
|
|
//}
|
2019-06-27 18:48:49 +03:00
|
|
|
|
|
|
|
struct C.sigaction {
|
2019-06-27 20:02:47 +03:00
|
|
|
mut:
|
|
|
|
sa_mask int
|
|
|
|
sa_sigaction int
|
|
|
|
sa_flags int
|
2019-06-27 18:48:49 +03:00
|
|
|
}
|
|
|
|
|
2019-06-26 13:56:49 +03:00
|
|
|
fn C.getline(voidptr, voidptr, voidptr) int
|
|
|
|
fn C.ftell(fp voidptr) int
|
2019-06-27 13:27:46 +03:00
|
|
|
fn C.getenv(byteptr) byteptr
|
2019-06-27 18:48:49 +03:00
|
|
|
fn C.sigaction(int, voidptr, int)
|
2019-06-26 13:56:49 +03:00
|
|
|
|
|
|
|
fn todo_remove(){}
|
|
|
|
|
2019-06-27 18:48:49 +03:00
|
|
|
fn init_os_args(argc int, argv *byteptr) []string {
|
2019-06-22 21:20:28 +03:00
|
|
|
mut args := []string
|
2019-07-24 14:49:00 +03:00
|
|
|
$if windows {
|
|
|
|
mut args_list := &voidptr(0)
|
|
|
|
mut args_count := 0
|
|
|
|
args_list = C.CommandLineToArgvW(C.GetCommandLine(), &args_count)
|
|
|
|
for i := 0; i < args_count; i++ {
|
|
|
|
args << string_from_wide(&u16(args_list[i]))
|
|
|
|
}
|
2019-07-24 19:25:18 +03:00
|
|
|
|
|
|
|
C.LocalFree(args_list)
|
2019-07-24 14:49:00 +03:00
|
|
|
} $else {
|
|
|
|
for i := 0; i < argc; i++ {
|
|
|
|
args << string(argv[i])
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
2019-06-26 00:43:04 +03:00
|
|
|
fn parse_windows_cmd_line(cmd byteptr) []string {
|
2019-06-27 13:27:46 +03:00
|
|
|
s := string(cmd)
|
2019-06-26 00:43:04 +03:00
|
|
|
return s.split(' ')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// read_file reads the file in `path` and returns the contents.
|
2019-07-03 22:07:42 +03:00
|
|
|
pub fn read_file(path string) ?string {
|
2019-07-24 13:16:45 +03:00
|
|
|
mode := 'rb'
|
|
|
|
mut fp := &C.FILE{}
|
|
|
|
$if windows {
|
|
|
|
fp = C._wfopen(path.to_wide(), mode.to_wide())
|
|
|
|
} $else {
|
|
|
|
cpath := path.str
|
|
|
|
fp = C.fopen(cpath, mode.str)
|
|
|
|
}
|
2019-07-03 22:07:42 +03:00
|
|
|
if isnil(fp) {
|
|
|
|
return error('failed to open file "$path"')
|
|
|
|
}
|
|
|
|
C.fseek(fp, 0, SEEK_END)
|
|
|
|
fsize := C.ftell(fp)
|
2019-07-21 13:22:41 +03:00
|
|
|
// C.fseek(fp, 0, SEEK_SET) // same as `C.rewind(fp)` below
|
2019-07-03 22:07:42 +03:00
|
|
|
C.rewind(fp)
|
|
|
|
mut str := malloc(fsize + 1)
|
|
|
|
C.fread(str, fsize, 1, fp)
|
|
|
|
C.fclose(fp)
|
|
|
|
str[fsize] = 0
|
2019-07-21 13:22:41 +03:00
|
|
|
return string(str, fsize)
|
2019-07-03 22:07:42 +03:00
|
|
|
}
|
|
|
|
|
2019-06-26 17:11:25 +03:00
|
|
|
// file_size returns the size of the file located in `path`.
|
2019-06-22 21:20:28 +03:00
|
|
|
pub fn file_size(path string) int {
|
2019-07-24 13:16:45 +03:00
|
|
|
mut s := C.stat{}
|
|
|
|
$if windows {
|
|
|
|
C._wstat(path.to_wide(), &s)
|
|
|
|
} $else {
|
|
|
|
C.stat(path.str, &s)
|
|
|
|
}
|
2019-06-27 18:48:49 +03:00
|
|
|
return s.st_size
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-25 21:41:46 +03:00
|
|
|
pub fn mv(old, new string) {
|
2019-07-24 13:16:45 +03:00
|
|
|
$if windows {
|
|
|
|
C._wrename(old.to_wide(), new.to_wide())
|
|
|
|
} $else {
|
|
|
|
C.rename(old.str, new.str)
|
|
|
|
}
|
2019-06-26 17:39:40 +03:00
|
|
|
}
|
2019-06-25 21:41:46 +03:00
|
|
|
|
2019-06-23 03:28:29 +03:00
|
|
|
// read_lines reads the file in `path` into an array of lines.
|
|
|
|
// TODO return `?[]string` TODO implement `?[]` support
|
2019-06-22 21:20:28 +03:00
|
|
|
pub fn read_lines(path string) []string {
|
2019-06-23 03:28:29 +03:00
|
|
|
mut res := []string
|
2019-07-31 11:56:36 +03:00
|
|
|
mut buf_len := 1024
|
|
|
|
mut buf := malloc(buf_len)
|
|
|
|
|
2019-07-24 13:16:45 +03:00
|
|
|
mode := 'rb'
|
|
|
|
mut fp := &C.FILE{}
|
|
|
|
$if windows {
|
|
|
|
fp = C._wfopen(path.to_wide(), mode.to_wide())
|
|
|
|
} $else {
|
2019-07-31 11:56:36 +03:00
|
|
|
fp = C.fopen(path.str, mode.str)
|
2019-07-24 13:16:45 +03:00
|
|
|
}
|
2019-06-23 03:28:29 +03:00
|
|
|
if isnil(fp) {
|
|
|
|
// TODO
|
|
|
|
// return error('failed to open file "$path"')
|
|
|
|
return res
|
|
|
|
}
|
2019-07-31 11:56:36 +03:00
|
|
|
|
|
|
|
mut buf_index := 0
|
|
|
|
for C.fgets(buf + buf_index, buf_len - buf_index, fp) != 0 {
|
|
|
|
len := C.strlen(buf)
|
|
|
|
if len == buf_len - 1 && buf[len - 1] != 10 {
|
|
|
|
buf_len *= 2
|
|
|
|
buf = C.realloc(buf, buf_len)
|
|
|
|
if isnil(buf) {
|
|
|
|
panic('Could not reallocate the read buffer')
|
|
|
|
}
|
|
|
|
buf_index = len
|
|
|
|
continue
|
|
|
|
}
|
2019-08-01 01:48:10 +03:00
|
|
|
if buf[len - 1] == 10 || buf[len - 1] == 13 {
|
2019-07-31 11:56:36 +03:00
|
|
|
buf[len - 1] = `\0`
|
|
|
|
}
|
2019-08-01 01:48:10 +03:00
|
|
|
if len > 1 && buf[len - 2] == 13 {
|
|
|
|
buf[len - 2] = `\0`
|
2019-06-23 03:28:29 +03:00
|
|
|
}
|
|
|
|
res << tos_clone(buf)
|
2019-07-31 11:56:36 +03:00
|
|
|
buf_index = 0
|
2019-06-23 03:28:29 +03:00
|
|
|
}
|
|
|
|
C.fclose(fp)
|
|
|
|
return res
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-26 17:11:25 +03:00
|
|
|
fn read_ulines(path string) []ustring {
|
2019-06-23 03:28:29 +03:00
|
|
|
lines := read_lines(path)
|
2019-06-22 21:20:28 +03:00
|
|
|
// mut ulines := new_array(0, lines.len, sizeof(ustring))
|
|
|
|
mut ulines := []ustring
|
|
|
|
for myline in lines {
|
|
|
|
// ulines[i] = ustr
|
|
|
|
ulines << myline.ustring()
|
|
|
|
}
|
|
|
|
return ulines
|
|
|
|
}
|
|
|
|
|
2019-07-03 22:07:42 +03:00
|
|
|
pub fn open(path string) ?File {
|
2019-07-24 13:16:45 +03:00
|
|
|
mut file := File{}
|
|
|
|
$if windows {
|
|
|
|
wpath := path.to_wide()
|
|
|
|
mode := 'rb'
|
|
|
|
file = File {
|
|
|
|
cfile: C._wfopen(wpath, mode.to_wide())
|
|
|
|
}
|
|
|
|
} $else {
|
|
|
|
cpath := path.str
|
|
|
|
file = File {
|
|
|
|
cfile: C.fopen(cpath, 'rb')
|
|
|
|
}
|
2019-07-03 22:07:42 +03:00
|
|
|
}
|
|
|
|
if isnil(file.cfile) {
|
|
|
|
return error('failed to open file "$path"')
|
|
|
|
}
|
|
|
|
return file
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-07-03 22:07:42 +03:00
|
|
|
// create creates a file at a specified location and returns a writable `File` object.
|
|
|
|
pub fn create(path string) ?File {
|
2019-07-24 13:16:45 +03:00
|
|
|
mut file := File{}
|
|
|
|
$if windows {
|
|
|
|
wpath := path.replace('/', '\\').to_wide()
|
|
|
|
mode := 'wb'
|
|
|
|
file = File {
|
|
|
|
cfile: C._wfopen(wpath, mode.to_wide())
|
|
|
|
}
|
|
|
|
} $else {
|
|
|
|
cpath := path.str
|
|
|
|
file = File {
|
|
|
|
cfile: C.fopen(cpath, 'wb')
|
|
|
|
}
|
2019-07-03 22:07:42 +03:00
|
|
|
}
|
|
|
|
if isnil(file.cfile) {
|
|
|
|
return error('failed to create file "$path"')
|
|
|
|
}
|
|
|
|
return file
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-07-03 22:07:42 +03:00
|
|
|
pub fn open_append(path string) ?File {
|
2019-07-24 13:16:45 +03:00
|
|
|
mut file := File{}
|
|
|
|
$if windows {
|
|
|
|
wpath := path.replace('/', '\\').to_wide()
|
|
|
|
mode := 'ab'
|
|
|
|
file = File {
|
|
|
|
cfile: C._wfopen(wpath, mode.to_wide())
|
|
|
|
}
|
|
|
|
} $else {
|
|
|
|
cpath := path.str
|
|
|
|
file = File {
|
|
|
|
cfile: C.fopen(cpath, 'ab')
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-07-03 22:07:42 +03:00
|
|
|
if isnil(file.cfile) {
|
2019-07-24 13:16:45 +03:00
|
|
|
return error('failed to create(append) file "$path"')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-07-03 22:07:42 +03:00
|
|
|
return file
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-30 17:11:55 +03:00
|
|
|
pub fn (f File) write(s string) {
|
2019-07-24 23:47:06 +03:00
|
|
|
C.fputs(s.str, f.cfile)
|
2019-06-22 21:20:28 +03:00
|
|
|
// C.fwrite(s.str, 1, s.len, f.cfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert any value to []byte (LittleEndian) and write it
|
|
|
|
// for example if we have write(7, 4), "07 00 00 00" gets written
|
|
|
|
// write(0x1234, 2) => "34 12"
|
2019-06-30 22:07:24 +03:00
|
|
|
pub fn (f File) write_bytes(data voidptr, size int) {
|
2019-06-22 21:20:28 +03:00
|
|
|
C.fwrite(data, 1, size, f.cfile)
|
|
|
|
}
|
|
|
|
|
2019-06-30 22:07:24 +03:00
|
|
|
pub fn (f File) write_bytes_at(data voidptr, size, pos int) {
|
2019-06-22 21:20:28 +03:00
|
|
|
C.fseek(f.cfile, pos, SEEK_SET)
|
|
|
|
C.fwrite(data, 1, size, f.cfile)
|
|
|
|
C.fseek(f.cfile, 0, SEEK_END)
|
|
|
|
}
|
|
|
|
|
2019-06-30 17:11:55 +03:00
|
|
|
pub fn (f File) writeln(s string) {
|
2019-06-22 21:20:28 +03:00
|
|
|
// C.fwrite(s.str, 1, s.len, f.cfile)
|
|
|
|
// ss := s.clone()
|
|
|
|
// TODO perf
|
2019-07-22 17:51:33 +03:00
|
|
|
C.fputs(s.str, f.cfile)
|
2019-06-22 21:20:28 +03:00
|
|
|
// ss.free()
|
|
|
|
C.fputs('\n', f.cfile)
|
|
|
|
}
|
|
|
|
|
2019-07-13 03:11:11 +03:00
|
|
|
pub fn (f File) flush() {
|
|
|
|
C.fflush(f.cfile)
|
|
|
|
}
|
|
|
|
|
2019-06-26 13:56:49 +03:00
|
|
|
pub fn (f File) close() {
|
2019-06-22 21:20:28 +03:00
|
|
|
C.fclose(f.cfile)
|
|
|
|
}
|
|
|
|
|
2019-06-26 17:11:25 +03:00
|
|
|
// system starts the specified command, waits for it to complete, and returns its code.
|
|
|
|
pub fn system(cmd string) int {
|
2019-07-24 13:16:45 +03:00
|
|
|
mut ret := int(0)
|
|
|
|
$if windows {
|
|
|
|
ret = C._wsystem(cmd.to_wide())
|
|
|
|
} $else {
|
|
|
|
ret = C.system(cmd.str)
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
if ret == -1 {
|
|
|
|
os.print_c_errno()
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
fn popen(path string) *FILE {
|
|
|
|
$if windows {
|
2019-07-24 13:16:45 +03:00
|
|
|
mode := 'rb'
|
|
|
|
wpath := path.to_wide()
|
|
|
|
return C._wpopen(wpath, mode.to_wide())
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
$else {
|
2019-07-24 13:16:45 +03:00
|
|
|
cpath := path.str
|
2019-06-22 21:20:28 +03:00
|
|
|
return C.popen(cpath, 'r')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 17:11:25 +03:00
|
|
|
// exec starts the specified command, waits for it to complete, and returns its output.
|
2019-08-06 19:13:04 +03:00
|
|
|
pub fn exec(_cmd string) string {
|
|
|
|
cmd := '$_cmd 2>&1'
|
2019-07-24 13:16:45 +03:00
|
|
|
f := popen(cmd)
|
2019-06-22 21:20:28 +03:00
|
|
|
if isnil(f) {
|
2019-06-26 17:11:25 +03:00
|
|
|
// TODO optional or error code
|
2019-06-22 21:20:28 +03:00
|
|
|
println('popen $cmd failed')
|
2019-06-26 17:11:25 +03:00
|
|
|
return ''
|
|
|
|
}
|
|
|
|
buf := [1000]byte
|
|
|
|
mut res := ''
|
|
|
|
for C.fgets(buf, 1000, f) != 0 {
|
|
|
|
res += tos(buf, strlen(buf))
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
return res.trim_space()
|
|
|
|
}
|
|
|
|
|
|
|
|
// `getenv` returns the value of the environment variable named by the key.
|
2019-07-24 13:16:45 +03:00
|
|
|
pub fn getenv(key string) string {
|
|
|
|
$if windows {
|
|
|
|
s := C._wgetenv(key.to_wide())
|
|
|
|
if isnil(s) {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
return string_from_wide(s)
|
|
|
|
} $else {
|
|
|
|
s := C.getenv(key.str)
|
|
|
|
if isnil(s) {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
return string(s)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 14:51:03 +03:00
|
|
|
pub fn setenv(name string, value string, overwrite bool) int {
|
2019-07-16 17:27:07 +03:00
|
|
|
$if windows {
|
|
|
|
format := '$name=$value'
|
|
|
|
|
|
|
|
if overwrite {
|
2019-07-22 17:51:33 +03:00
|
|
|
return C._putenv(format.str)
|
2019-07-16 17:27:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
$else {
|
2019-07-22 17:51:33 +03:00
|
|
|
return C.setenv(name.str, value.str, overwrite)
|
2019-07-16 17:27:07 +03:00
|
|
|
}
|
2019-06-27 14:51:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unsetenv(name string) int {
|
2019-07-16 17:27:07 +03:00
|
|
|
$if windows {
|
|
|
|
format := '${name}='
|
|
|
|
|
2019-07-22 17:51:33 +03:00
|
|
|
return C._putenv(format.str)
|
2019-07-16 17:27:07 +03:00
|
|
|
}
|
|
|
|
$else {
|
2019-07-22 17:51:33 +03:00
|
|
|
return C.unsetenv(name.str)
|
2019-07-16 17:27:07 +03:00
|
|
|
}
|
2019-06-27 14:51:03 +03:00
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
// `file_exists` returns true if `path` exists.
|
2019-08-06 19:13:04 +03:00
|
|
|
pub fn file_exists(_path string) bool {
|
2019-06-26 17:39:40 +03:00
|
|
|
$if windows {
|
2019-08-06 19:13:04 +03:00
|
|
|
path := _path.replace('/', '\\')
|
|
|
|
return C._waccess(path.to_wide(), 0) != -1
|
2019-07-24 13:16:45 +03:00
|
|
|
} $else {
|
2019-08-06 19:13:04 +03:00
|
|
|
return C.access(_path.str, 0 ) != -1
|
2019-06-26 17:39:40 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-23 15:59:44 +03:00
|
|
|
pub fn dir_exists(path string) bool {
|
2019-07-01 12:01:48 +03:00
|
|
|
$if windows {
|
2019-08-06 19:13:04 +03:00
|
|
|
_path := path.replace('/', '\\')
|
|
|
|
attr := int(C.GetFileAttributes(_path.to_wide()))
|
2019-07-16 19:01:36 +03:00
|
|
|
if attr == INVALID_FILE_ATTRIBUTES {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (attr & FILE_ATTRIBUTE_DIRECTORY) != 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2019-07-01 12:01:48 +03:00
|
|
|
}
|
|
|
|
$else {
|
2019-07-22 17:51:33 +03:00
|
|
|
dir := C.opendir(path.str)
|
2019-07-01 12:01:48 +03:00
|
|
|
res := !isnil(dir)
|
|
|
|
if res {
|
|
|
|
C.closedir(dir)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
2019-06-23 15:59:44 +03:00
|
|
|
}
|
|
|
|
|
2019-07-01 12:01:48 +03:00
|
|
|
// mkdir creates a new directory with the specified path.
|
2019-06-22 21:20:28 +03:00
|
|
|
pub fn mkdir(path string) {
|
|
|
|
$if windows {
|
2019-08-06 19:13:04 +03:00
|
|
|
_path := path.replace('/', '\\')
|
2019-07-24 00:23:13 +03:00
|
|
|
// Windows doesnt recursively create the folders
|
|
|
|
// so we need to help it out here
|
2019-08-06 19:13:04 +03:00
|
|
|
if _path.last_index('\\') != -1 {
|
|
|
|
mkdir(_path.all_before_last('\\'))
|
2019-07-24 00:23:13 +03:00
|
|
|
}
|
2019-08-06 19:13:04 +03:00
|
|
|
C.CreateDirectory(_path.to_wide(), 0)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
$else {
|
2019-07-22 17:51:33 +03:00
|
|
|
C.mkdir(path.str, 511)// S_IRWXU | S_IRWXG | S_IRWXO
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-01 12:01:48 +03:00
|
|
|
// rm removes file in `path`.
|
2019-06-22 21:20:28 +03:00
|
|
|
pub fn rm(path string) {
|
2019-07-24 13:16:45 +03:00
|
|
|
$if windows {
|
|
|
|
C._wremove(path.to_wide())
|
|
|
|
}
|
|
|
|
$else {
|
|
|
|
C.remove(path.str)
|
|
|
|
}
|
|
|
|
// C.unlink(path.cstr())
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-07-16 16:55:51 +03:00
|
|
|
|
|
|
|
// rmdir removes a specified directory.
|
|
|
|
pub fn rmdir(path string) {
|
2019-06-26 17:39:40 +03:00
|
|
|
$if !windows {
|
2019-07-22 17:51:33 +03:00
|
|
|
C.rmdir(path.str)
|
2019-06-26 17:39:40 +03:00
|
|
|
}
|
|
|
|
$else {
|
2019-07-24 13:16:45 +03:00
|
|
|
C.RemoveDirectory(path.to_wide())
|
2019-06-26 17:39:40 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-07-16 16:55:51 +03:00
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
|
|
|
|
fn print_c_errno() {
|
2019-06-26 18:04:38 +03:00
|
|
|
//C.printf('errno=%d err="%s"\n', errno, C.strerror(errno))
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-26 12:28:06 +03:00
|
|
|
|
2019-06-26 13:56:05 +03:00
|
|
|
pub fn ext(path string) string {
|
2019-06-26 12:28:06 +03:00
|
|
|
pos := path.last_index('.')
|
|
|
|
if pos == -1 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
return path.right(pos)
|
|
|
|
}
|
|
|
|
|
2019-07-16 02:57:03 +03:00
|
|
|
|
|
|
|
// dir returns all but the last element of path, typically the path's directory.
|
|
|
|
pub fn dir(path string) string {
|
2019-08-01 02:34:28 +03:00
|
|
|
if path == '.' {
|
|
|
|
return getwd()
|
|
|
|
}
|
2019-07-16 03:06:39 +03:00
|
|
|
mut pos := -1
|
|
|
|
// TODO PathSeparator defined in os_win.v doesn't work when building V,
|
|
|
|
// because v.c is generated for a nix system.
|
|
|
|
$if windows {
|
|
|
|
pos = path.last_index('\\')
|
|
|
|
}
|
|
|
|
$else {
|
|
|
|
pos = path.last_index(PathSeparator)
|
|
|
|
}
|
2019-07-16 02:57:03 +03:00
|
|
|
if pos == -1 {
|
|
|
|
return '.'
|
|
|
|
}
|
|
|
|
return path.left(pos)
|
|
|
|
}
|
|
|
|
|
2019-06-26 13:56:05 +03:00
|
|
|
fn path_sans_ext(path string) string {
|
|
|
|
pos := path.last_index('.')
|
|
|
|
if pos == -1 {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
return path.left(pos)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
pub fn basedir(path string) string {
|
|
|
|
pos := path.last_index('/')
|
|
|
|
if pos == -1 {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
return path.left(pos + 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn filename(path string) string {
|
|
|
|
return path.all_after('/')
|
|
|
|
}
|
|
|
|
|
2019-06-26 17:11:25 +03:00
|
|
|
// get_line returns a one-line string from stdin
|
2019-06-22 21:20:28 +03:00
|
|
|
pub fn get_line() string {
|
2019-07-02 17:39:40 +03:00
|
|
|
str := get_raw_line()
|
2019-07-29 01:23:41 +03:00
|
|
|
$if windows {
|
|
|
|
return str.trim_right('\r\n')
|
|
|
|
}
|
|
|
|
$else {
|
|
|
|
return str.trim_right('\n')
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-28 00:51:25 +03:00
|
|
|
// get_raw_line returns a one-line string from stdin along with '\n' if there is any
|
|
|
|
pub fn get_raw_line() string {
|
2019-06-28 22:16:29 +03:00
|
|
|
$if windows {
|
2019-07-24 13:16:45 +03:00
|
|
|
max := 512 // MAX_PATH * sizeof(wchar_t)
|
2019-07-24 19:25:18 +03:00
|
|
|
buf := &u16(malloc(max*2))
|
2019-07-04 18:39:35 +03:00
|
|
|
h_input := C.GetStdHandle(STD_INPUT_HANDLE)
|
|
|
|
if h_input == INVALID_HANDLE_VALUE {
|
|
|
|
panic('get_raw_line() error getting input handle.')
|
|
|
|
}
|
2019-07-24 13:16:45 +03:00
|
|
|
mut nr_chars := 0
|
2019-07-02 17:39:40 +03:00
|
|
|
C.ReadConsole(h_input, buf, max, &nr_chars, 0)
|
|
|
|
if nr_chars == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
2019-07-24 13:16:45 +03:00
|
|
|
return string_from_wide2(buf, nr_chars)
|
2019-07-02 17:39:40 +03:00
|
|
|
}
|
|
|
|
$else {
|
|
|
|
//u64 is used because C.getline needs a size_t as second argument
|
|
|
|
//Otherwise, it would cause a valgrind warning and may be dangerous
|
|
|
|
//Malloc takes an int as argument so a cast has to be made
|
2019-06-28 22:16:29 +03:00
|
|
|
max := u64(256)
|
|
|
|
buf := malloc(int(max))
|
|
|
|
nr_chars := C.getline(&buf, &max, stdin)
|
|
|
|
if nr_chars == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
2019-07-21 13:22:41 +03:00
|
|
|
return string(buf, nr_chars)
|
2019-06-28 22:16:29 +03:00
|
|
|
}
|
2019-06-28 00:51:25 +03:00
|
|
|
}
|
|
|
|
|
2019-07-24 19:14:13 +03:00
|
|
|
pub fn get_lines() []string {
|
|
|
|
mut line := ''
|
|
|
|
mut inputstr := []string
|
|
|
|
for {
|
|
|
|
line = get_line()
|
|
|
|
if(line.len <= 0) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
line = line.trim_space()
|
|
|
|
inputstr << line
|
|
|
|
}
|
|
|
|
return inputstr
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_lines_joined() string {
|
|
|
|
mut line := ''
|
|
|
|
mut inputstr := ''
|
|
|
|
for {
|
|
|
|
line = get_line()
|
|
|
|
if(line.len <= 0) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
line = line.trim_space()
|
|
|
|
inputstr += line
|
|
|
|
}
|
|
|
|
return inputstr
|
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
pub fn user_os() string {
|
|
|
|
$if linux {
|
|
|
|
return 'linux'
|
|
|
|
}
|
|
|
|
$if mac {
|
|
|
|
return 'mac'
|
|
|
|
}
|
|
|
|
$if windows {
|
|
|
|
return 'windows'
|
|
|
|
}
|
2019-07-15 18:24:40 +03:00
|
|
|
$if freebsd {
|
|
|
|
return 'freebsd'
|
|
|
|
}
|
2019-07-21 13:22:41 +03:00
|
|
|
$if openbsd {
|
|
|
|
return 'openbsd'
|
|
|
|
}
|
|
|
|
$if netbsd {
|
|
|
|
return 'netbsd'
|
|
|
|
}
|
|
|
|
$if dragonfly {
|
|
|
|
return 'dragonfly'
|
2019-07-24 00:23:13 +03:00
|
|
|
}
|
2019-07-24 19:08:19 +03:00
|
|
|
$if msvc {
|
|
|
|
return 'windows'
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
return 'unknown'
|
|
|
|
}
|
|
|
|
|
2019-06-26 17:11:25 +03:00
|
|
|
// home_dir returns path to user's home directory.
|
2019-06-22 21:20:28 +03:00
|
|
|
pub fn home_dir() string {
|
|
|
|
mut home := os.getenv('HOME')
|
|
|
|
$if windows {
|
|
|
|
home = os.getenv('HOMEDRIVE')
|
2019-07-12 20:05:22 +03:00
|
|
|
if home.len == 0 {
|
|
|
|
home = os.getenv('SYSTEMDRIVE')
|
|
|
|
}
|
|
|
|
mut homepath := os.getenv('HOMEPATH')
|
|
|
|
if homepath.len == 0 {
|
|
|
|
homepath = '\\Users\\' + os.getenv('USERNAME')
|
|
|
|
}
|
|
|
|
home += homepath
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
home += '/'
|
|
|
|
return home
|
|
|
|
}
|
|
|
|
|
2019-07-21 13:22:41 +03:00
|
|
|
// write_file writes `text` data to a file in `path`.
|
2019-06-22 21:20:28 +03:00
|
|
|
pub fn write_file(path, text string) {
|
2019-07-03 22:07:42 +03:00
|
|
|
f := os.create(path) or {
|
|
|
|
return
|
|
|
|
}
|
2019-07-02 00:24:19 +03:00
|
|
|
f.write(text)
|
2019-06-22 21:20:28 +03:00
|
|
|
f.close()
|
|
|
|
}
|
|
|
|
|
2019-06-26 19:56:33 +03:00
|
|
|
pub fn clear() {
|
|
|
|
C.printf('\x1b[2J')
|
|
|
|
C.printf('\x1b[H')
|
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
fn on_segfault(f voidptr) {
|
2019-06-26 17:39:40 +03:00
|
|
|
$if windows {
|
2019-06-24 14:51:11 +03:00
|
|
|
return
|
2019-06-26 17:39:40 +03:00
|
|
|
}
|
|
|
|
$if mac {
|
2019-06-27 18:48:49 +03:00
|
|
|
mut sa := C.sigaction{}
|
|
|
|
C.memset(&sa, 0, sizeof(sigaction))
|
|
|
|
C.sigemptyset(&sa.sa_mask)
|
|
|
|
sa.sa_sigaction = f
|
|
|
|
sa.sa_flags = SA_SIGINFO
|
|
|
|
C.sigaction(SIGSEGV, &sa, 0)
|
2019-06-26 17:39:40 +03:00
|
|
|
}
|
2019-06-27 13:27:46 +03:00
|
|
|
}
|
2019-06-27 23:17:13 +03:00
|
|
|
|
2019-07-16 00:22:29 +03:00
|
|
|
pub fn executable() string {
|
2019-06-27 23:17:13 +03:00
|
|
|
$if linux {
|
2019-07-24 13:16:45 +03:00
|
|
|
mut result := malloc(MAX_PATH)
|
2019-06-27 23:17:13 +03:00
|
|
|
count := int(C.readlink('/proc/self/exe', result, MAX_PATH ))
|
2019-07-16 17:37:59 +03:00
|
|
|
if count < 0 {
|
2019-06-27 23:17:13 +03:00
|
|
|
panic('error reading /proc/self/exe to get exe path')
|
|
|
|
}
|
2019-07-21 13:22:41 +03:00
|
|
|
return string(result, count)
|
2019-06-27 23:17:13 +03:00
|
|
|
}
|
|
|
|
$if windows {
|
2019-07-24 19:25:18 +03:00
|
|
|
max := 512
|
|
|
|
mut result := &u16(malloc(max*2)) // MAX_PATH * sizeof(wchar_t)
|
|
|
|
len := int(C.GetModuleFileName( 0, result, max ))
|
2019-07-24 13:16:45 +03:00
|
|
|
return string_from_wide2(result, len)
|
2019-06-27 23:17:13 +03:00
|
|
|
}
|
|
|
|
$if mac {
|
2019-07-24 13:16:45 +03:00
|
|
|
mut result := malloc(MAX_PATH)
|
2019-07-16 00:22:29 +03:00
|
|
|
pid := C.getpid()
|
2019-07-16 17:37:59 +03:00
|
|
|
ret := C.proc_pidpath (pid, result, MAX_PATH)
|
2019-07-16 00:22:29 +03:00
|
|
|
if ret <= 0 {
|
2019-07-16 17:37:59 +03:00
|
|
|
println('os.executable() failed')
|
|
|
|
return '.'
|
2019-07-16 00:22:29 +03:00
|
|
|
}
|
|
|
|
return string(result)
|
2019-06-27 23:17:13 +03:00
|
|
|
}
|
2019-07-16 17:19:52 +03:00
|
|
|
$if freebsd {
|
2019-07-24 13:16:45 +03:00
|
|
|
mut result := malloc(MAX_PATH)
|
2019-08-04 01:16:37 +03:00
|
|
|
mib := [1 /* CTL_KERN */, 14 /* KERN_PROC */, 12 /* KERN_PROC_PATHNAME */, -1]
|
2019-07-16 17:37:59 +03:00
|
|
|
size := MAX_PATH
|
2019-08-04 01:16:37 +03:00
|
|
|
C.sysctl(mib.data, 4, result, &size, 0, 0)
|
2019-07-16 17:37:59 +03:00
|
|
|
return string(result)
|
2019-07-16 17:19:52 +03:00
|
|
|
}
|
2019-07-16 17:37:59 +03:00
|
|
|
$if openbsd {
|
|
|
|
// "Sadly there is no way to get the full path of the executed file in OpenBSD."
|
|
|
|
// lol
|
|
|
|
return os.args[0]
|
|
|
|
}
|
|
|
|
$if netbsd {
|
2019-07-24 13:16:45 +03:00
|
|
|
mut result := malloc(MAX_PATH)
|
2019-07-16 17:37:59 +03:00
|
|
|
count := int(C.readlink('/proc/curproc/exe', result, MAX_PATH ))
|
|
|
|
if count < 0 {
|
|
|
|
panic('error reading /proc/curproc/exe to get exe path')
|
|
|
|
}
|
2019-07-21 13:22:41 +03:00
|
|
|
return string(result, count)
|
2019-07-16 17:37:59 +03:00
|
|
|
}
|
|
|
|
$if dragonfly {
|
2019-07-24 13:16:45 +03:00
|
|
|
mut result := malloc(MAX_PATH)
|
2019-07-16 17:37:59 +03:00
|
|
|
count := int(C.readlink('/proc/curproc/file', result, MAX_PATH ))
|
|
|
|
if count < 0 {
|
|
|
|
panic('error reading /proc/curproc/file to get exe path')
|
|
|
|
}
|
2019-07-21 13:22:41 +03:00
|
|
|
return string(result, count)
|
2019-07-16 17:37:59 +03:00
|
|
|
}
|
|
|
|
return '.'
|
2019-06-27 23:17:13 +03:00
|
|
|
}
|
2019-06-30 15:55:48 +03:00
|
|
|
|
|
|
|
pub fn is_dir(path string) bool {
|
|
|
|
$if windows {
|
2019-07-21 18:14:25 +03:00
|
|
|
return dir_exists(path)
|
|
|
|
//val := int(C.GetFileAttributes(path.to_wide()))
|
2019-07-04 03:16:39 +03:00
|
|
|
// Note: this return is broke (wrong). we have dir_exists already how will this differ?
|
2019-07-21 18:14:25 +03:00
|
|
|
//return (val &FILE_ATTRIBUTE_DIRECTORY) > 0
|
2019-06-30 15:55:48 +03:00
|
|
|
}
|
|
|
|
$else {
|
|
|
|
statbuf := C.stat{}
|
2019-07-22 17:51:33 +03:00
|
|
|
cstr := path.str
|
2019-06-30 15:55:48 +03:00
|
|
|
if C.stat(cstr, &statbuf) != 0 {
|
|
|
|
return false
|
|
|
|
}
|
2019-07-31 11:32:00 +03:00
|
|
|
// ref: https://code.woboq.org/gcc/include/sys/stat.h.html
|
|
|
|
return (statbuf.st_mode & S_IFMT) == S_IFDIR
|
2019-06-30 15:55:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-30 17:11:55 +03:00
|
|
|
pub fn chdir(path string) {
|
2019-06-30 15:55:48 +03:00
|
|
|
$if windows {
|
2019-07-24 13:16:45 +03:00
|
|
|
C._wchdir(path.to_wide())
|
2019-06-30 15:55:48 +03:00
|
|
|
}
|
|
|
|
$else {
|
2019-07-22 17:51:33 +03:00
|
|
|
C.chdir(path.str)
|
2019-06-30 15:55:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 13:16:45 +03:00
|
|
|
pub fn getwd() string {
|
2019-06-30 15:55:48 +03:00
|
|
|
$if windows {
|
2019-07-24 19:25:18 +03:00
|
|
|
max := 512 // MAX_PATH * sizeof(wchar_t)
|
|
|
|
buf := &u16(malloc(max*2))
|
|
|
|
if C._wgetcwd(buf, max) == 0 {
|
2019-06-30 15:55:48 +03:00
|
|
|
return ''
|
|
|
|
}
|
2019-07-24 13:16:45 +03:00
|
|
|
return string_from_wide(buf)
|
2019-06-30 15:55:48 +03:00
|
|
|
}
|
2019-07-24 13:16:45 +03:00
|
|
|
$else {
|
|
|
|
buf := malloc(512)
|
2019-06-30 15:55:48 +03:00
|
|
|
if C.getcwd(buf, 512) == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
2019-07-24 13:16:45 +03:00
|
|
|
return string(buf)
|
|
|
|
}
|
2019-06-30 15:55:48 +03:00
|
|
|
}
|
|
|
|
|
2019-07-04 03:16:39 +03:00
|
|
|
// win: FILETIME
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime
|
|
|
|
struct filetime {
|
|
|
|
dwLowDateTime u32
|
|
|
|
dwHighDateTime u32
|
|
|
|
}
|
|
|
|
|
|
|
|
// win: WIN32_FIND_DATA
|
2019-07-24 13:16:45 +03:00
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-_win32_find_dataw
|
2019-07-04 03:16:39 +03:00
|
|
|
struct win32finddata {
|
|
|
|
mut:
|
|
|
|
dwFileAttributes u32
|
|
|
|
ftCreationTime filetime
|
|
|
|
ftLastAccessTime filetime
|
|
|
|
ftLastWriteTime filetime
|
|
|
|
nFileSizeHigh u32
|
|
|
|
nFileSizeLow u32
|
|
|
|
dwReserved0 u32
|
|
|
|
dwReserved1 u32
|
|
|
|
cFileName [260]u16 // MAX_PATH = 260
|
|
|
|
cAlternateFileName [14]u16 // 14
|
|
|
|
dwFileType u32
|
|
|
|
dwCreatorType u32
|
|
|
|
wFinderFlags u16
|
|
|
|
}
|
|
|
|
|
2019-06-30 15:55:48 +03:00
|
|
|
pub fn ls(path string) []string {
|
2019-07-01 12:01:48 +03:00
|
|
|
$if windows {
|
2019-07-04 03:16:39 +03:00
|
|
|
mut find_file_data := win32finddata{}
|
|
|
|
mut dir_files := []string
|
2019-07-04 04:07:05 +03:00
|
|
|
// We can also check if the handle is valid. but using dir_exists instead
|
2019-07-22 17:51:33 +03:00
|
|
|
// h_find_dir := C.FindFirstFile(path.str, &find_file_data)
|
2019-07-04 04:07:05 +03:00
|
|
|
// if (INVALID_HANDLE_VALUE == h_find_dir) {
|
|
|
|
// return dir_files
|
|
|
|
// }
|
|
|
|
// C.FindClose(h_find_dir)
|
2019-07-04 03:16:39 +03:00
|
|
|
if !dir_exists(path) {
|
|
|
|
println('ls() couldnt open dir "$path" (does not exist).')
|
2019-07-04 04:07:05 +03:00
|
|
|
return dir_files
|
2019-06-30 15:55:48 +03:00
|
|
|
}
|
2019-07-04 03:16:39 +03:00
|
|
|
// NOTE: Should eventually have path struct & os dependant path seperator (eg os.PATH_SEPERATOR)
|
|
|
|
// we need to add files to path eg. c:\windows\*.dll or :\windows\*
|
|
|
|
path_files := '$path\\*'
|
|
|
|
// NOTE:TODO: once we have a way to convert utf16 wide character to utf8
|
|
|
|
// we should use FindFirstFileW and FindNextFileW
|
2019-07-24 13:16:45 +03:00
|
|
|
h_find_files := C.FindFirstFile(path_files.to_wide(), &find_file_data)
|
|
|
|
first_filename := string_from_wide(&u16(find_file_data.cFileName))
|
2019-07-04 03:16:39 +03:00
|
|
|
if first_filename != '.' && first_filename != '..' {
|
|
|
|
dir_files << first_filename
|
2019-06-30 15:55:48 +03:00
|
|
|
}
|
2019-07-04 03:16:39 +03:00
|
|
|
for C.FindNextFile(h_find_files, &find_file_data) {
|
2019-07-24 13:16:45 +03:00
|
|
|
filename := string_from_wide(&u16(find_file_data.cFileName))
|
2019-07-04 03:16:39 +03:00
|
|
|
if filename != '.' && filename != '..' {
|
|
|
|
dir_files << filename.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
C.FindClose(h_find_files)
|
|
|
|
return dir_files
|
|
|
|
}
|
|
|
|
$else {
|
|
|
|
mut res := []string
|
2019-07-22 17:51:33 +03:00
|
|
|
dir := C.opendir(path.str)
|
2019-07-04 03:16:39 +03: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
|
|
|
|
}
|
|
|
|
name := tos_clone(ent.d_name)
|
|
|
|
if name != '.' && name != '..' && name != '' {
|
|
|
|
res << name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
C.closedir(dir)
|
|
|
|
return res
|
|
|
|
}
|
2019-06-30 15:55:48 +03:00
|
|
|
}
|
|
|
|
|
2019-07-02 23:13:23 +03:00
|
|
|
pub fn signal(signum int, handler voidptr) {
|
|
|
|
C.signal(signum, handler)
|
|
|
|
}
|
|
|
|
|
2019-07-14 05:18:54 +03:00
|
|
|
pub fn fork() int {
|
2019-07-14 13:36:25 +03:00
|
|
|
$if !windows {
|
|
|
|
pid := C.fork()
|
|
|
|
return pid
|
|
|
|
}
|
2019-07-14 05:18:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn wait() int {
|
2019-07-14 13:36:25 +03:00
|
|
|
$if !windows {
|
|
|
|
pid := C.wait(0)
|
|
|
|
return pid
|
|
|
|
}
|
2019-07-14 05:18:54 +03:00
|
|
|
}
|
|
|
|
|
2019-07-07 22:46:21 +03:00
|
|
|
pub fn file_last_mod_unix(path string) int {
|
|
|
|
attr := C.stat{}
|
|
|
|
//# struct stat attr;
|
|
|
|
C.stat(path.str, &attr)
|
|
|
|
//# stat(path.str, &attr);
|
|
|
|
return attr.st_mtime
|
|
|
|
//# return attr.st_mtime ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-30 15:55:48 +03:00
|
|
|
fn log(s string) {
|
|
|
|
}
|
|
|
|
|
2019-07-24 17:15:21 +03:00
|
|
|
pub fn flush_stdout() {
|
|
|
|
C.fflush(stdout)
|
|
|
|
}
|
|
|
|
|
2019-06-30 17:11:55 +03:00
|
|
|
pub fn print_backtrace() {
|
2019-06-30 15:55:48 +03:00
|
|
|
/*
|
|
|
|
# void *buffer[100];
|
|
|
|
nptrs := 0
|
|
|
|
# nptrs = backtrace(buffer, 100);
|
|
|
|
# printf("%d!!\n", nptrs);
|
|
|
|
# backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO) ;
|
|
|
|
*/
|
|
|
|
}
|