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
|
|
|
|
|
2019-11-01 19:29:51 +03:00
|
|
|
import filepath
|
2019-10-15 18:08:46 +03:00
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
#include <sys/stat.h>
|
2019-11-05 19:24:00 +03:00
|
|
|
//#include <signal.h>
|
2019-06-30 15:55:48 +03:00
|
|
|
#include <errno.h>
|
2019-07-24 00:23:13 +03:00
|
|
|
|
2019-08-22 23:19:31 +03:00
|
|
|
/*
|
2019-07-01 12:01:48 +03:00
|
|
|
struct dirent {
|
2019-10-26 01:55:16 +03:00
|
|
|
d_ino int
|
|
|
|
d_off int
|
2019-08-22 23:19:31 +03:00
|
|
|
d_reclen u16
|
|
|
|
d_type byte
|
|
|
|
d_name [256]byte
|
|
|
|
}
|
|
|
|
*/
|
2019-07-01 12:01:48 +03:00
|
|
|
|
2019-08-17 02:09:36 +03:00
|
|
|
struct C.dirent {
|
2019-08-22 23:19:31 +03:00
|
|
|
d_name byteptr
|
|
|
|
}
|
2019-08-17 02:09:36 +03:00
|
|
|
|
2019-08-22 23:19:31 +03:00
|
|
|
fn C.readdir(voidptr) C.dirent
|
2019-08-17 02:09:36 +03:00
|
|
|
|
2019-10-24 12:36:57 +03:00
|
|
|
pub const (
|
2019-06-22 21:20:28 +03:00
|
|
|
args = []string
|
2019-06-27 23:17:13 +03:00
|
|
|
MAX_PATH = 4096
|
2019-06-22 21:20:28 +03:00
|
|
|
)
|
|
|
|
|
2019-10-24 12:47:21 +03:00
|
|
|
pub struct File {
|
2019-11-15 00:15:23 +03:00
|
|
|
cfile voidptr // Using void* instead of FILE*
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-26 17:11:25 +03:00
|
|
|
struct FileInfo {
|
|
|
|
name string
|
|
|
|
size int
|
|
|
|
}
|
|
|
|
|
2019-06-27 18:48:49 +03:00
|
|
|
struct C.stat {
|
|
|
|
st_size int
|
2019-11-08 03:02:02 +03:00
|
|
|
st_mode u32
|
2019-08-22 23:19:31 +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
|
|
|
|
2019-11-02 22:37:29 +03:00
|
|
|
fn C.GetLastError() u32
|
|
|
|
|
2019-10-20 20:45:16 +03:00
|
|
|
// read_bytes reads an amount of bytes from the beginning of the file
|
|
|
|
pub fn (f File) read_bytes(size int) []byte {
|
2019-10-26 20:21:07 +03:00
|
|
|
return f.read_bytes_at(size, 0)
|
2019-10-20 20:45:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// read_bytes_at reads an amount of bytes at the given position in the file
|
|
|
|
pub fn (f File) read_bytes_at(size, pos int) []byte {
|
2019-10-26 20:21:07 +03:00
|
|
|
mut data := malloc(size)
|
|
|
|
mut arr := [`0`].repeat(size)
|
|
|
|
C.fseek(f.cfile, pos, C.SEEK_SET)
|
|
|
|
C.fread(data, 1, size, f.cfile)
|
|
|
|
C.fseek(f.cfile, 0, C.SEEK_SET)
|
|
|
|
for e := 0; e < size; e++ {
|
|
|
|
arr[e] = data[e]
|
|
|
|
}
|
|
|
|
return arr
|
2019-10-20 20:45:16 +03:00
|
|
|
}
|
|
|
|
|
2019-11-19 09:53:52 +03:00
|
|
|
pub fn read_bytes(path string) ?[]byte {
|
|
|
|
mut fp := vfopen(path, 'rb')
|
|
|
|
if isnil(fp) {
|
|
|
|
return error('failed to open file "$path"')
|
|
|
|
}
|
|
|
|
C.fseek(fp, 0, C.SEEK_END)
|
|
|
|
fsize := C.ftell(fp)
|
|
|
|
C.rewind(fp)
|
|
|
|
println('fsize=$fsize')
|
|
|
|
mut data := malloc(fsize)
|
|
|
|
C.fread(data, fsize, 1, fp)
|
|
|
|
mut res := [`0`].repeat(fsize)
|
|
|
|
for i in 0..fsize {
|
|
|
|
res[i] = data[i]
|
|
|
|
}
|
|
|
|
C.fclose(fp)
|
|
|
|
//res := []byte(data, 10) // TODO can't `return []byte(data)`
|
|
|
|
//println('res0 = ' + res[0].str())
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
// read_file reads the file in `path` and returns the contents.
|
2019-08-22 23:19:31 +03:00
|
|
|
pub fn read_file(path string) ?string {
|
2019-07-24 13:16:45 +03:00
|
|
|
mode := 'rb'
|
2019-09-15 15:57:17 +03:00
|
|
|
mut fp := vfopen(path, mode)
|
2019-07-03 22:07:42 +03:00
|
|
|
if isnil(fp) {
|
|
|
|
return error('failed to open file "$path"')
|
|
|
|
}
|
2019-08-22 23:19:31 +03:00
|
|
|
C.fseek(fp, 0, C.SEEK_END)
|
2019-07-03 22:07:42 +03:00
|
|
|
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 {
|
2019-11-16 02:30:50 +03:00
|
|
|
C._wstat(path.to_wide(), voidptr(&s))
|
2019-07-24 13:16:45 +03:00
|
|
|
} $else {
|
2019-09-15 15:57:17 +03:00
|
|
|
C.stat(*char(path.str), &s)
|
2019-07-24 13:16:45 +03:00
|
|
|
}
|
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 {
|
2019-09-15 15:57:17 +03:00
|
|
|
C.rename(*char(old.str), *char(new.str))
|
2019-07-24 13:16:45 +03:00
|
|
|
}
|
2019-06-26 17:39:40 +03:00
|
|
|
}
|
2019-06-25 21:41:46 +03:00
|
|
|
|
2019-11-02 22:37:29 +03:00
|
|
|
fn C.CopyFile(&u32, &u32, int) int
|
|
|
|
|
|
|
|
// TODO implement actual cp for linux
|
|
|
|
pub fn cp(old, new string) ?bool {
|
2019-11-01 00:57:16 +03:00
|
|
|
$if windows {
|
2019-11-02 22:37:29 +03:00
|
|
|
_old := old.replace('/', '\\')
|
|
|
|
_new := new.replace('/', '\\')
|
|
|
|
C.CopyFile(_old.to_wide(), _new.to_wide(), false)
|
|
|
|
|
|
|
|
result := C.GetLastError()
|
|
|
|
if result == 0 {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return error_with_code('failed to copy $old to $new', int(result))
|
|
|
|
}
|
|
|
|
} $else {
|
2019-11-01 00:57:16 +03:00
|
|
|
os.system('cp $old $new')
|
2019-11-02 22:37:29 +03:00
|
|
|
return true // TODO make it return true or error when cp for linux is implemented
|
|
|
|
}
|
2019-11-01 00:57:16 +03:00
|
|
|
}
|
|
|
|
|
2019-11-17 06:45:20 +03:00
|
|
|
pub fn cp_r(osource_path, odest_path string, overwrite bool) ?bool{
|
|
|
|
source_path := os.realpath( osource_path )
|
|
|
|
dest_path := os.realpath( odest_path )
|
2019-11-06 23:05:35 +03:00
|
|
|
if !os.file_exists(source_path) {
|
|
|
|
return error('Source path doesn\'t exist')
|
|
|
|
}
|
|
|
|
//single file copy
|
|
|
|
if !os.is_dir(source_path) {
|
2019-11-09 22:05:44 +03:00
|
|
|
adjasted_path := if os.is_dir(dest_path) {
|
2019-11-19 02:25:55 +03:00
|
|
|
filepath.join(dest_path, os.filename(source_path))
|
|
|
|
} else {
|
|
|
|
dest_path
|
2019-11-17 06:45:20 +03:00
|
|
|
}
|
2019-11-06 23:05:35 +03:00
|
|
|
if os.file_exists(adjasted_path) {
|
2019-11-17 06:45:20 +03:00
|
|
|
if overwrite {
|
2019-11-19 02:25:55 +03:00
|
|
|
os.rm(adjasted_path)
|
2019-11-17 06:45:20 +03:00
|
|
|
}
|
2019-11-19 02:25:55 +03:00
|
|
|
else {
|
|
|
|
return error('Destination file path already exist')
|
2019-11-17 06:45:20 +03:00
|
|
|
}
|
2019-11-06 23:05:35 +03:00
|
|
|
}
|
|
|
|
os.cp(source_path, adjasted_path) or { return error(err) }
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if !os.is_dir(dest_path) {
|
|
|
|
return error('Destination path is not a valid directory')
|
|
|
|
}
|
|
|
|
files := os.ls(source_path) or { return error(err) }
|
|
|
|
for file in files {
|
|
|
|
sp := filepath.join(source_path, file)
|
|
|
|
dp := filepath.join(dest_path, file)
|
|
|
|
if os.is_dir(sp) {
|
2019-11-23 19:55:18 +03:00
|
|
|
os.mkdir(dp) or { panic(err) }
|
2019-11-06 23:05:35 +03:00
|
|
|
}
|
|
|
|
cp_r(sp, dp, overwrite) or {
|
|
|
|
os.rmdir(dp)
|
2019-11-09 22:05:44 +03:00
|
|
|
panic(err)
|
2019-11-06 23:05:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-11-19 12:55:03 +03:00
|
|
|
// mv_by_cp first copies the source file, and if it is copied successfully, deletes the source file.
|
|
|
|
// mv_by_cp may be used when you are not sure that the source and target are on the same mount/partition.
|
|
|
|
pub fn mv_by_cp(source string, target string) ?bool {
|
|
|
|
os.cp(source, target) or { return error(err) }
|
|
|
|
os.rm(source)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-11-06 23:05:35 +03:00
|
|
|
fn vfopen(path, mode string) *C.FILE {
|
2019-09-15 15:57:17 +03:00
|
|
|
$if windows {
|
|
|
|
return C._wfopen(path.to_wide(), mode.to_wide())
|
|
|
|
} $else {
|
|
|
|
return C.fopen(*char(path.str), *char(mode.str))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-23 03:28:29 +03:00
|
|
|
// read_lines reads the file in `path` into an array of lines.
|
2019-11-19 02:25:55 +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'
|
2019-09-15 15:57:17 +03:00
|
|
|
mut fp := vfopen(path, mode)
|
2019-06-23 03:28:29 +03:00
|
|
|
if isnil(fp) {
|
2019-11-19 02:25:55 +03:00
|
|
|
return error('read_lines() failed to open file "$path"')
|
2019-06-23 03:28:29 +03:00
|
|
|
}
|
2019-07-31 11:56:36 +03:00
|
|
|
|
|
|
|
mut buf_index := 0
|
|
|
|
for C.fgets(buf + buf_index, buf_len - buf_index, fp) != 0 {
|
2019-09-15 15:36:05 +03:00
|
|
|
len := vstrlen(buf)
|
2019-07-31 11:56:36 +03:00
|
|
|
if len == buf_len - 1 && buf[len - 1] != 10 {
|
|
|
|
buf_len *= 2
|
|
|
|
buf = C.realloc(buf, buf_len)
|
|
|
|
if isnil(buf) {
|
2019-11-19 02:25:55 +03:00
|
|
|
return error('could not reallocate the read buffer')
|
2019-07-31 11:56:36 +03:00
|
|
|
}
|
|
|
|
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-11-19 02:25:55 +03:00
|
|
|
fn read_ulines(path string) ?[]ustring {
|
|
|
|
lines := read_lines(path) or {
|
|
|
|
return err
|
|
|
|
}
|
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 {
|
2019-08-22 23:19:31 +03:00
|
|
|
cpath := path.str
|
2019-07-24 13:16:45 +03:00
|
|
|
file = File {
|
2019-09-15 15:57:17 +03:00
|
|
|
cfile: C.fopen(*char(cpath), 'rb')
|
2019-07-24 13:16:45 +03:00
|
|
|
}
|
2019-07-03 22:07:42 +03:00
|
|
|
}
|
|
|
|
if isnil(file.cfile) {
|
|
|
|
return error('failed to open file "$path"')
|
|
|
|
}
|
2019-08-22 23:19:31 +03:00
|
|
|
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 {
|
2019-08-22 23:19:31 +03:00
|
|
|
cpath := path.str
|
2019-07-24 13:16:45 +03:00
|
|
|
file = File {
|
2019-09-15 15:57:17 +03:00
|
|
|
cfile: C.fopen(*char(cpath), 'wb')
|
2019-07-24 13:16:45 +03:00
|
|
|
}
|
2019-07-03 22:07:42 +03:00
|
|
|
}
|
|
|
|
if isnil(file.cfile) {
|
|
|
|
return error('failed to create file "$path"')
|
|
|
|
}
|
2019-08-22 23:19:31 +03:00
|
|
|
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 {
|
2019-08-22 23:19:31 +03:00
|
|
|
cpath := path.str
|
2019-07-24 13:16:45 +03:00
|
|
|
file = File {
|
2019-09-15 15:57:17 +03:00
|
|
|
cfile: C.fopen(*char(cpath), 'ab')
|
2019-07-24 13:16:45 +03:00
|
|
|
}
|
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-08-22 23:19:31 +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-08-22 23:19:31 +03:00
|
|
|
C.fseek(f.cfile, pos, C.SEEK_SET)
|
2019-06-22 21:20:28 +03:00
|
|
|
C.fwrite(data, 1, size, f.cfile)
|
2019-08-22 23:19:31 +03:00
|
|
|
C.fseek(f.cfile, 0, C.SEEK_END)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
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.
|
2019-11-05 18:41:56 +03:00
|
|
|
fn vpopen(path string) voidptr {//*C.FILE {
|
2019-06-22 21:20:28 +03:00
|
|
|
$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-10-08 02:34:55 +03:00
|
|
|
fn posix_wait4_to_exit_status(waitret int) (int,bool) {
|
|
|
|
$if windows {
|
|
|
|
return waitret, false
|
|
|
|
}
|
|
|
|
$else {
|
|
|
|
mut ret := 0
|
|
|
|
mut is_signaled := true
|
|
|
|
// (see man system, man 2 waitpid: C macro WEXITSTATUS section)
|
|
|
|
if C.WIFEXITED( waitret ) {
|
|
|
|
ret = C.WEXITSTATUS( waitret )
|
|
|
|
is_signaled = false
|
|
|
|
} else if C.WIFSIGNALED( waitret ){
|
|
|
|
ret = C.WTERMSIG( waitret )
|
|
|
|
is_signaled = true
|
|
|
|
}
|
|
|
|
return ret , is_signaled
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 18:41:56 +03:00
|
|
|
fn vpclose(f voidptr) int {
|
2019-08-08 08:30:05 +03:00
|
|
|
$if windows {
|
2019-10-08 02:34:55 +03:00
|
|
|
return int( C._pclose(f) )
|
2019-08-08 08:30:05 +03:00
|
|
|
}
|
|
|
|
$else {
|
2019-10-08 02:34:55 +03:00
|
|
|
ret , _ := posix_wait4_to_exit_status( int( C.pclose(f) ) )
|
|
|
|
return ret
|
2019-08-08 08:30:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 21:58:28 +03:00
|
|
|
pub struct Result {
|
2019-08-22 23:19:31 +03:00
|
|
|
pub:
|
|
|
|
exit_code int
|
2019-08-17 20:21:59 +03:00
|
|
|
output string
|
2019-08-22 23:19:31 +03:00
|
|
|
//stderr string // TODO
|
|
|
|
}
|
2019-08-17 20:21:59 +03:00
|
|
|
|
2019-10-10 22:27:22 +03:00
|
|
|
// `system` works like `exec()`, but only returns a return code.
|
2019-08-17 20:21:59 +03:00
|
|
|
pub fn system(cmd string) int {
|
2019-11-04 02:41:15 +03:00
|
|
|
if cmd.contains(';') || cmd.contains('&&') || cmd.contains('||') || cmd.contains('\n') {
|
2019-11-03 23:13:56 +03:00
|
|
|
// TODO remove panic
|
2019-11-04 02:41:15 +03:00
|
|
|
panic(';, &&, || and \\n are not allowed in shell commands')
|
2019-11-03 23:13:56 +03:00
|
|
|
}
|
2019-08-17 20:21:59 +03:00
|
|
|
mut ret := int(0)
|
|
|
|
$if windows {
|
2019-11-17 14:36:05 +03:00
|
|
|
// overcome bug in system & _wsystem (cmd) when first char is quote `"`
|
|
|
|
wcmd := if cmd.len > 1 && cmd[0] == `"` && cmd[1] != `"` { '"$cmd"' } else { cmd }
|
|
|
|
ret = C._wsystem(wcmd.to_wide())
|
2019-08-17 20:21:59 +03:00
|
|
|
} $else {
|
2019-08-22 23:19:31 +03:00
|
|
|
ret = C.system(cmd.str)
|
2019-08-08 08:30:05 +03:00
|
|
|
}
|
2019-08-17 20:21:59 +03:00
|
|
|
if ret == -1 {
|
2019-10-08 02:34:55 +03:00
|
|
|
print_c_errno()
|
|
|
|
}
|
|
|
|
|
|
|
|
$if !windows {
|
|
|
|
pret , is_signaled := posix_wait4_to_exit_status( ret )
|
|
|
|
if is_signaled {
|
|
|
|
println('Terminated by signal ${ret:2d} (' + sigint_to_signal_name(pret) + ')' )
|
|
|
|
}
|
|
|
|
ret = pret
|
2019-08-17 20:21:59 +03:00
|
|
|
}
|
|
|
|
return ret
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-10-08 02:34:55 +03:00
|
|
|
pub fn sigint_to_signal_name(si int) string {
|
|
|
|
// POSIX signals:
|
2019-10-24 19:19:03 +03:00
|
|
|
match si {
|
|
|
|
1 {return 'SIGHUP'}
|
|
|
|
2 {return 'SIGINT'}
|
|
|
|
3 {return 'SIGQUIT'}
|
|
|
|
4 {return 'SIGILL'}
|
|
|
|
6 {return 'SIGABRT'}
|
|
|
|
8 {return 'SIGFPE'}
|
|
|
|
9 {return 'SIGKILL'}
|
|
|
|
11 {return 'SIGSEGV'}
|
|
|
|
13 {return 'SIGPIPE'}
|
|
|
|
14 {return 'SIGALRM'}
|
|
|
|
15 {return 'SIGTERM'}
|
2019-10-08 02:34:55 +03:00
|
|
|
}
|
|
|
|
$if linux {
|
|
|
|
// From `man 7 signal` on linux:
|
2019-10-24 19:19:03 +03:00
|
|
|
match si {
|
|
|
|
30,10,16{ return 'SIGUSR1'}
|
|
|
|
31,12,17{ return 'SIGUSR2'}
|
|
|
|
20,17,18{ return 'SIGCHLD'}
|
|
|
|
19,18,25{ return 'SIGCONT'}
|
|
|
|
17,19,23{ return 'SIGSTOP'}
|
|
|
|
18,20,24{ return 'SIGTSTP'}
|
|
|
|
21,21,26{ return 'SIGTTIN'}
|
|
|
|
22,22,27{ return 'SIGTTOU'}
|
|
|
|
///////////////////////////////
|
|
|
|
5{ return 'SIGTRAP'}
|
|
|
|
7{ return 'SIGBUS' }
|
2019-10-08 02:34:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 'unknown'
|
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
// `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 {
|
2019-09-15 15:57:17 +03:00
|
|
|
s := *byte(C.getenv(key.str))
|
2019-07-24 13:16:45 +03:00
|
|
|
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
|
2019-08-22 23:19:31 +03:00
|
|
|
}
|
|
|
|
$else {
|
2019-07-22 17:51:33 +03:00
|
|
|
return C.setenv(name.str, value.str, overwrite)
|
2019-08-22 23:19:31 +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-08-22 23:19:31 +03:00
|
|
|
}
|
|
|
|
$else {
|
2019-07-22 17:51:33 +03:00
|
|
|
return C.unsetenv(name.str)
|
2019-08-22 23:19:31 +03:00
|
|
|
}
|
2019-06-27 14:51:03 +03:00
|
|
|
}
|
|
|
|
|
2019-08-17 16:17:43 +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-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-10-08 02:34:55 +03:00
|
|
|
//C.printf('errno=%d err="%s"\n', C.errno, C.strerror(C.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 ''
|
|
|
|
}
|
2019-10-27 10:03:15 +03:00
|
|
|
return path[pos..]
|
2019-06-26 12:28:06 +03:00
|
|
|
}
|
|
|
|
|
2019-07-16 02:57:03 +03:00
|
|
|
|
2019-08-22 23:19:31 +03:00
|
|
|
// dir returns all but the last element of path, typically the path's directory.
|
2019-07-16 02:57:03 +03:00
|
|
|
pub fn dir(path string) string {
|
2019-08-01 02:34:28 +03:00
|
|
|
if path == '.' {
|
2019-08-22 23:19:31 +03:00
|
|
|
return getwd()
|
|
|
|
}
|
2019-10-12 22:31:05 +03:00
|
|
|
pos := path.last_index(path_separator)
|
2019-07-16 02:57:03 +03:00
|
|
|
if pos == -1 {
|
2019-08-22 23:19:31 +03:00
|
|
|
return '.'
|
|
|
|
}
|
2019-10-27 10:03:15 +03:00
|
|
|
return path[..pos]
|
2019-08-22 23:19:31 +03:00
|
|
|
}
|
2019-07-16 02:57:03 +03:00
|
|
|
|
2019-06-26 13:56:05 +03:00
|
|
|
fn path_sans_ext(path string) string {
|
|
|
|
pos := path.last_index('.')
|
|
|
|
if pos == -1 {
|
|
|
|
return path
|
|
|
|
}
|
2019-10-27 10:03:15 +03:00
|
|
|
return path[..pos]
|
2019-06-26 13:56:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
pub fn basedir(path string) string {
|
2019-10-12 22:31:05 +03:00
|
|
|
pos := path.last_index(path_separator)
|
2019-06-22 21:20:28 +03:00
|
|
|
if pos == -1 {
|
|
|
|
return path
|
|
|
|
}
|
2019-11-17 06:45:20 +03:00
|
|
|
return path[..pos ] // NB: *without* terminating /
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn filename(path string) string {
|
2019-10-12 22:31:05 +03:00
|
|
|
return path.all_after(path_separator)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-08-22 23:19:31 +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-09-15 19:07:40 +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-10-08 12:29:09 +03:00
|
|
|
$if windows {
|
2019-09-29 04:54:12 +03:00
|
|
|
max_line_chars := 256
|
|
|
|
buf := &byte(malloc(max_line_chars*2))
|
2019-11-06 23:04:40 +03:00
|
|
|
if is_atty(0) > 0 {
|
2019-10-08 12:29:09 +03:00
|
|
|
h_input := C.GetStdHandle(STD_INPUT_HANDLE)
|
2019-11-16 02:30:50 +03:00
|
|
|
mut nr_chars := u32(0)
|
|
|
|
C.ReadConsole(h_input, buf, max_line_chars * 2, voidptr(&nr_chars), 0)
|
|
|
|
return string_from_wide2(&u16(buf), int(nr_chars))
|
2019-10-08 12:29:09 +03:00
|
|
|
}
|
2019-11-16 02:30:50 +03:00
|
|
|
res := C.fgetws(&u16(buf), max_line_chars, C.stdin )
|
2019-09-20 17:03:13 +03:00
|
|
|
len := int( C.wcslen(&u16(buf)) )
|
2019-11-16 02:30:50 +03:00
|
|
|
if !isnil(res) { return string_from_wide2( &u16(buf), len ) }
|
2019-08-26 13:51:48 +03:00
|
|
|
return ''
|
2019-10-08 12:29:09 +03:00
|
|
|
} $else {
|
|
|
|
max := size_t(256)
|
|
|
|
buf := *char(malloc(int(max)))
|
|
|
|
nr_chars := C.getline(&buf, &max, stdin)
|
|
|
|
if nr_chars == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
return string(byteptr(buf), nr_chars)
|
2019-08-26 13:51:48 +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 {
|
2019-10-26 20:21:07 +03:00
|
|
|
mut line := ''
|
|
|
|
mut inputstr := ''
|
|
|
|
for {
|
|
|
|
line = get_line()
|
|
|
|
if line.len <= 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
line = line.trim_space()
|
|
|
|
inputstr += line
|
|
|
|
}
|
|
|
|
return inputstr
|
2019-07-24 19:14:13 +03:00
|
|
|
}
|
|
|
|
|
2019-10-26 20:21:07 +03:00
|
|
|
// user_os returns current user operating system name.
|
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 {
|
2019-08-22 23:19:31 +03:00
|
|
|
return 'freebsd'
|
|
|
|
}
|
2019-07-21 13:22:41 +03:00
|
|
|
$if openbsd {
|
2019-08-22 23:19:31 +03:00
|
|
|
return 'openbsd'
|
|
|
|
}
|
2019-07-21 13:22:41 +03:00
|
|
|
$if netbsd {
|
2019-08-22 23:19:31 +03:00
|
|
|
return 'netbsd'
|
|
|
|
}
|
2019-07-21 13:22:41 +03:00
|
|
|
$if dragonfly {
|
2019-08-22 23:19:31 +03:00
|
|
|
return 'dragonfly'
|
2019-07-24 00:23:13 +03:00
|
|
|
}
|
2019-09-16 18:23:11 +03:00
|
|
|
$if android{
|
|
|
|
return 'android'
|
|
|
|
}
|
2019-09-27 00:30:41 +03:00
|
|
|
$if solaris {
|
|
|
|
return 'solaris'
|
|
|
|
}
|
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
|
|
|
}
|
2019-10-12 22:31:05 +03:00
|
|
|
home += path_separator
|
2019-06-22 21:20:28 +03:00
|
|
|
return home
|
|
|
|
}
|
|
|
|
|
2019-08-22 23:19:31 +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 {
|
2019-08-22 23:19:31 +03:00
|
|
|
return
|
|
|
|
}
|
2019-07-02 00:24:19 +03:00
|
|
|
f.write(text)
|
2019-06-22 21:20:28 +03:00
|
|
|
f.close()
|
|
|
|
}
|
|
|
|
|
2019-10-26 20:21:07 +03:00
|
|
|
// clear will clear current terminal screen.
|
2019-06-26 19:56:33 +03:00
|
|
|
pub fn clear() {
|
2019-10-21 04:15:43 +03:00
|
|
|
$if !windows {
|
|
|
|
C.printf('\x1b[2J')
|
|
|
|
C.printf('\x1b[H')
|
|
|
|
}
|
2019-06-26 19:56:33 +03:00
|
|
|
}
|
|
|
|
|
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
|
2019-08-22 23:19:31 +03:00
|
|
|
sa.sa_flags = C.SA_SIGINFO
|
|
|
|
C.sigaction(C.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-09-15 19:07:40 +03:00
|
|
|
fn C.getpid() int
|
|
|
|
fn C.proc_pidpath (int, byteptr, int) int
|
2019-11-22 19:00:56 +03:00
|
|
|
fn C.readlink() int
|
2019-09-15 19:07:40 +03:00
|
|
|
|
2019-11-22 19:00:56 +03:00
|
|
|
|
|
|
|
// executable returns the path name of the executable that started the current
|
|
|
|
// process.
|
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-11-22 19:00:56 +03:00
|
|
|
count := 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-08-22 23:19:31 +03:00
|
|
|
pid := C.getpid()
|
2019-10-26 01:55:16 +03:00
|
|
|
ret := proc_pidpath (pid, result, MAX_PATH)
|
2019-07-16 00:22:29 +03:00
|
|
|
if ret <= 0 {
|
2019-08-22 23:19:31 +03:00
|
|
|
println('os.executable() failed')
|
|
|
|
return '.'
|
|
|
|
}
|
|
|
|
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-22 23:19:31 +03:00
|
|
|
mib := [1 /* CTL_KERN */, 14 /* KERN_PROC */, 12 /* KERN_PROC_PATHNAME */, -1]
|
|
|
|
size := MAX_PATH
|
|
|
|
C.sysctl(mib.data, 4, result, &size, 0, 0)
|
|
|
|
return string(result)
|
|
|
|
}
|
2019-07-16 17:37:59 +03:00
|
|
|
$if openbsd {
|
2019-08-22 23:19:31 +03:00
|
|
|
// "Sadly there is no way to get the full path of the executed file in OpenBSD."
|
|
|
|
// lol
|
|
|
|
return os.args[0]
|
|
|
|
}
|
2019-09-27 00:30:41 +03:00
|
|
|
$if solaris {
|
|
|
|
}
|
2019-07-16 17:37:59 +03:00
|
|
|
$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-08-22 23:19:31 +03:00
|
|
|
}
|
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-08-22 23:19:31 +03:00
|
|
|
}
|
2019-09-27 00:23:27 +03:00
|
|
|
return os.args[0]
|
2019-06-27 23:17:13 +03:00
|
|
|
}
|
2019-06-30 15:55:48 +03:00
|
|
|
|
2019-10-26 20:21:07 +03:00
|
|
|
// is_dir returns a boolean indicating whether the given path is a directory.
|
2019-06-30 15:55:48 +03:00
|
|
|
pub fn is_dir(path string) bool {
|
|
|
|
$if windows {
|
2019-08-22 23:19:31 +03:00
|
|
|
return dir_exists(path)
|
2019-07-21 18:14:25 +03:00
|
|
|
//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-08-22 23:19:31 +03:00
|
|
|
}
|
|
|
|
$else {
|
2019-06-30 15:55:48 +03:00
|
|
|
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
|
2019-11-08 03:02:02 +03:00
|
|
|
return (int(statbuf.st_mode) & S_IFMT) == S_IFDIR
|
2019-08-22 23:19:31 +03:00
|
|
|
}
|
2019-06-30 15:55:48 +03:00
|
|
|
}
|
|
|
|
|
2019-10-26 20:21:07 +03:00
|
|
|
// chdir changes the current working directory to the new directory path.
|
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
|
|
|
}
|
2019-08-22 23:19:31 +03:00
|
|
|
$else {
|
2019-07-22 17:51:33 +03:00
|
|
|
C.chdir(path.str)
|
2019-08-22 23:19:31 +03:00
|
|
|
}
|
2019-06-30 15:55:48 +03:00
|
|
|
}
|
|
|
|
|
2019-10-26 20:21:07 +03:00
|
|
|
// getwd returns the absolute path name of the current directory.
|
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)
|
2019-11-15 16:14:28 +03:00
|
|
|
buf := &u16(calloc(max*2))
|
2019-07-24 19:25:18 +03:00
|
|
|
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 {
|
2019-11-15 16:14:28 +03:00
|
|
|
buf := calloc(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-08-22 14:15:11 +03:00
|
|
|
// Returns the full absolute path for fpath, with all relative ../../, symlinks and so on resolved.
|
|
|
|
// See http://pubs.opengroup.org/onlinepubs/9699919799/functions/realpath.html
|
|
|
|
// Also https://insanecoding.blogspot.com/2007/11/pathmax-simply-isnt.html
|
|
|
|
// and https://insanecoding.blogspot.com/2007/11/implementing-realpath-in-c.html
|
|
|
|
// NB: this particular rabbit hole is *deep* ...
|
|
|
|
pub fn realpath(fpath string) string {
|
2019-11-15 16:14:28 +03:00
|
|
|
mut fullpath := calloc( MAX_PATH )
|
2019-08-22 14:15:11 +03:00
|
|
|
mut res := 0
|
|
|
|
$if windows {
|
2019-11-23 21:05:49 +03:00
|
|
|
ret := C._fullpath(fullpath, fpath.str, MAX_PATH)
|
2019-11-23 20:56:22 +03:00
|
|
|
if ret == 0 {
|
|
|
|
return fpath
|
|
|
|
}
|
|
|
|
return string(fullpath)
|
2019-08-22 14:15:11 +03:00
|
|
|
}
|
|
|
|
$else{
|
2019-11-15 01:07:38 +03:00
|
|
|
ret := C.realpath(fpath.str, fullpath)
|
|
|
|
if ret == 0 {
|
|
|
|
return fpath
|
|
|
|
}
|
|
|
|
return string(fullpath)
|
2019-08-22 14:15:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-22 23:19:31 +03:00
|
|
|
// walk_ext returns a recursive list of all file paths ending with `ext`.
|
2019-08-16 15:05:11 +03:00
|
|
|
pub fn walk_ext(path, ext string) []string {
|
2019-08-22 23:19:31 +03:00
|
|
|
if !os.is_dir(path) {
|
2019-11-14 09:18:07 +03:00
|
|
|
return []
|
2019-08-22 23:19:31 +03:00
|
|
|
}
|
2019-10-17 14:30:05 +03:00
|
|
|
mut files := os.ls(path) or { panic(err) }
|
2019-08-22 23:19:31 +03:00
|
|
|
mut res := []string
|
2019-11-09 19:35:26 +03:00
|
|
|
separator := if path.ends_with(path_separator) { '' } else { path_separator}
|
2019-08-16 15:05:11 +03:00
|
|
|
for i, file in files {
|
|
|
|
if file.starts_with('.') {
|
2019-08-22 23:19:31 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-11-09 19:35:26 +03:00
|
|
|
p := path + separator + file
|
2019-08-22 23:19:31 +03:00
|
|
|
if os.is_dir(p) {
|
|
|
|
res << walk_ext(p, ext)
|
|
|
|
}
|
2019-08-16 15:05:11 +03:00
|
|
|
else if file.ends_with(ext) {
|
2019-08-22 23:19:31 +03:00
|
|
|
res << p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
2019-06-30 15:55:48 +03:00
|
|
|
|
2019-10-24 15:17:09 +03:00
|
|
|
// walk recursively traverse the given directory path.
|
|
|
|
// When a file is encountred it will call the callback function with current file as argument.
|
|
|
|
pub fn walk(path string, fnc fn(path string)) {
|
|
|
|
if !os.is_dir(path) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mut files := os.ls(path) or { panic(err) }
|
|
|
|
for file in files {
|
|
|
|
p := path + os.path_separator + file
|
|
|
|
if os.is_dir(p) {
|
|
|
|
walk(p, fnc)
|
|
|
|
}
|
|
|
|
else if os.file_exists(p) {
|
|
|
|
fnc(p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-02 23:13:23 +03:00
|
|
|
pub fn signal(signum int, handler voidptr) {
|
|
|
|
C.signal(signum, handler)
|
|
|
|
}
|
|
|
|
|
2019-09-15 19:07:40 +03:00
|
|
|
|
|
|
|
fn C.fork() int
|
|
|
|
fn C.wait() int
|
|
|
|
|
2019-07-14 05:18:54 +03:00
|
|
|
pub fn fork() int {
|
2019-11-16 02:30:50 +03:00
|
|
|
mut pid := -1
|
2019-07-14 13:36:25 +03:00
|
|
|
$if !windows {
|
2019-11-16 02:30:50 +03:00
|
|
|
pid = C.fork()
|
2019-07-14 13:36:25 +03:00
|
|
|
}
|
2019-11-16 02:30:50 +03:00
|
|
|
$if windows {
|
|
|
|
panic('os.fork not supported in windows') // TODO
|
|
|
|
}
|
|
|
|
return pid
|
2019-07-14 05:18:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn wait() int {
|
2019-11-16 02:30:50 +03:00
|
|
|
mut pid := -1
|
|
|
|
$if !windows {
|
|
|
|
pid = C.wait(0)
|
|
|
|
}
|
2019-07-14 13:36:25 +03:00
|
|
|
$if !windows {
|
2019-11-16 02:30:50 +03:00
|
|
|
panic('os.wait not supported in windows') // TODO
|
2019-07-14 13:36:25 +03:00
|
|
|
}
|
2019-11-16 02:30:50 +03:00
|
|
|
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 {
|
2019-08-22 23:19:31 +03:00
|
|
|
attr := C.stat{}
|
2019-07-07 22:46:21 +03:00
|
|
|
//# struct stat attr;
|
2019-08-22 23:19:31 +03:00
|
|
|
C.stat(path.str, &attr)
|
2019-07-07 22:46:21 +03:00
|
|
|
//# stat(path.str, &attr);
|
2019-08-22 23:19:31 +03:00
|
|
|
return attr.st_mtime
|
2019-07-07 22:46:21 +03:00
|
|
|
//# return attr.st_mtime ;
|
|
|
|
}
|
2019-08-22 23:19:31 +03:00
|
|
|
|
2019-07-07 22:46:21 +03:00
|
|
|
|
2019-09-21 21:38:12 +03:00
|
|
|
pub fn log(s string) {
|
|
|
|
println('os.log: ' + s)
|
2019-06-30 15:55:48 +03:00
|
|
|
}
|
|
|
|
|
2019-07-24 17:15:21 +03:00
|
|
|
pub fn flush_stdout() {
|
|
|
|
C.fflush(stdout)
|
2019-08-22 23:19:31 +03:00
|
|
|
}
|
2019-07-24 17:15:21 +03:00
|
|
|
|
2019-06-30 17:11:55 +03:00
|
|
|
pub fn print_backtrace() {
|
2019-08-22 23:19:31 +03:00
|
|
|
/*
|
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) ;
|
2019-08-22 23:19:31 +03:00
|
|
|
*/
|
2019-06-30 15:55:48 +03:00
|
|
|
}
|
2019-10-10 20:08:36 +03:00
|
|
|
|
2019-10-12 05:04:56 +03:00
|
|
|
pub fn mkdir_all(path string) {
|
2019-10-12 22:31:05 +03:00
|
|
|
mut p := if path.starts_with(os.path_separator) { os.path_separator } else { '' }
|
|
|
|
for subdir in path.split(os.path_separator) {
|
|
|
|
p += subdir + os.path_separator
|
2019-10-12 05:04:56 +03:00
|
|
|
if !os.dir_exists(p) {
|
2019-11-23 19:55:18 +03:00
|
|
|
os.mkdir(p) or { panic(err) }
|
2019-10-12 05:04:56 +03:00
|
|
|
}
|
|
|
|
}
|
2019-10-12 08:41:41 +03:00
|
|
|
}
|
2019-10-15 18:08:46 +03:00
|
|
|
|
|
|
|
pub fn join(base string, dirs ...string) string {
|
2019-11-01 19:29:51 +03:00
|
|
|
println('use filepath.join')
|
|
|
|
return filepath.join(base, dirs)
|
2019-10-15 18:08:46 +03:00
|
|
|
}
|
2019-11-17 06:45:20 +03:00
|
|
|
|
|
|
|
// tmpdir returns the path to a folder, that is suitable for storing temporary files
|
|
|
|
pub fn tmpdir() string {
|
|
|
|
mut path := os.getenv('TMPDIR')
|
|
|
|
$if linux {
|
|
|
|
if path == '' { path = '/tmp' }
|
|
|
|
}
|
|
|
|
$if mac {
|
|
|
|
/*
|
|
|
|
if path == '' {
|
|
|
|
// TODO untested
|
2019-11-19 02:25:55 +03:00
|
|
|
path = C.NSTemporaryDirectory()
|
2019-11-17 06:45:20 +03:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
if path == '' { path = '/tmp' }
|
|
|
|
}
|
|
|
|
$if windows {
|
|
|
|
if path == '' {
|
|
|
|
// TODO see Qt's implementation?
|
|
|
|
// https://doc.qt.io/qt-5/qdir.html#tempPath
|
|
|
|
// https://github.com/qt/qtbase/blob/e164d61ca8263fc4b46fdd916e1ea77c7dd2b735/src/corelib/io/qfilesystemengine_win.cpp#L1275
|
|
|
|
path = os.getenv('TEMP')
|
|
|
|
if path == '' { path = os.getenv('TMP') }
|
|
|
|
if path == '' { path = 'C:/tmp' }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
2019-11-24 06:27:02 +03:00
|
|
|
|
|
|
|
|
|
|
|
pub fn chmod(path string, mode int) {
|
|
|
|
C.chmod(path.str, mode)
|
|
|
|
}
|