2019-06-23 05:21:30 +03:00
|
|
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
module os
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
const (
|
|
|
|
args = []string
|
|
|
|
)
|
|
|
|
|
|
|
|
struct FILE {
|
|
|
|
}
|
|
|
|
|
|
|
|
struct File {
|
|
|
|
cfile *FILE
|
|
|
|
}
|
|
|
|
|
2019-06-26 17:11:25 +03:00
|
|
|
struct FileInfo {
|
|
|
|
name string
|
|
|
|
size int
|
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
import const (
|
|
|
|
SEEK_SET
|
|
|
|
SEEK_END
|
2019-06-27 18:48:49 +03:00
|
|
|
SA_SIGINFO
|
|
|
|
SIGSEGV
|
2019-06-27 20:02:47 +03:00
|
|
|
|
|
|
|
S_IFMT
|
|
|
|
S_IFDIR
|
2019-06-22 21:20:28 +03:00
|
|
|
)
|
|
|
|
|
2019-06-27 18:48:49 +03:00
|
|
|
struct C.stat {
|
|
|
|
st_size int
|
2019-06-27 20:02:47 +03:00
|
|
|
st_mode int
|
|
|
|
}
|
|
|
|
|
|
|
|
struct C.DIR {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
struct C.dirent {
|
|
|
|
d_name byteptr
|
|
|
|
|
2019-06-27 18:48:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct C.sigaction {
|
2019-06-27 20:02:47 +03:00
|
|
|
mut:
|
|
|
|
sa_mask int
|
|
|
|
sa_sigaction int
|
|
|
|
sa_flags int
|
2019-06-27 18:48:49 +03:00
|
|
|
}
|
|
|
|
|
2019-06-26 13:56:49 +03:00
|
|
|
fn C.getline(voidptr, voidptr, voidptr) int
|
|
|
|
fn C.ftell(fp voidptr) int
|
2019-06-27 13:27:46 +03:00
|
|
|
fn C.getenv(byteptr) byteptr
|
2019-06-27 18:48:49 +03:00
|
|
|
fn C.sigaction(int, voidptr, int)
|
2019-06-26 13:56:49 +03:00
|
|
|
|
|
|
|
fn todo_remove(){}
|
|
|
|
|
2019-06-27 18:48:49 +03:00
|
|
|
fn init_os_args(argc int, argv *byteptr) []string {
|
2019-06-22 21:20:28 +03:00
|
|
|
mut args := []string
|
|
|
|
for i := 0; i < argc; i++ {
|
2019-06-27 18:48:49 +03:00
|
|
|
args << string(argv[i])
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
2019-06-26 00:43:04 +03:00
|
|
|
fn parse_windows_cmd_line(cmd byteptr) []string {
|
2019-06-27 13:27:46 +03:00
|
|
|
s := string(cmd)
|
2019-06-26 00:43:04 +03:00
|
|
|
return s.split(' ')
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// read_file reads the file in `path` and returns the contents.
|
2019-06-26 11:57:13 +03:00
|
|
|
pub fn read_file(path string) ?string {
|
|
|
|
mut res := ''
|
|
|
|
cpath := path.cstr()
|
|
|
|
fp := C.fopen(cpath, 'r')
|
|
|
|
if isnil(fp) {
|
|
|
|
return error('failed to open file "$path"')
|
|
|
|
}
|
|
|
|
C.fseek(fp, 0, SEEK_END)
|
|
|
|
fsize := C.ftell(fp)
|
|
|
|
// 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
|
|
|
|
res = tos(str, fsize)
|
2019-06-22 21:20:28 +03:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
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-06-27 18:48:49 +03:00
|
|
|
s := C.stat{}
|
|
|
|
C.stat(path.str, &s)
|
|
|
|
return s.st_size
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-25 21:41:46 +03:00
|
|
|
pub fn mv(old, new string) {
|
2019-06-26 17:39:40 +03:00
|
|
|
C.rename(old.cstr(), new.cstr())
|
|
|
|
}
|
2019-06-25 21:41:46 +03:00
|
|
|
|
2019-06-23 03:28:29 +03:00
|
|
|
// read_lines reads the file in `path` into an array of lines.
|
|
|
|
// TODO return `?[]string` TODO implement `?[]` support
|
2019-06-22 21:20:28 +03:00
|
|
|
pub fn read_lines(path string) []string {
|
2019-06-23 03:28:29 +03:00
|
|
|
mut res := []string
|
|
|
|
mut buf := [1000]byte
|
|
|
|
cpath := path.cstr()
|
|
|
|
fp := C.fopen(cpath, 'rb')
|
|
|
|
if isnil(fp) {
|
|
|
|
// TODO
|
|
|
|
// return error('failed to open file "$path"')
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
for C.fgets(buf, 1000, fp) != 0 {
|
|
|
|
mut val := ''
|
|
|
|
buf[C.strlen(buf) - 1] = `\0` // eat the newline fgets() stores
|
|
|
|
$if windows {
|
|
|
|
if buf[strlen(buf)-2] == 13 {
|
|
|
|
buf[strlen(buf) - 2] = `\0`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res << tos_clone(buf)
|
|
|
|
}
|
|
|
|
C.fclose(fp)
|
|
|
|
return res
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-26 17:11:25 +03:00
|
|
|
fn read_ulines(path string) []ustring {
|
2019-06-23 03:28:29 +03:00
|
|
|
lines := read_lines(path)
|
2019-06-22 21:20:28 +03:00
|
|
|
// mut ulines := new_array(0, lines.len, sizeof(ustring))
|
|
|
|
mut ulines := []ustring
|
|
|
|
for myline in lines {
|
|
|
|
// ulines[i] = ustr
|
|
|
|
ulines << myline.ustring()
|
|
|
|
}
|
|
|
|
return ulines
|
|
|
|
}
|
|
|
|
|
|
|
|
// fn open(file string) File? {
|
|
|
|
// return open_file(file)
|
|
|
|
// }
|
|
|
|
pub fn open(path string) File {
|
|
|
|
return open_file(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn open_file(file string) File {
|
|
|
|
return create_file2(file, 'r')
|
|
|
|
}
|
|
|
|
|
|
|
|
// `create` creates a file at a specified location and returns a writable `File` object.
|
|
|
|
pub fn create(path string) File {
|
|
|
|
return create_file(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn open_append(path string) File {
|
|
|
|
return create_file(path)
|
|
|
|
}
|
|
|
|
|
2019-06-26 13:56:49 +03:00
|
|
|
// TODO remove
|
2019-06-22 21:20:28 +03:00
|
|
|
fn create_file(file string) File {
|
|
|
|
return create_file2(file, 'w')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_file_a(file string) File {
|
|
|
|
return create_file2(file, 'a')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn open_file_a(file string) File {
|
|
|
|
return create_file2(file, 'a')
|
|
|
|
}
|
|
|
|
|
2019-06-26 17:39:40 +03:00
|
|
|
fn create_file2(file, mode string) File {
|
2019-06-22 21:20:28 +03:00
|
|
|
res := File {
|
|
|
|
cfile: C.fopen(file.cstr(), mode.cstr())
|
|
|
|
}
|
|
|
|
if isnil(res.cfile) {
|
|
|
|
println('coudlnt create file "$file"')
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (f File) append(s string) {
|
|
|
|
ss := s.clone()
|
|
|
|
C.fputs(ss.cstr(), f.cfile)
|
|
|
|
// ss.free()
|
|
|
|
// 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-26 17:11:25 +03:00
|
|
|
fn (f File) write_bytes(data voidptr, size int) {
|
2019-06-22 21:20:28 +03:00
|
|
|
C.fwrite(data, 1, size, f.cfile)
|
|
|
|
}
|
|
|
|
|
2019-06-26 17:11:25 +03:00
|
|
|
fn (f File) write_bytes_at(data voidptr, size, pos int) {
|
2019-06-22 21:20:28 +03:00
|
|
|
C.fseek(f.cfile, pos, SEEK_SET)
|
|
|
|
C.fwrite(data, 1, size, f.cfile)
|
|
|
|
C.fseek(f.cfile, 0, SEEK_END)
|
|
|
|
}
|
|
|
|
|
2019-06-26 13:56:49 +03:00
|
|
|
pub fn (f File) appendln(s string) {
|
2019-06-22 21:20:28 +03:00
|
|
|
// C.fwrite(s.str, 1, s.len, f.cfile)
|
|
|
|
// ss := s.clone()
|
|
|
|
// TODO perf
|
|
|
|
C.fputs(s.cstr(), f.cfile)
|
|
|
|
// ss.free()
|
|
|
|
C.fputs('\n', f.cfile)
|
|
|
|
}
|
|
|
|
|
2019-06-26 13:56:49 +03:00
|
|
|
pub fn (f File) close() {
|
2019-06-22 21:20:28 +03:00
|
|
|
C.fclose(f.cfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn close_file(fp *FILE) {
|
|
|
|
$if windows {
|
|
|
|
}
|
2019-06-27 18:48:49 +03:00
|
|
|
if isnil(fp) {
|
|
|
|
return
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
C.fclose(fp)
|
|
|
|
}
|
|
|
|
|
2019-06-26 17:11:25 +03:00
|
|
|
// system starts the specified command, waits for it to complete, and returns its code.
|
|
|
|
pub fn system(cmd string) int {
|
|
|
|
ret := C.system(cmd.cstr())
|
2019-06-22 21:20:28 +03:00
|
|
|
if ret == -1 {
|
|
|
|
os.print_c_errno()
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
fn popen(path string) *FILE {
|
|
|
|
cpath := path.cstr()
|
|
|
|
$if windows {
|
|
|
|
return C._popen(cpath, 'r')
|
|
|
|
}
|
|
|
|
$else {
|
|
|
|
return C.popen(cpath, 'r')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 17:11:25 +03:00
|
|
|
// exec starts the specified command, waits for it to complete, and returns its output.
|
|
|
|
pub fn exec(cmd string) string {
|
|
|
|
cmd = '$cmd 2>&1'
|
|
|
|
f := popen(cmd)
|
2019-06-22 21:20:28 +03:00
|
|
|
if isnil(f) {
|
2019-06-26 17:11:25 +03:00
|
|
|
// TODO optional or error code
|
2019-06-22 21:20:28 +03:00
|
|
|
println('popen $cmd failed')
|
2019-06-26 17:11:25 +03:00
|
|
|
return ''
|
|
|
|
}
|
|
|
|
buf := [1000]byte
|
|
|
|
mut res := ''
|
|
|
|
for C.fgets(buf, 1000, f) != 0 {
|
|
|
|
res += tos(buf, strlen(buf))
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
return res.trim_space()
|
|
|
|
}
|
|
|
|
|
|
|
|
// `getenv` returns the value of the environment variable named by the key.
|
|
|
|
pub fn getenv(key string) string {
|
|
|
|
s := C.getenv(key.cstr())
|
|
|
|
if isnil(s) {
|
|
|
|
return ''
|
|
|
|
}
|
2019-06-27 13:27:46 +03:00
|
|
|
return string(s)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-27 14:51:03 +03:00
|
|
|
pub fn setenv(name string, value string, overwrite bool) int {
|
|
|
|
return C.setenv(name.cstr(), value.cstr(), overwrite)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unsetenv(name string) int {
|
|
|
|
return C.unsetenv(name.cstr())
|
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
// `file_exists` returns true if `path` exists.
|
|
|
|
pub fn file_exists(path string) bool {
|
2019-06-26 17:39:40 +03:00
|
|
|
$if windows {
|
2019-06-27 18:48:49 +03:00
|
|
|
return C._access( path.str, 0 ) != -1
|
2019-06-26 17:39:40 +03:00
|
|
|
}
|
2019-06-27 18:48:49 +03:00
|
|
|
return C.access( path.str, 0 ) != -1
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-23 15:59:44 +03:00
|
|
|
pub fn dir_exists(path string) bool {
|
2019-06-23 19:12:21 +03:00
|
|
|
dir := C.opendir(path.cstr())
|
|
|
|
res := !isnil(dir)
|
|
|
|
if res {
|
|
|
|
C.closedir(dir)
|
2019-06-23 15:59:44 +03:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
// `mkdir` creates a new directory with the specified path.
|
|
|
|
pub fn mkdir(path string) {
|
|
|
|
$if windows {
|
|
|
|
path = path.replace('/', '\\')
|
|
|
|
C.CreateDirectory(path.cstr(), 0)
|
|
|
|
}
|
|
|
|
$else {
|
|
|
|
C.mkdir(path.cstr(), 511)// S_IRWXU | S_IRWXG | S_IRWXO
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// `rm` removes file in `path`.
|
|
|
|
pub fn rm(path string) {
|
|
|
|
$if windows {
|
|
|
|
// os.system2('del /f $path')
|
|
|
|
}
|
|
|
|
$else {
|
|
|
|
C.remove(path.cstr())
|
|
|
|
}
|
|
|
|
// C.unlink(path.cstr())
|
|
|
|
}
|
|
|
|
|
2019-06-26 17:39:40 +03:00
|
|
|
/*
|
|
|
|
// TODO
|
|
|
|
fn rmdir(path, guard string) {
|
2019-06-22 21:20:28 +03:00
|
|
|
if !path.contains(guard) {
|
|
|
|
println('rmdir canceled because the path doesnt contain $guard')
|
|
|
|
return
|
|
|
|
}
|
2019-06-26 17:39:40 +03:00
|
|
|
$if !windows {
|
|
|
|
}
|
|
|
|
$else {
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-06-26 17:39:40 +03:00
|
|
|
*/
|
2019-06-22 21:20:28 +03:00
|
|
|
|
|
|
|
fn print_c_errno() {
|
2019-06-26 18:04:38 +03:00
|
|
|
//C.printf('errno=%d err="%s"\n', errno, C.strerror(errno))
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-26 12:28:06 +03:00
|
|
|
|
2019-06-26 13:56:05 +03:00
|
|
|
pub fn ext(path string) string {
|
2019-06-26 12:28:06 +03:00
|
|
|
pos := path.last_index('.')
|
|
|
|
if pos == -1 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
return path.right(pos)
|
|
|
|
}
|
|
|
|
|
2019-06-26 13:56:05 +03:00
|
|
|
fn path_sans_ext(path string) string {
|
|
|
|
pos := path.last_index('.')
|
|
|
|
if pos == -1 {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
return path.left(pos)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
pub fn basedir(path string) string {
|
|
|
|
pos := path.last_index('/')
|
|
|
|
if pos == -1 {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
return path.left(pos + 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn filename(path string) string {
|
|
|
|
return path.all_after('/')
|
|
|
|
}
|
|
|
|
|
2019-06-26 17:11:25 +03:00
|
|
|
// get_line returns a one-line string from stdin
|
2019-06-27 13:14:33 +03:00
|
|
|
//u64 is used because C.getline needs a size_t as second argument
|
|
|
|
//Otherwise, it would cause a valgrind warning and may be dangerous
|
|
|
|
//Malloc takes an int as argument so a cast has to be made
|
2019-06-22 21:20:28 +03:00
|
|
|
pub fn get_line() string {
|
2019-06-27 13:14:33 +03:00
|
|
|
max := u64(256)
|
|
|
|
buf := malloc(int(max))
|
2019-06-22 21:20:28 +03:00
|
|
|
nr_chars := C.getline(&buf, &max, stdin)
|
|
|
|
if nr_chars == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
2019-06-27 03:12:50 +03:00
|
|
|
if buf[nr_chars - 1] == `\n` /* newline */ {
|
|
|
|
return tos(buf, nr_chars - 1)
|
|
|
|
}
|
|
|
|
/* To prevent cutting end of line if no newline */
|
|
|
|
return tos(buf, nr_chars)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn user_os() string {
|
|
|
|
$if linux {
|
|
|
|
return 'linux'
|
|
|
|
}
|
|
|
|
$if mac {
|
|
|
|
return 'mac'
|
|
|
|
}
|
|
|
|
$if windows {
|
|
|
|
return 'windows'
|
|
|
|
}
|
|
|
|
return 'unknown'
|
|
|
|
}
|
|
|
|
|
2019-06-26 17:11:25 +03:00
|
|
|
// home_dir returns path to user's home directory.
|
2019-06-22 21:20:28 +03:00
|
|
|
pub fn home_dir() string {
|
|
|
|
mut home := os.getenv('HOME')
|
|
|
|
$if windows {
|
|
|
|
home = os.getenv('HOMEDRIVE')
|
|
|
|
home += os.getenv('HOMEPATH')
|
|
|
|
}
|
|
|
|
home += '/'
|
|
|
|
return home
|
|
|
|
}
|
|
|
|
|
2019-06-26 17:11:25 +03:00
|
|
|
// write_file writes text data to a file in `path`.
|
2019-06-22 21:20:28 +03:00
|
|
|
pub fn write_file(path, text string) {
|
|
|
|
f := os.create(path)
|
|
|
|
f.appendln(text)
|
|
|
|
f.close()
|
|
|
|
}
|
|
|
|
|
2019-06-26 19:56:33 +03:00
|
|
|
pub fn clear() {
|
|
|
|
C.printf('\x1b[2J')
|
|
|
|
C.printf('\x1b[H')
|
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
fn on_segfault(f voidptr) {
|
2019-06-26 17:39:40 +03:00
|
|
|
$if windows {
|
2019-06-24 14:51:11 +03:00
|
|
|
return
|
2019-06-26 17:39:40 +03:00
|
|
|
}
|
|
|
|
$if mac {
|
2019-06-27 18:48:49 +03:00
|
|
|
mut sa := C.sigaction{}
|
|
|
|
C.memset(&sa, 0, sizeof(sigaction))
|
|
|
|
C.sigemptyset(&sa.sa_mask)
|
|
|
|
sa.sa_sigaction = f
|
|
|
|
sa.sa_flags = SA_SIGINFO
|
|
|
|
C.sigaction(SIGSEGV, &sa, 0)
|
2019-06-26 17:39:40 +03:00
|
|
|
}
|
2019-06-27 13:27:46 +03:00
|
|
|
}
|