Strip semicolons

This commit is contained in:
gingerBill
2021-09-19 11:59:44 +01:00
parent ceebd7b23c
commit ee876ad66b
20 changed files with 517 additions and 514 deletions
+7 -7
View File
@@ -5,8 +5,8 @@ package os2
// To distinguish between an empty value and an unset value, use lookup_env // To distinguish between an empty value and an unset value, use lookup_env
// NOTE: the value will be allocated with the supplied allocator // NOTE: the value will be allocated with the supplied allocator
get_env :: proc(key: string, allocator := context.allocator) -> string { get_env :: proc(key: string, allocator := context.allocator) -> string {
value, _ := lookup_env(key, allocator); value, _ := lookup_env(key, allocator)
return value; return value
} }
// lookup_env gets the value of the environment variable named by the key // lookup_env gets the value of the environment variable named by the key
@@ -14,30 +14,30 @@ get_env :: proc(key: string, allocator := context.allocator) -> string {
// Otherwise the returned value will be empty and the boolean will be false // Otherwise the returned value will be empty and the boolean will be false
// NOTE: the value will be allocated with the supplied allocator // NOTE: the value will be allocated with the supplied allocator
lookup_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) { lookup_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) {
return _lookup_env(key, allocator); return _lookup_env(key, allocator)
} }
// set_env sets the value of the environment variable named by the key // set_env sets the value of the environment variable named by the key
// Returns true on success, false on failure // Returns true on success, false on failure
set_env :: proc(key, value: string) -> bool { set_env :: proc(key, value: string) -> bool {
return _set_env(key, value); return _set_env(key, value)
} }
// unset_env unsets a single environment variable // unset_env unsets a single environment variable
// Returns true on success, false on failure // Returns true on success, false on failure
unset_env :: proc(key: string) -> bool { unset_env :: proc(key: string) -> bool {
return _unset_env(key); return _unset_env(key)
} }
clear_env :: proc() { clear_env :: proc() {
_clear_env(); _clear_env()
} }
// environ returns a copy of strings representing the environment, in the form "key=value" // environ returns a copy of strings representing the environment, in the form "key=value"
// NOTE: the slice of strings and the strings with be allocated using the supplied allocator // NOTE: the slice of strings and the strings with be allocated using the supplied allocator
environ :: proc(allocator := context.allocator) -> []string { environ :: proc(allocator := context.allocator) -> []string {
return _environ(allocator); return _environ(allocator)
} }
+28 -28
View File
@@ -6,75 +6,75 @@ import win32 "core:sys/windows"
_lookup_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) { _lookup_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) {
if key == "" { if key == "" {
return; return
} }
wkey := win32.utf8_to_wstring(key); wkey := win32.utf8_to_wstring(key)
b := make([dynamic]u16, 100, context.temp_allocator); b := make([dynamic]u16, 100, context.temp_allocator)
for { for {
n := win32.GetEnvironmentVariableW(wkey, raw_data(b), u32(len(b))); n := win32.GetEnvironmentVariableW(wkey, raw_data(b), u32(len(b)))
if n == 0 { if n == 0 {
err := win32.GetLastError(); err := win32.GetLastError()
if err == win32.ERROR_ENVVAR_NOT_FOUND { if err == win32.ERROR_ENVVAR_NOT_FOUND {
return "", false; return "", false
} }
} }
if n <= u32(len(b)) { if n <= u32(len(b)) {
value = win32.utf16_to_utf8(b[:n], allocator); value = win32.utf16_to_utf8(b[:n], allocator)
found = true; found = true
return; return
} }
resize(&b, len(b)*2); resize(&b, len(b)*2)
} }
} }
_set_env :: proc(key, value: string) -> bool { _set_env :: proc(key, value: string) -> bool {
k := win32.utf8_to_wstring(key); k := win32.utf8_to_wstring(key)
v := win32.utf8_to_wstring(value); v := win32.utf8_to_wstring(value)
return bool(win32.SetEnvironmentVariableW(k, v)); return bool(win32.SetEnvironmentVariableW(k, v))
} }
_unset_env :: proc(key: string) -> bool { _unset_env :: proc(key: string) -> bool {
k := win32.utf8_to_wstring(key); k := win32.utf8_to_wstring(key)
return bool(win32.SetEnvironmentVariableW(k, nil)); return bool(win32.SetEnvironmentVariableW(k, nil))
} }
_clear_env :: proc() { _clear_env :: proc() {
envs := environ(context.temp_allocator); envs := environ(context.temp_allocator)
for env in envs { for env in envs {
for j in 1..<len(env) { for j in 1..<len(env) {
if env[j] == '=' { if env[j] == '=' {
unset_env(env[0:j]); unset_env(env[0:j])
break; break
} }
} }
} }
} }
_environ :: proc(allocator := context.allocator) -> []string { _environ :: proc(allocator := context.allocator) -> []string {
envs := win32.GetEnvironmentStringsW(); envs := win32.GetEnvironmentStringsW()
if envs == nil { if envs == nil {
return nil; return nil
} }
defer win32.FreeEnvironmentStringsW(envs); defer win32.FreeEnvironmentStringsW(envs)
r := make([dynamic]string, 0, 50, allocator); r := make([dynamic]string, 0, 50, allocator)
for from, i, p := 0, 0, envs; true; i += 1 { for from, i, p := 0, 0, envs; true; i += 1 {
c := (^u16)(uintptr(p) + uintptr(i*2))^; c := (^u16)(uintptr(p) + uintptr(i*2))^
if c == 0 { if c == 0 {
if i <= from { if i <= from {
break; break
} }
w := mem.slice_ptr(mem.ptr_offset(p, from), i-from); w := mem.slice_ptr(mem.ptr_offset(p, from), i-from)
append(&r, win32.utf16_to_utf8(w, allocator)); append(&r, win32.utf16_to_utf8(w, allocator))
from = i + 1; from = i + 1
} }
} }
return r[:]; return r[:]
} }
+32 -32
View File
@@ -22,7 +22,7 @@ Error :: union {
io.Error, io.Error,
Platform_Error, Platform_Error,
} }
#assert(size_of(Error) == size_of(u64)); #assert(size_of(Error) == size_of(u64))
Path_Error :: struct { Path_Error :: struct {
op: string, op: string,
@@ -39,56 +39,56 @@ Link_Error :: struct {
path_error_delete :: proc(perr: Maybe(Path_Error)) { path_error_delete :: proc(perr: Maybe(Path_Error)) {
if err, ok := perr.?; ok { if err, ok := perr.?; ok {
context.allocator = error_allocator(); context.allocator = error_allocator()
delete(err.op); delete(err.op)
delete(err.path); delete(err.path)
} }
} }
link_error_delete :: proc(lerr: Maybe(Link_Error)) { link_error_delete :: proc(lerr: Maybe(Link_Error)) {
if err, ok := lerr.?; ok { if err, ok := lerr.?; ok {
context.allocator = error_allocator(); context.allocator = error_allocator()
delete(err.op); delete(err.op)
delete(err.old); delete(err.old)
delete(err.new); delete(err.new)
} }
} }
is_platform_error :: proc(ferr: Error) -> (err: i32, ok: bool) { is_platform_error :: proc(ferr: Error) -> (err: i32, ok: bool) {
v := ferr.(Platform_Error) or_else {}; v := ferr.(Platform_Error) or_else {}
return v.err, v.err != 0; return v.err, v.err != 0
} }
error_string :: proc(ferr: Error) -> string { error_string :: proc(ferr: Error) -> string {
switch ferr { switch ferr {
case nil: return ""; case nil: return ""
case .Invalid_Argument: return "invalid argument"; case .Invalid_Argument: return "invalid argument"
case .Permission_Denied: return "permission denied"; case .Permission_Denied: return "permission denied"
case .Exist: return "file already exists"; case .Exist: return "file already exists"
case .Not_Exist: return "file does not exist"; case .Not_Exist: return "file does not exist"
case .Closed: return "file already closed"; case .Closed: return "file already closed"
case .Timeout: return "i/o timeout"; case .Timeout: return "i/o timeout"
case .EOF: return "eof"; case .EOF: return "eof"
case .Unexpected_EOF: return "unexpected eof"; case .Unexpected_EOF: return "unexpected eof"
case .Short_Write: return "short write"; case .Short_Write: return "short write"
case .Invalid_Write: return "invalid write result"; case .Invalid_Write: return "invalid write result"
case .Short_Buffer: return "short buffer"; case .Short_Buffer: return "short buffer"
case .No_Progress: return "multiple read calls return no data or error"; case .No_Progress: return "multiple read calls return no data or error"
case .Invalid_Whence: return "invalid whence"; case .Invalid_Whence: return "invalid whence"
case .Invalid_Offset: return "invalid offset"; case .Invalid_Offset: return "invalid offset"
case .Invalid_Unread: return "invalid unread"; case .Invalid_Unread: return "invalid unread"
case .Negative_Read: return "negative read"; case .Negative_Read: return "negative read"
case .Negative_Write: return "negative write"; case .Negative_Write: return "negative write"
case .Negative_Count: return "negative count"; case .Negative_Count: return "negative count"
case .Buffer_Full: return "buffer full"; case .Buffer_Full: return "buffer full"
} }
if errno, ok := is_platform_error(ferr); ok { if errno, ok := is_platform_error(ferr); ok {
return _error_string(errno); return _error_string(errno)
} }
return "unknown error"; return "unknown error"
} }
+3 -3
View File
@@ -4,11 +4,11 @@ package os2
import win32 "core:sys/windows" import win32 "core:sys/windows"
_error_string :: proc(errno: i32) -> string { _error_string :: proc(errno: i32) -> string {
e := win32.DWORD(errno); e := win32.DWORD(errno)
if e == 0 { if e == 0 {
return ""; return ""
} }
// TODO(bill): _error_string for windows // TODO(bill): _error_string for windows
// FormatMessageW // FormatMessageW
return ""; return ""
} }
+47 -47
View File
@@ -3,7 +3,7 @@ package os2
import "core:io" import "core:io"
import "core:time" import "core:time"
Handle :: distinct uintptr; Handle :: distinct uintptr
Seek_From :: enum { Seek_From :: enum {
Start = 0, // seek relative to the origin of the file Start = 0, // seek relative to the origin of the file
@@ -11,148 +11,148 @@ Seek_From :: enum {
End = 2, // seek relative to the end End = 2, // seek relative to the end
} }
File_Mode :: distinct u32; File_Mode :: distinct u32
File_Mode_Dir :: File_Mode(1<<16); File_Mode_Dir :: File_Mode(1<<16)
File_Mode_Named_Pipe :: File_Mode(1<<17); File_Mode_Named_Pipe :: File_Mode(1<<17)
File_Mode_Device :: File_Mode(1<<18); File_Mode_Device :: File_Mode(1<<18)
File_Mode_Char_Device :: File_Mode(1<<19); File_Mode_Char_Device :: File_Mode(1<<19)
File_Mode_Sym_Link :: File_Mode(1<<20); File_Mode_Sym_Link :: File_Mode(1<<20)
O_RDONLY :: int( 0); O_RDONLY :: int( 0)
O_WRONLY :: int( 1); O_WRONLY :: int( 1)
O_RDWR :: int( 2); O_RDWR :: int( 2)
O_APPEND :: int( 4); O_APPEND :: int( 4)
O_CREATE :: int( 8); O_CREATE :: int( 8)
O_EXCL :: int(16); O_EXCL :: int(16)
O_SYNC :: int(32); O_SYNC :: int(32)
O_TRUNC :: int(64); O_TRUNC :: int(64)
stdin: Handle = 0; // OS-Specific stdin: Handle = 0 // OS-Specific
stdout: Handle = 1; // OS-Specific stdout: Handle = 1 // OS-Specific
stderr: Handle = 2; // OS-Specific stderr: Handle = 2 // OS-Specific
create :: proc(name: string) -> (Handle, Error) { create :: proc(name: string) -> (Handle, Error) {
return _create(name); return _create(name)
} }
open :: proc(name: string) -> (Handle, Error) { open :: proc(name: string) -> (Handle, Error) {
return _open(name); return _open(name)
} }
open_file :: proc(name: string, flag: int, perm: File_Mode) -> (Handle, Error) { open_file :: proc(name: string, flag: int, perm: File_Mode) -> (Handle, Error) {
return _open_file(name, flag, perm); return _open_file(name, flag, perm)
} }
close :: proc(fd: Handle) -> Error { close :: proc(fd: Handle) -> Error {
return _close(fd); return _close(fd)
} }
name :: proc(fd: Handle, allocator := context.allocator) -> string { name :: proc(fd: Handle, allocator := context.allocator) -> string {
return _name(fd); return _name(fd)
} }
seek :: proc(fd: Handle, offset: i64, whence: Seek_From) -> (ret: i64, err: Error) { seek :: proc(fd: Handle, offset: i64, whence: Seek_From) -> (ret: i64, err: Error) {
return _seek(fd, offset, whence); return _seek(fd, offset, whence)
} }
read :: proc(fd: Handle, p: []byte) -> (n: int, err: Error) { read :: proc(fd: Handle, p: []byte) -> (n: int, err: Error) {
return _read(fd, p); return _read(fd, p)
} }
read_at :: proc(fd: Handle, p: []byte, offset: i64) -> (n: int, err: Error) { read_at :: proc(fd: Handle, p: []byte, offset: i64) -> (n: int, err: Error) {
return _read_at(fd, p, offset); return _read_at(fd, p, offset)
} }
read_from :: proc(fd: Handle, r: io.Reader) -> (n: i64, err: Error) { read_from :: proc(fd: Handle, r: io.Reader) -> (n: i64, err: Error) {
return _read_from(fd, r); return _read_from(fd, r)
} }
write :: proc(fd: Handle, p: []byte) -> (n: int, err: Error) { write :: proc(fd: Handle, p: []byte) -> (n: int, err: Error) {
return _write(fd, p); return _write(fd, p)
} }
write_at :: proc(fd: Handle, p: []byte, offset: i64) -> (n: int, err: Error) { write_at :: proc(fd: Handle, p: []byte, offset: i64) -> (n: int, err: Error) {
return _write_at(fd, p, offset); return _write_at(fd, p, offset)
} }
write_to :: proc(fd: Handle, w: io.Writer) -> (n: i64, err: Error) { write_to :: proc(fd: Handle, w: io.Writer) -> (n: i64, err: Error) {
return _write_to(fd, w); return _write_to(fd, w)
} }
file_size :: proc(fd: Handle) -> (n: i64, err: Error) { file_size :: proc(fd: Handle) -> (n: i64, err: Error) {
return _file_size(fd); return _file_size(fd)
} }
sync :: proc(fd: Handle) -> Error { sync :: proc(fd: Handle) -> Error {
return _sync(fd); return _sync(fd)
} }
flush :: proc(fd: Handle) -> Error { flush :: proc(fd: Handle) -> Error {
return _flush(fd); return _flush(fd)
} }
truncate :: proc(fd: Handle, size: i64) -> Maybe(Path_Error) { truncate :: proc(fd: Handle, size: i64) -> Maybe(Path_Error) {
return _truncate(fd, size); return _truncate(fd, size)
} }
remove :: proc(name: string) -> Maybe(Path_Error) { remove :: proc(name: string) -> Maybe(Path_Error) {
return _remove(name); return _remove(name)
} }
rename :: proc(old_path, new_path: string) -> Maybe(Path_Error) { rename :: proc(old_path, new_path: string) -> Maybe(Path_Error) {
return _rename(old_path, new_path); return _rename(old_path, new_path)
} }
link :: proc(old_name, new_name: string) -> Maybe(Link_Error) { link :: proc(old_name, new_name: string) -> Maybe(Link_Error) {
return _link(old_name, new_name); return _link(old_name, new_name)
} }
symlink :: proc(old_name, new_name: string) -> Maybe(Link_Error) { symlink :: proc(old_name, new_name: string) -> Maybe(Link_Error) {
return _symlink(old_name, new_name); return _symlink(old_name, new_name)
} }
read_link :: proc(name: string) -> (string, Maybe(Path_Error)) { read_link :: proc(name: string) -> (string, Maybe(Path_Error)) {
return _read_link(name); return _read_link(name)
} }
chdir :: proc(fd: Handle) -> Error { chdir :: proc(fd: Handle) -> Error {
return _chdir(fd); return _chdir(fd)
} }
chmod :: proc(fd: Handle, mode: File_Mode) -> Error { chmod :: proc(fd: Handle, mode: File_Mode) -> Error {
return _chmod(fd, mode); return _chmod(fd, mode)
} }
chown :: proc(fd: Handle, uid, gid: int) -> Error { chown :: proc(fd: Handle, uid, gid: int) -> Error {
return _chown(fd, uid, gid); return _chown(fd, uid, gid)
} }
lchown :: proc(name: string, uid, gid: int) -> Error { lchown :: proc(name: string, uid, gid: int) -> Error {
return _lchown(name, uid, gid); return _lchown(name, uid, gid)
} }
chtimes :: proc(name: string, atime, mtime: time.Time) -> Maybe(Path_Error) { chtimes :: proc(name: string, atime, mtime: time.Time) -> Maybe(Path_Error) {
return _chtimes(name, atime, mtime); return _chtimes(name, atime, mtime)
} }
exists :: proc(path: string) -> bool { exists :: proc(path: string) -> bool {
return _exists(path); return _exists(path)
} }
is_file :: proc(path: string) -> bool { is_file :: proc(path: string) -> bool {
return _is_file(path); return _is_file(path)
} }
is_dir :: proc(path: string) -> bool { is_dir :: proc(path: string) -> bool {
return _is_dir(path); return _is_dir(path)
} }
+49 -49
View File
@@ -3,83 +3,83 @@ package os2
import "core:io" import "core:io"
file_to_stream :: proc(fd: Handle) -> (s: io.Stream) { file_to_stream :: proc(fd: Handle) -> (s: io.Stream) {
s.stream_data = rawptr(uintptr(fd)); s.stream_data = rawptr(uintptr(fd))
s.stream_vtable = _file_stream_vtable; s.stream_vtable = _file_stream_vtable
return; return
} }
@(private) @(private)
error_to_io_error :: proc(ferr: Error) -> io.Error { error_to_io_error :: proc(ferr: Error) -> io.Error {
if ferr == nil { if ferr == nil {
return .None; return .None
} }
return ferr.(io.Error) or_else .Unknown; return ferr.(io.Error) or_else .Unknown
} }
@(private) @(private)
_file_stream_vtable := &io.Stream_VTable{ _file_stream_vtable := &io.Stream_VTable{
impl_read = proc(s: io.Stream, p: []byte) -> (n: int, err: io.Error) { impl_read = proc(s: io.Stream, p: []byte) -> (n: int, err: io.Error) {
fd := Handle(uintptr(s.stream_data)); fd := Handle(uintptr(s.stream_data))
ferr: Error; ferr: Error
n, ferr = read(fd, p); n, ferr = read(fd, p)
err = error_to_io_error(ferr); err = error_to_io_error(ferr)
return; return
}, },
impl_read_at = proc(s: io.Stream, p: []byte, offset: i64) -> (n: int, err: io.Error) { impl_read_at = proc(s: io.Stream, p: []byte, offset: i64) -> (n: int, err: io.Error) {
fd := Handle(uintptr(s.stream_data)); fd := Handle(uintptr(s.stream_data))
ferr: Error; ferr: Error
n, ferr = read_at(fd, p, offset); n, ferr = read_at(fd, p, offset)
err = error_to_io_error(ferr); err = error_to_io_error(ferr)
return; return
}, },
impl_write_to = proc(s: io.Stream, w: io.Writer) -> (n: i64, err: io.Error) { impl_write_to = proc(s: io.Stream, w: io.Writer) -> (n: i64, err: io.Error) {
fd := Handle(uintptr(s.stream_data)); fd := Handle(uintptr(s.stream_data))
ferr: Error; ferr: Error
n, ferr = write_to(fd, w); n, ferr = write_to(fd, w)
err = error_to_io_error(ferr); err = error_to_io_error(ferr)
return; return
}, },
impl_write = proc(s: io.Stream, p: []byte) -> (n: int, err: io.Error) { impl_write = proc(s: io.Stream, p: []byte) -> (n: int, err: io.Error) {
fd := Handle(uintptr(s.stream_data)); fd := Handle(uintptr(s.stream_data))
ferr: Error; ferr: Error
n, ferr = write(fd, p); n, ferr = write(fd, p)
err = error_to_io_error(ferr); err = error_to_io_error(ferr)
return; return
}, },
impl_write_at = proc(s: io.Stream, p: []byte, offset: i64) -> (n: int, err: io.Error) { impl_write_at = proc(s: io.Stream, p: []byte, offset: i64) -> (n: int, err: io.Error) {
fd := Handle(uintptr(s.stream_data)); fd := Handle(uintptr(s.stream_data))
ferr: Error; ferr: Error
n, ferr = write_at(fd, p, offset); n, ferr = write_at(fd, p, offset)
err = error_to_io_error(ferr); err = error_to_io_error(ferr)
return; return
}, },
impl_read_from = proc(s: io.Stream, r: io.Reader) -> (n: i64, err: io.Error) { impl_read_from = proc(s: io.Stream, r: io.Reader) -> (n: i64, err: io.Error) {
fd := Handle(uintptr(s.stream_data)); fd := Handle(uintptr(s.stream_data))
ferr: Error; ferr: Error
n, ferr = read_from(fd, r); n, ferr = read_from(fd, r)
err = error_to_io_error(ferr); err = error_to_io_error(ferr)
return; return
}, },
impl_seek = proc(s: io.Stream, offset: i64, whence: io.Seek_From) -> (i64, io.Error) { impl_seek = proc(s: io.Stream, offset: i64, whence: io.Seek_From) -> (i64, io.Error) {
fd := Handle(uintptr(s.stream_data)); fd := Handle(uintptr(s.stream_data))
n, ferr := seek(fd, offset, Seek_From(whence)); n, ferr := seek(fd, offset, Seek_From(whence))
err := error_to_io_error(ferr); err := error_to_io_error(ferr)
return n, err; return n, err
}, },
impl_size = proc(s: io.Stream) -> i64 { impl_size = proc(s: io.Stream) -> i64 {
fd := Handle(uintptr(s.stream_data)); fd := Handle(uintptr(s.stream_data))
sz, _ := file_size(fd); sz, _ := file_size(fd)
return sz; return sz
}, },
impl_flush = proc(s: io.Stream) -> io.Error { impl_flush = proc(s: io.Stream) -> io.Error {
fd := Handle(uintptr(s.stream_data)); fd := Handle(uintptr(s.stream_data))
ferr := flush(fd); ferr := flush(fd)
return error_to_io_error(ferr); return error_to_io_error(ferr)
}, },
impl_close = proc(s: io.Stream) -> io.Error { impl_close = proc(s: io.Stream) -> io.Error {
fd := Handle(uintptr(s.stream_data)); fd := Handle(uintptr(s.stream_data))
ferr := close(fd); ferr := close(fd)
return error_to_io_error(ferr); return error_to_io_error(ferr)
}, },
}; }
+51 -51
View File
@@ -6,118 +6,118 @@ import "core:strconv"
import "core:unicode/utf8" import "core:unicode/utf8"
write_string :: proc(fd: Handle, s: string) -> (n: int, err: Error) { write_string :: proc(fd: Handle, s: string) -> (n: int, err: Error) {
return write(fd, transmute([]byte)s); return write(fd, transmute([]byte)s)
} }
write_byte :: proc(fd: Handle, b: byte) -> (n: int, err: Error) { write_byte :: proc(fd: Handle, b: byte) -> (n: int, err: Error) {
return write(fd, []byte{b}); return write(fd, []byte{b})
} }
write_rune :: proc(fd: Handle, r: rune) -> (n: int, err: Error) { write_rune :: proc(fd: Handle, r: rune) -> (n: int, err: Error) {
if r < utf8.RUNE_SELF { if r < utf8.RUNE_SELF {
return write_byte(fd, byte(r)); return write_byte(fd, byte(r))
} }
b: [4]byte; b: [4]byte
b, n = utf8.encode_rune(r); b, n = utf8.encode_rune(r)
return write(fd, b[:n]); return write(fd, b[:n])
} }
write_encoded_rune :: proc(fd: Handle, r: rune) -> (n: int, err: Error) { write_encoded_rune :: proc(fd: Handle, r: rune) -> (n: int, err: Error) {
wrap :: proc(m: int, merr: Error, n: ^int, err: ^Error) -> bool { wrap :: proc(m: int, merr: Error, n: ^int, err: ^Error) -> bool {
n^ += m; n^ += m
if merr != nil { if merr != nil {
err^ = merr; err^ = merr
return true; return true
} }
return false; return false
} }
if wrap(write_byte(fd, '\''), &n, &err) { return; } if wrap(write_byte(fd, '\''), &n, &err) { return }
switch r { switch r {
case '\a': if wrap(write_string(fd, "\\a"), &n, &err) { return; } case '\a': if wrap(write_string(fd, "\\a"), &n, &err) { return }
case '\b': if wrap(write_string(fd, "\\b"), &n, &err) { return; } case '\b': if wrap(write_string(fd, "\\b"), &n, &err) { return }
case '\e': if wrap(write_string(fd, "\\e"), &n, &err) { return; } case '\e': if wrap(write_string(fd, "\\e"), &n, &err) { return }
case '\f': if wrap(write_string(fd, "\\f"), &n, &err) { return; } case '\f': if wrap(write_string(fd, "\\f"), &n, &err) { return }
case '\n': if wrap(write_string(fd, "\\n"), &n, &err) { return; } case '\n': if wrap(write_string(fd, "\\n"), &n, &err) { return }
case '\r': if wrap(write_string(fd, "\\r"), &n, &err) { return; } case '\r': if wrap(write_string(fd, "\\r"), &n, &err) { return }
case '\t': if wrap(write_string(fd, "\\t"), &n, &err) { return; } case '\t': if wrap(write_string(fd, "\\t"), &n, &err) { return }
case '\v': if wrap(write_string(fd, "\\v"), &n, &err) { return; } case '\v': if wrap(write_string(fd, "\\v"), &n, &err) { return }
case: case:
if r < 32 { if r < 32 {
if wrap(write_string(fd, "\\x"), &n, &err) { return; } if wrap(write_string(fd, "\\x"), &n, &err) { return }
b: [2]byte; b: [2]byte
s := strconv.append_bits(b[:], u64(r), 16, true, 64, strconv.digits, nil); s := strconv.append_bits(b[:], u64(r), 16, true, 64, strconv.digits, nil)
switch len(s) { switch len(s) {
case 0: if wrap(write_string(fd, "00"), &n, &err) { return; } case 0: if wrap(write_string(fd, "00"), &n, &err) { return }
case 1: if wrap(write_rune(fd, '0'), &n, &err) { return; } case 1: if wrap(write_rune(fd, '0'), &n, &err) { return }
case 2: if wrap(write_string(fd, s), &n, &err) { return; } case 2: if wrap(write_string(fd, s), &n, &err) { return }
} }
} else { } else {
if wrap(write_rune(fd, r), &n, &err) { return; } if wrap(write_rune(fd, r), &n, &err) { return }
} }
} }
_ = wrap(write_byte(fd, '\''), &n, &err); _ = wrap(write_byte(fd, '\''), &n, &err)
return; return
} }
write_ptr :: proc(fd: Handle, data: rawptr, len: int) -> (n: int, err: Error) { write_ptr :: proc(fd: Handle, data: rawptr, len: int) -> (n: int, err: Error) {
s := transmute([]byte)mem.Raw_Slice{data, len}; s := transmute([]byte)mem.Raw_Slice{data, len}
return write(fd, s); return write(fd, s)
} }
read_ptr :: proc(fd: Handle, data: rawptr, len: int) -> (n: int, err: Error) { read_ptr :: proc(fd: Handle, data: rawptr, len: int) -> (n: int, err: Error) {
s := transmute([]byte)mem.Raw_Slice{data, len}; s := transmute([]byte)mem.Raw_Slice{data, len}
return read(fd, s); return read(fd, s)
} }
read_entire_file :: proc(name: string, allocator := context.allocator) -> ([]byte, Error) { read_entire_file :: proc(name: string, allocator := context.allocator) -> ([]byte, Error) {
f, ferr := open(name); f, ferr := open(name)
if ferr != nil { if ferr != nil {
return nil, ferr; return nil, ferr
} }
defer close(f); defer close(f)
size: int; size: int
if size64, err := file_size(f); err == nil { if size64, err := file_size(f); err == nil {
if i64(int(size64)) != size64 { if i64(int(size64)) != size64 {
size = int(size64); size = int(size64)
} }
} }
size += 1; // for EOF size += 1 // for EOF
// TODO(bill): Is this correct logic? // TODO(bill): Is this correct logic?
total: int; total: int
data := make([]byte, size, allocator); data := make([]byte, size, allocator)
for { for {
n, err := read(f, data[total:]); n, err := read(f, data[total:])
total += n; total += n
if err != nil { if err != nil {
if err == .EOF { if err == .EOF {
err = nil; err = nil
} }
return data[:total], err; return data[:total], err
} }
} }
} }
write_entire_file :: proc(name: string, data: []byte, perm: File_Mode, truncate := true) -> Error { write_entire_file :: proc(name: string, data: []byte, perm: File_Mode, truncate := true) -> Error {
flags := O_WRONLY|O_CREATE; flags := O_WRONLY|O_CREATE
if truncate { if truncate {
flags |= O_TRUNC; flags |= O_TRUNC
} }
f, err := open_file(name, flags, perm); f, err := open_file(name, flags, perm)
if err != nil { if err != nil {
return err; return err
} }
_, err = write(f, data); _, err = write(f, data)
if cerr := close(f); cerr != nil && err == nil { if cerr := close(f); cerr != nil && err == nil {
err = cerr; err = cerr
} }
return err; return err
} }
+29 -29
View File
@@ -5,125 +5,125 @@ import "core:io"
import "core:time" import "core:time"
_create :: proc(name: string) -> (Handle, Error) { _create :: proc(name: string) -> (Handle, Error) {
return 0, nil; return 0, nil
} }
_open :: proc(name: string) -> (Handle, Error) { _open :: proc(name: string) -> (Handle, Error) {
return 0, nil; return 0, nil
} }
_open_file :: proc(name: string, flag: int, perm: File_Mode) -> (Handle, Error) { _open_file :: proc(name: string, flag: int, perm: File_Mode) -> (Handle, Error) {
return 0, nil; return 0, nil
} }
_close :: proc(fd: Handle) -> Error { _close :: proc(fd: Handle) -> Error {
return nil; return nil
} }
_name :: proc(fd: Handle, allocator := context.allocator) -> string { _name :: proc(fd: Handle, allocator := context.allocator) -> string {
return ""; return ""
} }
_seek :: proc(fd: Handle, offset: i64, whence: Seek_From) -> (ret: i64, err: Error) { _seek :: proc(fd: Handle, offset: i64, whence: Seek_From) -> (ret: i64, err: Error) {
return; return
} }
_read :: proc(fd: Handle, p: []byte) -> (n: int, err: Error) { _read :: proc(fd: Handle, p: []byte) -> (n: int, err: Error) {
return; return
} }
_read_at :: proc(fd: Handle, p: []byte, offset: i64) -> (n: int, err: Error) { _read_at :: proc(fd: Handle, p: []byte, offset: i64) -> (n: int, err: Error) {
return; return
} }
_read_from :: proc(fd: Handle, r: io.Reader) -> (n: i64, err: Error) { _read_from :: proc(fd: Handle, r: io.Reader) -> (n: i64, err: Error) {
return; return
} }
_write :: proc(fd: Handle, p: []byte) -> (n: int, err: Error) { _write :: proc(fd: Handle, p: []byte) -> (n: int, err: Error) {
return; return
} }
_write_at :: proc(fd: Handle, p: []byte, offset: i64) -> (n: int, err: Error) { _write_at :: proc(fd: Handle, p: []byte, offset: i64) -> (n: int, err: Error) {
return; return
} }
_write_to :: proc(fd: Handle, w: io.Writer) -> (n: i64, err: Error) { _write_to :: proc(fd: Handle, w: io.Writer) -> (n: i64, err: Error) {
return; return
} }
_file_size :: proc(fd: Handle) -> (n: i64, err: Error) { _file_size :: proc(fd: Handle) -> (n: i64, err: Error) {
return; return
} }
_sync :: proc(fd: Handle) -> Error { _sync :: proc(fd: Handle) -> Error {
return nil; return nil
} }
_flush :: proc(fd: Handle) -> Error { _flush :: proc(fd: Handle) -> Error {
return nil; return nil
} }
_truncate :: proc(fd: Handle, size: i64) -> Maybe(Path_Error) { _truncate :: proc(fd: Handle, size: i64) -> Maybe(Path_Error) {
return nil; return nil
} }
_remove :: proc(name: string) -> Maybe(Path_Error) { _remove :: proc(name: string) -> Maybe(Path_Error) {
return nil; return nil
} }
_rename :: proc(old_path, new_path: string) -> Maybe(Path_Error) { _rename :: proc(old_path, new_path: string) -> Maybe(Path_Error) {
return nil; return nil
} }
_link :: proc(old_name, new_name: string) -> Maybe(Link_Error) { _link :: proc(old_name, new_name: string) -> Maybe(Link_Error) {
return nil; return nil
} }
_symlink :: proc(old_name, new_name: string) -> Maybe(Link_Error) { _symlink :: proc(old_name, new_name: string) -> Maybe(Link_Error) {
return nil; return nil
} }
_read_link :: proc(name: string) -> (string, Maybe(Path_Error)) { _read_link :: proc(name: string) -> (string, Maybe(Path_Error)) {
return "", nil; return "", nil
} }
_chdir :: proc(fd: Handle) -> Error { _chdir :: proc(fd: Handle) -> Error {
return nil; return nil
} }
_chmod :: proc(fd: Handle, mode: File_Mode) -> Error { _chmod :: proc(fd: Handle, mode: File_Mode) -> Error {
return nil; return nil
} }
_chown :: proc(fd: Handle, uid, gid: int) -> Error { _chown :: proc(fd: Handle, uid, gid: int) -> Error {
return nil; return nil
} }
_lchown :: proc(name: string, uid, gid: int) -> Error { _lchown :: proc(name: string, uid, gid: int) -> Error {
return nil; return nil
} }
_chtimes :: proc(name: string, atime, mtime: time.Time) -> Maybe(Path_Error) { _chtimes :: proc(name: string, atime, mtime: time.Time) -> Maybe(Path_Error) {
return nil; return nil
} }
_exists :: proc(path: string) -> bool { _exists :: proc(path: string) -> bool {
return false; return false
} }
_is_file :: proc(path: string) -> bool { _is_file :: proc(path: string) -> bool {
return false; return false
} }
_is_dir :: proc(path: string) -> bool { _is_dir :: proc(path: string) -> bool {
return false; return false
} }
+3 -3
View File
@@ -6,16 +6,16 @@ heap_allocator :: proc() -> runtime.Allocator {
return runtime.Allocator{ return runtime.Allocator{
procedure = heap_allocator_proc, procedure = heap_allocator_proc,
data = nil, data = nil,
}; }
} }
heap_allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mode, heap_allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mode,
size, alignment: int, size, alignment: int,
old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, runtime.Allocator_Error) { old_memory: rawptr, old_size: int, loc := #caller_location) -> ([]byte, runtime.Allocator_Error) {
return _heap_allocator_proc(allocator_data, mode, size, alignment, old_memory, old_size, loc); return _heap_allocator_proc(allocator_data, mode, size, alignment, old_memory, old_size, loc)
} }
@(private) @(private)
error_allocator := heap_allocator; error_allocator := heap_allocator
+34 -34
View File
@@ -6,25 +6,25 @@ import "core:mem"
import win32 "core:sys/windows" import win32 "core:sys/windows"
heap_alloc :: proc(size: int) -> rawptr { heap_alloc :: proc(size: int) -> rawptr {
return win32.HeapAlloc(win32.GetProcessHeap(), win32.HEAP_ZERO_MEMORY, uint(size)); return win32.HeapAlloc(win32.GetProcessHeap(), win32.HEAP_ZERO_MEMORY, uint(size))
} }
heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr { heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
if new_size == 0 { if new_size == 0 {
heap_free(ptr); heap_free(ptr)
return nil; return nil
} }
if ptr == nil { if ptr == nil {
return heap_alloc(new_size); return heap_alloc(new_size)
} }
return win32.HeapReAlloc(win32.GetProcessHeap(), win32.HEAP_ZERO_MEMORY, ptr, uint(new_size)); return win32.HeapReAlloc(win32.GetProcessHeap(), win32.HEAP_ZERO_MEMORY, ptr, uint(new_size))
} }
heap_free :: proc(ptr: rawptr) { heap_free :: proc(ptr: rawptr) {
if ptr == nil { if ptr == nil {
return; return
} }
win32.HeapFree(win32.GetProcessHeap(), 0, ptr); win32.HeapFree(win32.GetProcessHeap(), 0, ptr)
} }
_heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode, _heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode,
@@ -38,70 +38,70 @@ _heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode,
// //
aligned_alloc :: proc(size, alignment: int, old_ptr: rawptr = nil) -> ([]byte, mem.Allocator_Error) { aligned_alloc :: proc(size, alignment: int, old_ptr: rawptr = nil) -> ([]byte, mem.Allocator_Error) {
a := max(alignment, align_of(rawptr)); a := max(alignment, align_of(rawptr))
space := size + a - 1; space := size + a - 1
allocated_mem: rawptr; allocated_mem: rawptr
if old_ptr != nil { if old_ptr != nil {
original_old_ptr := mem.ptr_offset((^rawptr)(old_ptr), -1)^; original_old_ptr := mem.ptr_offset((^rawptr)(old_ptr), -1)^
allocated_mem = heap_resize(original_old_ptr, space+size_of(rawptr)); allocated_mem = heap_resize(original_old_ptr, space+size_of(rawptr))
} else { } else {
allocated_mem = heap_alloc(space+size_of(rawptr)); allocated_mem = heap_alloc(space+size_of(rawptr))
} }
aligned_mem := rawptr(mem.ptr_offset((^u8)(allocated_mem), size_of(rawptr))); aligned_mem := rawptr(mem.ptr_offset((^u8)(allocated_mem), size_of(rawptr)))
ptr := uintptr(aligned_mem); ptr := uintptr(aligned_mem)
aligned_ptr := (ptr - 1 + uintptr(a)) & -uintptr(a); aligned_ptr := (ptr - 1 + uintptr(a)) & -uintptr(a)
diff := int(aligned_ptr - ptr); diff := int(aligned_ptr - ptr)
if (size + diff) > space { if (size + diff) > space {
return nil, .Out_Of_Memory; return nil, .Out_Of_Memory
} }
aligned_mem = rawptr(aligned_ptr); aligned_mem = rawptr(aligned_ptr)
mem.ptr_offset((^rawptr)(aligned_mem), -1)^ = allocated_mem; mem.ptr_offset((^rawptr)(aligned_mem), -1)^ = allocated_mem
return mem.byte_slice(aligned_mem, size), nil; return mem.byte_slice(aligned_mem, size), nil
} }
aligned_free :: proc(p: rawptr) { aligned_free :: proc(p: rawptr) {
if p != nil { if p != nil {
heap_free(mem.ptr_offset((^rawptr)(p), -1)^); heap_free(mem.ptr_offset((^rawptr)(p), -1)^)
} }
} }
aligned_resize :: proc(p: rawptr, old_size: int, new_size: int, new_alignment: int) -> ([]byte, mem.Allocator_Error) { aligned_resize :: proc(p: rawptr, old_size: int, new_size: int, new_alignment: int) -> ([]byte, mem.Allocator_Error) {
if p == nil { if p == nil {
return nil, nil; return nil, nil
} }
return aligned_alloc(new_size, new_alignment, p); return aligned_alloc(new_size, new_alignment, p)
} }
switch mode { switch mode {
case .Alloc: case .Alloc:
return aligned_alloc(size, alignment); return aligned_alloc(size, alignment)
case .Free: case .Free:
aligned_free(old_memory); aligned_free(old_memory)
case .Free_All: case .Free_All:
return nil, .Mode_Not_Implemented; return nil, .Mode_Not_Implemented
case .Resize: case .Resize:
if old_memory == nil { if old_memory == nil {
return aligned_alloc(size, alignment); return aligned_alloc(size, alignment)
} }
return aligned_resize(old_memory, old_size, size, alignment); return aligned_resize(old_memory, old_size, size, alignment)
case .Query_Features: case .Query_Features:
set := (^mem.Allocator_Mode_Set)(old_memory); set := (^mem.Allocator_Mode_Set)(old_memory)
if set != nil { if set != nil {
set^ = {.Alloc, .Free, .Resize, .Query_Features}; set^ = {.Alloc, .Free, .Resize, .Query_Features}
} }
return nil, nil; return nil, nil
case .Query_Info: case .Query_Info:
return nil, nil; return nil, nil
} }
return nil, nil; return nil, nil
} }
+8 -8
View File
@@ -1,29 +1,29 @@
package os2 package os2
Path_Separator :: _Path_Separator; // OS-Specific Path_Separator :: _Path_Separator // OS-Specific
Path_List_Separator :: _Path_List_Separator; // OS-Specific Path_List_Separator :: _Path_List_Separator // OS-Specific
is_path_separator :: proc(c: byte) -> bool { is_path_separator :: proc(c: byte) -> bool {
return _is_path_separator(c); return _is_path_separator(c)
} }
mkdir :: proc(name: string, perm: File_Mode) -> Maybe(Path_Error) { mkdir :: proc(name: string, perm: File_Mode) -> Maybe(Path_Error) {
return _mkdir(name, perm); return _mkdir(name, perm)
} }
mkdir_all :: proc(path: string, perm: File_Mode) -> Maybe(Path_Error) { mkdir_all :: proc(path: string, perm: File_Mode) -> Maybe(Path_Error) {
return _mkdir_all(path, perm); return _mkdir_all(path, perm)
} }
remove_all :: proc(path: string) -> Maybe(Path_Error) { remove_all :: proc(path: string) -> Maybe(Path_Error) {
return _remove_all(path); return _remove_all(path)
} }
getwd :: proc(allocator := context.allocator) -> (dir: string, err: Error) { getwd :: proc(allocator := context.allocator) -> (dir: string, err: Error) {
return _getwd(allocator); return _getwd(allocator)
} }
setwd :: proc(dir: string) -> (err: Error) { setwd :: proc(dir: string) -> (err: Error) {
return _setwd(dir); return _setwd(dir)
} }
+8 -8
View File
@@ -1,31 +1,31 @@
//+private //+private
package os2 package os2
_Path_Separator :: '\\'; _Path_Separator :: '\\'
_Path_List_Separator :: ';'; _Path_List_Separator :: ';'
_is_path_separator :: proc(c: byte) -> bool { _is_path_separator :: proc(c: byte) -> bool {
return c == '\\' || c == '/'; return c == '\\' || c == '/'
} }
_mkdir :: proc(name: string, perm: File_Mode) -> Maybe(Path_Error) { _mkdir :: proc(name: string, perm: File_Mode) -> Maybe(Path_Error) {
return nil; return nil
} }
_mkdir_all :: proc(path: string, perm: File_Mode) -> Maybe(Path_Error) { _mkdir_all :: proc(path: string, perm: File_Mode) -> Maybe(Path_Error) {
// TODO(bill): _mkdir_all for windows // TODO(bill): _mkdir_all for windows
return nil; return nil
} }
_remove_all :: proc(path: string) -> Maybe(Path_Error) { _remove_all :: proc(path: string) -> Maybe(Path_Error) {
// TODO(bill): _remove_all for windows // TODO(bill): _remove_all for windows
return nil; return nil
} }
_getwd :: proc(allocator := context.allocator) -> (dir: string, err: Error) { _getwd :: proc(allocator := context.allocator) -> (dir: string, err: Error) {
return "", nil; return "", nil
} }
_setwd :: proc(dir: string) -> (err: Error) { _setwd :: proc(dir: string) -> (err: Error) {
return nil; return nil
} }
+1 -1
View File
@@ -1,5 +1,5 @@
package os2 package os2
pipe :: proc() -> (r, w: Handle, err: Error) { pipe :: proc() -> (r, w: Handle, err: Error) {
return _pipe(); return _pipe()
} }
+3 -3
View File
@@ -4,10 +4,10 @@ package os2
import win32 "core:sys/windows" import win32 "core:sys/windows"
_pipe :: proc() -> (r, w: Handle, err: Error) { _pipe :: proc() -> (r, w: Handle, err: Error) {
p: [2]win32.HANDLE; p: [2]win32.HANDLE
if !win32.CreatePipe(&p[0], &p[1], nil, 0) { if !win32.CreatePipe(&p[0], &p[1], nil, 0) {
return 0, 0, Platform_Error{i32(win32.GetLastError())}; return 0, 0, Platform_Error{i32(win32.GetLastError())}
} }
return Handle(p[0]), Handle(p[1]), nil; return Handle(p[0]), Handle(p[1]), nil
} }
+19 -18
View File
@@ -2,35 +2,36 @@ package os2
import sync "core:sync/sync2" import sync "core:sync/sync2"
import "core:time" import "core:time"
import "core:runtime"
args: []string; args: []string
exit :: proc "contextless" (code: int) -> ! { exit :: proc "contextless" (code: int) -> ! {
// runtime.trap()
} }
get_uid :: proc() -> int { get_uid :: proc() -> int {
return -1; return -1
} }
get_euid :: proc() -> int { get_euid :: proc() -> int {
return -1; return -1
} }
get_gid :: proc() -> int { get_gid :: proc() -> int {
return -1; return -1
} }
get_egid :: proc() -> int { get_egid :: proc() -> int {
return -1; return -1
} }
get_pid :: proc() -> int { get_pid :: proc() -> int {
return -1; return -1
} }
get_ppid :: proc() -> int { get_ppid :: proc() -> int {
return -1; return -1
} }
@@ -49,7 +50,7 @@ Process_Attributes :: struct {
sys: ^Process_Attributes_OS_Specific, sys: ^Process_Attributes_OS_Specific,
} }
Process_Attributes_OS_Specific :: struct{}; Process_Attributes_OS_Specific :: struct{}
Process_Error :: enum { Process_Error :: enum {
None, None,
@@ -65,35 +66,35 @@ Process_State :: struct {
sys: rawptr, sys: rawptr,
} }
Signal :: #type proc(); Signal :: #type proc()
Kill: Signal = nil; Kill: Signal = nil
Interrupt: Signal = nil; Interrupt: Signal = nil
find_process :: proc(pid: int) -> (^Process, Process_Error) { find_process :: proc(pid: int) -> (^Process, Process_Error) {
return nil, .None; return nil, .None
} }
process_start :: proc(name: string, argv: []string, attr: ^Process_Attributes) -> (^Process, Process_Error) { process_start :: proc(name: string, argv: []string, attr: ^Process_Attributes) -> (^Process, Process_Error) {
return nil, .None; return nil, .None
} }
process_release :: proc(p: ^Process) -> Process_Error { process_release :: proc(p: ^Process) -> Process_Error {
return .None; return .None
} }
process_kill :: proc(p: ^Process) -> Process_Error { process_kill :: proc(p: ^Process) -> Process_Error {
return .None; return .None
} }
process_signal :: proc(p: ^Process, sig: Signal) -> Process_Error { process_signal :: proc(p: ^Process, sig: Signal) -> Process_Error {
return .None; return .None
} }
process_wait :: proc(p: ^Process) -> (Process_State, Process_Error) { process_wait :: proc(p: ^Process) -> (Process_State, Process_Error) {
return {}, .None; return {}, .None
} }
+7 -7
View File
@@ -15,28 +15,28 @@ File_Info :: struct {
file_info_slice_delete :: proc(infos: []File_Info, allocator := context.allocator) { file_info_slice_delete :: proc(infos: []File_Info, allocator := context.allocator) {
for i := len(infos)-1; i >= 0; i -= 1 { for i := len(infos)-1; i >= 0; i -= 1 {
file_info_delete(infos[i], allocator); file_info_delete(infos[i], allocator)
} }
delete(infos, allocator); delete(infos, allocator)
} }
file_info_delete :: proc(fi: File_Info, allocator := context.allocator) { file_info_delete :: proc(fi: File_Info, allocator := context.allocator) {
delete(fi.fullpath, allocator); delete(fi.fullpath, allocator)
} }
fstat :: proc(fd: Handle, allocator := context.allocator) -> (File_Info, Maybe(Path_Error)) { fstat :: proc(fd: Handle, allocator := context.allocator) -> (File_Info, Maybe(Path_Error)) {
return _fstat(fd, allocator); return _fstat(fd, allocator)
} }
stat :: proc(name: string, allocator := context.allocator) -> (File_Info, Maybe(Path_Error)) { stat :: proc(name: string, allocator := context.allocator) -> (File_Info, Maybe(Path_Error)) {
return _stat(name, allocator); return _stat(name, allocator)
} }
lstat :: proc(name: string, allocator := context.allocator) -> (File_Info, Maybe(Path_Error)) { lstat :: proc(name: string, allocator := context.allocator) -> (File_Info, Maybe(Path_Error)) {
return _lstat(name, allocator); return _lstat(name, allocator)
} }
same_file :: proc(fi1, fi2: File_Info) -> bool { same_file :: proc(fi1, fi2: File_Info) -> bool {
return _same_file(fi1, fi2); return _same_file(fi1, fi2)
} }
+153 -151
View File
@@ -6,368 +6,370 @@ import win32 "core:sys/windows"
_fstat :: proc(fd: Handle, allocator := context.allocator) -> (File_Info, Maybe(Path_Error)) { _fstat :: proc(fd: Handle, allocator := context.allocator) -> (File_Info, Maybe(Path_Error)) {
if fd == 0 { if fd == 0 {
return {}, Path_Error{err = .Invalid_Argument}; return {}, Path_Error{err = .Invalid_Argument}
} }
context.allocator = allocator; context.allocator = allocator
path, err := _cleanpath_from_handle(fd); path, err := _cleanpath_from_handle(fd)
if err != nil { if err != nil {
return {}, err; return {}, err
} }
h := win32.HANDLE(fd); h := win32.HANDLE(fd)
switch win32.GetFileType(h) { switch win32.GetFileType(h) {
case win32.FILE_TYPE_PIPE, win32.FILE_TYPE_CHAR: case win32.FILE_TYPE_PIPE, win32.FILE_TYPE_CHAR:
fi: File_Info; fi: File_Info
fi.fullpath = path; fi.fullpath = path
fi.name = basename(path); fi.name = basename(path)
fi.mode |= file_type_mode(h); fi.mode |= file_type_mode(h)
return fi, nil; return fi, nil
} }
return _file_info_from_get_file_information_by_handle(path, h); return _file_info_from_get_file_information_by_handle(path, h)
} }
_stat :: proc(name: string, allocator := context.allocator) -> (File_Info, Maybe(Path_Error)) { _stat :: proc(name: string, allocator := context.allocator) -> (File_Info, Maybe(Path_Error)) {
return internal_stat(name, win32.FILE_FLAG_BACKUP_SEMANTICS); return internal_stat(name, win32.FILE_FLAG_BACKUP_SEMANTICS)
} }
_lstat :: proc(name: string, allocator := context.allocator) -> (File_Info, Maybe(Path_Error)) { _lstat :: proc(name: string, allocator := context.allocator) -> (File_Info, Maybe(Path_Error)) {
return internal_stat(name, win32.FILE_FLAG_BACKUP_SEMANTICS|win32.FILE_FLAG_OPEN_REPARSE_POINT); return internal_stat(name, win32.FILE_FLAG_BACKUP_SEMANTICS|win32.FILE_FLAG_OPEN_REPARSE_POINT)
} }
_same_file :: proc(fi1, fi2: File_Info) -> bool { _same_file :: proc(fi1, fi2: File_Info) -> bool {
return fi1.fullpath == fi2.fullpath; return fi1.fullpath == fi2.fullpath
} }
_stat_errno :: proc(errno: win32.DWORD) -> Path_Error { _stat_errno :: proc(errno: win32.DWORD) -> Path_Error {
return Path_Error{err = Platform_Error{i32(errno)}}; return Path_Error{err = Platform_Error{i32(errno)}}
} }
full_path_from_name :: proc(name: string, allocator := context.allocator) -> (path: string, err: Maybe(Path_Error)) { full_path_from_name :: proc(name: string, allocator := context.allocator) -> (path: string, err: Maybe(Path_Error)) {
name := name; context.allocator = allocator
name := name
if name == "" { if name == "" {
name = "."; name = "."
} }
p := win32.utf8_to_utf16(name, context.temp_allocator); p := win32.utf8_to_utf16(name, context.temp_allocator)
buf := make([dynamic]u16, 100, allocator); buf := make([dynamic]u16, 100)
for { for {
n := win32.GetFullPathNameW(raw_data(p), u32(len(buf)), raw_data(buf), nil); n := win32.GetFullPathNameW(raw_data(p), u32(len(buf)), raw_data(buf), nil)
if n == 0 { if n == 0 {
delete(buf); delete(buf)
return "", _stat_errno(win32.GetLastError()); return "", _stat_errno(win32.GetLastError())
} }
if n <= u32(len(buf)) { if n <= u32(len(buf)) {
return win32.utf16_to_utf8(buf[:n]), nil; return win32.utf16_to_utf8(buf[:n]), nil
} }
resize(&buf, len(buf)*2); resize(&buf, len(buf)*2)
} }
return; return
} }
internal_stat :: proc(name: string, create_file_attributes: u32, allocator := context.allocator) -> (fi: File_Info, e: Maybe(Path_Error)) { internal_stat :: proc(name: string, create_file_attributes: u32, allocator := context.allocator) -> (fi: File_Info, e: Maybe(Path_Error)) {
if len(name) == 0 { if len(name) == 0 {
return {}, Path_Error{err = .Not_Exist}; return {}, Path_Error{err = .Not_Exist}
} }
context.allocator = allocator; context.allocator = allocator
wname := win32.utf8_to_wstring(_fix_long_path(name), context.temp_allocator); wname := win32.utf8_to_wstring(_fix_long_path(name), context.temp_allocator)
fa: win32.WIN32_FILE_ATTRIBUTE_DATA; fa: win32.WIN32_FILE_ATTRIBUTE_DATA
ok := win32.GetFileAttributesExW(wname, win32.GetFileExInfoStandard, &fa); ok := win32.GetFileAttributesExW(wname, win32.GetFileExInfoStandard, &fa)
if ok && fa.dwFileAttributes & win32.FILE_ATTRIBUTE_REPARSE_POINT == 0 { if ok && fa.dwFileAttributes & win32.FILE_ATTRIBUTE_REPARSE_POINT == 0 {
// Not a symlink // Not a symlink
return _file_info_from_win32_file_attribute_data(&fa, name); return _file_info_from_win32_file_attribute_data(&fa, name)
} }
err := 0 if ok else win32.GetLastError(); err := 0 if ok else win32.GetLastError()
if err == win32.ERROR_SHARING_VIOLATION { if err == win32.ERROR_SHARING_VIOLATION {
fd: win32.WIN32_FIND_DATAW; fd: win32.WIN32_FIND_DATAW
sh := win32.FindFirstFileW(wname, &fd); sh := win32.FindFirstFileW(wname, &fd)
if sh == win32.INVALID_HANDLE_VALUE { if sh == win32.INVALID_HANDLE_VALUE {
e = Path_Error{err = Platform_Error{i32(win32.GetLastError())}}; e = Path_Error{err = Platform_Error{i32(win32.GetLastError())}}
return; return
} }
win32.FindClose(sh); win32.FindClose(sh)
return _file_info_from_win32_find_data(&fd, name); return _file_info_from_win32_find_data(&fd, name)
} }
h := win32.CreateFileW(wname, 0, 0, nil, win32.OPEN_EXISTING, create_file_attributes, nil); h := win32.CreateFileW(wname, 0, 0, nil, win32.OPEN_EXISTING, create_file_attributes, nil)
if h == win32.INVALID_HANDLE_VALUE { if h == win32.INVALID_HANDLE_VALUE {
e = Path_Error{err = Platform_Error{i32(win32.GetLastError())}}; e = Path_Error{err = Platform_Error{i32(win32.GetLastError())}}
return; return
} }
defer win32.CloseHandle(h); defer win32.CloseHandle(h)
return _file_info_from_get_file_information_by_handle(name, h); return _file_info_from_get_file_information_by_handle(name, h)
} }
_cleanpath_strip_prefix :: proc(buf: []u16) -> []u16 { _cleanpath_strip_prefix :: proc(buf: []u16) -> []u16 {
buf := buf; buf := buf
N := 0; N := 0
for c, i in buf { for c, i in buf {
if c == 0 { break; } if c == 0 { break }
N = i+1; N = i+1
} }
buf = buf[:N]; buf = buf[:N]
if len(buf) >= 4 { if len(buf) >= 4 {
if buf[0] == '\\' && if buf[0] == '\\' &&
buf[1] == '\\' && buf[1] == '\\' &&
buf[2] == '?' && buf[2] == '?' &&
buf[3] == '\\' { buf[3] == '\\' {
buf = buf[4:]; buf = buf[4:]
} }
} }
return buf; return buf
} }
_cleanpath_from_handle :: proc(fd: Handle) -> (string, Maybe(Path_Error)) { _cleanpath_from_handle :: proc(fd: Handle) -> (string, Maybe(Path_Error)) {
if fd == 0 { if fd == 0 {
return "", Path_Error{err = .Invalid_Argument}; return "", Path_Error{err = .Invalid_Argument}
} }
h := win32.HANDLE(fd); h := win32.HANDLE(fd)
MAX_PATH := win32.DWORD(260) + 1; MAX_PATH := win32.DWORD(260) + 1
buf: []u16; buf: []u16
for { for {
buf = make([]u16, MAX_PATH, context.temp_allocator); buf = make([]u16, MAX_PATH, context.temp_allocator)
err := win32.GetFinalPathNameByHandleW(h, raw_data(buf), MAX_PATH, 0); err := win32.GetFinalPathNameByHandleW(h, raw_data(buf), MAX_PATH, 0)
switch err { switch err {
case win32.ERROR_PATH_NOT_FOUND, win32.ERROR_INVALID_PARAMETER: case win32.ERROR_PATH_NOT_FOUND, win32.ERROR_INVALID_PARAMETER:
return "", _stat_errno(err); return "", _stat_errno(err)
case win32.ERROR_NOT_ENOUGH_MEMORY: case win32.ERROR_NOT_ENOUGH_MEMORY:
MAX_PATH = MAX_PATH*2 + 1; MAX_PATH = MAX_PATH*2 + 1
continue; continue
} }
break; break
} }
return _cleanpath_from_buf(buf), nil; return _cleanpath_from_buf(buf), nil
} }
_cleanpath_from_handle_u16 :: proc(fd: Handle) -> ([]u16, Maybe(Path_Error)) { _cleanpath_from_handle_u16 :: proc(fd: Handle) -> ([]u16, Maybe(Path_Error)) {
if fd == 0 { if fd == 0 {
return nil, Path_Error{err = .Invalid_Argument}; return nil, Path_Error{err = .Invalid_Argument}
} }
h := win32.HANDLE(fd); h := win32.HANDLE(fd)
MAX_PATH := win32.DWORD(260) + 1; MAX_PATH := win32.DWORD(260) + 1
buf: []u16; buf: []u16
for { for {
buf = make([]u16, MAX_PATH, context.temp_allocator); buf = make([]u16, MAX_PATH, context.temp_allocator)
err := win32.GetFinalPathNameByHandleW(h, raw_data(buf), MAX_PATH, 0); err := win32.GetFinalPathNameByHandleW(h, raw_data(buf), MAX_PATH, 0)
switch err { switch err {
case win32.ERROR_PATH_NOT_FOUND, win32.ERROR_INVALID_PARAMETER: case win32.ERROR_PATH_NOT_FOUND, win32.ERROR_INVALID_PARAMETER:
return nil, _stat_errno(err); return nil, _stat_errno(err)
case win32.ERROR_NOT_ENOUGH_MEMORY: case win32.ERROR_NOT_ENOUGH_MEMORY:
MAX_PATH = MAX_PATH*2 + 1; MAX_PATH = MAX_PATH*2 + 1
continue; continue
} }
break; break
} }
return _cleanpath_strip_prefix(buf), nil; return _cleanpath_strip_prefix(buf), nil
} }
_cleanpath_from_buf :: proc(buf: []u16) -> string { _cleanpath_from_buf :: proc(buf: []u16) -> string {
buf := buf; buf := buf
buf = _cleanpath_strip_prefix(buf); buf = _cleanpath_strip_prefix(buf)
return win32.utf16_to_utf8(buf, context.allocator); return win32.utf16_to_utf8(buf, context.allocator)
} }
basename :: proc(name: string) -> (base: string) { basename :: proc(name: string) -> (base: string) {
name := name; name := name
if len(name) > 3 && name[:3] == `\\?` { if len(name) > 3 && name[:3] == `\\?` {
name = name[3:]; name = name[3:]
} }
if len(name) == 2 && name[1] == ':' { if len(name) == 2 && name[1] == ':' {
return "."; return "."
} else if len(name) > 2 && name[1] == ':' { } else if len(name) > 2 && name[1] == ':' {
name = name[2:]; name = name[2:]
} }
i := len(name)-1; i := len(name)-1
for ; i > 0 && (name[i] == '/' || name[i] == '\\'); i -= 1 { for ; i > 0 && (name[i] == '/' || name[i] == '\\'); i -= 1 {
name = name[:i]; name = name[:i]
} }
for i -= 1; i >= 0; i -= 1 { for i -= 1; i >= 0; i -= 1 {
if name[i] == '/' || name[i] == '\\' { if name[i] == '/' || name[i] == '\\' {
name = name[i+1:]; name = name[i+1:]
break; break
} }
} }
return name; return name
} }
file_type_mode :: proc(h: win32.HANDLE) -> File_Mode { file_type_mode :: proc(h: win32.HANDLE) -> File_Mode {
switch win32.GetFileType(h) { switch win32.GetFileType(h) {
case win32.FILE_TYPE_PIPE: case win32.FILE_TYPE_PIPE:
return File_Mode_Named_Pipe; return File_Mode_Named_Pipe
case win32.FILE_TYPE_CHAR: case win32.FILE_TYPE_CHAR:
return File_Mode_Device | File_Mode_Char_Device; return File_Mode_Device | File_Mode_Char_Device
} }
return 0; return 0
} }
_file_mode_from_file_attributes :: proc(FileAttributes: win32.DWORD, h: win32.HANDLE, ReparseTag: win32.DWORD) -> (mode: File_Mode) { _file_mode_from_file_attributes :: proc(FileAttributes: win32.DWORD, h: win32.HANDLE, ReparseTag: win32.DWORD) -> (mode: File_Mode) {
if FileAttributes & win32.FILE_ATTRIBUTE_READONLY != 0 { if FileAttributes & win32.FILE_ATTRIBUTE_READONLY != 0 {
mode |= 0o444; mode |= 0o444
} else { } else {
mode |= 0o666; mode |= 0o666
} }
is_sym := false; is_sym := false
if FileAttributes & win32.FILE_ATTRIBUTE_REPARSE_POINT == 0 { if FileAttributes & win32.FILE_ATTRIBUTE_REPARSE_POINT == 0 {
is_sym = false; is_sym = false
} else { } else {
is_sym = ReparseTag == win32.IO_REPARSE_TAG_SYMLINK || ReparseTag == win32.IO_REPARSE_TAG_MOUNT_POINT; is_sym = ReparseTag == win32.IO_REPARSE_TAG_SYMLINK || ReparseTag == win32.IO_REPARSE_TAG_MOUNT_POINT
} }
if is_sym { if is_sym {
mode |= File_Mode_Sym_Link; mode |= File_Mode_Sym_Link
} else { } else {
if FileAttributes & win32.FILE_ATTRIBUTE_DIRECTORY != 0 { if FileAttributes & win32.FILE_ATTRIBUTE_DIRECTORY != 0 {
mode |= 0o111 | File_Mode_Dir; mode |= 0o111 | File_Mode_Dir
} }
if h != nil { if h != nil {
mode |= file_type_mode(h); mode |= file_type_mode(h)
} }
} }
return; return
} }
_file_info_from_win32_file_attribute_data :: proc(d: ^win32.WIN32_FILE_ATTRIBUTE_DATA, name: string) -> (fi: File_Info, e: Maybe(Path_Error)) { _file_info_from_win32_file_attribute_data :: proc(d: ^win32.WIN32_FILE_ATTRIBUTE_DATA, name: string) -> (fi: File_Info, e: Maybe(Path_Error)) {
fi.size = i64(d.nFileSizeHigh)<<32 + i64(d.nFileSizeLow); fi.size = i64(d.nFileSizeHigh)<<32 + i64(d.nFileSizeLow)
fi.mode |= _file_mode_from_file_attributes(d.dwFileAttributes, nil, 0); fi.mode |= _file_mode_from_file_attributes(d.dwFileAttributes, nil, 0)
fi.is_dir = fi.mode & File_Mode_Dir != 0; fi.is_dir = fi.mode & File_Mode_Dir != 0
fi.creation_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftCreationTime)); fi.creation_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftCreationTime))
fi.modification_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastWriteTime)); fi.modification_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastWriteTime))
fi.access_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastAccessTime)); fi.access_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastAccessTime))
fi.fullpath, e = full_path_from_name(name); fi.fullpath, e = full_path_from_name(name)
fi.name = basename(fi.fullpath); fi.name = basename(fi.fullpath)
return; return
} }
_file_info_from_win32_find_data :: proc(d: ^win32.WIN32_FIND_DATAW, name: string) -> (fi: File_Info, e: Maybe(Path_Error)) { _file_info_from_win32_find_data :: proc(d: ^win32.WIN32_FIND_DATAW, name: string) -> (fi: File_Info, e: Maybe(Path_Error)) {
fi.size = i64(d.nFileSizeHigh)<<32 + i64(d.nFileSizeLow); fi.size = i64(d.nFileSizeHigh)<<32 + i64(d.nFileSizeLow)
fi.mode |= _file_mode_from_file_attributes(d.dwFileAttributes, nil, 0); fi.mode |= _file_mode_from_file_attributes(d.dwFileAttributes, nil, 0)
fi.is_dir = fi.mode & File_Mode_Dir != 0; fi.is_dir = fi.mode & File_Mode_Dir != 0
fi.creation_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftCreationTime)); fi.creation_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftCreationTime))
fi.modification_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastWriteTime)); fi.modification_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastWriteTime))
fi.access_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastAccessTime)); fi.access_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastAccessTime))
fi.fullpath, e = full_path_from_name(name); fi.fullpath, e = full_path_from_name(name)
fi.name = basename(fi.fullpath); fi.name = basename(fi.fullpath)
return; return
} }
_file_info_from_get_file_information_by_handle :: proc(path: string, h: win32.HANDLE) -> (File_Info, Maybe(Path_Error)) { _file_info_from_get_file_information_by_handle :: proc(path: string, h: win32.HANDLE) -> (File_Info, Maybe(Path_Error)) {
d: win32.BY_HANDLE_FILE_INFORMATION; d: win32.BY_HANDLE_FILE_INFORMATION
if !win32.GetFileInformationByHandle(h, &d) { if !win32.GetFileInformationByHandle(h, &d) {
return {}, _stat_errno(win32.GetLastError()); return {}, _stat_errno(win32.GetLastError())
} }
ti: win32.FILE_ATTRIBUTE_TAG_INFO; ti: win32.FILE_ATTRIBUTE_TAG_INFO
if !win32.GetFileInformationByHandleEx(h, .FileAttributeTagInfo, &ti, size_of(ti)) { if !win32.GetFileInformationByHandleEx(h, .FileAttributeTagInfo, &ti, size_of(ti)) {
err := win32.GetLastError(); err := win32.GetLastError()
if err != win32.ERROR_INVALID_PARAMETER { if err != win32.ERROR_INVALID_PARAMETER {
return {}, _stat_errno(err); return {}, _stat_errno(err)
} }
// Indicate this is a symlink on FAT file systems // Indicate this is a symlink on FAT file systems
ti.ReparseTag = 0; ti.ReparseTag = 0
} }
fi: File_Info; fi: File_Info
fi.fullpath = path; fi.fullpath = path
fi.name = basename(path); fi.name = basename(path)
fi.size = i64(d.nFileSizeHigh)<<32 + i64(d.nFileSizeLow); fi.size = i64(d.nFileSizeHigh)<<32 + i64(d.nFileSizeLow)
fi.mode |= _file_mode_from_file_attributes(ti.FileAttributes, h, ti.ReparseTag); fi.mode |= _file_mode_from_file_attributes(ti.FileAttributes, h, ti.ReparseTag)
fi.is_dir = fi.mode & File_Mode_Dir != 0; fi.is_dir = fi.mode & File_Mode_Dir != 0
fi.creation_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftCreationTime)); fi.creation_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftCreationTime))
fi.modification_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastWriteTime)); fi.modification_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastWriteTime))
fi.access_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastAccessTime)); fi.access_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastAccessTime))
return fi, nil; return fi, nil
} }
_is_abs :: proc(path: string) -> bool { _is_abs :: proc(path: string) -> bool {
if len(path) > 0 && path[0] == '/' { if len(path) > 0 && path[0] == '/' {
return true; return true
} }
if len(path) > 2 { if len(path) > 2 {
switch path[0] { switch path[0] {
case 'A'..='Z', 'a'..='z': case 'A'..='Z', 'a'..='z':
return path[1] == ':' && is_path_separator(path[2]); return path[1] == ':' && is_path_separator(path[2])
} }
} }
return false; return false
} }
_fix_long_path :: proc(path: string) -> string { _fix_long_path :: proc(path: string) -> string {
if len(path) < 248 { if len(path) < 248 {
return path; return path
} }
if len(path) >= 2 && path[:2] == `\\` { if len(path) >= 2 && path[:2] == `\\` {
return path; return path
} }
if !_is_abs(path) { if !_is_abs(path) {
return path; return path
} }
prefix :: `\\?`; prefix :: `\\?`
path_buf := make([]byte, len(prefix)+len(path)+len(`\`), context.temp_allocator); path_buf := make([]byte, len(prefix)+len(path)+len(`\`), context.temp_allocator)
copy(path_buf, prefix); copy(path_buf, prefix)
n := len(path); n := len(path)
r, w := 0, len(prefix); r, w := 0, len(prefix)
for r < n { for r < n {
switch { switch {
case is_path_separator(path[r]): case is_path_separator(path[r]):
r += 1; r += 1
case path[r] == '.' && (r+1 == n || is_path_separator(path[r+1])): case path[r] == '.' && (r+1 == n || is_path_separator(path[r+1])):
r += 1; r += 1
case r+1 < n && path[r] == '.' && path[r+1] == '.' && (r+2 == n || is_path_separator(path[r+2])): case r+1 < n && path[r] == '.' && path[r+1] == '.' && (r+2 == n || is_path_separator(path[r+2])):
return path; return path
case: case:
path_buf[w] = '\\'; path_buf[w] = '\\'
w += 1; w += 1
for ; r < n && !is_path_separator(path[r]); r += 1 { for ; r < n && !is_path_separator(path[r]); r += 1 {
path_buf[w] = path[r]; path_buf[w] = path[r]
w += 1; w += 1
} }
} }
} }
if w == len(`\\?\c:`) { if w == len(`\\?\c:`) {
path_buf[w] = '\\'; path_buf[w] = '\\'
w += 1; w += 1
} }
return string(path_buf[:w]); return string(path_buf[:w])
} }
+3 -3
View File
@@ -2,13 +2,13 @@ package os2
create_temp :: proc(dir, pattern: string) -> (Handle, Error) { create_temp :: proc(dir, pattern: string) -> (Handle, Error) {
return _create_temp(dir, pattern); return _create_temp(dir, pattern)
} }
mkdir_temp :: proc(dir, pattern: string, allocator := context.allocator) -> (string, Error) { mkdir_temp :: proc(dir, pattern: string, allocator := context.allocator) -> (string, Error) {
return _mkdir_temp(dir, pattern); return _mkdir_temp(dir, pattern)
} }
temp_dir :: proc(allocator := context.allocator) -> string { temp_dir :: proc(allocator := context.allocator) -> string {
return _temp_dir(allocator); return _temp_dir(allocator)
} }
+8 -8
View File
@@ -4,26 +4,26 @@ package os2
import win32 "core:sys/windows" import win32 "core:sys/windows"
_create_temp :: proc(dir, pattern: string) -> (Handle, Error) { _create_temp :: proc(dir, pattern: string) -> (Handle, Error) {
return 0, nil; return 0, nil
} }
_mkdir_temp :: proc(dir, pattern: string, allocator := context.allocator) -> (string, Error) { _mkdir_temp :: proc(dir, pattern: string, allocator := context.allocator) -> (string, Error) {
return "", nil; return "", nil
} }
_temp_dir :: proc(allocator := context.allocator) -> string { _temp_dir :: proc(allocator := context.allocator) -> string {
b := make([dynamic]u16, u32(win32.MAX_PATH), context.temp_allocator); b := make([dynamic]u16, u32(win32.MAX_PATH), context.temp_allocator)
for { for {
n := win32.GetTempPathW(u32(len(b)), raw_data(b)); n := win32.GetTempPathW(u32(len(b)), raw_data(b))
if n > u32(len(b)) { if n > u32(len(b)) {
resize(&b, int(n)); resize(&b, int(n))
continue; continue
} }
if n == 3 && b[1] == ':' && b[2] == '\\' { if n == 3 && b[1] == ':' && b[2] == '\\' {
} else if n > 0 && b[n-1] == '\\' { } else if n > 0 && b[n-1] == '\\' {
n -= 1; n -= 1
} }
return win32.utf16_to_utf8(b[:n], allocator); return win32.utf16_to_utf8(b[:n], allocator)
} }
} }
+24 -24
View File
@@ -5,64 +5,64 @@ import "core:strings"
user_cache_dir :: proc(allocator := context.allocator) -> (dir: string, is_defined: bool) { user_cache_dir :: proc(allocator := context.allocator) -> (dir: string, is_defined: bool) {
switch ODIN_OS { switch ODIN_OS {
case "windows": case "windows":
dir = get_env("LocalAppData"); dir = get_env("LocalAppData")
if dir != "" { if dir != "" {
dir = strings.clone(dir, allocator); dir = strings.clone(dir, allocator)
} }
case "darwin": case "darwin":
dir = get_env("HOME"); dir = get_env("HOME")
if dir != "" { if dir != "" {
dir = strings.concatenate({dir, "/Library/Caches"}, allocator); dir = strings.concatenate({dir, "/Library/Caches"}, allocator)
} }
case: // All other UNIX systems case: // All other UNIX systems
dir = get_env("XDG_CACHE_HOME"); dir = get_env("XDG_CACHE_HOME")
if dir == "" { if dir == "" {
dir = get_env("HOME"); dir = get_env("HOME")
if dir == "" { if dir == "" {
return; return
} }
dir = strings.concatenate({dir, "/.cache"}, allocator); dir = strings.concatenate({dir, "/.cache"}, allocator)
} }
} }
is_defined = dir != ""; is_defined = dir != ""
return; return
} }
user_config_dir :: proc(allocator := context.allocator) -> (dir: string, is_defined: bool) { user_config_dir :: proc(allocator := context.allocator) -> (dir: string, is_defined: bool) {
switch ODIN_OS { switch ODIN_OS {
case "windows": case "windows":
dir = get_env("AppData"); dir = get_env("AppData")
if dir != "" { if dir != "" {
dir = strings.clone(dir, allocator); dir = strings.clone(dir, allocator)
} }
case "darwin": case "darwin":
dir = get_env("HOME"); dir = get_env("HOME")
if dir != "" { if dir != "" {
dir = strings.concatenate({dir, "/Library/Application Support"}, allocator); dir = strings.concatenate({dir, "/Library/Application Support"}, allocator)
} }
case: // All other UNIX systems case: // All other UNIX systems
dir = get_env("XDG_CACHE_HOME"); dir = get_env("XDG_CACHE_HOME")
if dir == "" { if dir == "" {
dir = get_env("HOME"); dir = get_env("HOME")
if dir == "" { if dir == "" {
return; return
} }
dir = strings.concatenate({dir, "/.config"}, allocator); dir = strings.concatenate({dir, "/.config"}, allocator)
} }
} }
is_defined = dir != ""; is_defined = dir != ""
return; return
} }
user_home_dir :: proc() -> (dir: string, is_defined: bool) { user_home_dir :: proc() -> (dir: string, is_defined: bool) {
env := "HOME"; env := "HOME"
switch ODIN_OS { switch ODIN_OS {
case "windows": case "windows":
env = "USERPROFILE"; env = "USERPROFILE"
} }
if v := get_env(env); v != "" { if v := get_env(env); v != "" {
return v, true; return v, true
} }
return "", false; return "", false
} }