|
@@ -3,10 +3,7 @@
|
|
|
|
|
|
Handle :: i32;
|
|
Handle :: i32;
|
|
File_Time :: u64;
|
|
File_Time :: u64;
|
|
-Errno :: int;
|
|
|
|
-
|
|
|
|
-// TODO(zangent): Find out how to make this work on x64 and x32.
|
|
|
|
-AddressSize :: i64;
|
|
|
|
|
|
+Errno :: i32;
|
|
|
|
|
|
// INVALID_HANDLE: Handle : -1;
|
|
// INVALID_HANDLE: Handle : -1;
|
|
|
|
|
|
@@ -36,12 +33,13 @@ RTLD_NOW :: 0x002;
|
|
RTLD_BINDING_MASK :: 0x3;
|
|
RTLD_BINDING_MASK :: 0x3;
|
|
RTLD_GLOBAL :: 0x100;
|
|
RTLD_GLOBAL :: 0x100;
|
|
|
|
|
|
-args: [dynamic]string;
|
|
|
|
|
|
+// "Argv" arguments converted to Odin strings
|
|
|
|
+immutable args := _alloc_command_line_arguments();
|
|
|
|
|
|
-FileTime :: struct #ordered {
|
|
|
|
- seconds: i64,
|
|
|
|
|
|
+_File_Time :: struct #ordered {
|
|
|
|
+ seconds: i64,
|
|
nanoseconds: i32,
|
|
nanoseconds: i32,
|
|
- reserved: i32
|
|
|
|
|
|
+ reserved: i32,
|
|
}
|
|
}
|
|
|
|
|
|
// Translated from
|
|
// Translated from
|
|
@@ -49,27 +47,27 @@ FileTime :: struct #ordered {
|
|
// Validity is not guaranteed.
|
|
// Validity is not guaranteed.
|
|
|
|
|
|
Stat :: struct #ordered {
|
|
Stat :: struct #ordered {
|
|
- device_id : u64, // ID of device containing file
|
|
|
|
- serial : u64, // File serial number
|
|
|
|
- nlink : u32, // Number of hard links
|
|
|
|
- mode : u32, // Mode of the file
|
|
|
|
- uid : u32, // User ID of the file's owner
|
|
|
|
- gid : u32, // Group ID of the file's group
|
|
|
|
- _padding : i32, // 32 bits of padding
|
|
|
|
- rdev : u64, // Device ID, if device
|
|
|
|
- size : i64, // Size of the file, in bytes
|
|
|
|
- block_size : i64, // Optimal bllocksize for I/O
|
|
|
|
- blocks : i64, // Number of 512-byte blocks allocated
|
|
|
|
|
|
+ device_id: u64, // ID of device containing file
|
|
|
|
+ serial: u64, // File serial number
|
|
|
|
+ nlink: u32, // Number of hard links
|
|
|
|
+ mode: u32, // Mode of the file
|
|
|
|
+ uid: u32, // User ID of the file's owner
|
|
|
|
+ gid: u32, // Group ID of the file's group
|
|
|
|
+ _padding: i32, // 32 bits of padding
|
|
|
|
+ rdev: u64, // Device ID, if device
|
|
|
|
+ size: i64, // Size of the file, in bytes
|
|
|
|
+ block_size: i64, // Optimal bllocksize for I/O
|
|
|
|
+ blocks: i64, // Number of 512-byte blocks allocated
|
|
|
|
|
|
- last_access : FileTime, // Time of last access
|
|
|
|
- modified : FileTime, // Time of last modification
|
|
|
|
- status_change : FileTime, // Time of last status change
|
|
|
|
|
|
+ last_access: _File_Time, // Time of last access
|
|
|
|
+ modified: _File_Time, // Time of last modification
|
|
|
|
+ status_change: _File_Time, // Time of last status change
|
|
|
|
|
|
_reserve1,
|
|
_reserve1,
|
|
_reserve2,
|
|
_reserve2,
|
|
- _reserve3 : i64,
|
|
|
|
- serial : u64, // File serial number...? Maybe.
|
|
|
|
- _reserve4 : i64
|
|
|
|
|
|
+ _reserve3: i64,
|
|
|
|
+ serial_numbe: u64, // File serial number...? Maybe.
|
|
|
|
+ _reserve4: i64,
|
|
};
|
|
};
|
|
|
|
|
|
// File type
|
|
// File type
|
|
@@ -125,32 +123,32 @@ F_OK :: 0; // Test for file existance
|
|
#foreign_system_library dl "dl";
|
|
#foreign_system_library dl "dl";
|
|
#foreign_system_library libc "c";
|
|
#foreign_system_library libc "c";
|
|
|
|
|
|
-unix_open :: proc(path: ^u8, mode: int) -> Handle #foreign libc "open";
|
|
|
|
-unix_close :: proc(handle: Handle) #foreign libc "close";
|
|
|
|
-unix_read :: proc(handle: Handle, buffer: rawptr, count: int) -> AddressSize #foreign libc "read";
|
|
|
|
-unix_write :: proc(handle: Handle, buffer: rawptr, count: int) -> AddressSize #foreign libc "write";
|
|
|
|
-unix_lseek :: proc(fs: Handle, offset: AddressSize, whence: int) -> AddressSize #foreign libc "lseek";
|
|
|
|
-unix_gettid :: proc() -> u64 #foreign libc "gettid";
|
|
|
|
-unix_stat :: proc(path: ^u8, stat: ^Stat) -> int #foreign libc "stat";
|
|
|
|
-unix_access :: proc(path: ^u8, mask: int) -> int #foreign libc "access";
|
|
|
|
|
|
+_unix_open :: proc(path: ^u8, mode: int) -> Handle #foreign libc "open";
|
|
|
|
+_unix_close :: proc(fd: Handle) -> i32 #foreign libc "close";
|
|
|
|
+_unix_read :: proc(fd: Handle, buf: rawptr, size: int) -> int #foreign libc "read";
|
|
|
|
+_unix_write :: proc(fd: Handle, buf: rawptr, size: int) -> int #foreign libc "write";
|
|
|
|
+_unix_seek :: proc(fd: Handle, offset: i64, whence: i32) -> i64 #foreign libc "lseek64";
|
|
|
|
+_unix_gettid :: proc() -> u64 #foreign libc "gettid";
|
|
|
|
+_unix_stat :: proc(path: ^u8, stat: ^Stat) -> i32 #foreign libc "stat";
|
|
|
|
+_unix_access :: proc(path: ^u8, mask: int) -> i32 #foreign libc "access";
|
|
|
|
|
|
-unix_malloc :: proc(size: int) -> rawptr #foreign libc "malloc";
|
|
|
|
-unix_free :: proc(ptr: rawptr) #foreign libc "free";
|
|
|
|
-unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr #foreign libc "realloc";
|
|
|
|
-unix_getenv :: proc(^u8) -> ^u8 #foreign libc "getenv";
|
|
|
|
|
|
+_unix_malloc :: proc(size: int) -> rawptr #foreign libc "malloc";
|
|
|
|
+_unix_free :: proc(ptr: rawptr) #foreign libc "free";
|
|
|
|
+_unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr #foreign libc "realloc";
|
|
|
|
+_unix_getenv :: proc(^u8) -> ^u8 #foreign libc "getenv";
|
|
|
|
|
|
-unix_exit :: proc(status: int) #foreign libc "exit";
|
|
|
|
|
|
+_unix_exit :: proc(status: int) #foreign libc "exit";
|
|
|
|
|
|
-unix_dlopen :: proc(filename: ^u8, flags: int) -> rawptr #foreign dl "dlopen";
|
|
|
|
-unix_dlsym :: proc(handle: rawptr, symbol: ^u8) -> (proc() #cc_c) #foreign dl "dlsym";
|
|
|
|
-unix_dlclose :: proc(handle: rawptr) -> int #foreign dl "dlclose";
|
|
|
|
-unix_dlerror :: proc() -> ^u8 #foreign dl "dlerror";
|
|
|
|
|
|
+_unix_dlopen :: proc(filename: ^u8, flags: int) -> rawptr #foreign dl "dlopen";
|
|
|
|
+_unix_dlsym :: proc(handle: rawptr, symbol: ^u8) -> (proc() #cc_c) #foreign dl "dlsym";
|
|
|
|
+_unix_dlclose :: proc(handle: rawptr) -> int #foreign dl "dlclose";
|
|
|
|
+_unix_dlerror :: proc() -> ^u8 #foreign dl "dlerror";
|
|
|
|
|
|
// TODO(zangent): Change this to just `open` when Bill fixes overloading.
|
|
// TODO(zangent): Change this to just `open` when Bill fixes overloading.
|
|
open_simple :: proc(path: string, mode: int) -> (Handle, Errno) {
|
|
open_simple :: proc(path: string, mode: int) -> (Handle, Errno) {
|
|
|
|
|
|
cstr := strings.new_c_string(path);
|
|
cstr := strings.new_c_string(path);
|
|
- handle := unix_open(cstr, mode);
|
|
|
|
|
|
+ handle := _unix_open(cstr, mode);
|
|
free(cstr);
|
|
free(cstr);
|
|
if(handle == -1) {
|
|
if(handle == -1) {
|
|
return 0, 1;
|
|
return 0, 1;
|
|
@@ -163,44 +161,29 @@ open :: proc(path: string, mode: int, perm: u32) -> (Handle, Errno) {
|
|
}
|
|
}
|
|
|
|
|
|
close :: proc(fd: Handle) {
|
|
close :: proc(fd: Handle) {
|
|
- unix_close(fd);
|
|
|
|
|
|
+ _unix_close(fd);
|
|
}
|
|
}
|
|
|
|
|
|
-write :: proc(fd: Handle, data: []byte) -> (AddressSize, Errno) {
|
|
|
|
- assert(fd != -1);
|
|
|
|
-
|
|
|
|
- bytes_written := unix_write(fd, ^data[0], len(data));
|
|
|
|
- if(bytes_written == -1) {
|
|
|
|
- return 0, 1;
|
|
|
|
- }
|
|
|
|
- return bytes_written, 0;
|
|
|
|
|
|
+read :: proc(fd: Handle, data: []byte) -> (int, Errno) {
|
|
|
|
+ sz := _unix_read(fd, ^data[0], len(data));
|
|
|
|
+ return sz, 0;
|
|
}
|
|
}
|
|
|
|
|
|
-read :: proc(fd: Handle, data: []byte) -> (AddressSize, Errno) {
|
|
|
|
- assert(fd != -1);
|
|
|
|
-
|
|
|
|
- bytes_read := unix_read(fd, ^data[0], len(data));
|
|
|
|
- if(bytes_read == -1) {
|
|
|
|
- return 0, 1;
|
|
|
|
- }
|
|
|
|
- return bytes_read, 0;
|
|
|
|
|
|
+write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
|
|
|
|
+ sz := _unix_write(fd, ^data[0], len(data));
|
|
|
|
+ return sz, 0;
|
|
}
|
|
}
|
|
|
|
|
|
-seek :: proc(fd: Handle, offset: AddressSize, whence: int) -> (AddressSize, Errno) {
|
|
|
|
- assert(fd != -1);
|
|
|
|
-
|
|
|
|
- final_offset := unix_lseek(fd, offset, whence);
|
|
|
|
- if(final_offset == -1) {
|
|
|
|
- return 0, 1;
|
|
|
|
- }
|
|
|
|
- return final_offset, 0;
|
|
|
|
|
|
+seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
|
|
|
|
+ res := _unix_seek(fd, offset, cast(i32)whence);
|
|
|
|
+ return res, 0;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
// NOTE(bill): Uses startup to initialize it
|
|
// NOTE(bill): Uses startup to initialize it
|
|
-stdin: Handle = 0; // get_std_handle(win32.STD_INPUT_HANDLE);
|
|
|
|
-stdout: Handle = 1; // get_std_handle(win32.STD_OUTPUT_HANDLE);
|
|
|
|
-stderr: Handle = 2; // get_std_handle(win32.STD_ERROR_HANDLE);
|
|
|
|
|
|
+stdin: Handle = 0;
|
|
|
|
+stdout: Handle = 1;
|
|
|
|
+stderr: Handle = 2;
|
|
|
|
|
|
/* TODO(zangent): Implement these!
|
|
/* TODO(zangent): Implement these!
|
|
last_write_time :: proc(fd: Handle) -> File_Time {}
|
|
last_write_time :: proc(fd: Handle) -> File_Time {}
|
|
@@ -211,35 +194,36 @@ stat :: proc(path: string) -> (Stat, int) #inline {
|
|
s: Stat;
|
|
s: Stat;
|
|
cstr := strings.new_c_string(path);
|
|
cstr := strings.new_c_string(path);
|
|
defer free(cstr);
|
|
defer free(cstr);
|
|
- ret_int := unix_stat(cstr, ^s);
|
|
|
|
- return s, ret_int;
|
|
|
|
|
|
+ ret_int := _unix_stat(cstr, ^s);
|
|
|
|
+ return s, cast(int)ret_int;
|
|
}
|
|
}
|
|
|
|
|
|
access :: proc(path: string, mask: int) -> bool #inline {
|
|
access :: proc(path: string, mask: int) -> bool #inline {
|
|
cstr := strings.new_c_string(path);
|
|
cstr := strings.new_c_string(path);
|
|
defer free(cstr);
|
|
defer free(cstr);
|
|
- return unix_access(cstr, mask) == 0;
|
|
|
|
|
|
+ return _unix_access(cstr, mask) == 0;
|
|
}
|
|
}
|
|
|
|
|
|
read_entire_file :: proc(name: string) -> ([]byte, bool) {
|
|
read_entire_file :: proc(name: string) -> ([]byte, bool) {
|
|
|
|
+ fd: Handle;
|
|
|
|
+ err: Errno;
|
|
|
|
+ size: i64;
|
|
|
|
|
|
- handle, err := open_simple(name, O_RDONLY);
|
|
|
|
|
|
+ fd, err = open_simple(name, O_RDONLY);
|
|
if(err != 0) {
|
|
if(err != 0) {
|
|
fmt.println("Failed to open file.");
|
|
fmt.println("Failed to open file.");
|
|
return nil, false;
|
|
return nil, false;
|
|
}
|
|
}
|
|
- defer(close(handle));
|
|
|
|
-
|
|
|
|
- // We have a file!
|
|
|
|
|
|
+ defer close(fd);
|
|
|
|
|
|
- size: AddressSize;
|
|
|
|
- size, err = seek(handle, 0, SEEK_END);
|
|
|
|
|
|
+ // We have a file
|
|
|
|
+ size, err = seek(fd, 0, SEEK_END);
|
|
if(err != 0) {
|
|
if(err != 0) {
|
|
fmt.println("Failed to seek to end of file.");
|
|
fmt.println("Failed to seek to end of file.");
|
|
return nil, false;
|
|
return nil, false;
|
|
}
|
|
}
|
|
|
|
|
|
- _, err = seek(handle, 0, SEEK_SET);
|
|
|
|
|
|
+ _, err = seek(fd, 0, SEEK_SET);
|
|
if(err != 0) {
|
|
if(err != 0) {
|
|
fmt.println("Failed to seek to beginning of file.");
|
|
fmt.println("Failed to seek to beginning of file.");
|
|
return nil, false;
|
|
return nil, false;
|
|
@@ -253,7 +237,7 @@ read_entire_file :: proc(name: string) -> ([]byte, bool) {
|
|
return nil, false;
|
|
return nil, false;
|
|
}
|
|
}
|
|
|
|
|
|
- read(handle, data);
|
|
|
|
|
|
+ read(fd, data);
|
|
data[size] = 0;
|
|
data[size] = 0;
|
|
|
|
|
|
return data, true;
|
|
return data, true;
|
|
@@ -261,20 +245,20 @@ read_entire_file :: proc(name: string) -> ([]byte, bool) {
|
|
|
|
|
|
heap_alloc :: proc(size: int) -> rawptr {
|
|
heap_alloc :: proc(size: int) -> rawptr {
|
|
assert(size > 0);
|
|
assert(size > 0);
|
|
- return unix_malloc(size);
|
|
|
|
|
|
+ return _unix_malloc(size);
|
|
}
|
|
}
|
|
|
|
|
|
heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
|
|
heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
|
|
- return unix_realloc(ptr, new_size);
|
|
|
|
|
|
+ return _unix_realloc(ptr, new_size);
|
|
}
|
|
}
|
|
|
|
|
|
heap_free :: proc(ptr: rawptr) {
|
|
heap_free :: proc(ptr: rawptr) {
|
|
- unix_free(ptr);
|
|
|
|
|
|
+ _unix_free(ptr);
|
|
}
|
|
}
|
|
|
|
|
|
getenv :: proc(name: string) -> (string, bool) {
|
|
getenv :: proc(name: string) -> (string, bool) {
|
|
path_str := strings.new_c_string(name);
|
|
path_str := strings.new_c_string(name);
|
|
- cstr: ^u8 = unix_getenv(path_str);
|
|
|
|
|
|
+ cstr: ^u8 = _unix_getenv(path_str);
|
|
free(path_str);
|
|
free(path_str);
|
|
if(cstr == nil) {
|
|
if(cstr == nil) {
|
|
return "", false;
|
|
return "", false;
|
|
@@ -283,31 +267,37 @@ getenv :: proc(name: string) -> (string, bool) {
|
|
}
|
|
}
|
|
|
|
|
|
exit :: proc(code: int) {
|
|
exit :: proc(code: int) {
|
|
- unix_exit(code);
|
|
|
|
|
|
+ _unix_exit(code);
|
|
}
|
|
}
|
|
|
|
|
|
current_thread_id :: proc() -> int {
|
|
current_thread_id :: proc() -> int {
|
|
- // return cast(int) unix_gettid();
|
|
|
|
|
|
+ // return cast(int) _unix_gettid();
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
dlopen :: proc(filename: string, flags: int) -> rawptr #inline {
|
|
dlopen :: proc(filename: string, flags: int) -> rawptr #inline {
|
|
cstr := strings.new_c_string(filename);
|
|
cstr := strings.new_c_string(filename);
|
|
- handle := unix_dlopen(cstr, flags);
|
|
|
|
|
|
+ handle := _unix_dlopen(cstr, flags);
|
|
free(cstr);
|
|
free(cstr);
|
|
return handle;
|
|
return handle;
|
|
}
|
|
}
|
|
dlsym :: proc(handle: rawptr, symbol: string) -> (proc() #cc_c) #inline {
|
|
dlsym :: proc(handle: rawptr, symbol: string) -> (proc() #cc_c) #inline {
|
|
assert(handle != nil);
|
|
assert(handle != nil);
|
|
cstr := strings.new_c_string(symbol);
|
|
cstr := strings.new_c_string(symbol);
|
|
- proc_handle := unix_dlsym(handle, cstr);
|
|
|
|
|
|
+ proc_handle := _unix_dlsym(handle, cstr);
|
|
free(cstr);
|
|
free(cstr);
|
|
return proc_handle;
|
|
return proc_handle;
|
|
}
|
|
}
|
|
dlclose :: proc(handle: rawptr) -> bool #inline {
|
|
dlclose :: proc(handle: rawptr) -> bool #inline {
|
|
assert(handle != nil);
|
|
assert(handle != nil);
|
|
- return unix_dlclose(handle) == 0;
|
|
|
|
|
|
+ return _unix_dlclose(handle) == 0;
|
|
}
|
|
}
|
|
dlerror :: proc() -> string {
|
|
dlerror :: proc() -> string {
|
|
- return strings.to_odin_string(unix_dlerror());
|
|
|
|
|
|
+ return strings.to_odin_string(_unix_dlerror());
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+_alloc_command_line_arguments :: proc() -> []string {
|
|
|
|
+ // TODO(bill):
|
|
|
|
+ return nil;
|
|
}
|
|
}
|