2020-01-23 21:04:46 +01:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-06-23 04:21:30 +02:00
|
|
|
// 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-11-01 17:29:51 +01:00
|
|
|
import filepath
|
2019-10-15 17:08:46 +02:00
|
|
|
|
2019-12-19 22:29:37 +01:00
|
|
|
#include <sys/stat.h> // #include <signal.h>
|
2019-06-30 14:55:48 +02:00
|
|
|
#include <errno.h>
|
2020-01-21 16:58:47 +01:00
|
|
|
|
2019-08-22 22:19:31 +02:00
|
|
|
/*
|
2019-07-01 11:01:48 +02:00
|
|
|
struct dirent {
|
2019-10-26 00:55:16 +02:00
|
|
|
d_ino int
|
|
|
|
d_off int
|
2019-08-22 22:19:31 +02:00
|
|
|
d_reclen u16
|
|
|
|
d_type byte
|
|
|
|
d_name [256]byte
|
|
|
|
}
|
|
|
|
*/
|
2019-07-01 11:01:48 +02:00
|
|
|
|
2019-08-17 01:09:36 +02:00
|
|
|
struct C.dirent {
|
2019-08-22 22:19:31 +02:00
|
|
|
d_name byteptr
|
|
|
|
}
|
2019-08-17 01:09:36 +02:00
|
|
|
|
2019-08-22 22:19:31 +02:00
|
|
|
fn C.readdir(voidptr) C.dirent
|
2019-08-17 01:09:36 +02:00
|
|
|
|
2019-12-30 05:23:54 +01:00
|
|
|
|
2019-10-24 11:36:57 +02:00
|
|
|
pub const (
|
2019-06-22 20:20:28 +02:00
|
|
|
args = []string
|
2019-06-27 22:17:13 +02:00
|
|
|
MAX_PATH = 4096
|
2019-06-22 20:20:28 +02:00
|
|
|
)
|
|
|
|
|
2019-10-24 11:47:21 +02:00
|
|
|
pub struct File {
|
2019-12-19 22:29:37 +01:00
|
|
|
cfile voidptr // Using void* instead of FILE*
|
2020-01-21 16:58:47 +01:00
|
|
|
pub:
|
2019-12-31 08:53:53 +01:00
|
|
|
fd int
|
2019-12-03 11:08:57 +01:00
|
|
|
mut:
|
2019-12-01 10:50:13 +01:00
|
|
|
opened bool
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-06-26 16:11:25 +02:00
|
|
|
struct FileInfo {
|
|
|
|
name string
|
|
|
|
size int
|
|
|
|
}
|
|
|
|
|
2019-06-27 17:48:49 +02:00
|
|
|
struct C.stat {
|
2019-12-19 22:29:37 +01:00
|
|
|
st_size int
|
|
|
|
st_mode u32
|
2019-08-22 22:19:31 +02:00
|
|
|
st_mtime int
|
2019-06-27 19:02:47 +02:00
|
|
|
}
|
|
|
|
|
2019-12-27 17:59:04 +01:00
|
|
|
struct C.DIR {}
|
2019-06-27 19:02:47 +02:00
|
|
|
|
2019-12-19 22:29:37 +01:00
|
|
|
// struct C.dirent {
|
|
|
|
// d_name byteptr
|
|
|
|
// }
|
2019-06-27 17:48:49 +02:00
|
|
|
struct C.sigaction {
|
2019-06-27 19:02:47 +02:00
|
|
|
mut:
|
2019-12-19 22:29:37 +01:00
|
|
|
sa_mask int
|
2019-06-27 19:02:47 +02:00
|
|
|
sa_sigaction int
|
2019-12-19 22:29:37 +01:00
|
|
|
sa_flags int
|
2019-06-27 17:48:49 +02:00
|
|
|
}
|
|
|
|
|
2019-06-26 12:56:49 +02:00
|
|
|
fn C.getline(voidptr, voidptr, voidptr) int
|
2019-12-19 22:29:37 +01:00
|
|
|
|
|
|
|
|
2019-06-26 12:56:49 +02:00
|
|
|
fn C.ftell(fp voidptr) int
|
2019-12-19 22:29:37 +01:00
|
|
|
|
|
|
|
|
2019-11-28 09:46:52 +01:00
|
|
|
fn C.getenv(byteptr) &char
|
2019-12-19 22:29:37 +01:00
|
|
|
|
|
|
|
|
2019-06-27 17:48:49 +02:00
|
|
|
fn C.sigaction(int, voidptr, int)
|
2019-06-26 12:56:49 +02:00
|
|
|
|
2019-11-25 02:35:41 +01:00
|
|
|
|
2020-01-21 16:58:47 +01:00
|
|
|
fn C.open(charptr, int, int) int
|
|
|
|
|
|
|
|
|
|
|
|
fn C.fdopen(int, string) voidptr
|
|
|
|
|
|
|
|
|
2019-12-01 10:50:13 +01:00
|
|
|
pub fn (f File) is_opened() bool {
|
|
|
|
return f.opened
|
|
|
|
}
|
2019-11-02 20:37:29 +01:00
|
|
|
|
2019-10-20 19:45:16 +02:00
|
|
|
// read_bytes reads an amount of bytes from the beginning of the file
|
2020-01-09 12:00:39 +01:00
|
|
|
pub fn (f &File) read_bytes(size int) []byte {
|
2019-10-26 19:21:07 +02:00
|
|
|
return f.read_bytes_at(size, 0)
|
2019-10-20 19:45:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// read_bytes_at reads an amount of bytes at the given position in the file
|
2020-01-09 12:00:39 +01:00
|
|
|
pub fn (f &File) read_bytes_at(size, pos int) []byte {
|
2019-12-19 22:29:37 +01:00
|
|
|
mut arr := [`0`].repeat(size)
|
2019-10-26 19:21:07 +02:00
|
|
|
C.fseek(f.cfile, pos, C.SEEK_SET)
|
2019-11-28 09:46:52 +01:00
|
|
|
nreadbytes := C.fread(arr.data, 1, size, f.cfile)
|
2019-10-26 19:21:07 +02:00
|
|
|
C.fseek(f.cfile, 0, C.SEEK_SET)
|
2019-11-30 10:37:34 +01:00
|
|
|
return arr[0..nreadbytes]
|
2019-10-20 19:45:16 +02:00
|
|
|
}
|
|
|
|
|
2020-01-03 19:46:23 +01:00
|
|
|
|
2019-11-19 07:53:52 +01: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)
|
2019-12-19 22:29:37 +01:00
|
|
|
mut res := [`0`].repeat(fsize)
|
2019-12-18 06:22:52 +01:00
|
|
|
nr_read_elements := C.fread(res.data, fsize, 1, fp)
|
2019-11-19 07:53:52 +01:00
|
|
|
C.fclose(fp)
|
2019-12-18 06:22:52 +01:00
|
|
|
return res[0..nr_read_elements * fsize]
|
2019-11-19 07:53:52 +01:00
|
|
|
}
|
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
// read_file reads the file in `path` and returns the contents.
|
2019-08-22 22:19:31 +02:00
|
|
|
pub fn read_file(path string) ?string {
|
2019-07-24 12:16:45 +02:00
|
|
|
mode := 'rb'
|
2019-09-15 14:57:17 +02:00
|
|
|
mut fp := vfopen(path, mode)
|
2019-07-03 21:07:42 +02:00
|
|
|
if isnil(fp) {
|
|
|
|
return error('failed to open file "$path"')
|
|
|
|
}
|
2020-02-05 11:23:49 +01:00
|
|
|
defer { C.fclose(fp) }
|
2019-08-22 22:19:31 +02:00
|
|
|
C.fseek(fp, 0, C.SEEK_END)
|
2019-07-03 21:07:42 +02:00
|
|
|
fsize := C.ftell(fp)
|
2019-07-21 12:22:41 +02:00
|
|
|
// C.fseek(fp, 0, SEEK_SET) // same as `C.rewind(fp)` below
|
2019-07-03 21:07:42 +02:00
|
|
|
C.rewind(fp)
|
2020-02-05 11:23:49 +01:00
|
|
|
mut str := &byte(0)
|
|
|
|
unsafe { str = malloc(fsize + 1) }
|
2019-07-03 21:07:42 +02:00
|
|
|
C.fread(str, fsize, 1, fp)
|
|
|
|
str[fsize] = 0
|
2019-12-19 22:29:37 +01:00
|
|
|
return string(str,fsize)
|
2019-07-03 21:07:42 +02:00
|
|
|
}
|
|
|
|
|
2019-06-26 16:11:25 +02:00
|
|
|
// file_size returns the size of the file located in `path`.
|
2019-06-22 20:20:28 +02:00
|
|
|
pub fn file_size(path string) int {
|
2019-12-27 17:59:04 +01:00
|
|
|
mut s := C.stat{}
|
2019-07-24 12:16:45 +02:00
|
|
|
$if windows {
|
2019-11-16 00:30:50 +01:00
|
|
|
C._wstat(path.to_wide(), voidptr(&s))
|
2019-07-24 12:16:45 +02:00
|
|
|
} $else {
|
2019-12-01 08:33:26 +01:00
|
|
|
C.stat(charptr(path.str), &s)
|
2019-07-24 12:16:45 +02:00
|
|
|
}
|
2019-06-27 17:48:49 +02:00
|
|
|
return s.st_size
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-06-25 20:41:46 +02:00
|
|
|
pub fn mv(old, new string) {
|
2019-07-24 12:16:45 +02:00
|
|
|
$if windows {
|
|
|
|
C._wrename(old.to_wide(), new.to_wide())
|
|
|
|
} $else {
|
2019-12-01 08:33:26 +01:00
|
|
|
C.rename(charptr(old.str), charptr(new.str))
|
2019-07-24 12:16:45 +02:00
|
|
|
}
|
2019-06-26 16:39:40 +02:00
|
|
|
}
|
2019-06-25 20:41:46 +02:00
|
|
|
|
2019-11-02 20:37:29 +01:00
|
|
|
fn C.CopyFile(&u32, &u32, int) int
|
|
|
|
// TODO implement actual cp for linux
|
|
|
|
pub fn cp(old, new string) ?bool {
|
2019-10-31 22:57:16 +01:00
|
|
|
$if windows {
|
2019-11-02 20:37:29 +01:00
|
|
|
_old := old.replace('/', '\\')
|
|
|
|
_new := new.replace('/', '\\')
|
|
|
|
C.CopyFile(_old.to_wide(), _new.to_wide(), false)
|
|
|
|
result := C.GetLastError()
|
|
|
|
if result == 0 {
|
|
|
|
return true
|
2019-12-19 22:29:37 +01:00
|
|
|
}
|
|
|
|
else {
|
2019-11-02 20:37:29 +01:00
|
|
|
return error_with_code('failed to copy $old to $new', int(result))
|
|
|
|
}
|
|
|
|
} $else {
|
2019-10-31 22:57:16 +01:00
|
|
|
os.system('cp $old $new')
|
2019-11-02 20:37:29 +01:00
|
|
|
return true // TODO make it return true or error when cp for linux is implemented
|
|
|
|
}
|
2019-10-31 22:57:16 +01:00
|
|
|
}
|
|
|
|
|
2019-12-19 22:29:37 +01: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-12-04 21:03:12 +01:00
|
|
|
if !os.exists(source_path) {
|
2019-12-19 22:29:37 +01:00
|
|
|
return error("Source path doesn\'t exist")
|
2019-11-06 21:05:35 +01:00
|
|
|
}
|
2019-12-19 22:29:37 +01:00
|
|
|
// single file copy
|
2019-11-06 21:05:35 +01:00
|
|
|
if !os.is_dir(source_path) {
|
2019-12-23 11:09:22 +01:00
|
|
|
adjasted_path := if os.is_dir(dest_path) { filepath.join(dest_path,filepath.filename(source_path)) } else { dest_path }
|
2019-12-04 21:03:12 +01:00
|
|
|
if os.exists(adjasted_path) {
|
2019-11-17 04:45:20 +01:00
|
|
|
if overwrite {
|
2019-11-19 00:25:55 +01:00
|
|
|
os.rm(adjasted_path)
|
2019-11-17 04:45:20 +01:00
|
|
|
}
|
2019-11-19 00:25:55 +01:00
|
|
|
else {
|
|
|
|
return error('Destination file path already exist')
|
2019-11-17 04:45:20 +01:00
|
|
|
}
|
2019-11-06 21:05:35 +01:00
|
|
|
}
|
2019-12-21 23:41:42 +01:00
|
|
|
os.cp(source_path, adjasted_path) or {
|
2019-12-19 22:29:37 +01:00
|
|
|
return error(err)
|
|
|
|
}
|
2019-11-06 21:05:35 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
if !os.is_dir(dest_path) {
|
|
|
|
return error('Destination path is not a valid directory')
|
|
|
|
}
|
2019-12-21 23:41:42 +01:00
|
|
|
files := os.ls(source_path) or {
|
2019-12-19 22:29:37 +01:00
|
|
|
return error(err)
|
|
|
|
}
|
2019-11-06 21:05:35 +01:00
|
|
|
for file in files {
|
2019-12-19 22:29:37 +01:00
|
|
|
sp := filepath.join(source_path,file)
|
|
|
|
dp := filepath.join(dest_path,file)
|
2019-11-06 21:05:35 +01:00
|
|
|
if os.is_dir(sp) {
|
2019-12-21 23:41:42 +01:00
|
|
|
os.mkdir(dp) or {
|
2019-12-19 22:29:37 +01:00
|
|
|
panic(err)
|
|
|
|
}
|
2019-11-06 21:05:35 +01:00
|
|
|
}
|
2019-12-21 23:41:42 +01:00
|
|
|
cp_r(sp, dp, overwrite) or {
|
2019-11-06 21:05:35 +01:00
|
|
|
os.rmdir(dp)
|
2019-11-09 20:05:44 +01:00
|
|
|
panic(err)
|
2019-11-06 21:05:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-11-19 10:55:03 +01: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 {
|
2019-12-21 23:41:42 +01:00
|
|
|
os.cp(source, target) or {
|
2019-12-19 22:29:37 +01:00
|
|
|
return error(err)
|
|
|
|
}
|
2019-11-19 10:55:03 +01:00
|
|
|
os.rm(source)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-02-11 10:26:46 +01:00
|
|
|
fn vfopen(path, mode string) &C.FILE {
|
2019-09-15 14:57:17 +02:00
|
|
|
$if windows {
|
|
|
|
return C._wfopen(path.to_wide(), mode.to_wide())
|
|
|
|
} $else {
|
2019-12-01 08:33:26 +01:00
|
|
|
return C.fopen(charptr(path.str), charptr(mode.str))
|
2019-09-15 14:57:17 +02:00
|
|
|
}
|
2019-11-29 17:14:26 +01:00
|
|
|
}
|
2019-09-15 14:57:17 +02:00
|
|
|
|
2019-06-23 02:28:29 +02:00
|
|
|
// read_lines reads the file in `path` into an array of lines.
|
2019-11-19 00:25:55 +01:00
|
|
|
pub fn read_lines(path string) ?[]string {
|
2020-01-31 15:59:23 +01:00
|
|
|
buf := read_file(path) or {
|
|
|
|
return err
|
2019-06-23 02:28:29 +02:00
|
|
|
}
|
2020-01-31 15:59:23 +01:00
|
|
|
return buf.split_into_lines()
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-11-19 00:25:55 +01:00
|
|
|
fn read_ulines(path string) ?[]ustring {
|
2019-12-21 23:41:42 +01:00
|
|
|
lines := read_lines(path) or {
|
2019-11-19 00:25:55 +01:00
|
|
|
return err
|
2019-11-29 17:14:26 +01:00
|
|
|
}
|
2019-06-22 20:20:28 +02: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 21:07:42 +02:00
|
|
|
pub fn open_append(path string) ?File {
|
2019-12-27 17:59:04 +01:00
|
|
|
mut file := File{}
|
2019-07-24 12:16:45 +02:00
|
|
|
$if windows {
|
|
|
|
wpath := path.replace('/', '\\').to_wide()
|
|
|
|
mode := 'ab'
|
2019-12-19 22:29:37 +01:00
|
|
|
file = File{
|
2019-07-24 12:16:45 +02:00
|
|
|
cfile: C._wfopen(wpath, mode.to_wide())
|
|
|
|
}
|
|
|
|
} $else {
|
2019-08-22 22:19:31 +02:00
|
|
|
cpath := path.str
|
2019-12-19 22:29:37 +01:00
|
|
|
file = File{
|
2019-12-01 08:33:26 +01:00
|
|
|
cfile: C.fopen(charptr(cpath), 'ab')
|
2019-07-24 12:16:45 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-03 21:07:42 +02:00
|
|
|
if isnil(file.cfile) {
|
2019-07-24 12:16:45 +02:00
|
|
|
return error('failed to create(append) file "$path"')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-12-01 10:50:13 +01:00
|
|
|
file.opened = true
|
2019-08-22 22:19:31 +02:00
|
|
|
return file
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-01-21 16:58:47 +01:00
|
|
|
// open_file can be used to open or create a file with custom flags and permissions and returns a `File` object
|
|
|
|
pub fn open_file(path string, mode string, options ...int) ?File {
|
|
|
|
mut flags := 0
|
|
|
|
for m in mode {
|
|
|
|
match m {
|
|
|
|
`r` { flags |= O_RDONLY }
|
|
|
|
`w` { flags |= O_CREATE | O_TRUNC }
|
|
|
|
`a` { flags |= O_CREATE | O_APPEND }
|
|
|
|
`s` { flags |= O_SYNC }
|
|
|
|
`n` { flags |= O_NONBLOCK }
|
|
|
|
`c` { flags |= O_NOCTTY }
|
2020-01-31 15:59:23 +01:00
|
|
|
`+` { flags |= O_RDWR }
|
2020-01-21 16:58:47 +01:00
|
|
|
else {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mut permission := 0666
|
|
|
|
if options.len > 0 {
|
|
|
|
permission = options[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
$if windows {
|
|
|
|
if permission < 0600 {
|
|
|
|
permission = 0x0100
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
permission = 0x0100 | 0x0080
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mut p := path
|
|
|
|
$if windows {
|
|
|
|
p = path.replace('/', '\\')
|
|
|
|
}
|
|
|
|
|
|
|
|
fd := C.open(charptr(p.str), flags, permission)
|
|
|
|
if fd == -1 {
|
|
|
|
return error(posix_get_error_msg(C.errno))
|
|
|
|
}
|
|
|
|
|
|
|
|
cfile := C.fdopen(fd, charptr(mode.str))
|
|
|
|
if isnil(cfile) {
|
|
|
|
return error('Failed to open or create file "$path"')
|
|
|
|
}
|
|
|
|
|
|
|
|
return File{
|
|
|
|
cfile: cfile
|
|
|
|
fd: fd
|
|
|
|
opened: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 11:28:10 +01:00
|
|
|
/*
|
2019-12-01 10:50:13 +01:00
|
|
|
pub fn (f mut File) write_bytes_at(data voidptr, size, pos int) {
|
2019-12-31 11:28:10 +01:00
|
|
|
$if linux {
|
|
|
|
}
|
|
|
|
$else {
|
2019-08-22 22:19:31 +02:00
|
|
|
C.fseek(f.cfile, pos, C.SEEK_SET)
|
2019-06-22 20:20:28 +02:00
|
|
|
C.fwrite(data, 1, size, f.cfile)
|
2019-08-22 22:19:31 +02:00
|
|
|
C.fseek(f.cfile, 0, C.SEEK_END)
|
2019-12-31 11:28:10 +01:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-12-31 11:28:10 +01:00
|
|
|
*/
|
2019-06-22 20:20:28 +02:00
|
|
|
|
|
|
|
|
2019-12-01 10:50:13 +01:00
|
|
|
pub fn (f mut File) flush() {
|
2019-12-19 22:29:37 +01:00
|
|
|
if !f.opened {
|
|
|
|
return
|
|
|
|
}
|
2019-07-13 02:11:11 +02:00
|
|
|
C.fflush(f.cfile)
|
|
|
|
}
|
|
|
|
|
2019-06-26 16:11:25 +02:00
|
|
|
// system starts the specified command, waits for it to complete, and returns its code.
|
2019-12-19 22:29:37 +01:00
|
|
|
fn vpopen(path string) voidptr {
|
|
|
|
// *C.FILE {
|
2019-06-22 20:20:28 +02:00
|
|
|
$if windows {
|
2019-07-24 12:16:45 +02:00
|
|
|
mode := 'rb'
|
|
|
|
wpath := path.to_wide()
|
|
|
|
return C._wpopen(wpath, mode.to_wide())
|
2019-12-19 22:29:37 +01:00
|
|
|
} $else {
|
2019-07-24 12:16:45 +02:00
|
|
|
cpath := path.str
|
2019-06-22 20:20:28 +02:00
|
|
|
return C.popen(cpath, 'r')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 01:34:55 +02:00
|
|
|
fn posix_wait4_to_exit_status(waitret int) (int,bool) {
|
|
|
|
$if windows {
|
2019-12-19 22:29:37 +01:00
|
|
|
return waitret,false
|
|
|
|
} $else {
|
2019-10-08 01:34:55 +02:00
|
|
|
mut ret := 0
|
|
|
|
mut is_signaled := true
|
|
|
|
// (see man system, man 2 waitpid: C macro WEXITSTATUS section)
|
2019-12-19 22:29:37 +01:00
|
|
|
if C.WIFEXITED(waitret) {
|
|
|
|
ret = C.WEXITSTATUS(waitret)
|
2019-10-08 01:34:55 +02:00
|
|
|
is_signaled = false
|
2019-12-19 22:29:37 +01:00
|
|
|
}
|
|
|
|
else if C.WIFSIGNALED(waitret) {
|
|
|
|
ret = C.WTERMSIG(waitret)
|
2019-10-08 01:34:55 +02:00
|
|
|
is_signaled = true
|
|
|
|
}
|
2019-12-19 22:29:37 +01:00
|
|
|
return ret,is_signaled
|
2019-10-08 01:34:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-21 16:58:47 +01:00
|
|
|
// posix_get_error_msg return error code representation in string.
|
|
|
|
pub fn posix_get_error_msg(code int) string {
|
|
|
|
ptr_text := C.strerror(code) // voidptr?
|
|
|
|
if ptr_text == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
return tos3(ptr_text)
|
|
|
|
}
|
|
|
|
|
2019-11-05 16:41:56 +01:00
|
|
|
fn vpclose(f voidptr) int {
|
2019-08-08 07:30:05 +02:00
|
|
|
$if windows {
|
2019-12-07 20:42:13 +01:00
|
|
|
return C._pclose(f)
|
2019-12-19 22:29:37 +01:00
|
|
|
} $else {
|
|
|
|
ret,_ := posix_wait4_to_exit_status(C.pclose(f))
|
2019-10-08 01:34:55 +02:00
|
|
|
return ret
|
2019-08-08 07:30:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 20:58:28 +02:00
|
|
|
pub struct Result {
|
2019-08-22 22:19:31 +02:00
|
|
|
pub:
|
|
|
|
exit_code int
|
2019-12-19 22:29:37 +01:00
|
|
|
output string
|
|
|
|
// stderr string // TODO
|
2019-08-22 22:19:31 +02:00
|
|
|
}
|
2019-10-10 21:27:22 +02:00
|
|
|
// `system` works like `exec()`, but only returns a return code.
|
2019-08-17 19:21:59 +02:00
|
|
|
pub fn system(cmd string) int {
|
2019-12-19 22:29:37 +01:00
|
|
|
// if cmd.contains(';') || cmd.contains('&&') || cmd.contains('||') || cmd.contains('\n') {
|
|
|
|
// TODO remove panic
|
|
|
|
// panic(';, &&, || and \\n are not allowed in shell commands')
|
|
|
|
// }
|
2019-12-07 13:51:00 +01:00
|
|
|
mut ret := 0
|
2019-08-17 19:21:59 +02:00
|
|
|
$if windows {
|
2019-11-17 12:36:05 +01: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 19:21:59 +02:00
|
|
|
} $else {
|
2019-08-22 22:19:31 +02:00
|
|
|
ret = C.system(cmd.str)
|
2019-08-08 07:30:05 +02:00
|
|
|
}
|
2019-08-17 19:21:59 +02:00
|
|
|
if ret == -1 {
|
2019-10-08 01:34:55 +02:00
|
|
|
print_c_errno()
|
|
|
|
}
|
|
|
|
$if !windows {
|
2019-12-19 22:29:37 +01:00
|
|
|
pret,is_signaled := posix_wait4_to_exit_status(ret)
|
2019-10-08 01:34:55 +02:00
|
|
|
if is_signaled {
|
2019-12-19 22:29:37 +01:00
|
|
|
println('Terminated by signal ${ret:2d} (' + sigint_to_signal_name(pret) + ')')
|
2019-10-08 01:34:55 +02:00
|
|
|
}
|
|
|
|
ret = pret
|
2019-08-17 19:21:59 +02:00
|
|
|
}
|
|
|
|
return ret
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-10-08 01:34:55 +02:00
|
|
|
pub fn sigint_to_signal_name(si int) string {
|
|
|
|
// POSIX signals:
|
2019-10-24 18:19:03 +02:00
|
|
|
match si {
|
2019-12-19 22:29:37 +01:00
|
|
|
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-12-27 17:59:04 +01:00
|
|
|
else {}
|
|
|
|
}
|
2019-10-08 01:34:55 +02:00
|
|
|
$if linux {
|
|
|
|
// From `man 7 signal` on linux:
|
2019-10-24 18:19:03 +02:00
|
|
|
match si {
|
2019-12-19 22:29:37 +01:00
|
|
|
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-12-27 17:59:04 +01:00
|
|
|
else {}
|
2020-01-21 16:58:47 +01:00
|
|
|
}
|
2019-10-08 01:34:55 +02:00
|
|
|
}
|
|
|
|
return 'unknown'
|
|
|
|
}
|
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
// `getenv` returns the value of the environment variable named by the key.
|
2019-11-29 17:14:26 +01:00
|
|
|
pub fn getenv(key string) string {
|
2019-07-24 12:16:45 +02:00
|
|
|
$if windows {
|
|
|
|
s := C._wgetenv(key.to_wide())
|
2019-11-28 09:46:52 +01:00
|
|
|
if s == 0 {
|
2019-07-24 12:16:45 +02:00
|
|
|
return ''
|
|
|
|
}
|
|
|
|
return string_from_wide(s)
|
|
|
|
} $else {
|
2019-11-28 09:46:52 +01:00
|
|
|
s := C.getenv(key.str)
|
|
|
|
if s == 0 {
|
2019-07-24 12:16:45 +02:00
|
|
|
return ''
|
|
|
|
}
|
2019-11-28 09:46:52 +01:00
|
|
|
// NB: C.getenv *requires* that the result be copied.
|
2019-12-19 22:29:37 +01:00
|
|
|
return cstring_to_vstring(byteptr(s))
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 13:51:03 +02:00
|
|
|
pub fn setenv(name string, value string, overwrite bool) int {
|
2019-07-16 16:27:07 +02:00
|
|
|
$if windows {
|
|
|
|
format := '$name=$value'
|
|
|
|
if overwrite {
|
2019-07-22 16:51:33 +02:00
|
|
|
return C._putenv(format.str)
|
2019-07-16 16:27:07 +02:00
|
|
|
}
|
|
|
|
return -1
|
2019-11-28 09:46:52 +01:00
|
|
|
} $else {
|
2019-07-22 16:51:33 +02:00
|
|
|
return C.setenv(name.str, value.str, overwrite)
|
2019-08-22 22:19:31 +02:00
|
|
|
}
|
2019-06-27 13:51:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unsetenv(name string) int {
|
2019-07-16 16:27:07 +02:00
|
|
|
$if windows {
|
2019-11-29 17:14:26 +01:00
|
|
|
format := '${name}='
|
2019-07-22 16:51:33 +02:00
|
|
|
return C._putenv(format.str)
|
2019-11-28 09:46:52 +01:00
|
|
|
} $else {
|
2019-07-22 16:51:33 +02:00
|
|
|
return C.unsetenv(name.str)
|
2019-08-22 22:19:31 +02:00
|
|
|
}
|
2019-06-27 13:51:03 +02:00
|
|
|
}
|
|
|
|
|
2020-01-06 09:15:06 +01:00
|
|
|
const (
|
|
|
|
F_OK = 0
|
|
|
|
X_OK = 1
|
|
|
|
W_OK = 2
|
|
|
|
R_OK = 4
|
|
|
|
)
|
|
|
|
|
2019-12-04 21:03:12 +01:00
|
|
|
// exists returns true if `path` exists.
|
|
|
|
pub fn exists(path string) bool {
|
2019-06-26 16:39:40 +02:00
|
|
|
$if windows {
|
2019-12-04 21:03:12 +01:00
|
|
|
p := path.replace('/', '\\')
|
2020-01-06 09:15:06 +01:00
|
|
|
return C._waccess(p.to_wide(), F_OK) != -1
|
2019-07-24 12:16:45 +02:00
|
|
|
} $else {
|
2020-01-06 09:15:06 +01:00
|
|
|
return C.access(path.str, F_OK) != -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// `is_executable` returns `true` if `path` is executable.
|
|
|
|
pub fn is_executable(path string) bool {
|
|
|
|
$if windows {
|
|
|
|
// NB: https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/access-waccess?view=vs-2019
|
|
|
|
// i.e. there is no X bit there, the modes can be:
|
|
|
|
// 00 Existence only
|
|
|
|
// 02 Write-only
|
|
|
|
// 04 Read-only
|
|
|
|
// 06 Read and write
|
|
|
|
p := os.realpath( path )
|
|
|
|
return ( os.exists( p ) && p.ends_with('.exe') )
|
|
|
|
} $else {
|
|
|
|
return C.access(path.str, X_OK) != -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// `is_writable` returns `true` if `path` is writable.
|
|
|
|
pub fn is_writable(path string) bool {
|
|
|
|
$if windows {
|
|
|
|
p := path.replace('/', '\\')
|
|
|
|
return C._waccess(p.to_wide(), W_OK) != -1
|
|
|
|
} $else {
|
|
|
|
return C.access(path.str, W_OK) != -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// `is_readable` returns `true` if `path` is readable.
|
|
|
|
pub fn is_readable(path string) bool {
|
|
|
|
$if windows {
|
|
|
|
p := path.replace('/', '\\')
|
|
|
|
return C._waccess(p.to_wide(), R_OK) != -1
|
|
|
|
} $else {
|
|
|
|
return C.access(path.str, R_OK) != -1
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-12-04 21:03:12 +01:00
|
|
|
[deprecated]
|
|
|
|
pub fn file_exists(_path string) bool {
|
|
|
|
panic('use os.exists(path) instead of os.file_exists(path)')
|
|
|
|
}
|
|
|
|
|
2019-07-01 11:01:48 +02:00
|
|
|
// rm removes file in `path`.
|
2019-06-22 20:20:28 +02:00
|
|
|
pub fn rm(path string) {
|
2019-07-24 12:16:45 +02:00
|
|
|
$if windows {
|
|
|
|
C._wremove(path.to_wide())
|
2019-12-19 22:29:37 +01:00
|
|
|
} $else {
|
2019-07-24 12:16:45 +02:00
|
|
|
C.remove(path.str)
|
|
|
|
}
|
|
|
|
// C.unlink(path.cstr())
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-16 15:55:51 +02:00
|
|
|
// rmdir removes a specified directory.
|
|
|
|
pub fn rmdir(path string) {
|
2019-06-26 16:39:40 +02:00
|
|
|
$if !windows {
|
2019-11-29 17:14:26 +01:00
|
|
|
C.rmdir(path.str)
|
2019-12-19 22:29:37 +01:00
|
|
|
} $else {
|
2019-07-24 12:16:45 +02:00
|
|
|
C.RemoveDirectory(path.to_wide())
|
2019-06-26 16:39:40 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-16 15:55:51 +02:00
|
|
|
|
2019-12-30 05:22:28 +01:00
|
|
|
pub fn rmdir_recursive(path string) {
|
2020-01-03 19:46:23 +01:00
|
|
|
items := os.ls(path) or {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-12-30 05:22:28 +01:00
|
|
|
for item in items {
|
2020-01-03 19:46:23 +01:00
|
|
|
if os.is_dir(filepath.join(path,item)) {
|
|
|
|
rmdir_recursive(filepath.join(path,item))
|
2019-12-30 05:22:28 +01:00
|
|
|
}
|
2020-01-03 19:46:23 +01:00
|
|
|
os.rm(filepath.join(path,item))
|
2019-12-30 05:22:28 +01:00
|
|
|
}
|
|
|
|
os.rmdir(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_dir_empty(path string) bool {
|
2020-01-03 19:46:23 +01:00
|
|
|
items := os.ls(path) or {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-12-30 05:22:28 +01:00
|
|
|
return items.len == 0
|
|
|
|
}
|
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
fn print_c_errno() {
|
2019-12-27 17:59:04 +01:00
|
|
|
e := C.errno
|
|
|
|
se := tos_clone(byteptr(C.strerror(C.errno)))
|
|
|
|
println('errno=$e err=$se')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-12-23 11:09:22 +01:00
|
|
|
[deprecated]
|
2019-06-26 12:56:05 +02:00
|
|
|
pub fn ext(path string) string {
|
2019-12-23 11:09:22 +01:00
|
|
|
println('Use filepath.ext')
|
|
|
|
return filepath.ext(path)
|
2019-06-26 11:28:06 +02:00
|
|
|
}
|
|
|
|
|
2019-12-23 11:09:22 +01:00
|
|
|
[deprecated]
|
2019-07-16 01:57:03 +02:00
|
|
|
pub fn dir(path string) string {
|
2019-12-23 11:09:22 +01:00
|
|
|
println('Use filepath.dir')
|
2019-12-28 08:49:27 +01:00
|
|
|
return filepath.dir(path)
|
2019-06-26 12:56:05 +02:00
|
|
|
}
|
|
|
|
|
2019-12-23 11:09:22 +01:00
|
|
|
[deprecated]
|
2019-06-22 20:20:28 +02:00
|
|
|
pub fn basedir(path string) string {
|
2019-12-23 11:09:22 +01:00
|
|
|
println('Use filepath.basedir')
|
|
|
|
return filepath.basedir(path)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-12-23 11:09:22 +01:00
|
|
|
[deprecated]
|
2019-06-22 20:20:28 +02:00
|
|
|
pub fn filename(path string) string {
|
2019-12-23 11:09:22 +01:00
|
|
|
println('Use filepath.filename')
|
|
|
|
return filepath.filename(path)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-08-22 22:19:31 +02:00
|
|
|
// get_line returns a one-line string from stdin
|
2019-06-22 20:20:28 +02:00
|
|
|
pub fn get_line() string {
|
2019-12-19 22:29:37 +01:00
|
|
|
str := get_raw_line()
|
2019-09-15 18:07:40 +02:00
|
|
|
$if windows {
|
|
|
|
return str.trim_right('\r\n')
|
2019-12-19 22:29:37 +01:00
|
|
|
} $else {
|
2019-09-15 18:07:40 +02:00
|
|
|
return str.trim_right('\n')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-06-27 23:51:25 +02: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-12-19 22:29:37 +01:00
|
|
|
$if windows {
|
|
|
|
max_line_chars := 256
|
|
|
|
buf := malloc(max_line_chars * 2)
|
2020-01-10 14:06:40 +01:00
|
|
|
h_input := C.GetStdHandle(STD_INPUT_HANDLE)
|
|
|
|
mut bytes_read := 0
|
2019-12-19 22:29:37 +01:00
|
|
|
if is_atty(0) > 0 {
|
2020-01-10 14:06:40 +01:00
|
|
|
C.ReadConsole(h_input, buf, max_line_chars * 2, &bytes_read, 0)
|
|
|
|
return string_from_wide2(&u16(buf), bytes_read)
|
|
|
|
}
|
|
|
|
mut offset := 0
|
|
|
|
for {
|
|
|
|
pos := buf + offset
|
|
|
|
res := C.ReadFile(h_input, pos, 1, &bytes_read, 0)
|
|
|
|
if !res || bytes_read == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if *pos == `\n` || *pos == `\r` {
|
|
|
|
offset++
|
|
|
|
break
|
|
|
|
}
|
|
|
|
offset++
|
2019-12-19 22:29:37 +01:00
|
|
|
}
|
2020-01-10 14:06:40 +01:00
|
|
|
return string(buf, offset)
|
2019-12-19 22:29:37 +01:00
|
|
|
} $else {
|
2020-02-05 11:23:49 +01:00
|
|
|
max := size_t(0)
|
|
|
|
mut buf := byteptr(0)
|
2019-12-19 22:29:37 +01:00
|
|
|
nr_chars := C.getline(&buf, &max, stdin)
|
2020-02-05 11:23:49 +01:00
|
|
|
defer { unsafe{ free(buf) } }
|
|
|
|
if nr_chars == 0 || nr_chars == -1 {
|
2019-12-19 22:29:37 +01:00
|
|
|
return ''
|
|
|
|
}
|
2020-02-05 11:23:49 +01:00
|
|
|
res := tos_clone( buf )
|
|
|
|
return res
|
2019-12-19 22:29:37 +01:00
|
|
|
}
|
2019-06-27 23:51:25 +02:00
|
|
|
}
|
|
|
|
|
2019-07-24 18:14:13 +02:00
|
|
|
pub fn get_lines() []string {
|
2019-12-19 22:29:37 +01:00
|
|
|
mut line := ''
|
|
|
|
mut inputstr := []string
|
|
|
|
for {
|
|
|
|
line = get_line()
|
|
|
|
if (line.len <= 0) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
line = line.trim_space()
|
|
|
|
inputstr << line
|
|
|
|
}
|
|
|
|
return inputstr
|
2019-07-24 18:14:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_lines_joined() string {
|
2019-10-26 19:21:07 +02: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 18:14:13 +02:00
|
|
|
}
|
|
|
|
|
2019-10-26 19:21:07 +02:00
|
|
|
// user_os returns current user operating system name.
|
2019-06-22 20:20:28 +02:00
|
|
|
pub fn user_os() string {
|
|
|
|
$if linux {
|
|
|
|
return 'linux'
|
|
|
|
}
|
2019-12-03 14:29:24 +01:00
|
|
|
$if macos {
|
2019-06-22 20:20:28 +02:00
|
|
|
return 'mac'
|
|
|
|
}
|
|
|
|
$if windows {
|
|
|
|
return 'windows'
|
|
|
|
}
|
2019-07-15 17:24:40 +02:00
|
|
|
$if freebsd {
|
2019-08-22 22:19:31 +02:00
|
|
|
return 'freebsd'
|
|
|
|
}
|
2019-07-21 12:22:41 +02:00
|
|
|
$if openbsd {
|
2019-08-22 22:19:31 +02:00
|
|
|
return 'openbsd'
|
|
|
|
}
|
2019-07-21 12:22:41 +02:00
|
|
|
$if netbsd {
|
2019-08-22 22:19:31 +02:00
|
|
|
return 'netbsd'
|
|
|
|
}
|
2019-07-21 12:22:41 +02:00
|
|
|
$if dragonfly {
|
2019-08-22 22:19:31 +02:00
|
|
|
return 'dragonfly'
|
2019-07-23 23:23:13 +02:00
|
|
|
}
|
2019-12-19 22:29:37 +01:00
|
|
|
$if android {
|
2019-09-16 17:23:11 +02:00
|
|
|
return 'android'
|
|
|
|
}
|
2019-09-26 23:30:41 +02:00
|
|
|
$if solaris {
|
|
|
|
return 'solaris'
|
|
|
|
}
|
2019-12-03 09:26:47 +01:00
|
|
|
$if haiku {
|
|
|
|
return 'haiku'
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
return 'unknown'
|
|
|
|
}
|
|
|
|
|
2019-06-26 16:11:25 +02:00
|
|
|
// home_dir returns path to user's home directory.
|
2019-06-22 20:20:28 +02:00
|
|
|
pub fn home_dir() string {
|
|
|
|
mut home := os.getenv('HOME')
|
|
|
|
$if windows {
|
|
|
|
home = os.getenv('HOMEDRIVE')
|
2019-07-12 19:05:22 +02: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 20:20:28 +02:00
|
|
|
}
|
2019-10-12 21:31:05 +02:00
|
|
|
home += path_separator
|
2019-06-22 20:20:28 +02:00
|
|
|
return home
|
|
|
|
}
|
|
|
|
|
2019-08-22 22:19:31 +02:00
|
|
|
// write_file writes `text` data to a file in `path`.
|
2019-06-22 20:20:28 +02:00
|
|
|
pub fn write_file(path, text string) {
|
2019-12-21 23:41:42 +01:00
|
|
|
mut f := os.create(path) or {
|
2019-08-22 22:19:31 +02:00
|
|
|
return
|
|
|
|
}
|
2019-07-01 23:24:19 +02:00
|
|
|
f.write(text)
|
2019-06-22 20:20:28 +02:00
|
|
|
f.close()
|
|
|
|
}
|
|
|
|
|
2019-12-04 21:53:11 +01:00
|
|
|
// clear clears current terminal screen.
|
2019-06-26 18:56:33 +02:00
|
|
|
pub fn clear() {
|
2019-10-21 03:15:43 +02:00
|
|
|
$if !windows {
|
|
|
|
C.printf('\x1b[2J')
|
|
|
|
C.printf('\x1b[H')
|
|
|
|
}
|
2019-06-26 18:56:33 +02:00
|
|
|
}
|
|
|
|
|
2019-12-03 11:08:57 +01:00
|
|
|
pub fn on_segfault(f voidptr) {
|
2019-06-26 16:39:40 +02:00
|
|
|
$if windows {
|
2019-06-24 13:51:11 +02:00
|
|
|
return
|
2019-06-26 16:39:40 +02:00
|
|
|
}
|
2019-12-03 14:29:24 +01:00
|
|
|
$if macos {
|
2019-12-27 17:59:04 +01:00
|
|
|
mut sa := C.sigaction{}
|
2019-06-27 17:48:49 +02:00
|
|
|
C.memset(&sa, 0, sizeof(sigaction))
|
|
|
|
C.sigemptyset(&sa.sa_mask)
|
|
|
|
sa.sa_sigaction = f
|
2019-08-22 22:19:31 +02:00
|
|
|
sa.sa_flags = C.SA_SIGINFO
|
|
|
|
C.sigaction(C.SIGSEGV, &sa, 0)
|
2019-06-26 16:39:40 +02:00
|
|
|
}
|
2019-06-27 12:27:46 +02:00
|
|
|
}
|
2019-06-27 22:17:13 +02:00
|
|
|
|
2019-09-15 18:07:40 +02:00
|
|
|
fn C.getpid() int
|
|
|
|
|
2019-11-22 17:00:56 +01:00
|
|
|
|
2019-12-19 22:29:37 +01:00
|
|
|
fn C.proc_pidpath(int, byteptr, int) int
|
|
|
|
|
|
|
|
|
|
|
|
fn C.readlink() int
|
2019-11-22 17:00:56 +01:00
|
|
|
// executable returns the path name of the executable that started the current
|
|
|
|
// process.
|
2019-07-15 23:22:29 +02:00
|
|
|
pub fn executable() string {
|
2019-06-27 22:17:13 +02:00
|
|
|
$if linux {
|
2019-11-28 09:46:52 +01:00
|
|
|
mut result := calloc(MAX_PATH)
|
2019-11-22 17:00:56 +01:00
|
|
|
count := C.readlink('/proc/self/exe', result, MAX_PATH)
|
2019-07-16 16:37:59 +02:00
|
|
|
if count < 0 {
|
2019-11-28 09:46:52 +01:00
|
|
|
eprintln('os.executable() failed at reading /proc/self/exe to get exe path')
|
|
|
|
return os.args[0]
|
2019-06-27 22:17:13 +02:00
|
|
|
}
|
2019-11-28 09:46:52 +01:00
|
|
|
return string(result)
|
2019-06-27 22:17:13 +02:00
|
|
|
}
|
|
|
|
$if windows {
|
2019-07-24 18:25:18 +02:00
|
|
|
max := 512
|
2019-12-19 22:29:37 +01:00
|
|
|
mut result := &u16(calloc(max * 2)) // MAX_PATH * sizeof(wchar_t)
|
|
|
|
len := C.GetModuleFileName(0, result, max)
|
2019-07-24 12:16:45 +02:00
|
|
|
return string_from_wide2(result, len)
|
2019-06-27 22:17:13 +02:00
|
|
|
}
|
2019-12-03 14:29:24 +01:00
|
|
|
$if macos {
|
2019-11-28 09:46:52 +01:00
|
|
|
mut result := calloc(MAX_PATH)
|
2019-08-22 22:19:31 +02:00
|
|
|
pid := C.getpid()
|
2019-12-19 22:29:37 +01:00
|
|
|
ret := proc_pidpath(pid, result, MAX_PATH)
|
|
|
|
if ret <= 0 {
|
2019-11-28 09:46:52 +01:00
|
|
|
eprintln('os.executable() failed at calling proc_pidpath with pid: $pid . proc_pidpath returned $ret ')
|
|
|
|
return os.args[0]
|
2019-08-22 22:19:31 +02:00
|
|
|
}
|
|
|
|
return string(result)
|
2019-06-27 22:17:13 +02:00
|
|
|
}
|
2019-07-16 16:19:52 +02:00
|
|
|
$if freebsd {
|
2019-11-28 09:46:52 +01:00
|
|
|
mut result := calloc(MAX_PATH)
|
2019-12-19 22:29:37 +01:00
|
|
|
mib := [1/* CTL_KERN */, 14/* KERN_PROC */, 12/* KERN_PROC_PATHNAME */, -1]
|
2019-08-22 22:19:31 +02:00
|
|
|
size := MAX_PATH
|
|
|
|
C.sysctl(mib.data, 4, result, &size, 0, 0)
|
|
|
|
return string(result)
|
|
|
|
}
|
2019-07-16 16:37:59 +02:00
|
|
|
$if openbsd {
|
2019-08-22 22:19:31 +02:00
|
|
|
// "Sadly there is no way to get the full path of the executed file in OpenBSD."
|
|
|
|
// lol
|
|
|
|
return os.args[0]
|
|
|
|
}
|
2019-12-27 17:59:04 +01:00
|
|
|
$if solaris {}
|
|
|
|
$if haiku {}
|
2019-07-16 16:37:59 +02:00
|
|
|
$if netbsd {
|
2019-11-28 09:46:52 +01:00
|
|
|
mut result := calloc(MAX_PATH)
|
2020-01-05 20:13:35 +01:00
|
|
|
count := C.readlink('/proc/curproc/exe', result, MAX_PATH)
|
2019-07-16 16:37:59 +02:00
|
|
|
if count < 0 {
|
2019-11-28 09:46:52 +01:00
|
|
|
eprintln('os.executable() failed at reading /proc/curproc/exe to get exe path')
|
|
|
|
return os.args[0]
|
2019-07-16 16:37:59 +02:00
|
|
|
}
|
2019-12-19 22:29:37 +01:00
|
|
|
return string(result,count)
|
2019-08-22 22:19:31 +02:00
|
|
|
}
|
2019-07-16 16:37:59 +02:00
|
|
|
$if dragonfly {
|
2019-11-28 09:46:52 +01:00
|
|
|
mut result := calloc(MAX_PATH)
|
2020-01-05 20:13:35 +01:00
|
|
|
count := C.readlink('/proc/curproc/file', result, MAX_PATH)
|
2019-07-16 16:37:59 +02:00
|
|
|
if count < 0 {
|
2019-11-28 09:46:52 +01:00
|
|
|
eprintln('os.executable() failed at reading /proc/curproc/file to get exe path')
|
|
|
|
return os.args[0]
|
2019-07-16 16:37:59 +02:00
|
|
|
}
|
2019-12-19 22:29:37 +01:00
|
|
|
return string(result,count)
|
2019-08-22 22:19:31 +02:00
|
|
|
}
|
2019-09-26 23:23:27 +02:00
|
|
|
return os.args[0]
|
2019-06-27 22:17:13 +02:00
|
|
|
}
|
2019-06-30 14:55:48 +02:00
|
|
|
|
2019-12-04 21:03:12 +01:00
|
|
|
[deprecated]
|
|
|
|
pub fn dir_exists(path string) bool {
|
|
|
|
panic('use os.is_dir()')
|
2019-12-19 22:29:37 +01:00
|
|
|
// return false
|
2019-12-04 21:03:12 +01:00
|
|
|
}
|
2019-10-26 19:21:07 +02:00
|
|
|
// is_dir returns a boolean indicating whether the given path is a directory.
|
2019-06-30 14:55:48 +02:00
|
|
|
pub fn is_dir(path string) bool {
|
|
|
|
$if windows {
|
2019-12-04 21:03:12 +01:00
|
|
|
_path := path.replace('/', '\\')
|
|
|
|
attr := C.GetFileAttributesW(_path.to_wide())
|
2019-12-05 09:25:55 +01:00
|
|
|
if attr == u32(C.INVALID_FILE_ATTRIBUTES) {
|
2019-12-04 21:03:12 +01:00
|
|
|
return false
|
|
|
|
}
|
2019-12-04 21:53:11 +01:00
|
|
|
if int(attr) & C.FILE_ATTRIBUTE_DIRECTORY != 0 {
|
2019-12-04 21:03:12 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2019-12-19 22:29:37 +01:00
|
|
|
} $else {
|
2019-12-27 17:59:04 +01:00
|
|
|
statbuf := C.stat{}
|
2019-12-04 21:53:11 +01:00
|
|
|
if C.stat(path.str, &statbuf) != 0 {
|
2019-06-30 14:55:48 +02:00
|
|
|
return false
|
|
|
|
}
|
2019-07-31 10:32:00 +02:00
|
|
|
// ref: https://code.woboq.org/gcc/include/sys/stat.h.html
|
2019-12-04 21:53:11 +01:00
|
|
|
return int(statbuf.st_mode) & S_IFMT == S_IFDIR
|
2019-08-22 22:19:31 +02:00
|
|
|
}
|
2019-06-30 14:55:48 +02:00
|
|
|
}
|
|
|
|
|
2019-12-04 21:53:11 +01:00
|
|
|
// is_link returns a boolean indicating whether the given path is a link.
|
|
|
|
pub fn is_link(path string) bool {
|
2019-12-04 22:14:23 +01:00
|
|
|
$if windows {
|
|
|
|
return false // TODO
|
|
|
|
} $else {
|
2019-12-27 17:59:04 +01:00
|
|
|
statbuf := C.stat{}
|
2019-12-04 22:14:23 +01:00
|
|
|
if C.lstat(path.str, &statbuf) != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return int(statbuf.st_mode) & S_IFMT == S_IFLNK
|
2019-12-04 21:53:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-26 19:21:07 +02:00
|
|
|
// chdir changes the current working directory to the new directory path.
|
2019-06-30 16:11:55 +02:00
|
|
|
pub fn chdir(path string) {
|
2019-06-30 14:55:48 +02:00
|
|
|
$if windows {
|
2019-07-24 12:16:45 +02:00
|
|
|
C._wchdir(path.to_wide())
|
2019-12-19 22:29:37 +01:00
|
|
|
} $else {
|
2019-07-22 16:51:33 +02:00
|
|
|
C.chdir(path.str)
|
2019-08-22 22:19:31 +02:00
|
|
|
}
|
2019-06-30 14:55:48 +02:00
|
|
|
}
|
|
|
|
|
2019-10-26 19:21:07 +02:00
|
|
|
// getwd returns the absolute path name of the current directory.
|
2019-11-29 17:14:26 +01:00
|
|
|
pub fn getwd() string {
|
2019-06-30 14:55:48 +02:00
|
|
|
$if windows {
|
2019-07-24 18:25:18 +02:00
|
|
|
max := 512 // MAX_PATH * sizeof(wchar_t)
|
2019-12-19 22:29:37 +01:00
|
|
|
buf := &u16(calloc(max * 2))
|
2019-07-24 18:25:18 +02:00
|
|
|
if C._wgetcwd(buf, max) == 0 {
|
2019-06-30 14:55:48 +02:00
|
|
|
return ''
|
|
|
|
}
|
2019-07-24 12:16:45 +02:00
|
|
|
return string_from_wide(buf)
|
2019-12-19 22:29:37 +01:00
|
|
|
} $else {
|
2019-11-15 14:14:28 +01:00
|
|
|
buf := calloc(512)
|
2019-06-30 14:55:48 +02:00
|
|
|
if C.getcwd(buf, 512) == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
2019-07-24 12:16:45 +02:00
|
|
|
return string(buf)
|
|
|
|
}
|
2019-06-30 14:55:48 +02:00
|
|
|
}
|
|
|
|
|
2019-08-22 13:15:11 +02: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
|
2019-12-19 22:29:37 +01:00
|
|
|
// and https://insanecoding.blogspot.com/2007/11/implementing-realpath-in-c.html
|
2019-08-22 13:15:11 +02:00
|
|
|
// NB: this particular rabbit hole is *deep* ...
|
|
|
|
pub fn realpath(fpath string) string {
|
2019-11-28 09:46:52 +01:00
|
|
|
mut fullpath := calloc(MAX_PATH)
|
2019-12-01 08:33:26 +01:00
|
|
|
mut ret := charptr(0)
|
2019-08-22 13:15:11 +02:00
|
|
|
$if windows {
|
2019-11-28 09:46:52 +01:00
|
|
|
ret = C._fullpath(fullpath, fpath.str, MAX_PATH)
|
2019-11-23 18:56:22 +01:00
|
|
|
if ret == 0 {
|
|
|
|
return fpath
|
2019-11-29 17:14:26 +01:00
|
|
|
}
|
2019-11-28 09:46:52 +01:00
|
|
|
} $else {
|
|
|
|
ret = C.realpath(fpath.str, fullpath)
|
2019-11-14 23:07:38 +01:00
|
|
|
if ret == 0 {
|
|
|
|
return fpath
|
2019-11-29 17:14:26 +01:00
|
|
|
}
|
2019-08-22 13:15:11 +02:00
|
|
|
}
|
2019-11-28 09:46:52 +01:00
|
|
|
return string(fullpath)
|
2019-08-22 13:15:11 +02:00
|
|
|
}
|
|
|
|
|
2019-08-22 22:19:31 +02:00
|
|
|
// walk_ext returns a recursive list of all file paths ending with `ext`.
|
2019-08-16 14:05:11 +02:00
|
|
|
pub fn walk_ext(path, ext string) []string {
|
2019-08-22 22:19:31 +02:00
|
|
|
if !os.is_dir(path) {
|
2019-11-14 07:18:07 +01:00
|
|
|
return []
|
2019-08-22 22:19:31 +02:00
|
|
|
}
|
2019-12-21 23:41:42 +01:00
|
|
|
mut files := os.ls(path) or {
|
2019-12-19 22:29:37 +01:00
|
|
|
panic(err)
|
|
|
|
}
|
2019-08-22 22:19:31 +02:00
|
|
|
mut res := []string
|
2019-12-19 22:29:37 +01:00
|
|
|
separator := if path.ends_with(path_separator) { '' } else { path_separator }
|
2019-08-16 14:05:11 +02:00
|
|
|
for i, file in files {
|
|
|
|
if file.starts_with('.') {
|
2019-08-22 22:19:31 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-11-09 17:35:26 +01:00
|
|
|
p := path + separator + file
|
2019-12-30 05:23:54 +01:00
|
|
|
if os.is_dir(p) && !os.is_link(p) {
|
2019-08-22 22:19:31 +02:00
|
|
|
res << walk_ext(p, ext)
|
|
|
|
}
|
2019-08-16 14:05:11 +02:00
|
|
|
else if file.ends_with(ext) {
|
2019-08-22 22:19:31 +02:00
|
|
|
res << p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
2019-06-30 14:55:48 +02:00
|
|
|
|
2020-02-11 10:26:46 +01:00
|
|
|
// walk recursively traverses the given directory path.
|
2019-10-24 14:17:09 +02:00
|
|
|
// When a file is encountred it will call the callback function with current file as argument.
|
2020-02-11 10:26:46 +01:00
|
|
|
pub fn walk(path string, f fn(path string)) {
|
2019-10-24 14:17:09 +02:00
|
|
|
if !os.is_dir(path) {
|
|
|
|
return
|
|
|
|
}
|
2019-12-21 23:41:42 +01:00
|
|
|
mut files := os.ls(path) or {
|
2019-12-19 22:29:37 +01:00
|
|
|
panic(err)
|
|
|
|
}
|
2019-10-24 14:17:09 +02:00
|
|
|
for file in files {
|
|
|
|
p := path + os.path_separator + file
|
2019-12-30 05:23:54 +01:00
|
|
|
if os.is_dir(p) && !os.is_link(p) {
|
2020-02-11 10:26:46 +01:00
|
|
|
walk(p, f)
|
2019-10-24 14:17:09 +02:00
|
|
|
}
|
2019-12-04 21:03:12 +01:00
|
|
|
else if os.exists(p) {
|
2020-02-11 10:26:46 +01:00
|
|
|
f(p)
|
2019-10-24 14:17:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-02 22:13:23 +02:00
|
|
|
pub fn signal(signum int, handler voidptr) {
|
|
|
|
C.signal(signum, handler)
|
|
|
|
}
|
|
|
|
|
2019-09-15 18:07:40 +02:00
|
|
|
fn C.fork() int
|
2019-12-19 22:29:37 +01:00
|
|
|
|
|
|
|
|
2019-09-15 18:07:40 +02:00
|
|
|
fn C.wait() int
|
|
|
|
|
2019-12-19 22:29:37 +01:00
|
|
|
|
2019-07-14 04:18:54 +02:00
|
|
|
pub fn fork() int {
|
2019-11-16 00:30:50 +01:00
|
|
|
mut pid := -1
|
2019-07-14 12:36:25 +02:00
|
|
|
$if !windows {
|
2019-11-16 00:30:50 +01:00
|
|
|
pid = C.fork()
|
2019-07-14 12:36:25 +02:00
|
|
|
}
|
2019-11-16 00:30:50 +01:00
|
|
|
$if windows {
|
|
|
|
panic('os.fork not supported in windows') // TODO
|
|
|
|
}
|
|
|
|
return pid
|
2019-07-14 04:18:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn wait() int {
|
2019-11-16 00:30:50 +01:00
|
|
|
mut pid := -1
|
|
|
|
$if !windows {
|
|
|
|
pid = C.wait(0)
|
|
|
|
}
|
2020-02-10 18:48:01 +01:00
|
|
|
$if windows {
|
2019-11-16 00:30:50 +01:00
|
|
|
panic('os.wait not supported in windows') // TODO
|
2019-07-14 12:36:25 +02:00
|
|
|
}
|
2019-11-16 00:30:50 +01:00
|
|
|
return pid
|
2019-07-14 04:18:54 +02:00
|
|
|
}
|
|
|
|
|
2019-07-07 21:46:21 +02:00
|
|
|
pub fn file_last_mod_unix(path string) int {
|
2019-12-27 17:59:04 +01:00
|
|
|
attr := C.stat{}
|
2019-12-19 22:29:37 +01:00
|
|
|
// # struct stat attr;
|
2019-08-22 22:19:31 +02:00
|
|
|
C.stat(path.str, &attr)
|
2019-12-19 22:29:37 +01:00
|
|
|
// # stat(path.str, &attr);
|
2019-08-22 22:19:31 +02:00
|
|
|
return attr.st_mtime
|
2019-12-19 22:29:37 +01:00
|
|
|
// # return attr.st_mtime ;
|
2019-07-07 21:46:21 +02:00
|
|
|
}
|
2019-08-22 22:19:31 +02:00
|
|
|
|
2019-09-21 20:38:12 +02:00
|
|
|
pub fn log(s string) {
|
|
|
|
println('os.log: ' + s)
|
2019-06-30 14:55:48 +02:00
|
|
|
}
|
|
|
|
|
2019-07-24 16:15:21 +02:00
|
|
|
pub fn flush_stdout() {
|
|
|
|
C.fflush(stdout)
|
2019-08-22 22:19:31 +02:00
|
|
|
}
|
2019-07-24 16:15:21 +02:00
|
|
|
|
2019-10-12 04:04:56 +02:00
|
|
|
pub fn mkdir_all(path string) {
|
2019-10-12 21:31:05 +02: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-12-04 21:03:12 +01:00
|
|
|
if !os.is_dir(p) {
|
2019-12-21 23:41:42 +01:00
|
|
|
os.mkdir(p) or {
|
2019-12-19 22:29:37 +01:00
|
|
|
panic(err)
|
|
|
|
}
|
2019-10-12 04:04:56 +02:00
|
|
|
}
|
|
|
|
}
|
2019-10-12 07:41:41 +02:00
|
|
|
}
|
2019-10-15 17:08:46 +02:00
|
|
|
|
|
|
|
pub fn join(base string, dirs ...string) string {
|
2019-11-01 17:29:51 +01:00
|
|
|
println('use filepath.join')
|
2019-12-19 22:29:37 +01:00
|
|
|
return filepath.join(base,dirs)
|
2019-10-15 17:08:46 +02:00
|
|
|
}
|
2019-11-17 04:45:20 +01:00
|
|
|
|
2020-01-03 19:46:23 +01:00
|
|
|
// cachedir returns the path to a *writable* user specific folder, suitable for writing non-essential data.
|
|
|
|
pub fn cachedir() string {
|
|
|
|
// See: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
|
|
|
// There is a single base directory relative to which user-specific non-essential
|
|
|
|
// (cached) data should be written. This directory is defined by the environment
|
|
|
|
// variable $XDG_CACHE_HOME.
|
|
|
|
// $XDG_CACHE_HOME defines the base directory relative to which user specific
|
|
|
|
// non-essential data files should be stored. If $XDG_CACHE_HOME is either not set
|
|
|
|
// or empty, a default equal to $HOME/.cache should be used.
|
|
|
|
$if !windows {
|
|
|
|
xdg_cache_home := os.getenv('XDG_CACHE_HOME')
|
|
|
|
if xdg_cache_home != '' {
|
|
|
|
return xdg_cache_home
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cdir := os.home_dir() + '.cache'
|
|
|
|
if !os.is_dir(cdir) && !os.is_link(cdir) {
|
|
|
|
os.mkdir(cdir) or {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cdir
|
|
|
|
}
|
|
|
|
|
2019-11-17 04:45:20 +01: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 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')
|
2019-12-19 22:29:37 +01:00
|
|
|
if path == '' {
|
|
|
|
path = os.getenv('TMP')
|
|
|
|
}
|
|
|
|
if path == '' {
|
|
|
|
path = 'C:/tmp'
|
|
|
|
}
|
2019-11-17 04:45:20 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-03 19:46:23 +01:00
|
|
|
if path == '' {
|
|
|
|
path = os.cachedir()
|
|
|
|
}
|
2019-12-30 05:25:26 +01:00
|
|
|
if path == '' {
|
|
|
|
path = '/tmp'
|
|
|
|
}
|
2019-11-17 04:45:20 +01:00
|
|
|
return path
|
|
|
|
}
|
2019-11-24 04:27:02 +01:00
|
|
|
|
|
|
|
pub fn chmod(path string, mode int) {
|
|
|
|
C.chmod(path.str, mode)
|
2019-11-29 17:14:26 +01:00
|
|
|
}
|
2019-12-08 18:21:17 +01:00
|
|
|
|
|
|
|
pub const (
|
2019-12-19 22:29:37 +01:00
|
|
|
wd_at_startup = getwd()
|
2019-12-08 18:21:17 +01:00
|
|
|
)
|
2020-01-12 19:35:06 +01:00
|
|
|
|
2020-02-11 10:26:46 +01:00
|
|
|
// resource_abs_path returns an absolute path, for the given `path`
|
2020-01-12 19:35:06 +01:00
|
|
|
// (the path is expected to be relative to the executable program)
|
|
|
|
// See https://discordapp.com/channels/592103645835821068/592294828432424960/630806741373943808
|
|
|
|
// It gives a convenient way to access program resources like images, fonts, sounds and so on,
|
|
|
|
// *no matter* how the program was started, and what is the current working directory.
|
|
|
|
pub fn resource_abs_path(path string) string {
|
|
|
|
mut base_path := os.realpath(filepath.dir(os.executable()))
|
|
|
|
vresource := os.getenv('V_RESOURCE_PATH')
|
|
|
|
if vresource.len != 0 {
|
|
|
|
base_path = vresource
|
|
|
|
}
|
|
|
|
return os.realpath( filepath.join( base_path, path ) )
|
|
|
|
}
|