2020-01-23 23:04:46 +03:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-06-23 05:21:30 +03:00
|
|
|
// 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-10-24 12:36:57 +03:00
|
|
|
pub const (
|
2020-04-26 10:17:13 +03:00
|
|
|
args = []string{}
|
2020-05-22 18:36:09 +03:00
|
|
|
max_path_len = 4096
|
2019-06-22 21:20:28 +03:00
|
|
|
)
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// read_bytes returns all bytes read from file in `path`.
|
2019-11-19 09:53:52 +03:00
|
|
|
pub fn read_bytes(path string) ?[]byte {
|
2020-08-28 15:24:00 +03:00
|
|
|
mut fp := vfopen(path, 'rb')?
|
2020-09-13 18:03:13 +03:00
|
|
|
cseek := C.fseek(fp, 0, C.SEEK_END)
|
|
|
|
if cseek != 0 {
|
|
|
|
return error('fseek failed')
|
|
|
|
}
|
2019-11-19 09:53:52 +03:00
|
|
|
fsize := C.ftell(fp)
|
2020-09-13 18:03:13 +03:00
|
|
|
if fsize < 0 {
|
|
|
|
return error('ftell failed')
|
|
|
|
}
|
2019-11-19 09:53:52 +03:00
|
|
|
C.rewind(fp)
|
2020-09-13 18:03:13 +03:00
|
|
|
mut res := []byte{len: fsize}
|
2019-12-18 08:22:52 +03:00
|
|
|
nr_read_elements := C.fread(res.data, fsize, 1, fp)
|
2020-09-13 18:03:13 +03:00
|
|
|
if nr_read_elements == 0 && fsize > 0 {
|
|
|
|
return error('fread failed')
|
|
|
|
}
|
2019-11-19 09:53:52 +03:00
|
|
|
C.fclose(fp)
|
2019-12-18 08:22:52 +03:00
|
|
|
return res[0..nr_read_elements * fsize]
|
2019-11-19 09:53:52 +03:00
|
|
|
}
|
|
|
|
|
2020-05-22 07:21:11 +03:00
|
|
|
|
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'
|
2020-08-28 15:24:00 +03:00
|
|
|
mut fp := vfopen(path, mode)?
|
2020-02-05 13:23:49 +03:00
|
|
|
defer { C.fclose(fp) }
|
2020-09-13 18:03:13 +03:00
|
|
|
cseek := C.fseek(fp, 0, C.SEEK_END)
|
|
|
|
if cseek != 0 {
|
|
|
|
return error('fseek failed')
|
|
|
|
}
|
2019-07-03 22:07:42 +03:00
|
|
|
fsize := C.ftell(fp)
|
2020-09-13 18:03:13 +03:00
|
|
|
if fsize < 0 {
|
|
|
|
return error('ftell failed')
|
|
|
|
}
|
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)
|
2020-07-22 20:28:53 +03:00
|
|
|
unsafe {
|
|
|
|
mut str := malloc(fsize + 1)
|
2020-09-13 18:03:13 +03:00
|
|
|
nelements := C.fread(str, fsize, 1, fp)
|
|
|
|
if nelements == 0 && fsize > 0 {
|
|
|
|
free(str)
|
|
|
|
return error('fread failed')
|
|
|
|
}
|
2020-07-22 20:28:53 +03:00
|
|
|
str[fsize] = 0
|
2020-08-10 19:05:26 +03:00
|
|
|
return str.vstring_with_len(fsize)
|
2020-07-22 20:28:53 +03:00
|
|
|
}
|
2019-07-03 22:07:42 +03:00
|
|
|
}
|
|
|
|
|
2020-05-22 07:21:11 +03:00
|
|
|
/***************************** OS ops ************************/
|
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-12-27 19:59:04 +03:00
|
|
|
mut s := C.stat{}
|
2020-07-20 20:06:41 +03:00
|
|
|
unsafe {
|
|
|
|
$if windows {
|
|
|
|
$if tinyc {
|
|
|
|
C.stat(charptr(path.str), &s)
|
|
|
|
} $else {
|
|
|
|
C._wstat(path.to_wide(), voidptr(&s))
|
|
|
|
}
|
2020-06-19 13:54:56 +03:00
|
|
|
} $else {
|
2020-07-20 20:06:41 +03:00
|
|
|
C.stat(charptr(path.str), &s)
|
2020-06-19 13:54:56 +03:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// mv moves files or folders from `src` to `dst`.
|
|
|
|
pub fn mv(src, dst string) {
|
2020-09-04 14:08:47 +03:00
|
|
|
mut rdst := dst
|
|
|
|
if is_dir(rdst) {
|
2020-09-06 14:12:07 +03:00
|
|
|
rdst = join_path(rdst.trim_right(path_separator),file_name(src.trim_right(path_separator)))
|
2020-09-04 14:08:47 +03:00
|
|
|
}
|
2019-07-24 13:16:45 +03:00
|
|
|
$if windows {
|
2020-09-06 14:12:07 +03:00
|
|
|
w_src := src.replace('/', '\\')
|
|
|
|
w_dst := rdst.replace('/', '\\')
|
|
|
|
C._wrename(w_src.to_wide(), w_dst.to_wide())
|
2019-07-24 13:16:45 +03:00
|
|
|
} $else {
|
2020-09-04 14:08:47 +03:00
|
|
|
C.rename(charptr(src.str), charptr(rdst.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
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// cp copies files or folders from `src` to `dst`.
|
|
|
|
pub fn cp(src, dst string) ? {
|
2019-11-01 00:57:16 +03:00
|
|
|
$if windows {
|
2020-08-27 17:57:01 +03:00
|
|
|
w_src := src.replace('/', '\\')
|
|
|
|
w_dst := dst.replace('/', '\\')
|
|
|
|
if C.CopyFile(w_src.to_wide(), w_dst.to_wide(), false) == 0 {
|
2020-08-02 14:08:45 +03:00
|
|
|
result := C.GetLastError()
|
2020-08-27 17:57:01 +03:00
|
|
|
return error_with_code('failed to copy $src to $dst', int(result))
|
2019-11-02 22:37:29 +03:00
|
|
|
}
|
|
|
|
} $else {
|
2020-08-27 17:57:01 +03:00
|
|
|
fp_from := C.open(charptr(src.str), C.O_RDONLY)
|
2020-06-01 22:11:40 +03:00
|
|
|
if fp_from < 0 { // Check if file opened
|
2020-08-27 17:57:01 +03:00
|
|
|
return error_with_code('cp: failed to open $src', int(fp_from))
|
2020-06-01 22:11:40 +03:00
|
|
|
}
|
2020-08-27 17:57:01 +03:00
|
|
|
fp_to := C.open(charptr(dst.str), C.O_WRONLY | C.O_CREAT | C.O_TRUNC, C.S_IWUSR | C.S_IRUSR)
|
2020-06-01 22:11:40 +03:00
|
|
|
if fp_to < 0 { // Check if file opened (permissions problems ...)
|
|
|
|
C.close(fp_from)
|
2020-08-27 17:57:01 +03:00
|
|
|
return error_with_code('cp (permission): failed to write to $dst (fp_to: $fp_to)', int(fp_to))
|
2020-06-01 22:11:40 +03:00
|
|
|
}
|
2020-08-16 05:54:05 +03:00
|
|
|
mut buf := [1024]byte{}
|
2020-06-01 22:11:40 +03:00
|
|
|
mut count := 0
|
|
|
|
for {
|
|
|
|
// FIXME: use sizeof, bug: 'os__buf' undeclared
|
|
|
|
//count = C.read(fp_from, buf, sizeof(buf))
|
|
|
|
count = C.read(fp_from, buf, 1024)
|
|
|
|
if count == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if C.write(fp_to, buf, count) < 0 {
|
2020-08-27 17:57:01 +03:00
|
|
|
return error_with_code('cp: failed to write to $dst', int(-1))
|
2020-06-01 22:11:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
from_attr := C.stat{}
|
2020-07-20 20:06:41 +03:00
|
|
|
unsafe {
|
2020-08-27 17:57:01 +03:00
|
|
|
C.stat(charptr(src.str), &from_attr)
|
2020-07-20 20:06:41 +03:00
|
|
|
}
|
2020-08-27 17:57:01 +03:00
|
|
|
if C.chmod(charptr(dst.str), from_attr.st_mode) < 0 {
|
|
|
|
return error_with_code('failed to set permissions for $dst', int(-1))
|
2020-06-01 22:11:40 +03:00
|
|
|
}
|
2020-08-16 05:55:10 +03:00
|
|
|
C.close(fp_to)
|
|
|
|
C.close(fp_from)
|
2019-11-02 22:37:29 +03:00
|
|
|
}
|
2019-11-01 00:57:16 +03:00
|
|
|
}
|
|
|
|
|
2020-03-02 21:30:04 +03:00
|
|
|
[deprecated]
|
2020-06-25 13:06:47 +03:00
|
|
|
pub fn cp_r(osource_path, odest_path string, overwrite bool) ? {
|
2020-06-23 17:25:24 +03:00
|
|
|
eprintln('warning: `os.cp_r` has been deprecated, use `os.cp_all` instead')
|
|
|
|
return cp_all(osource_path, odest_path, overwrite)
|
2020-03-02 21:30:04 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// cp_all will recursively copy `src` to `dst`,
|
|
|
|
// optionally overwriting files or dirs in `dst`.
|
|
|
|
pub fn cp_all(src, dst string, overwrite bool) ? {
|
|
|
|
source_path := os.real_path(src)
|
|
|
|
dest_path := os.real_path(dst)
|
2019-12-04 23:03:12 +03:00
|
|
|
if !os.exists(source_path) {
|
2019-12-20 00:29:37 +03:00
|
|
|
return error("Source path doesn\'t exist")
|
2019-11-06 23:05:35 +03:00
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
// single file copy
|
2019-11-06 23:05:35 +03:00
|
|
|
if !os.is_dir(source_path) {
|
2020-08-27 12:30:28 +03:00
|
|
|
adjusted_path := if os.is_dir(dest_path) {
|
|
|
|
os.join_path(dest_path,os.file_name(source_path)) } else { dest_path }
|
2020-03-25 19:24:55 +03:00
|
|
|
if os.exists(adjusted_path) {
|
2019-11-17 06:45:20 +03:00
|
|
|
if overwrite {
|
2020-03-25 19:24:55 +03:00
|
|
|
os.rm(adjusted_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
|
|
|
}
|
2020-08-29 02:58:03 +03:00
|
|
|
os.cp(source_path, adjusted_path)?
|
2020-06-29 16:06:26 +03:00
|
|
|
return
|
2019-11-06 23:05:35 +03:00
|
|
|
}
|
|
|
|
if !os.is_dir(dest_path) {
|
|
|
|
return error('Destination path is not a valid directory')
|
|
|
|
}
|
2020-08-29 02:58:03 +03:00
|
|
|
files := os.ls(source_path)?
|
2019-11-06 23:05:35 +03:00
|
|
|
for file in files {
|
2020-03-09 04:23:34 +03:00
|
|
|
sp := os.join_path(source_path, file)
|
|
|
|
dp := os.join_path(dest_path, file)
|
2019-11-06 23:05:35 +03:00
|
|
|
if os.is_dir(sp) {
|
2020-08-29 02:58:03 +03:00
|
|
|
os.mkdir(dp)?
|
2019-11-06 23:05:35 +03:00
|
|
|
}
|
2020-03-02 21:30:04 +03:00
|
|
|
cp_all(sp, dp, overwrite) or {
|
2019-11-06 23:05:35 +03:00
|
|
|
os.rmdir(dp)
|
2020-07-07 13:41:42 +03:00
|
|
|
return error(err)
|
2019-11-06 23:05:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2020-08-27 17:57:01 +03:00
|
|
|
// may be used when you are not sure that the source and target are on the same mount/partition.
|
2020-06-25 13:06:47 +03:00
|
|
|
pub fn mv_by_cp(source string, target string) ? {
|
2020-08-29 02:58:03 +03:00
|
|
|
os.cp(source, target)?
|
|
|
|
os.rm(source)?
|
2019-11-19 12:55:03 +03:00
|
|
|
}
|
|
|
|
|
2020-05-19 13:38:06 +03:00
|
|
|
// vfopen returns an opened C file, given its path and open mode.
|
|
|
|
// NB: os.vfopen is useful for compatibility with C libraries, that expect `FILE *`.
|
|
|
|
// If you write pure V code, os.create or os.open are more convenient.
|
2020-08-28 15:24:00 +03:00
|
|
|
pub fn vfopen(path, mode string) ?&C.FILE {
|
2020-09-13 18:08:58 +03:00
|
|
|
if path.len == 0 {
|
|
|
|
return error('vfopen called with ""')
|
|
|
|
}
|
2020-08-28 15:24:00 +03:00
|
|
|
mut fp := voidptr(0)
|
2019-09-15 15:57:17 +03:00
|
|
|
$if windows {
|
2020-08-28 15:24:00 +03:00
|
|
|
fp = C._wfopen(path.to_wide(), mode.to_wide())
|
2019-09-15 15:57:17 +03:00
|
|
|
} $else {
|
2020-08-28 15:24:00 +03:00
|
|
|
fp = C.fopen(charptr(path.str), charptr(mode.str))
|
|
|
|
}
|
|
|
|
if isnil(fp) {
|
|
|
|
return error('failed to open file "$path"')
|
|
|
|
} else {
|
|
|
|
return fp
|
2019-09-15 15:57:17 +03:00
|
|
|
}
|
2019-11-29 19:14:26 +03:00
|
|
|
}
|
2019-09-15 15:57:17 +03:00
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// fileno returns the file descriptor of an opened C file.
|
2020-05-19 13:38:06 +03:00
|
|
|
pub fn fileno(cfile voidptr) int {
|
|
|
|
$if windows {
|
|
|
|
return C._fileno(cfile)
|
|
|
|
} $else {
|
2020-07-24 00:16:36 +03:00
|
|
|
mut cfile_casted := &C.FILE(0) // FILE* cfile_casted = 0;
|
2020-05-28 12:08:59 +03:00
|
|
|
cfile_casted = cfile
|
|
|
|
// Required on FreeBSD/OpenBSD/NetBSD as stdio.h defines fileno(..) with a macro
|
|
|
|
// that performs a field access on its argument without casting from void*.
|
|
|
|
return C.fileno(cfile_casted)
|
2020-05-19 13:38:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-08-29 02:58:03 +03:00
|
|
|
buf := read_file(path)?
|
2020-01-31 17:59:23 +03:00
|
|
|
return buf.split_into_lines()
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// read_ulines reads the file in `path` into an array of ustring lines.
|
2019-11-19 02:25:55 +03:00
|
|
|
fn read_ulines(path string) ?[]ustring {
|
2020-08-29 02:58:03 +03:00
|
|
|
lines := read_lines(path)?
|
2019-06-22 21:20:28 +03:00
|
|
|
// mut ulines := new_array(0, lines.len, sizeof(ustring))
|
2020-04-26 10:17:13 +03:00
|
|
|
mut ulines := []ustring{}
|
2019-06-22 21:20:28 +03:00
|
|
|
for myline in lines {
|
|
|
|
// ulines[i] = ustr
|
|
|
|
ulines << myline.ustring()
|
|
|
|
}
|
|
|
|
return ulines
|
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// open_append opens `path` file for appending.
|
2019-07-03 22:07:42 +03:00
|
|
|
pub fn open_append(path string) ?File {
|
2019-12-27 19:59:04 +03:00
|
|
|
mut file := File{}
|
2019-07-24 13:16:45 +03:00
|
|
|
$if windows {
|
|
|
|
wpath := path.replace('/', '\\').to_wide()
|
|
|
|
mode := 'ab'
|
2019-12-20 00:29:37 +03:00
|
|
|
file = File{
|
2019-07-24 13:16:45 +03:00
|
|
|
cfile: C._wfopen(wpath, mode.to_wide())
|
|
|
|
}
|
|
|
|
} $else {
|
2019-08-22 23:19:31 +03:00
|
|
|
cpath := path.str
|
2019-12-20 00:29:37 +03:00
|
|
|
file = File{
|
2019-12-01 10:33:26 +03:00
|
|
|
cfile: C.fopen(charptr(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
|
|
|
}
|
2020-08-02 00:07:37 +03:00
|
|
|
file.is_opened = true
|
2019-08-22 23:19:31 +03:00
|
|
|
return file
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// open_file can be used to open or create a file with custom flags and permissions and returns a `File` object.
|
2020-01-21 18:58:47 +03:00
|
|
|
pub fn open_file(path string, mode string, options ...int) ?File {
|
|
|
|
mut flags := 0
|
|
|
|
for m in mode {
|
|
|
|
match m {
|
2020-05-22 18:36:09 +03:00
|
|
|
`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 }
|
|
|
|
`+` { flags |= o_rdwr }
|
2020-01-21 18:58:47 +03:00
|
|
|
else {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-24 01:43:04 +03:00
|
|
|
mut permission := 0o666
|
2020-01-21 18:58:47 +03:00
|
|
|
if options.len > 0 {
|
|
|
|
permission = options[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
$if windows {
|
2020-02-24 01:43:04 +03:00
|
|
|
if permission < 0o600 {
|
2020-01-21 18:58:47 +03:00
|
|
|
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
|
2020-08-02 00:07:37 +03:00
|
|
|
is_opened: true
|
2020-01-21 18:58:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// vpopen system starts the specified command, waits for it to complete, and returns its code.
|
2019-12-20 00:29:37 +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-12-20 00:29:37 +03:00
|
|
|
} $else {
|
2019-07-24 13:16:45 +03:00
|
|
|
cpath := path.str
|
2020-07-14 01:16:31 +03:00
|
|
|
return C.popen(charptr(cpath), 'r')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 02:34:55 +03:00
|
|
|
fn posix_wait4_to_exit_status(waitret int) (int,bool) {
|
|
|
|
$if windows {
|
2019-12-20 00:29:37 +03:00
|
|
|
return waitret,false
|
|
|
|
} $else {
|
2019-10-08 02:34:55 +03:00
|
|
|
mut ret := 0
|
|
|
|
mut is_signaled := true
|
|
|
|
// (see man system, man 2 waitpid: C macro WEXITSTATUS section)
|
2019-12-20 00:29:37 +03:00
|
|
|
if C.WIFEXITED(waitret) {
|
|
|
|
ret = C.WEXITSTATUS(waitret)
|
2019-10-08 02:34:55 +03:00
|
|
|
is_signaled = false
|
2019-12-20 00:29:37 +03:00
|
|
|
}
|
|
|
|
else if C.WIFSIGNALED(waitret) {
|
|
|
|
ret = C.WTERMSIG(waitret)
|
2019-10-08 02:34:55 +03:00
|
|
|
is_signaled = true
|
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
return ret,is_signaled
|
2019-10-08 02:34:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-21 18:58:47 +03: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)
|
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// vpclose will close a file pointer opened with `vpopen`.
|
2019-11-05 18:41:56 +03:00
|
|
|
fn vpclose(f voidptr) int {
|
2019-08-08 08:30:05 +03:00
|
|
|
$if windows {
|
2019-12-07 22:42:13 +03:00
|
|
|
return C._pclose(f)
|
2019-12-20 00:29:37 +03:00
|
|
|
} $else {
|
|
|
|
ret,_ := posix_wait4_to_exit_status(C.pclose(f))
|
2019-10-08 02:34:55 +03:00
|
|
|
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-12-20 00:29:37 +03:00
|
|
|
output string
|
|
|
|
// stderr string // TODO
|
2019-08-22 23:19:31 +03:00
|
|
|
}
|
2020-08-27 17:57:01 +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-12-20 00:29:37 +03: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 15:51:00 +03:00
|
|
|
mut ret := 0
|
2019-08-17 20:21:59 +03:00
|
|
|
$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 }
|
2020-07-20 20:06:41 +03:00
|
|
|
unsafe {
|
|
|
|
ret = C._wsystem(wcmd.to_wide())
|
|
|
|
}
|
2019-08-17 20:21:59 +03:00
|
|
|
} $else {
|
2020-07-23 07:58:44 +03:00
|
|
|
$if ios {
|
2020-07-24 10:17:32 +03:00
|
|
|
unsafe {
|
|
|
|
arg := [ c'/bin/sh', c'-c', byteptr(cmd.str), 0 ]
|
|
|
|
pid := 0
|
|
|
|
ret = C.posix_spawn(&pid, '/bin/sh', 0, 0, arg.data, 0)
|
|
|
|
status := 0
|
|
|
|
ret = C.waitpid(pid, &status, 0)
|
|
|
|
if C.WIFEXITED(status) {
|
|
|
|
ret = C.WEXITSTATUS(status)
|
|
|
|
}
|
|
|
|
}
|
2020-07-23 07:58:44 +03:00
|
|
|
} $else {
|
|
|
|
unsafe {
|
|
|
|
ret = C.system(charptr(cmd.str))
|
|
|
|
}
|
2020-07-20 20:06:41 +03:00
|
|
|
}
|
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 {
|
2019-12-20 00:29:37 +03:00
|
|
|
pret,is_signaled := posix_wait4_to_exit_status(ret)
|
2019-10-08 02:34:55 +03:00
|
|
|
if is_signaled {
|
2019-12-20 00:29:37 +03:00
|
|
|
println('Terminated by signal ${ret:2d} (' + sigint_to_signal_name(pret) + ')')
|
2019-10-08 02:34:55 +03:00
|
|
|
}
|
|
|
|
ret = pret
|
2019-08-17 20:21:59 +03:00
|
|
|
}
|
|
|
|
return ret
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// sigint_to_signal_name will translate `si` signal integer code to it's string code representation.
|
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 {
|
2019-12-20 00:29:37 +03: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 19:59:04 +03:00
|
|
|
else {}
|
|
|
|
}
|
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 {
|
2020-04-24 17:04:39 +03:00
|
|
|
// TODO dependent on platform
|
|
|
|
// works only on x86/ARM/most others
|
|
|
|
10 /*, 30, 16 */ {
|
2019-12-20 00:29:37 +03:00
|
|
|
return 'SIGUSR1'
|
|
|
|
}
|
2020-04-24 17:04:39 +03:00
|
|
|
12 /*, 31, 17 */ {
|
2019-12-20 00:29:37 +03:00
|
|
|
return 'SIGUSR2'
|
|
|
|
}
|
2020-04-24 17:04:39 +03:00
|
|
|
17 /*, 20, 18 */ {
|
2019-12-20 00:29:37 +03:00
|
|
|
return 'SIGCHLD'
|
|
|
|
}
|
2020-04-24 17:04:39 +03:00
|
|
|
18 /*, 19, 25 */ {
|
2019-12-20 00:29:37 +03:00
|
|
|
return 'SIGCONT'
|
|
|
|
}
|
2020-04-24 17:04:39 +03:00
|
|
|
19 /*, 17, 23 */ {
|
2019-12-20 00:29:37 +03:00
|
|
|
return 'SIGSTOP'
|
|
|
|
}
|
2020-04-24 17:04:39 +03:00
|
|
|
20 /*, 18, 24 */ {
|
2019-12-20 00:29:37 +03:00
|
|
|
return 'SIGTSTP'
|
|
|
|
}
|
2020-04-24 17:04:39 +03:00
|
|
|
21 /*, 26 */ {
|
2019-12-20 00:29:37 +03:00
|
|
|
return 'SIGTTIN'
|
|
|
|
}
|
2020-04-24 17:04:39 +03:00
|
|
|
22 /*, 27 */ {
|
2019-12-20 00:29:37 +03:00
|
|
|
return 'SIGTTOU'
|
|
|
|
}
|
|
|
|
// /////////////////////////////
|
|
|
|
5 {
|
|
|
|
return 'SIGTRAP'
|
|
|
|
}
|
|
|
|
7 {
|
|
|
|
return 'SIGBUS'
|
|
|
|
}
|
2019-12-27 19:59:04 +03:00
|
|
|
else {}
|
2020-01-21 18:58:47 +03:00
|
|
|
}
|
2019-10-08 02:34:55 +03:00
|
|
|
}
|
|
|
|
return 'unknown'
|
|
|
|
}
|
|
|
|
|
2020-01-06 11:15:06 +03:00
|
|
|
const (
|
2020-05-22 18:36:09 +03:00
|
|
|
f_ok = 0
|
|
|
|
x_ok = 1
|
|
|
|
w_ok = 2
|
|
|
|
r_ok = 4
|
2020-01-06 11:15:06 +03:00
|
|
|
)
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// exists returns true if `path` (file or directory) exists.
|
2019-12-04 23:03:12 +03:00
|
|
|
pub fn exists(path string) bool {
|
2019-06-26 17:39:40 +03:00
|
|
|
$if windows {
|
2019-12-04 23:03:12 +03:00
|
|
|
p := path.replace('/', '\\')
|
2020-05-22 18:36:09 +03:00
|
|
|
return C._waccess(p.to_wide(), f_ok) != -1
|
2019-07-24 13:16:45 +03:00
|
|
|
} $else {
|
2020-07-14 01:16:31 +03:00
|
|
|
return C.access(charptr(path.str), f_ok) != -1
|
2020-01-06 11:15:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// is_executable returns `true` if `path` is executable.
|
2020-01-06 11:15:06 +03:00
|
|
|
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
|
2020-03-20 18:41:18 +03:00
|
|
|
p := os.real_path( path )
|
2020-01-06 11:15:06 +03:00
|
|
|
return ( os.exists( p ) && p.ends_with('.exe') )
|
|
|
|
}
|
2020-03-21 11:48:02 +03:00
|
|
|
$if solaris {
|
|
|
|
statbuf := C.stat{}
|
2020-08-02 00:07:37 +03:00
|
|
|
unsafe {
|
|
|
|
if C.stat(charptr(path.str), &statbuf) != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2020-05-22 18:36:09 +03:00
|
|
|
return (int(statbuf.st_mode) & ( s_ixusr | s_ixgrp | s_ixoth )) != 0
|
2020-03-21 11:48:02 +03:00
|
|
|
}
|
2020-07-14 01:16:31 +03:00
|
|
|
return C.access(charptr(path.str), x_ok) != -1
|
2020-01-06 11:15:06 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// is_writable returns `true` if `path` is writable.
|
2020-01-06 11:15:06 +03:00
|
|
|
pub fn is_writable(path string) bool {
|
|
|
|
$if windows {
|
|
|
|
p := path.replace('/', '\\')
|
2020-05-22 18:36:09 +03:00
|
|
|
return C._waccess(p.to_wide(), w_ok) != -1
|
2020-01-06 11:15:06 +03:00
|
|
|
} $else {
|
2020-07-14 01:16:31 +03:00
|
|
|
return C.access(charptr(path.str), w_ok) != -1
|
2020-01-06 11:15:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// is_readable returns `true` if `path` is readable.
|
2020-01-06 11:15:06 +03:00
|
|
|
pub fn is_readable(path string) bool {
|
|
|
|
$if windows {
|
|
|
|
p := path.replace('/', '\\')
|
2020-05-22 18:36:09 +03:00
|
|
|
return C._waccess(p.to_wide(), r_ok) != -1
|
2020-01-06 11:15:06 +03:00
|
|
|
} $else {
|
2020-07-14 01:16:31 +03:00
|
|
|
return C.access(charptr(path.str), r_ok) != -1
|
2020-01-06 11:15:06 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-12-04 23:03:12 +03:00
|
|
|
[deprecated]
|
|
|
|
pub fn file_exists(_path string) bool {
|
2020-06-23 17:25:24 +03:00
|
|
|
eprintln('warning: `os.file_exists` has been deprecated, use `os.exists` instead')
|
|
|
|
return exists(_path)
|
2019-12-04 23:03:12 +03:00
|
|
|
}
|
|
|
|
|
2019-07-01 12:01:48 +03:00
|
|
|
// rm removes file in `path`.
|
2020-06-24 15:01:19 +03:00
|
|
|
pub fn rm(path string) ? {
|
2019-07-24 13:16:45 +03:00
|
|
|
$if windows {
|
2020-06-24 15:01:19 +03:00
|
|
|
rc := C._wremove(path.to_wide())
|
|
|
|
if rc == -1 {
|
|
|
|
//TODO: proper error as soon as it's supported on windows
|
|
|
|
return error('Failed to remove "$path"')
|
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
} $else {
|
2020-07-14 01:16:31 +03:00
|
|
|
rc := C.remove(charptr(path.str))
|
2020-06-24 15:01:19 +03:00
|
|
|
if rc == -1 {
|
|
|
|
return error(posix_get_error_msg(C.errno))
|
|
|
|
}
|
2019-07-24 13:16:45 +03:00
|
|
|
}
|
|
|
|
// C.unlink(path.cstr())
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2020-08-27 17:57:01 +03:00
|
|
|
|
2019-07-16 16:55:51 +03:00
|
|
|
// rmdir removes a specified directory.
|
2020-06-26 16:01:10 +03:00
|
|
|
pub fn rmdir(path string) ? {
|
2020-06-26 17:56:34 +03:00
|
|
|
$if windows {
|
2020-06-26 16:01:10 +03:00
|
|
|
rc := C.RemoveDirectory(path.to_wide())
|
2020-06-26 17:24:36 +03:00
|
|
|
if rc == 0 {
|
2020-06-26 17:56:34 +03:00
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-removedirectorya - 0 is failure
|
2020-06-26 16:01:10 +03:00
|
|
|
return error('Failed to remove "$path"')
|
|
|
|
}
|
2020-06-26 17:56:34 +03:00
|
|
|
} $else {
|
2020-07-14 01:16:31 +03:00
|
|
|
rc := C.rmdir(charptr(path.str))
|
2020-06-26 17:56:34 +03:00
|
|
|
if rc == -1 {
|
|
|
|
return error(posix_get_error_msg(C.errno))
|
|
|
|
}
|
2019-06-26 17:39:40 +03:00
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-07-16 16:55:51 +03:00
|
|
|
|
2020-03-03 17:02:50 +03:00
|
|
|
[deprecated]
|
2019-12-30 07:22:28 +03:00
|
|
|
pub fn rmdir_recursive(path string) {
|
2020-06-23 17:25:24 +03:00
|
|
|
eprintln('warning: `os.rmdir_recursive` has been deprecated, use `os.rmdir_all` instead')
|
|
|
|
rmdir_all(path)
|
2020-03-03 17:02:50 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// rmdir_all recursively removes the specified directory.
|
2020-06-26 16:01:10 +03:00
|
|
|
pub fn rmdir_all(path string) ? {
|
|
|
|
mut ret_err := ''
|
2020-08-29 02:58:03 +03:00
|
|
|
items := os.ls(path)?
|
2019-12-30 07:22:28 +03:00
|
|
|
for item in items {
|
2020-03-09 04:23:34 +03:00
|
|
|
if os.is_dir(os.join_path(path, item)) {
|
|
|
|
rmdir_all(os.join_path(path, item))
|
2019-12-30 07:22:28 +03:00
|
|
|
}
|
2020-06-26 16:01:10 +03:00
|
|
|
os.rm(os.join_path(path, item)) or { ret_err = err }
|
|
|
|
}
|
|
|
|
os.rmdir(path) or { ret_err = err }
|
|
|
|
if ret_err.len > 0 {
|
|
|
|
return error(ret_err)
|
2019-12-30 07:22:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// is_dir_empty will return a `bool` whether or not `path` is empty.
|
2019-12-30 07:22:28 +03:00
|
|
|
pub fn is_dir_empty(path string) bool {
|
2020-01-03 21:46:23 +03:00
|
|
|
items := os.ls(path) or {
|
2020-02-21 14:24:13 +03:00
|
|
|
return true
|
2020-01-03 21:46:23 +03:00
|
|
|
}
|
2019-12-30 07:22:28 +03:00
|
|
|
return items.len == 0
|
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// print_c_errno will print the current value of `C.errno`.
|
2019-06-22 21:20:28 +03:00
|
|
|
fn print_c_errno() {
|
2019-12-27 19:59:04 +03:00
|
|
|
e := C.errno
|
|
|
|
se := tos_clone(byteptr(C.strerror(C.errno)))
|
|
|
|
println('errno=$e err=$se')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// file_ext will return the part after the last occurence of `.` in `path`.
|
|
|
|
// The `.` is included.
|
2020-03-26 16:18:08 +03:00
|
|
|
pub fn file_ext(path string) string {
|
2020-03-08 00:26:26 +03:00
|
|
|
pos := path.last_index('.') or {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
return path[pos..]
|
2019-06-26 12:28:06 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// dir will return the part of `path` before the last occurence of a `path_separator`.
|
2019-07-16 02:57:03 +03:00
|
|
|
pub fn dir(path string) string {
|
2020-03-08 00:26:26 +03:00
|
|
|
pos := path.last_index(path_separator) or {
|
|
|
|
return '.'
|
|
|
|
}
|
|
|
|
return path[..pos]
|
2019-06-26 13:56:05 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// base_dir will return the base directory of `path`.
|
|
|
|
// The `path_separator` is included.
|
2020-03-08 17:43:56 +03:00
|
|
|
pub fn base_dir(path string) string {
|
2020-03-13 14:22:36 +03:00
|
|
|
posx := path.last_index(path_separator) or {
|
2020-08-03 18:19:36 +03:00
|
|
|
return path.clone()
|
2020-03-08 00:26:26 +03:00
|
|
|
}
|
|
|
|
// NB: *without* terminating /
|
2020-03-13 14:22:36 +03:00
|
|
|
return path[..posx]
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// file_name will return all characters found after the last occurence of `path_separator`.
|
|
|
|
// file extension is included.
|
2020-03-19 17:49:07 +03:00
|
|
|
pub fn file_name(path string) string {
|
2020-05-20 12:04:28 +03:00
|
|
|
return path.all_after_last(path_separator)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// input returns a one-line string from stdin, after printing a prompt.
|
2020-04-25 22:03:51 +03:00
|
|
|
pub fn input(prompt string) string {
|
|
|
|
print(prompt)
|
|
|
|
flush()
|
|
|
|
return get_line()
|
|
|
|
}
|
|
|
|
|
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-12-20 00:29:37 +03:00
|
|
|
str := get_raw_line()
|
2019-09-15 19:07:40 +03:00
|
|
|
$if windows {
|
|
|
|
return str.trim_right('\r\n')
|
2019-12-20 00:29:37 +03:00
|
|
|
} $else {
|
2019-09-15 19:07:40 +03:00
|
|
|
return str.trim_right('\n')
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// get_raw_line returns a one-line string from stdin along with '\n' if there is any.
|
2019-06-28 00:51:25 +03:00
|
|
|
pub fn get_raw_line() string {
|
2019-12-20 00:29:37 +03:00
|
|
|
$if windows {
|
2020-02-22 14:41:24 +03:00
|
|
|
unsafe {
|
|
|
|
max_line_chars := 256
|
|
|
|
buf := malloc(max_line_chars * 2)
|
2020-05-22 18:36:09 +03:00
|
|
|
h_input := C.GetStdHandle(std_input_handle)
|
2020-02-22 14:41:24 +03:00
|
|
|
mut bytes_read := 0
|
|
|
|
if is_atty(0) > 0 {
|
2020-09-16 17:40:02 +03:00
|
|
|
C.ReadConsole(h_input, buf, max_line_chars * 2, C.LPDWORD(&bytes_read), 0)
|
2020-02-22 14:41:24 +03:00
|
|
|
return string_from_wide2(&u16(buf), bytes_read)
|
2020-01-10 16:06:40 +03:00
|
|
|
}
|
2020-02-22 14:41:24 +03:00
|
|
|
mut offset := 0
|
|
|
|
for {
|
|
|
|
pos := buf + offset
|
2020-09-16 17:40:02 +03:00
|
|
|
res := C.ReadFile(h_input, pos, 1, C.LPDWORD(&bytes_read), 0)
|
2020-02-22 14:41:24 +03:00
|
|
|
if !res || bytes_read == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if *pos == `\n` || *pos == `\r` {
|
|
|
|
offset++
|
|
|
|
break
|
|
|
|
}
|
2020-01-10 16:06:40 +03:00
|
|
|
offset++
|
|
|
|
}
|
2020-08-10 19:05:26 +03:00
|
|
|
return buf.vstring_with_len(offset)
|
2019-12-20 00:29:37 +03:00
|
|
|
}
|
|
|
|
} $else {
|
2020-02-05 13:23:49 +03:00
|
|
|
max := size_t(0)
|
2020-02-28 16:05:20 +03:00
|
|
|
mut buf := charptr(0)
|
2020-04-03 11:52:48 +03:00
|
|
|
nr_chars := C.getline(&buf, &max, C.stdin)
|
2020-02-28 16:05:20 +03:00
|
|
|
//defer { unsafe{ free(buf) } }
|
2020-02-05 13:23:49 +03:00
|
|
|
if nr_chars == 0 || nr_chars == -1 {
|
2019-12-20 00:29:37 +03:00
|
|
|
return ''
|
|
|
|
}
|
2020-02-28 16:05:20 +03:00
|
|
|
return tos3(buf)
|
|
|
|
//res := tos_clone(buf)
|
|
|
|
//return res
|
2019-12-20 00:29:37 +03:00
|
|
|
}
|
2019-06-28 00:51:25 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// get_raw_stdin will get the raw input from stdin.
|
2020-05-29 04:06:27 +03:00
|
|
|
pub fn get_raw_stdin() []byte {
|
|
|
|
$if windows {
|
|
|
|
unsafe {
|
|
|
|
block_bytes := 512
|
|
|
|
mut buf := malloc(block_bytes)
|
|
|
|
h_input := C.GetStdHandle(std_input_handle)
|
|
|
|
mut bytes_read := 0
|
|
|
|
mut offset := 0
|
|
|
|
for {
|
|
|
|
pos := buf + offset
|
2020-09-16 17:40:02 +03:00
|
|
|
res := C.ReadFile(h_input, pos, block_bytes, C.LPDWORD(&bytes_read), 0)
|
2020-05-31 13:57:26 +03:00
|
|
|
offset += bytes_read
|
|
|
|
|
2020-05-29 04:06:27 +03:00
|
|
|
if !res {
|
|
|
|
break
|
|
|
|
}
|
2020-05-31 13:57:26 +03:00
|
|
|
|
2020-07-11 13:45:22 +03:00
|
|
|
buf = v_realloc(buf, u32(offset + block_bytes + (block_bytes-bytes_read)))
|
2020-05-29 04:06:27 +03:00
|
|
|
}
|
2020-05-31 13:57:26 +03:00
|
|
|
|
|
|
|
C.CloseHandle(h_input)
|
|
|
|
|
2020-05-29 04:06:27 +03:00
|
|
|
return array{element_size: 1 data: voidptr(buf) len: offset cap: offset }
|
|
|
|
}
|
|
|
|
} $else {
|
|
|
|
panic('get_raw_stdin not implemented on this platform...')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// get_lines returns an array of strings read from from stdin.
|
|
|
|
// reading is stopped when an empty line is read.
|
2019-07-24 19:14:13 +03:00
|
|
|
pub fn get_lines() []string {
|
2019-12-20 00:29:37 +03:00
|
|
|
mut line := ''
|
2020-04-26 10:17:13 +03:00
|
|
|
mut inputstr := []string{}
|
2019-12-20 00:29:37 +03:00
|
|
|
for {
|
|
|
|
line = get_line()
|
2020-03-27 16:57:19 +03:00
|
|
|
if line.len <= 0 {
|
2019-12-20 00:29:37 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
line = line.trim_space()
|
|
|
|
inputstr << line
|
|
|
|
}
|
|
|
|
return inputstr
|
2019-07-24 19:14:13 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// get_lines_joined returns a string of the values read from from stdin.
|
|
|
|
// reading is stopped when an empty line is read.
|
2019-07-24 19:14:13 +03:00
|
|
|
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'
|
|
|
|
}
|
2019-12-03 16:29:24 +03:00
|
|
|
$if macos {
|
2019-06-22 21:20:28 +03:00
|
|
|
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-12-20 00:29:37 +03:00
|
|
|
$if android {
|
2019-09-16 18:23:11 +03:00
|
|
|
return 'android'
|
|
|
|
}
|
2019-09-27 00:30:41 +03:00
|
|
|
$if solaris {
|
|
|
|
return 'solaris'
|
|
|
|
}
|
2019-12-03 11:26:47 +03:00
|
|
|
$if haiku {
|
|
|
|
return 'haiku'
|
|
|
|
}
|
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 {
|
|
|
|
$if windows {
|
2020-03-08 00:26:26 +03:00
|
|
|
return os.getenv('USERPROFILE') + os.path_separator
|
2020-02-18 04:12:10 +03:00
|
|
|
} $else {
|
2020-03-22 12:12:43 +03:00
|
|
|
//println('home_dir() call')
|
|
|
|
//res:= os.getenv('HOME') + os.path_separator
|
|
|
|
//println('res="$res"')
|
2020-03-08 00:26:26 +03:00
|
|
|
return os.getenv('HOME') + os.path_separator
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-22 23:19:31 +03:00
|
|
|
// write_file writes `text` data to a file in `path`.
|
2020-06-29 16:06:26 +03:00
|
|
|
pub fn write_file(path, text string) ? {
|
2020-08-29 02:58:03 +03:00
|
|
|
mut f := os.create(path)?
|
2019-07-02 00:24:19 +03:00
|
|
|
f.write(text)
|
2019-06-22 21:20:28 +03:00
|
|
|
f.close()
|
|
|
|
}
|
|
|
|
|
2020-07-13 10:45:10 +03:00
|
|
|
// write_file_array writes the data in `buffer` to a file in `path`.
|
|
|
|
pub fn write_file_array(path string, buffer array) ? {
|
2020-08-29 02:58:03 +03:00
|
|
|
mut f := os.create(path)?
|
2020-07-13 10:45:10 +03:00
|
|
|
f.write_bytes_at(buffer.data, (buffer.len * buffer.element_size), 0)
|
|
|
|
f.close()
|
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// read_file_array reads an array of `T` values from file `path`.
|
2020-07-13 10:45:10 +03:00
|
|
|
pub fn read_file_array<T>(path string) []T {
|
|
|
|
a := T{}
|
2020-07-24 00:16:36 +03:00
|
|
|
tsize := int(sizeof(a))
|
2020-07-13 10:45:10 +03:00
|
|
|
// prepare for reading, get current file size
|
2020-08-28 15:24:00 +03:00
|
|
|
mut fp := vfopen(path, 'rb') or { return array{} }
|
2020-07-13 10:45:10 +03:00
|
|
|
C.fseek(fp, 0, C.SEEK_END)
|
|
|
|
fsize := C.ftell(fp)
|
|
|
|
C.rewind(fp)
|
|
|
|
// read the actual data from the file
|
|
|
|
len := fsize / tsize
|
|
|
|
buf := malloc(fsize)
|
|
|
|
C.fread(buf, fsize, 1, fp)
|
2020-07-24 00:16:36 +03:00
|
|
|
C.fclose(fp)
|
2020-07-13 10:45:10 +03:00
|
|
|
return array{element_size: tsize data: buf len: len cap: len }
|
|
|
|
}
|
|
|
|
|
2019-12-03 13:08:57 +03:00
|
|
|
pub 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
|
|
|
}
|
2019-12-03 16:29:24 +03:00
|
|
|
$if macos {
|
2020-03-22 16:47:43 +03:00
|
|
|
C.printf("TODO")
|
|
|
|
/*
|
2019-12-27 19:59:04 +03:00
|
|
|
mut sa := C.sigaction{}
|
2020-03-22 16:47:43 +03:00
|
|
|
C.memset(&sa, 0, sizeof(C.sigaction_size))
|
2019-06-27 18:48:49 +03:00
|
|
|
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)
|
2020-03-22 16:47:43 +03:00
|
|
|
*/
|
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-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 {
|
2020-05-22 18:36:09 +03:00
|
|
|
mut result := vcalloc(max_path_len)
|
2020-07-14 01:16:31 +03:00
|
|
|
count := C.readlink('/proc/self/exe', charptr(result), max_path_len)
|
2019-07-16 17:37:59 +03:00
|
|
|
if count < 0 {
|
2019-11-28 11:46:52 +03:00
|
|
|
eprintln('os.executable() failed at reading /proc/self/exe to get exe path')
|
2020-03-14 00:19:02 +03:00
|
|
|
return executable_fallback()
|
2019-06-27 23:17:13 +03:00
|
|
|
}
|
2020-08-10 19:05:26 +03:00
|
|
|
return unsafe { result.vstring() }
|
2019-06-27 23:17:13 +03:00
|
|
|
}
|
|
|
|
$if windows {
|
2019-07-24 19:25:18 +03:00
|
|
|
max := 512
|
2020-07-16 19:33:26 +03:00
|
|
|
size := max * 2 // max_path_len * sizeof(wchar_t)
|
|
|
|
mut result := &u16(vcalloc(size))
|
2019-12-20 00:29:37 +03:00
|
|
|
len := C.GetModuleFileName(0, result, max)
|
2020-07-16 19:33:26 +03:00
|
|
|
// determine if the file is a windows symlink
|
|
|
|
attrs := C.GetFileAttributesW(result)
|
|
|
|
is_set := attrs & 0x400 // FILE_ATTRIBUTE_REPARSE_POINT
|
|
|
|
if is_set != 0 { // it's a windows symlink
|
|
|
|
// gets handle with GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0
|
|
|
|
file := C.CreateFile(result, 0x80000000, 1, 0, 3, 0x80, 0)
|
|
|
|
if file != -1 {
|
|
|
|
final_path := &u16(vcalloc(size))
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfinalpathnamebyhandlew
|
|
|
|
final_len := C.GetFinalPathNameByHandleW(file, final_path, size, 0)
|
|
|
|
if final_len < size {
|
|
|
|
ret := string_from_wide2(final_path, final_len)
|
|
|
|
// remove '\\?\' from beginning (see link above)
|
|
|
|
return ret[4..]
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
eprintln('os.executable() saw that the executable file path was too long')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
C.CloseHandle(file)
|
|
|
|
}
|
2019-07-24 13:16:45 +03:00
|
|
|
return string_from_wide2(result, len)
|
2019-06-27 23:17:13 +03:00
|
|
|
}
|
2019-12-03 16:29:24 +03:00
|
|
|
$if macos {
|
2020-05-22 18:36:09 +03:00
|
|
|
mut result := vcalloc(max_path_len)
|
2019-08-22 23:19:31 +03:00
|
|
|
pid := C.getpid()
|
2020-05-22 18:36:09 +03:00
|
|
|
ret := proc_pidpath(pid, result, max_path_len)
|
2019-12-20 00:29:37 +03:00
|
|
|
if ret <= 0 {
|
2019-11-28 11:46:52 +03:00
|
|
|
eprintln('os.executable() failed at calling proc_pidpath with pid: $pid . proc_pidpath returned $ret ')
|
2020-03-14 00:19:02 +03:00
|
|
|
return executable_fallback()
|
2019-08-22 23:19:31 +03:00
|
|
|
}
|
2020-08-10 19:05:26 +03:00
|
|
|
return unsafe { result.vstring() }
|
2019-06-27 23:17:13 +03:00
|
|
|
}
|
2019-07-16 17:19:52 +03:00
|
|
|
$if freebsd {
|
2020-05-22 18:36:09 +03:00
|
|
|
mut result := vcalloc(max_path_len)
|
2019-12-20 00:29:37 +03:00
|
|
|
mib := [1/* CTL_KERN */, 14/* KERN_PROC */, 12/* KERN_PROC_PATHNAME */, -1]
|
2020-05-22 18:36:09 +03:00
|
|
|
size := max_path_len
|
2020-08-02 00:07:37 +03:00
|
|
|
unsafe {
|
|
|
|
C.sysctl(mib.data, 4, result, &size, 0, 0)
|
|
|
|
}
|
2020-08-10 19:05:26 +03:00
|
|
|
return unsafe { result.vstring() }
|
2019-08-22 23:19:31 +03:00
|
|
|
}
|
2020-03-14 00:19:02 +03:00
|
|
|
// "Sadly there is no way to get the full path of the executed file in OpenBSD."
|
|
|
|
$if openbsd {}
|
2019-12-27 19:59:04 +03:00
|
|
|
$if solaris {}
|
|
|
|
$if haiku {}
|
2019-07-16 17:37:59 +03:00
|
|
|
$if netbsd {
|
2020-05-22 18:36:09 +03:00
|
|
|
mut result := vcalloc(max_path_len)
|
2020-07-14 01:16:31 +03:00
|
|
|
count := C.readlink('/proc/curproc/exe', charptr(result), max_path_len)
|
2019-07-16 17:37:59 +03:00
|
|
|
if count < 0 {
|
2019-11-28 11:46:52 +03:00
|
|
|
eprintln('os.executable() failed at reading /proc/curproc/exe to get exe path')
|
2020-03-14 00:19:02 +03:00
|
|
|
return executable_fallback()
|
2019-07-16 17:37:59 +03:00
|
|
|
}
|
2020-09-09 19:37:38 +03:00
|
|
|
return unsafe { result.vstring_with_len(count) }
|
2019-08-22 23:19:31 +03:00
|
|
|
}
|
2019-07-16 17:37:59 +03:00
|
|
|
$if dragonfly {
|
2020-05-22 18:36:09 +03:00
|
|
|
mut result := vcalloc(max_path_len)
|
2020-07-14 01:16:31 +03:00
|
|
|
count := C.readlink('/proc/curproc/file', charptr(result), max_path_len)
|
2019-07-16 17:37:59 +03:00
|
|
|
if count < 0 {
|
2019-11-28 11:46:52 +03:00
|
|
|
eprintln('os.executable() failed at reading /proc/curproc/file to get exe path')
|
2020-03-14 00:19:02 +03:00
|
|
|
return executable_fallback()
|
2019-07-16 17:37:59 +03:00
|
|
|
}
|
2020-08-10 19:05:26 +03:00
|
|
|
return unsafe { result.vstring_with_len(count) }
|
2019-08-22 23:19:31 +03:00
|
|
|
}
|
2020-03-14 00:19:02 +03:00
|
|
|
return executable_fallback()
|
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// executable_fallback is used when there is not a more platform specific and accurate implementation.
|
|
|
|
// It relies on path manipulation of os.args[0] and os.wd_at_startup, so it may not work properly in
|
2020-03-14 00:19:02 +03:00
|
|
|
// all cases, but it should be better, than just using os.args[0] directly.
|
|
|
|
fn executable_fallback() string {
|
2020-04-23 21:52:13 +03:00
|
|
|
if os.args.len == 0 {
|
|
|
|
// we are early in the bootstrap, os.args has not been initialized yet :-|
|
|
|
|
return ''
|
|
|
|
}
|
2020-03-14 00:19:02 +03:00
|
|
|
mut exepath := os.args[0]
|
2020-07-16 19:33:26 +03:00
|
|
|
$if windows {
|
|
|
|
if !exepath.contains('.exe') {
|
|
|
|
exepath += '.exe'
|
|
|
|
}
|
|
|
|
}
|
2020-03-14 00:19:02 +03:00
|
|
|
if !os.is_abs_path(exepath) {
|
|
|
|
if exepath.contains( os.path_separator ) {
|
|
|
|
exepath = os.join_path(os.wd_at_startup, exepath)
|
|
|
|
}else{
|
|
|
|
// no choice but to try to walk the PATH folders :-| ...
|
|
|
|
foundpath := os.find_abs_path_of_executable(exepath) or { '' }
|
|
|
|
if foundpath.len > 0 {
|
|
|
|
exepath = foundpath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-20 18:41:18 +03:00
|
|
|
exepath = os.real_path(exepath)
|
2020-03-14 00:19:02 +03:00
|
|
|
return exepath
|
|
|
|
}
|
|
|
|
|
|
|
|
// find_exe_path walks the environment PATH, just like most shell do, it returns
|
|
|
|
// the absolute path of the executable if found
|
|
|
|
pub fn find_abs_path_of_executable(exepath string) ?string {
|
|
|
|
if os.is_abs_path(exepath) {
|
2020-08-20 19:23:12 +03:00
|
|
|
return os.real_path(exepath)
|
2020-03-14 00:19:02 +03:00
|
|
|
}
|
|
|
|
mut res := ''
|
|
|
|
env_path_delimiter := if os.user_os() == 'windows' { ';' } else { ':' }
|
|
|
|
paths := os.getenv('PATH').split(env_path_delimiter)
|
|
|
|
for p in paths {
|
|
|
|
found_abs_path := os.join_path( p, exepath )
|
|
|
|
if os.exists( found_abs_path ) && os.is_executable( found_abs_path ) {
|
|
|
|
res = found_abs_path
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if res.len>0 {
|
2020-08-20 19:23:12 +03:00
|
|
|
return os.real_path(res)
|
2020-03-14 00:19:02 +03:00
|
|
|
}
|
|
|
|
return error('failed to find executable')
|
2019-06-27 23:17:13 +03:00
|
|
|
}
|
2019-06-30 15:55:48 +03:00
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// exists_in_system_path returns `true` if `prog` exists in the system's PATH
|
2020-05-16 23:46:05 +03:00
|
|
|
pub fn exists_in_system_path(prog string) bool {
|
2020-05-16 18:10:34 +03:00
|
|
|
os.find_abs_path_of_executable(prog) or {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-12-04 23:03:12 +03:00
|
|
|
[deprecated]
|
|
|
|
pub fn dir_exists(path string) bool {
|
2020-06-23 17:25:24 +03:00
|
|
|
eprintln('warning: `os.dir_exists` has been deprecated, use `os.is_dir` instead')
|
|
|
|
return is_dir(path)
|
2019-12-04 23:03:12 +03:00
|
|
|
}
|
2020-02-21 01:00:02 +03:00
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// is_dir returns a `bool` 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 {
|
2020-05-16 17:12:23 +03:00
|
|
|
w_path := path.replace('/', '\\')
|
|
|
|
attr := C.GetFileAttributesW(w_path.to_wide())
|
2019-12-05 11:25:55 +03:00
|
|
|
if attr == u32(C.INVALID_FILE_ATTRIBUTES) {
|
2019-12-04 23:03:12 +03:00
|
|
|
return false
|
|
|
|
}
|
2019-12-04 23:53:11 +03:00
|
|
|
if int(attr) & C.FILE_ATTRIBUTE_DIRECTORY != 0 {
|
2019-12-04 23:03:12 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2019-12-20 00:29:37 +03:00
|
|
|
} $else {
|
2019-12-27 19:59:04 +03:00
|
|
|
statbuf := C.stat{}
|
2020-07-20 20:06:41 +03:00
|
|
|
if unsafe {C.stat(charptr(path.str), &statbuf)} != 0 {
|
2019-06-30 15:55:48 +03:00
|
|
|
return false
|
|
|
|
}
|
2019-07-31 11:32:00 +03:00
|
|
|
// ref: https://code.woboq.org/gcc/include/sys/stat.h.html
|
2020-05-22 18:36:09 +03:00
|
|
|
val:= int(statbuf.st_mode) & os.s_ifmt
|
|
|
|
return val == s_ifdir
|
2019-08-22 23:19:31 +03:00
|
|
|
}
|
2019-06-30 15:55:48 +03:00
|
|
|
}
|
|
|
|
|
2020-09-04 23:27:52 +03:00
|
|
|
// is_file returns a `bool` indicating whether the given `path` is a file.
|
|
|
|
pub fn is_file(path string) bool {
|
|
|
|
return exists(path) && !is_dir(path)
|
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// is_link returns a boolean indicating whether `path` is a link.
|
2019-12-04 23:53:11 +03:00
|
|
|
pub fn is_link(path string) bool {
|
2019-12-05 00:14:23 +03:00
|
|
|
$if windows {
|
|
|
|
return false // TODO
|
|
|
|
} $else {
|
2019-12-27 19:59:04 +03:00
|
|
|
statbuf := C.stat{}
|
2020-07-14 01:16:31 +03:00
|
|
|
if C.lstat(charptr(path.str), &statbuf) != 0 {
|
2019-12-05 00:14:23 +03:00
|
|
|
return false
|
|
|
|
}
|
2020-05-22 18:36:09 +03:00
|
|
|
return int(statbuf.st_mode) & s_ifmt == s_iflnk
|
2019-12-04 23:53:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// chdir changes the current working directory to the new directory in `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-12-20 00:29:37 +03:00
|
|
|
} $else {
|
2020-07-14 01:16:31 +03:00
|
|
|
C.chdir(charptr(path.str))
|
2019-08-22 23:19:31 +03:00
|
|
|
}
|
2019-06-30 15:55:48 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// getwd returns the absolute path of the current directory.
|
2019-11-29 19:14:26 +03:00
|
|
|
pub fn getwd() string {
|
2019-06-30 15:55:48 +03:00
|
|
|
$if windows {
|
2020-05-22 18:36:09 +03:00
|
|
|
max := 512 // max_path_len * sizeof(wchar_t)
|
2020-03-04 19:08:28 +03:00
|
|
|
buf := &u16(vcalloc(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-12-20 00:29:37 +03:00
|
|
|
} $else {
|
2020-03-04 19:08:28 +03:00
|
|
|
buf := vcalloc(512)
|
2020-07-14 01:16:31 +03:00
|
|
|
if C.getcwd(charptr(buf), 512) == 0 {
|
2019-06-30 15:55:48 +03:00
|
|
|
return ''
|
|
|
|
}
|
2020-08-10 19:05:26 +03:00
|
|
|
return unsafe { buf.vstring() }
|
2019-07-24 13:16:45 +03:00
|
|
|
}
|
2019-06-30 15:55:48 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// real_path returns the full absolute path for fpath, with all relative ../../, symlinks and so on resolved.
|
2019-08-22 14:15:11 +03:00
|
|
|
// See http://pubs.opengroup.org/onlinepubs/9699919799/functions/realpath.html
|
|
|
|
// Also https://insanecoding.blogspot.com/2007/11/pathmax-simply-isnt.html
|
2019-12-20 00:29:37 +03:00
|
|
|
// and https://insanecoding.blogspot.com/2007/11/implementing-realpath-in-c.html
|
2019-08-22 14:15:11 +03:00
|
|
|
// NB: this particular rabbit hole is *deep* ...
|
2020-03-20 18:41:18 +03:00
|
|
|
pub fn real_path(fpath string) string {
|
2020-05-22 18:36:09 +03:00
|
|
|
mut fullpath := vcalloc(max_path_len)
|
2019-12-01 10:33:26 +03:00
|
|
|
mut ret := charptr(0)
|
2019-08-22 14:15:11 +03:00
|
|
|
$if windows {
|
2020-05-27 06:42:48 +03:00
|
|
|
ret = charptr(C._fullpath(fullpath, fpath.str, max_path_len))
|
2019-11-23 20:56:22 +03:00
|
|
|
if ret == 0 {
|
|
|
|
return fpath
|
2019-11-29 19:14:26 +03:00
|
|
|
}
|
2019-11-28 11:46:52 +03:00
|
|
|
} $else {
|
2020-07-14 01:16:31 +03:00
|
|
|
ret = charptr(C.realpath(charptr(fpath.str), charptr(fullpath)))
|
2019-11-15 01:07:38 +03:00
|
|
|
if ret == 0 {
|
|
|
|
return fpath
|
2019-11-29 19:14:26 +03:00
|
|
|
}
|
2019-08-22 14:15:11 +03:00
|
|
|
}
|
2020-08-10 19:05:26 +03:00
|
|
|
return unsafe { fullpath.vstring() }
|
2019-08-22 14:15:11 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// is_abs_path returns `true` if `path` is absolute.
|
2020-03-10 18:09:37 +03:00
|
|
|
pub fn is_abs_path(path string) bool {
|
2020-03-08 00:26:26 +03:00
|
|
|
$if windows {
|
|
|
|
return path[0] == `/` || // incase we're in MingGW bash
|
|
|
|
(path[0].is_letter() && path[1] == `:`)
|
|
|
|
}
|
|
|
|
return path[0] == `/`
|
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// join_path returns a path as string from input string parameter(s).
|
2020-03-09 04:23:34 +03:00
|
|
|
pub fn join_path(base string, dirs ...string) string {
|
2020-04-26 10:17:13 +03:00
|
|
|
mut result := []string{}
|
2020-03-08 00:26:26 +03:00
|
|
|
result << base.trim_right('\\/')
|
|
|
|
for d in dirs {
|
|
|
|
result << d
|
|
|
|
}
|
|
|
|
return result.join(path_separator)
|
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// walk_ext returns a recursive list of all files in `path` 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-12-22 01:41:42 +03:00
|
|
|
mut files := os.ls(path) or {
|
2020-02-21 22:14:01 +03:00
|
|
|
return []
|
2019-12-20 00:29:37 +03:00
|
|
|
}
|
2020-04-26 10:17:13 +03:00
|
|
|
mut res := []string{}
|
2020-03-08 00:26:26 +03:00
|
|
|
separator := if path.ends_with(os.path_separator) { '' } else { os.path_separator }
|
2020-04-27 16:16:31 +03:00
|
|
|
for file in files {
|
2019-08-16 15:05:11 +03:00
|
|
|
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-12-30 07:23:54 +03:00
|
|
|
if os.is_dir(p) && !os.is_link(p) {
|
2019-08-22 23:19:31 +03:00
|
|
|
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
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// walk recursively traverses the given directory `path`.
|
2019-10-24 15:17:09 +03:00
|
|
|
// When a file is encountred it will call the callback function with current file as argument.
|
2020-02-11 12:26:46 +03:00
|
|
|
pub fn walk(path string, f fn(path string)) {
|
2019-10-24 15:17:09 +03:00
|
|
|
if !os.is_dir(path) {
|
|
|
|
return
|
|
|
|
}
|
2019-12-22 01:41:42 +03:00
|
|
|
mut files := os.ls(path) or {
|
2020-02-21 22:14:01 +03:00
|
|
|
return
|
2019-12-20 00:29:37 +03:00
|
|
|
}
|
2019-10-24 15:17:09 +03:00
|
|
|
for file in files {
|
2020-03-08 00:26:26 +03:00
|
|
|
p := path + os.path_separator + file
|
2019-12-30 07:23:54 +03:00
|
|
|
if os.is_dir(p) && !os.is_link(p) {
|
2020-02-11 12:26:46 +03:00
|
|
|
walk(p, f)
|
2019-10-24 15:17:09 +03:00
|
|
|
}
|
2019-12-04 23:03:12 +03:00
|
|
|
else if os.exists(p) {
|
2020-02-11 12:26:46 +03:00
|
|
|
f(p)
|
2019-10-24 15:17:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
[unsafe] // signal will assign `handler` callback to be called when `signum` signal is recieved.
|
2019-07-02 23:13:23 +03:00
|
|
|
pub fn signal(signum int, handler voidptr) {
|
2020-07-20 20:06:41 +03:00
|
|
|
unsafe {
|
|
|
|
C.signal(signum, handler)
|
|
|
|
}
|
2019-07-02 23:13:23 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// fork will fork the current system process and return the pid of the fork.
|
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
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// wait blocks the calling process until one of its child processes exits or a signal is received.
|
|
|
|
// After child process terminates, parent continues its execution after wait system call instruction.
|
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)
|
|
|
|
}
|
2020-02-10 20:48:01 +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
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// file_last_mod_unix returns the "last modified" time stamp of file in `path`.
|
2019-07-07 22:46:21 +03:00
|
|
|
pub fn file_last_mod_unix(path string) int {
|
2019-12-27 19:59:04 +03:00
|
|
|
attr := C.stat{}
|
2019-12-20 00:29:37 +03:00
|
|
|
// # struct stat attr;
|
2020-07-20 20:06:41 +03:00
|
|
|
unsafe {
|
|
|
|
C.stat(charptr(path.str), &attr)
|
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
// # stat(path.str, &attr);
|
2019-08-22 23:19:31 +03:00
|
|
|
return attr.st_mtime
|
2019-12-20 00:29:37 +03:00
|
|
|
// # return attr.st_mtime ;
|
2019-07-07 22:46:21 +03:00
|
|
|
}
|
2019-08-22 23:19:31 +03:00
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// log will print "os.log: "+`s` ...
|
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
|
|
|
}
|
|
|
|
|
2020-03-03 02:00:30 +03:00
|
|
|
[deprecated]
|
2019-07-24 17:15:21 +03:00
|
|
|
pub fn flush_stdout() {
|
2020-06-23 17:25:24 +03:00
|
|
|
eprintln('warning: `os.flush_stdout` has been deprecated, use `os.flush` instead')
|
|
|
|
flush()
|
2020-03-03 02:00:30 +03:00
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// flush will flush the stdout buffer.
|
2020-03-03 02:00:30 +03:00
|
|
|
pub fn flush() {
|
2020-04-03 11:52:48 +03:00
|
|
|
C.fflush(C.stdout)
|
2019-08-22 23:19:31 +03:00
|
|
|
}
|
2019-07-24 17:15:21 +03:00
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// mkdir_all will create a valid full path of all directories given in `path`.
|
2020-07-07 13:41:42 +03:00
|
|
|
pub fn mkdir_all(path string) ? {
|
2020-03-08 00:26:26 +03:00
|
|
|
mut p := if path.starts_with(os.path_separator) { os.path_separator } else { '' }
|
2020-07-06 15:08:38 +03:00
|
|
|
path_parts := path.trim_left(os.path_separator).split(os.path_separator)
|
|
|
|
for subdir in path_parts {
|
2020-03-08 00:26:26 +03:00
|
|
|
p += subdir + os.path_separator
|
2020-07-06 15:08:38 +03:00
|
|
|
if os.exists(p) && os.is_dir(p) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
os.mkdir(p) or {
|
2020-07-07 13:41:42 +03:00
|
|
|
return error('folder: $p, error: $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
|
|
|
|
2020-03-08 17:57:47 +03:00
|
|
|
// cache_dir returns the path to a *writable* user specific folder, suitable for writing non-essential data.
|
|
|
|
pub fn cache_dir() string {
|
2020-01-03 21:46:23 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// temp_dir returns the path to a folder, that is suitable for storing temporary files.
|
2020-03-10 17:02:09 +03:00
|
|
|
pub fn temp_dir() string {
|
2019-11-17 06:45:20 +03:00
|
|
|
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-20 00:29:37 +03:00
|
|
|
if path == '' {
|
|
|
|
path = os.getenv('TMP')
|
|
|
|
}
|
|
|
|
if path == '' {
|
|
|
|
path = 'C:/tmp'
|
|
|
|
}
|
2019-11-17 06:45:20 +03:00
|
|
|
}
|
|
|
|
}
|
2020-08-05 20:02:57 +03:00
|
|
|
$if android {
|
|
|
|
// TODO test+use '/data/local/tmp' on Android before using cache_dir()
|
|
|
|
if path == '' {
|
|
|
|
path = os.cache_dir()
|
|
|
|
}
|
2020-01-03 21:46:23 +03:00
|
|
|
}
|
2019-12-30 07:25:26 +03:00
|
|
|
if path == '' {
|
|
|
|
path = '/tmp'
|
|
|
|
}
|
2019-11-17 06:45:20 +03:00
|
|
|
return path
|
|
|
|
}
|
2019-11-24 06:27:02 +03:00
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// chmod change file access attributes of `path` to `mode`.
|
|
|
|
// Octals like `0o600` can be used.
|
2019-11-24 06:27:02 +03:00
|
|
|
pub fn chmod(path string, mode int) {
|
2020-07-14 01:16:31 +03:00
|
|
|
C.chmod(charptr(path.str), mode)
|
2019-11-29 19:14:26 +03:00
|
|
|
}
|
2019-12-08 20:21:17 +03:00
|
|
|
|
|
|
|
pub const (
|
2019-12-20 00:29:37 +03:00
|
|
|
wd_at_startup = getwd()
|
2019-12-08 20:21:17 +03:00
|
|
|
)
|
2020-01-12 21:35:06 +03:00
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// resource_abs_path returns an absolute path, for the given `path`.
|
2020-01-12 21:35:06 +03: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 {
|
2020-03-20 18:41:18 +03:00
|
|
|
mut base_path := os.real_path(os.dir(os.executable()))
|
2020-01-12 21:35:06 +03:00
|
|
|
vresource := os.getenv('V_RESOURCE_PATH')
|
|
|
|
if vresource.len != 0 {
|
|
|
|
base_path = vresource
|
|
|
|
}
|
2020-03-20 18:41:18 +03:00
|
|
|
return os.real_path(os.join_path(base_path, path))
|
2020-01-12 21:35:06 +03:00
|
|
|
}
|
2020-05-19 13:38:06 +03:00
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// open tries to open a file for reading and returns back a read-only `File` object.
|
2020-05-19 13:38:06 +03:00
|
|
|
pub fn open(path string) ?File {
|
|
|
|
/*
|
|
|
|
$if linux {
|
|
|
|
$if !android {
|
|
|
|
fd := C.syscall(sys_open, path.str, 511)
|
|
|
|
if fd == -1 {
|
|
|
|
return error('failed to open file "$path"')
|
|
|
|
}
|
|
|
|
return File{
|
|
|
|
fd: fd
|
2020-08-02 00:07:37 +03:00
|
|
|
is_opened: true
|
2020-05-19 13:38:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2020-08-28 15:24:00 +03:00
|
|
|
cfile := vfopen(path, 'rb')?
|
2020-05-19 13:38:06 +03:00
|
|
|
fd := fileno(cfile)
|
|
|
|
return File {
|
|
|
|
cfile: cfile
|
|
|
|
fd: fd
|
2020-08-02 00:07:37 +03:00
|
|
|
is_opened: true
|
2020-05-19 13:38:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 17:57:01 +03:00
|
|
|
// create creates or opens a file at a specified location and returns a write-only `File` object.
|
2020-05-19 13:38:06 +03:00
|
|
|
pub fn create(path string) ?File {
|
|
|
|
/*
|
|
|
|
// NB: android/termux/bionic is also a kind of linux,
|
|
|
|
// but linux syscalls there sometimes fail,
|
|
|
|
// while the libc version should work.
|
|
|
|
$if linux {
|
|
|
|
$if !android {
|
|
|
|
//$if macos {
|
|
|
|
// fd = C.syscall(398, path.str, 0x601, 0x1b6)
|
|
|
|
//}
|
|
|
|
//$if linux {
|
|
|
|
fd = C.syscall(sys_creat, path.str, 511)
|
|
|
|
//}
|
|
|
|
if fd == -1 {
|
|
|
|
return error('failed to create file "$path"')
|
|
|
|
}
|
|
|
|
file = File{
|
|
|
|
fd: fd
|
2020-08-02 00:07:37 +03:00
|
|
|
is_opened: true
|
2020-05-19 13:38:06 +03:00
|
|
|
}
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2020-08-28 15:24:00 +03:00
|
|
|
cfile := vfopen(path, 'wb')?
|
2020-05-19 13:38:06 +03:00
|
|
|
fd := fileno(cfile)
|
|
|
|
return File {
|
|
|
|
cfile: cfile
|
|
|
|
fd: fd
|
2020-08-02 00:07:37 +03:00
|
|
|
is_opened: true
|
2020-05-19 13:38:06 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-14 16:46:30 +03:00
|
|
|
|
|
|
|
pub struct Uname {
|
|
|
|
pub mut:
|
|
|
|
sysname string
|
|
|
|
nodename string
|
|
|
|
release string
|
|
|
|
version string
|
|
|
|
machine string
|
|
|
|
}
|