os_darwin.odin 8.3 KB

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