os_osx.odin 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. package os
  2. foreign import dl "system:dl"
  3. foreign import libc "system:c"
  4. import "core:runtime"
  5. import "core:strings"
  6. OS :: "osx";
  7. Handle :: distinct i32;
  8. File_Time :: distinct u64;
  9. Errno :: distinct int;
  10. O_RDONLY :: 0x00000;
  11. O_WRONLY :: 0x00001;
  12. O_RDWR :: 0x00002;
  13. O_CREATE :: 0x00040;
  14. O_EXCL :: 0x00080;
  15. O_NOCTTY :: 0x00100;
  16. O_TRUNC :: 0x00200;
  17. O_NONBLOCK :: 0x00800;
  18. O_APPEND :: 0x00400;
  19. O_SYNC :: 0x01000;
  20. O_ASYNC :: 0x02000;
  21. O_CLOEXEC :: 0x80000;
  22. SEEK_SET :: 0;
  23. SEEK_CUR :: 1;
  24. SEEK_END :: 2;
  25. SEEK_DATA :: 3;
  26. SEEK_HOLE :: 4;
  27. SEEK_MAX :: SEEK_HOLE;
  28. // NOTE(zangent): These are OS specific!
  29. // Do not mix these up!
  30. RTLD_LAZY :: 0x1;
  31. RTLD_NOW :: 0x2;
  32. RTLD_LOCAL :: 0x4;
  33. RTLD_GLOBAL :: 0x8;
  34. RTLD_NODELETE :: 0x80;
  35. RTLD_NOLOAD :: 0x10;
  36. RTLD_FIRST :: 0x100;
  37. // "Argv" arguments converted to Odin strings
  38. args := _alloc_command_line_arguments();
  39. _File_Time :: struct {
  40. seconds: i64,
  41. nanoseconds: i64,
  42. }
  43. Stat :: struct {
  44. device_id: i32, // ID of device containing file
  45. mode: u16, // Mode of the file
  46. nlink: u16, // Number of hard links
  47. serial: u64, // File serial number
  48. uid: u32, // User ID of the file's owner
  49. gid: u32, // Group ID of the file's group
  50. rdev: i32, // Device ID, if device
  51. last_access: File_Time, // Time of last access
  52. modified: File_Time, // Time of last modification
  53. status_change: File_Time, // Time of last status change
  54. created: File_Time, // Time of creation
  55. size: i64, // Size of the file, in bytes
  56. blocks: i64, // Number of blocks allocated for the file
  57. block_size: i32, // Optimal blocksize for I/O
  58. flags: u32, // User-defined flags for the file
  59. gen_num: u32, // File generation number ..?
  60. _spare: i32, // RESERVED
  61. _reserve1,
  62. _reserve2: i64, // RESERVED
  63. };
  64. // File type
  65. S_IFMT :: 0170000; // Type of file mask
  66. S_IFIFO :: 0010000; // Named pipe (fifo)
  67. S_IFCHR :: 0020000; // Character special
  68. S_IFDIR :: 0040000; // Directory
  69. S_IFBLK :: 0060000; // Block special
  70. S_IFREG :: 0100000; // Regular
  71. S_IFLNK :: 0120000; // Symbolic link
  72. S_IFSOCK :: 0140000; // Socket
  73. // File mode
  74. // Read, write, execute/search by owner
  75. S_IRWXU :: 0000700; // RWX mask for owner
  76. S_IRUSR :: 0000400; // R for owner
  77. S_IWUSR :: 0000200; // W for owner
  78. S_IXUSR :: 0000100; // X for owner
  79. // Read, write, execute/search by group
  80. S_IRWXG :: 0000070; // RWX mask for group
  81. S_IRGRP :: 0000040; // R for group
  82. S_IWGRP :: 0000020; // W for group
  83. S_IXGRP :: 0000010; // X for group
  84. // Read, write, execute/search by others
  85. S_IRWXO :: 0000007; // RWX mask for other
  86. S_IROTH :: 0000004; // R for other
  87. S_IWOTH :: 0000002; // W for other
  88. S_IXOTH :: 0000001; // X for other
  89. S_ISUID :: 0004000; // Set user id on execution
  90. S_ISGID :: 0002000; // Set group id on execution
  91. S_ISVTX :: 0001000; // Directory restrcted delete
  92. S_ISLNK :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFLNK;
  93. S_ISREG :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFREG;
  94. S_ISDIR :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFDIR;
  95. S_ISCHR :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFCHR;
  96. S_ISBLK :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFBLK;
  97. S_ISFIFO :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFIFO;
  98. S_ISSOCK :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFSOCK;
  99. R_OK :: 4; // Test for read permission
  100. W_OK :: 2; // Test for write permission
  101. X_OK :: 1; // Test for execute permission
  102. F_OK :: 0; // Test for file existance
  103. foreign libc {
  104. @(link_name="open") _unix_open :: proc(path: cstring, mode: int) -> Handle ---;
  105. @(link_name="close") _unix_close :: proc(handle: Handle) ---;
  106. @(link_name="read") _unix_read :: proc(handle: Handle, buffer: rawptr, count: int) -> int ---;
  107. @(link_name="write") _unix_write :: proc(handle: Handle, buffer: rawptr, count: int) -> int ---;
  108. @(link_name="lseek") _unix_lseek :: proc(fs: Handle, offset: int, whence: int) -> int ---;
  109. @(link_name="gettid") _unix_gettid :: proc() -> u64 ---;
  110. @(link_name="stat") _unix_stat :: proc(path: cstring, stat: ^Stat) -> int ---;
  111. @(link_name="access") _unix_access :: proc(path: cstring, mask: int) -> int ---;
  112. @(link_name="malloc") _unix_malloc :: proc(size: int) -> rawptr ---;
  113. @(link_name="calloc") _unix_calloc :: proc(num, size: int) -> rawptr ---;
  114. @(link_name="free") _unix_free :: proc(ptr: rawptr) ---;
  115. @(link_name="realloc") _unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr ---;
  116. @(link_name="getenv") _unix_getenv :: proc(cstring) -> cstring ---;
  117. @(link_name="exit") _unix_exit :: proc(status: int) ---;
  118. }
  119. foreign dl {
  120. @(link_name="dlopen") _unix_dlopen :: proc(filename: cstring, flags: int) -> rawptr ---;
  121. @(link_name="dlsym") _unix_dlsym :: proc(handle: rawptr, symbol: cstring) -> rawptr ---;
  122. @(link_name="dlclose") _unix_dlclose :: proc(handle: rawptr) -> int ---;
  123. @(link_name="dlerror") _unix_dlerror :: proc() -> cstring ---;
  124. }
  125. // TODO(zangent): Change this to just `open` when Bill fixes overloading.
  126. open_simple :: proc(path: string, mode: int) -> (Handle, Errno) {
  127. cstr := strings.new_cstring(path);
  128. defer delete(cstr);
  129. handle := _unix_open(cstr, mode);
  130. if handle == -1 {
  131. return 0, 1;
  132. }
  133. return handle, 0;
  134. }
  135. // NOTE(zangent): This is here for compatability reasons. Should this be here?
  136. open :: proc(path: string, mode: int = O_RDONLY, perm: u32 = 0) -> (Handle, Errno) {
  137. return open_simple(path, mode);
  138. }
  139. close :: proc(fd: Handle) {
  140. _unix_close(fd);
  141. }
  142. write :: proc(fd: Handle, data: []u8) -> (int, Errno) {
  143. assert(fd != -1);
  144. bytes_written := _unix_write(fd, &data[0], len(data));
  145. if(bytes_written == -1) {
  146. return 0, 1;
  147. }
  148. return bytes_written, 0;
  149. }
  150. read :: proc(fd: Handle, data: []u8) -> (int, Errno) {
  151. assert(fd != -1);
  152. bytes_read := _unix_read(fd, &data[0], len(data));
  153. if bytes_read == -1 {
  154. return 0, 1;
  155. }
  156. return bytes_read, 0;
  157. }
  158. seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
  159. assert(fd != -1);
  160. final_offset := i64(_unix_lseek(fd, int(offset), whence));
  161. if final_offset == -1 {
  162. return 0, 1;
  163. }
  164. return final_offset, 0;
  165. }
  166. file_size :: proc(fd: Handle) -> (i64, Errno) {
  167. prev, _ := seek(fd, 0, SEEK_CUR);
  168. size, err := seek(fd, 0, SEEK_END);
  169. seek(fd, prev, SEEK_SET);
  170. return i64(size), err;
  171. }
  172. // NOTE(bill): Uses startup to initialize it
  173. stdin: Handle = 0; // get_std_handle(win32.STD_INPUT_HANDLE);
  174. stdout: Handle = 1; // get_std_handle(win32.STD_OUTPUT_HANDLE);
  175. stderr: Handle = 2; // get_std_handle(win32.STD_ERROR_HANDLE);
  176. /* TODO(zangent): Implement these!
  177. last_write_time :: proc(fd: Handle) -> File_Time {}
  178. last_write_time_by_name :: proc(name: string) -> File_Time {}
  179. */
  180. stat :: inline proc(path: string) -> (Stat, bool) {
  181. s: Stat;
  182. cstr := strings.new_cstring(path);
  183. defer delete(cstr);
  184. ret_int := _unix_stat(cstr, &s);
  185. return s, ret_int==0;
  186. }
  187. access :: inline proc(path: string, mask: int) -> bool {
  188. cstr := strings.new_cstring(path);
  189. defer delete(cstr);
  190. return _unix_access(cstr, mask) == 0;
  191. }
  192. heap_alloc :: inline proc(size: int) -> rawptr {
  193. assert(size > 0);
  194. return _unix_calloc(1, size);
  195. }
  196. heap_resize :: inline proc(ptr: rawptr, new_size: int) -> rawptr {
  197. return _unix_realloc(ptr, new_size);
  198. }
  199. heap_free :: inline proc(ptr: rawptr) {
  200. _unix_free(ptr);
  201. }
  202. getenv :: proc(name: string) -> (string, bool) {
  203. path_str := strings.new_cstring(name);
  204. defer delete(path_str);
  205. cstr := _unix_getenv(path_str);
  206. if cstr == nil {
  207. return "", false;
  208. }
  209. return string(cstr), true;
  210. }
  211. exit :: inline proc(code: int) {
  212. _unix_exit(code);
  213. }
  214. current_thread_id :: proc() -> int {
  215. // return int(_unix_gettid());
  216. return 0;
  217. }
  218. dlopen :: inline proc(filename: string, flags: int) -> rawptr {
  219. cstr := strings.new_cstring(filename);
  220. defer delete(cstr);
  221. handle := _unix_dlopen(cstr, flags);
  222. return handle;
  223. }
  224. dlsym :: inline proc(handle: rawptr, symbol: string) -> rawptr {
  225. assert(handle != nil);
  226. cstr := strings.new_cstring(symbol);
  227. defer delete(cstr);
  228. proc_handle := _unix_dlsym(handle, cstr);
  229. return proc_handle;
  230. }
  231. dlclose :: inline proc(handle: rawptr) -> bool {
  232. assert(handle != nil);
  233. return _unix_dlclose(handle) == 0;
  234. }
  235. dlerror :: proc() -> string {
  236. return string(_unix_dlerror());
  237. }
  238. _alloc_command_line_arguments :: proc() -> []string {
  239. args := make([]string, len(runtime.args__));
  240. for arg, i in runtime.args__ {
  241. args[i] = string(arg);
  242. }
  243. return args;
  244. }