v/vlib/os/os.v

903 lines
18 KiB
V
Raw Normal View History

2019-06-23 04:21:30 +02:00
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
2019-06-22 20:20:28 +02:00
module os
2019-10-15 17:08:46 +02:00
import strings
2019-06-22 20:20:28 +02:00
#include <sys/stat.h>
#include <signal.h>
#include <errno.h>
2019-08-22 22:19:31 +02:00
/*
struct dirent {
2019-08-22 22:19:31 +02:00
d_ino int
d_off int
d_reclen u16
d_type byte
d_name [256]byte
}
*/
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-10-24 11:36:57 +02:00
pub const (
2019-06-22 20:20:28 +02:00
args = []string
MAX_PATH = 4096
2019-06-22 20:20:28 +02:00
)
2019-07-24 12:16:45 +02:00
struct C.FILE {
2019-06-22 20:20:28 +02:00
}
2019-10-24 11:47:21 +02:00
pub struct File {
2019-09-01 21:51:16 +02:00
cfile &FILE
2019-06-22 20:20:28 +02:00
}
struct FileInfo {
name string
size int
}
2019-06-27 17:48:49 +02:00
struct C.stat {
st_size int
st_mode int
2019-08-22 22:19:31 +02:00
st_mtime int
}
struct C.DIR {
}
//struct C.dirent {
//d_name byteptr
//}
2019-06-27 17:48:49 +02:00
struct C.sigaction {
mut:
sa_mask int
sa_sigaction int
sa_flags int
2019-06-27 17:48:49 +02:00
}
fn C.getline(voidptr, voidptr, voidptr) int
fn C.ftell(fp voidptr) int
fn C.getenv(byteptr) byteptr
2019-06-27 17:48:49 +02:00
fn C.sigaction(int, voidptr, int)
// read_bytes reads an amount of bytes from the beginning of the file
pub fn (f File) read_bytes(size int) []byte {
return f.read_bytes_at(size, 0)
}
// read_bytes_at reads an amount of bytes at the given position in the file
pub fn (f File) read_bytes_at(size, pos int) []byte {
mut data := malloc(size)
mut arr := [`0`].repeat(size)
C.fseek(f.cfile, pos, C.SEEK_SET)
C.fread(data, 1, size, f.cfile)
C.fseek(f.cfile, 0, C.SEEK_SET)
for e := 0; e < size; e++ {
arr[e] = data[e]
}
return arr
}
2019-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'
mut fp := vfopen(path, mode)
if isnil(fp) {
return error('failed to open file "$path"')
}
2019-08-22 22:19:31 +02:00
C.fseek(fp, 0, C.SEEK_END)
fsize := C.ftell(fp)
2019-07-21 12:22:41 +02:00
// C.fseek(fp, 0, SEEK_SET) // same as `C.rewind(fp)` below
C.rewind(fp)
mut str := malloc(fsize + 1)
C.fread(str, fsize, 1, fp)
C.fclose(fp)
str[fsize] = 0
2019-07-21 12:22:41 +02:00
return string(str, fsize)
}
// 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-07-24 12:16:45 +02:00
mut s := C.stat{}
$if windows {
C._wstat(path.to_wide(), &s)
} $else {
C.stat(*char(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 {
C.rename(*char(old.str), *char(new.str))
2019-07-24 12:16:45 +02:00
}
}
2019-06-25 20:41:46 +02:00
fn vfopen(path, mode string) *C.FILE {
$if windows {
return C._wfopen(path.to_wide(), mode.to_wide())
} $else {
return C.fopen(*char(path.str), *char(mode.str))
}
}
// read_lines reads the file in `path` into an array of lines.
// TODO return `?[]string` TODO implement `?[]` support
2019-06-22 20:20:28 +02:00
pub fn read_lines(path string) []string {
mut res := []string
mut buf_len := 1024
mut buf := malloc(buf_len)
2019-07-24 12:16:45 +02:00
mode := 'rb'
mut fp := vfopen(path, mode)
if isnil(fp) {
// TODO
// return error('failed to open file "$path"')
return res
}
mut buf_index := 0
for C.fgets(buf + buf_index, buf_len - buf_index, fp) != 0 {
2019-09-15 14:36:05 +02:00
len := vstrlen(buf)
if len == buf_len - 1 && buf[len - 1] != 10 {
buf_len *= 2
buf = C.realloc(buf, buf_len)
if isnil(buf) {
panic('Could not reallocate the read buffer')
}
buf_index = len
continue
}
if buf[len - 1] == 10 || buf[len - 1] == 13 {
buf[len - 1] = `\0`
}
if len > 1 && buf[len - 2] == 13 {
buf[len - 2] = `\0`
}
res << tos_clone(buf)
buf_index = 0
}
C.fclose(fp)
return res
2019-06-22 20:20:28 +02:00
}
fn read_ulines(path string) []ustring {
lines := read_lines(path)
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
}
pub fn open(path string) ?File {
2019-07-24 12:16:45 +02:00
mut file := File{}
$if windows {
wpath := path.to_wide()
mode := 'rb'
file = File {
cfile: C._wfopen(wpath, mode.to_wide())
}
} $else {
2019-08-22 22:19:31 +02:00
cpath := path.str
2019-07-24 12:16:45 +02:00
file = File {
cfile: C.fopen(*char(cpath), 'rb')
2019-07-24 12:16:45 +02:00
}
}
if isnil(file.cfile) {
return error('failed to open file "$path"')
}
2019-08-22 22:19:31 +02:00
return file
2019-06-22 20:20:28 +02:00
}
// create creates a file at a specified location and returns a writable `File` object.
pub fn create(path string) ?File {
2019-07-24 12:16:45 +02:00
mut file := File{}
$if windows {
wpath := path.replace('/', '\\').to_wide()
mode := 'wb'
file = File {
cfile: C._wfopen(wpath, mode.to_wide())
}
} $else {
2019-08-22 22:19:31 +02:00
cpath := path.str
2019-07-24 12:16:45 +02:00
file = File {
cfile: C.fopen(*char(cpath), 'wb')
2019-07-24 12:16:45 +02:00
}
}
if isnil(file.cfile) {
return error('failed to create file "$path"')
}
2019-08-22 22:19:31 +02:00
return file
2019-06-22 20:20:28 +02:00
}
pub fn open_append(path string) ?File {
2019-07-24 12:16:45 +02:00
mut file := File{}
$if windows {
wpath := path.replace('/', '\\').to_wide()
mode := 'ab'
file = File {
cfile: C._wfopen(wpath, mode.to_wide())
}
} $else {
2019-08-22 22:19:31 +02:00
cpath := path.str
2019-07-24 12:16:45 +02:00
file = File {
cfile: C.fopen(*char(cpath), 'ab')
2019-07-24 12:16:45 +02:00
}
2019-06-22 20:20:28 +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-08-22 22:19:31 +02:00
return file
2019-06-22 20:20:28 +02:00
}
2019-06-30 16:11:55 +02:00
pub fn (f File) write(s string) {
C.fputs(s.str, f.cfile)
2019-06-22 20:20:28 +02:00
// C.fwrite(s.str, 1, s.len, f.cfile)
}
// convert any value to []byte (LittleEndian) and write it
// for example if we have write(7, 4), "07 00 00 00" gets written
// write(0x1234, 2) => "34 12"
2019-06-30 21:07:24 +02:00
pub fn (f File) write_bytes(data voidptr, size int) {
2019-06-22 20:20:28 +02:00
C.fwrite(data, 1, size, f.cfile)
}
2019-06-30 21:07:24 +02:00
pub fn (f File) write_bytes_at(data voidptr, size, pos int) {
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-06-22 20:20:28 +02:00
}
2019-06-30 16:11:55 +02:00
pub fn (f File) writeln(s string) {
2019-06-22 20:20:28 +02:00
// C.fwrite(s.str, 1, s.len, f.cfile)
// ss := s.clone()
// TODO perf
C.fputs(s.str, f.cfile)
2019-06-22 20:20:28 +02:00
// ss.free()
C.fputs('\n', f.cfile)
}
2019-07-13 02:11:11 +02:00
pub fn (f File) flush() {
C.fflush(f.cfile)
}
pub fn (f File) close() {
2019-06-22 20:20:28 +02:00
C.fclose(f.cfile)
}
// system starts the specified command, waits for it to complete, and returns its code.
fn popen(path string) *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-06-22 20:20:28 +02: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')
}
}
fn posix_wait4_to_exit_status(waitret int) (int,bool) {
$if windows {
return waitret, false
}
$else {
mut ret := 0
mut is_signaled := true
// (see man system, man 2 waitpid: C macro WEXITSTATUS section)
if C.WIFEXITED( waitret ) {
ret = C.WEXITSTATUS( waitret )
is_signaled = false
} else if C.WIFSIGNALED( waitret ){
ret = C.WTERMSIG( waitret )
is_signaled = true
}
return ret , is_signaled
}
}
fn pclose(f *C.FILE) int {
$if windows {
return int( C._pclose(f) )
}
$else {
ret , _ := posix_wait4_to_exit_status( int( C.pclose(f) ) )
return ret
}
}
struct Result {
2019-08-22 22:19:31 +02:00
pub:
exit_code int
output string
2019-08-22 22:19:31 +02:00
//stderr string // TODO
}
// exec starts the specified command, waits for it to complete, and returns its output.
pub fn exec(cmd string) ?Result {
pcmd := '$cmd 2>&1'
f := popen(pcmd)
2019-06-22 20:20:28 +02:00
if isnil(f) {
return error('exec("$cmd") failed')
}
2019-08-22 22:19:31 +02:00
buf := [1000]byte
mut res := ''
for C.fgets(*char(buf), 1000, f) != 0 {
2019-09-15 14:36:05 +02:00
res += tos(buf, vstrlen(buf))
2019-06-22 20:20:28 +02:00
}
res = res.trim_space()
exit_code := pclose(f)
//if exit_code != 0 {
//return error(res)
//}
return Result {
output: res
2019-08-22 22:19:31 +02:00
exit_code: exit_code
}
}
// `system` works like `exec()`, but only returns a return code.
pub fn system(cmd string) int {
mut ret := int(0)
$if windows {
ret = C._wsystem(cmd.to_wide())
} $else {
2019-08-22 22:19:31 +02:00
ret = C.system(cmd.str)
}
if ret == -1 {
print_c_errno()
}
$if !windows {
pret , is_signaled := posix_wait4_to_exit_status( ret )
if is_signaled {
println('Terminated by signal ${ret:2d} (' + sigint_to_signal_name(pret) + ')' )
}
ret = pret
}
return ret
2019-06-22 20:20:28 +02:00
}
pub fn sigint_to_signal_name(si int) string {
// POSIX signals:
switch si {
case 1: return 'SIGHUP'
case 2: return 'SIGINT'
case 3: return 'SIGQUIT'
case 4: return 'SIGILL'
case 6: return 'SIGABRT'
case 8: return 'SIGFPE'
case 9: return 'SIGKILL'
case 11: return 'SIGSEGV'
case 13: return 'SIGPIPE'
case 14: return 'SIGALRM'
case 15: return 'SIGTERM'
}
///////////////////////////////////
$if linux {
// From `man 7 signal` on linux:
switch si {
case 30,10,16: return 'SIGUSR1'
case 31,12,17: return 'SIGUSR2'
case 20,17,18: return 'SIGCHLD'
case 19,18,25: return 'SIGCONT'
case 17,19,23: return 'SIGSTOP'
case 18,20,24: return 'SIGTSTP'
case 21,21,26: return 'SIGTTIN'
case 22,22,27: return 'SIGTTOU'
///////////////////////////////
case 5: return 'SIGTRAP'
case 7: return 'SIGBUS'
}
}
return 'unknown'
}
2019-06-22 20:20:28 +02:00
// `getenv` returns the value of the environment variable named by the key.
2019-07-24 12:16:45 +02:00
pub fn getenv(key string) string {
$if windows {
s := C._wgetenv(key.to_wide())
if isnil(s) {
return ''
}
return string_from_wide(s)
} $else {
s := *byte(C.getenv(key.str))
2019-07-24 12:16:45 +02:00
if isnil(s) {
return ''
}
return string(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 {
$if windows {
format := '$name=$value'
if overwrite {
return C._putenv(format.str)
}
return -1
2019-08-22 22:19:31 +02:00
}
$else {
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 {
$if windows {
format := '${name}='
return C._putenv(format.str)
2019-08-22 22:19:31 +02:00
}
$else {
return C.unsetenv(name.str)
2019-08-22 22:19:31 +02:00
}
2019-06-27 13:51:03 +02:00
}
2019-08-17 15:17:43 +02:00
// file_exists returns true if `path` exists.
2019-08-06 18:13:04 +02:00
pub fn file_exists(_path string) bool {
$if windows {
2019-08-06 18:13:04 +02:00
path := _path.replace('/', '\\')
return C._waccess(path.to_wide(), 0) != -1
2019-07-24 12:16:45 +02:00
} $else {
2019-08-06 18:13:04 +02:00
return C.access(_path.str, 0 ) != -1
}
2019-06-22 20:20:28 +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())
}
$else {
C.remove(path.str)
}
// C.unlink(path.cstr())
2019-06-22 20:20:28 +02:00
}
// rmdir removes a specified directory.
pub fn rmdir(path string) {
$if !windows {
C.rmdir(path.str)
}
$else {
2019-07-24 12:16:45 +02:00
C.RemoveDirectory(path.to_wide())
}
2019-06-22 20:20:28 +02:00
}
2019-06-22 20:20:28 +02:00
fn print_c_errno() {
//C.printf('errno=%d err="%s"\n', C.errno, C.strerror(C.errno))
2019-06-22 20:20:28 +02:00
}
2019-06-26 11:28:06 +02:00
pub fn ext(path string) string {
2019-06-26 11:28:06 +02:00
pos := path.last_index('.')
if pos == -1 {
return ''
}
return path.right(pos)
}
2019-07-16 01:57:03 +02:00
2019-08-22 22:19:31 +02:00
// dir returns all but the last element of path, typically the path's directory.
2019-07-16 01:57:03 +02:00
pub fn dir(path string) string {
2019-08-01 01:34:28 +02:00
if path == '.' {
2019-08-22 22:19:31 +02:00
return getwd()
}
2019-10-12 21:31:05 +02:00
pos := path.last_index(path_separator)
2019-07-16 01:57:03 +02:00
if pos == -1 {
2019-08-22 22:19:31 +02:00
return '.'
}
return path.left(pos)
}
2019-07-16 01:57:03 +02:00
fn path_sans_ext(path string) string {
pos := path.last_index('.')
if pos == -1 {
return path
}
return path.left(pos)
}
2019-06-22 20:20:28 +02:00
pub fn basedir(path string) string {
2019-10-12 21:31:05 +02:00
pos := path.last_index(path_separator)
2019-06-22 20:20:28 +02:00
if pos == -1 {
return path
}
return path.left(pos + 1)
}
pub fn filename(path string) string {
2019-10-12 21:31:05 +02:00
return path.all_after(path_separator)
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 {
str := get_raw_line()
2019-09-15 18:07:40 +02:00
$if windows {
return str.trim_right('\r\n')
}
$else {
return str.trim_right('\n')
}
2019-06-22 20:20:28 +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 {
$if windows {
max_line_chars := 256
buf := &byte(malloc(max_line_chars*2))
if is_atty(0) {
h_input := C.GetStdHandle(STD_INPUT_HANDLE)
mut nr_chars := 0
C.ReadConsole(h_input, buf, max_line_chars * 2, &nr_chars, 0)
return string_from_wide2(&u16(buf), nr_chars)
}
res := int( C.fgetws(buf, max_line_chars, C.stdin ) )
2019-09-20 16:03:13 +02:00
len := int( C.wcslen(&u16(buf)) )
if 0 != res { return string_from_wide2( &u16(buf), len ) }
return ''
} $else {
max := size_t(256)
buf := *char(malloc(int(max)))
nr_chars := C.getline(&buf, &max, stdin)
if nr_chars == 0 {
return ''
}
return string(byteptr(buf), nr_chars)
}
}
2019-07-24 18:14:13 +02:00
pub fn get_lines() []string {
mut line := ''
mut inputstr := []string
for {
line = get_line()
if(line.len <= 0) {
break
}
line = line.trim_space()
inputstr << line
}
return inputstr
}
pub fn get_lines_joined() string {
mut line := ''
mut inputstr := ''
for {
line = get_line()
if(line.len <= 0) {
break
}
line = line.trim_space()
inputstr += line
}
return inputstr
}
2019-06-22 20:20:28 +02:00
pub fn user_os() string {
$if linux {
return 'linux'
}
$if mac {
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'
}
$if android{
return 'android'
}
2019-09-26 23:30:41 +02:00
$if solaris {
return 'solaris'
}
2019-06-22 20:20:28 +02:00
return 'unknown'
}
// 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) {
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()
}
pub fn clear() {
$if !windows {
C.printf('\x1b[2J')
C.printf('\x1b[H')
}
}
2019-06-22 20:20:28 +02:00
fn on_segfault(f voidptr) {
$if windows {
return
}
$if mac {
2019-06-27 17:48:49 +02:00
mut sa := C.sigaction{}
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-09-15 18:07:40 +02:00
fn C.getpid() int
fn C.proc_pidpath (int, byteptr, int) int
2019-07-15 23:22:29 +02:00
pub fn executable() string {
$if linux {
2019-07-24 12:16:45 +02:00
mut result := malloc(MAX_PATH)
count := int(C.readlink('/proc/self/exe', result, MAX_PATH ))
if count < 0 {
panic('error reading /proc/self/exe to get exe path')
}
2019-07-21 12:22:41 +02:00
return string(result, count)
}
$if windows {
max := 512
mut result := &u16(malloc(max*2)) // MAX_PATH * sizeof(wchar_t)
len := int(C.GetModuleFileName( 0, result, max ))
2019-07-24 12:16:45 +02:00
return string_from_wide2(result, len)
}
$if mac {
2019-07-24 12:16:45 +02:00
mut result := malloc(MAX_PATH)
2019-08-22 22:19:31 +02:00
pid := C.getpid()
ret := C.proc_pidpath (pid, result, MAX_PATH)
2019-07-15 23:22:29 +02:00
if ret <= 0 {
2019-08-22 22:19:31 +02:00
println('os.executable() failed')
return '.'
}
return string(result)
}
2019-07-16 16:19:52 +02:00
$if freebsd {
2019-07-24 12:16:45 +02:00
mut result := malloc(MAX_PATH)
2019-08-22 22:19:31 +02:00
mib := [1 /* CTL_KERN */, 14 /* KERN_PROC */, 12 /* KERN_PROC_PATHNAME */, -1]
size := MAX_PATH
C.sysctl(mib.data, 4, result, &size, 0, 0)
return string(result)
}
$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-09-26 23:30:41 +02:00
$if solaris {
}
$if netbsd {
2019-07-24 12:16:45 +02:00
mut result := malloc(MAX_PATH)
count := int(C.readlink('/proc/curproc/exe', result, MAX_PATH ))
if count < 0 {
panic('error reading /proc/curproc/exe to get exe path')
}
2019-07-21 12:22:41 +02:00
return string(result, count)
2019-08-22 22:19:31 +02:00
}
$if dragonfly {
2019-07-24 12:16:45 +02:00
mut result := malloc(MAX_PATH)
count := int(C.readlink('/proc/curproc/file', result, MAX_PATH ))
if count < 0 {
panic('error reading /proc/curproc/file to get exe path')
}
2019-07-21 12:22:41 +02: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]
}
pub fn is_dir(path string) bool {
$if windows {
2019-08-22 22:19:31 +02:00
return dir_exists(path)
2019-07-21 17:14:25 +02:00
//val := int(C.GetFileAttributes(path.to_wide()))
// Note: this return is broke (wrong). we have dir_exists already how will this differ?
2019-07-21 17:14:25 +02:00
//return (val &FILE_ATTRIBUTE_DIRECTORY) > 0
2019-08-22 22:19:31 +02:00
}
$else {
statbuf := C.stat{}
cstr := path.str
if C.stat(cstr, &statbuf) != 0 {
return false
}
2019-07-31 10:32:00 +02:00
// ref: https://code.woboq.org/gcc/include/sys/stat.h.html
return (statbuf.st_mode & S_IFMT) == S_IFDIR
2019-08-22 22:19:31 +02:00
}
}
2019-06-30 16:11:55 +02:00
pub fn chdir(path string) {
$if windows {
2019-07-24 12:16:45 +02:00
C._wchdir(path.to_wide())
}
2019-08-22 22:19:31 +02:00
$else {
C.chdir(path.str)
2019-08-22 22:19:31 +02:00
}
}
2019-07-24 12:16:45 +02:00
pub fn getwd() string {
$if windows {
max := 512 // MAX_PATH * sizeof(wchar_t)
buf := &u16(malloc(max*2))
if C._wgetcwd(buf, max) == 0 {
return ''
}
2019-07-24 12:16:45 +02:00
return string_from_wide(buf)
}
2019-07-24 12:16:45 +02:00
$else {
2019-08-22 22:19:31 +02:00
buf := malloc(512)
if C.getcwd(buf, 512) == 0 {
return ''
}
2019-07-24 12:16:45 +02:00
return string(buf)
}
}
// Returns the full absolute path for fpath, with all relative ../../, symlinks and so on resolved.
// See http://pubs.opengroup.org/onlinepubs/9699919799/functions/realpath.html
// Also https://insanecoding.blogspot.com/2007/11/pathmax-simply-isnt.html
// and https://insanecoding.blogspot.com/2007/11/implementing-realpath-in-c.html
// NB: this particular rabbit hole is *deep* ...
pub fn realpath(fpath string) string {
mut fullpath := malloc( MAX_PATH )
mut res := 0
$if windows {
res = int( C._fullpath( fullpath, fpath.str, MAX_PATH ) )
}
$else{
res = int( C.realpath( fpath.str, fullpath ) )
}
if res != 0 {
2019-09-15 14:36:05 +02:00
return string(fullpath, vstrlen(fullpath))
}
return fpath
}
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) {
return []string
}
2019-10-17 13:30:05 +02:00
mut files := os.ls(path) or { panic(err) }
2019-08-22 22:19:31 +02:00
mut res := []string
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-10-12 21:31:05 +02:00
p := path + path_separator + file
2019-08-22 22:19:31 +02:00
if os.is_dir(p) {
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-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
fn C.wait() int
2019-07-14 04:18:54 +02:00
pub fn fork() int {
$if !windows {
pid := C.fork()
return pid
}
panic('os.fork not supported in windows') // TODO
2019-07-14 04:18:54 +02:00
}
pub fn wait() int {
$if !windows {
pid := C.wait(0)
return pid
}
panic('os.wait not supported in windows') // TODO
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-08-22 22:19:31 +02:00
attr := C.stat{}
2019-07-07 21:46:21 +02:00
//# struct stat attr;
2019-08-22 22:19:31 +02:00
C.stat(path.str, &attr)
2019-07-07 21:46:21 +02:00
//# stat(path.str, &attr);
2019-08-22 22:19:31 +02:00
return attr.st_mtime
2019-07-07 21:46:21 +02:00
//# return attr.st_mtime ;
}
2019-08-22 22:19:31 +02:00
2019-07-07 21:46:21 +02:00
pub fn log(s string) {
println('os.log: ' + s)
}
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-06-30 16:11:55 +02:00
pub fn print_backtrace() {
2019-08-22 22:19:31 +02:00
/*
# void *buffer[100];
nptrs := 0
# nptrs = backtrace(buffer, 100);
# printf("%d!!\n", nptrs);
# backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO) ;
2019-08-22 22:19:31 +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
if !os.dir_exists(p) {
os.mkdir(p)
}
}
}
2019-10-15 17:08:46 +02:00
// TODO use []string.join once ...string becomes "[]string"
pub fn join(base string, dirs ...string) string {
mut path := strings.new_builder(50)
path.write(base.trim_right('\\/'))
for d in dirs {
path.write(os.path_separator)
path.write(d)
}
return path.str()
}