os_darwin.odin 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. ERROR_NONE: Errno : 0;
  12. EPERM: Errno : 1; /* Operation not permitted */
  13. ENOENT: Errno : 2; /* No such file or directory */
  14. ESRCH: Errno : 3; /* No such process */
  15. EINTR: Errno : 4; /* Interrupted system call */
  16. EIO: Errno : 5; /* Input/output error */
  17. ENXIO: Errno : 6; /* Device not configured */
  18. E2BIG: Errno : 7; /* Argument list too long */
  19. ENOEXEC: Errno : 8; /* Exec format error */
  20. EBADF: Errno : 9; /* Bad file descriptor */
  21. ECHILD: Errno : 10; /* No child processes */
  22. EDEADLK: Errno : 11; /* Resource deadlock avoided */
  23. ENOMEM: Errno : 12; /* Cannot allocate memory */
  24. EACCES: Errno : 13; /* Permission denied */
  25. EFAULT: Errno : 14; /* Bad address */
  26. ENOTBLK: Errno : 15; /* Block device required */
  27. EBUSY: Errno : 16; /* Device / Resource busy */
  28. EEXIST: Errno : 17; /* File exists */
  29. EXDEV: Errno : 18; /* Cross-device link */
  30. ENODEV: Errno : 19; /* Operation not supported by device */
  31. ENOTDIR: Errno : 20; /* Not a directory */
  32. EISDIR: Errno : 21; /* Is a directory */
  33. EINVAL: Errno : 22; /* Invalid argument */
  34. ENFILE: Errno : 23; /* Too many open files in system */
  35. EMFILE: Errno : 24; /* Too many open files */
  36. ENOTTY: Errno : 25; /* Inappropriate ioctl for device */
  37. ETXTBSY: Errno : 26; /* Text file busy */
  38. EFBIG: Errno : 27; /* File too large */
  39. ENOSPC: Errno : 28; /* No space left on device */
  40. ESPIPE: Errno : 29; /* Illegal seek */
  41. EROFS: Errno : 30; /* Read-only file system */
  42. EMLINK: Errno : 31; /* Too many links */
  43. EPIPE: Errno : 32; /* Broken pipe */
  44. /* math software */
  45. EDOM: Errno : 33; /* Numerical argument out of domain */
  46. ERANGE: Errno : 34; /* Result too large */
  47. /* non-blocking and interrupt i/o */
  48. EAGAIN: Errno : 35; /* Resource temporarily unavailable */
  49. EWOULDBLOCK: Errno : EAGAIN; /* Operation would block */
  50. EINPROGRESS: Errno : 36; /* Operation now in progress */
  51. EALREADY: Errno : 37; /* Operation already in progress */
  52. /* ipc/network software -- argument errors */
  53. ENOTSOCK: Errno : 38; /* Socket operation on non-socket */
  54. EDESTADDRREQ: Errno : 39; /* Destination address required */
  55. EMSGSIZE: Errno : 40; /* Message too long */
  56. EPROTOTYPE: Errno : 41; /* Protocol wrong type for socket */
  57. ENOPROTOOPT: Errno : 42; /* Protocol not available */
  58. EPROTONOSUPPORT: Errno : 43; /* Protocol not supported */
  59. ESOCKTNOSUPPORT: Errno : 44; /* Socket type not supported */
  60. ENOTSUP: Errno : 45; /* Operation not supported */
  61. EPFNOSUPPORT: Errno : 46; /* Protocol family not supported */
  62. EAFNOSUPPORT: Errno : 47; /* Address family not supported by protocol family */
  63. EADDRINUSE: Errno : 48; /* Address already in use */
  64. EADDRNOTAVAIL: Errno : 49; /* Can't assign requested address */
  65. /* ipc/network software -- operational errors */
  66. ENETDOWN: Errno : 50; /* Network is down */
  67. ENETUNREACH: Errno : 51; /* Network is unreachable */
  68. ENETRESET: Errno : 52; /* Network dropped connection on reset */
  69. ECONNABORTED: Errno : 53; /* Software caused connection abort */
  70. ECONNRESET: Errno : 54; /* Connection reset by peer */
  71. ENOBUFS: Errno : 55; /* No buffer space available */
  72. EISCONN: Errno : 56; /* Socket is already connected */
  73. ENOTCONN: Errno : 57; /* Socket is not connected */
  74. ESHUTDOWN: Errno : 58; /* Can't send after socket shutdown */
  75. ETOOMANYREFS: Errno : 59; /* Too many references: can't splice */
  76. ETIMEDOUT: Errno : 60; /* Operation timed out */
  77. ECONNREFUSED: Errno : 61; /* Connection refused */
  78. ELOOP: Errno : 62; /* Too many levels of symbolic links */
  79. ENAMETOOLONG: Errno : 63; /* File name too long */
  80. /* should be rearranged */
  81. EHOSTDOWN: Errno : 64; /* Host is down */
  82. EHOSTUNREACH: Errno : 65; /* No route to host */
  83. ENOTEMPTY: Errno : 66; /* Directory not empty */
  84. /* quotas & mush */
  85. EPROCLIM: Errno : 67; /* Too many processes */
  86. EUSERS: Errno : 68; /* Too many users */
  87. EDQUOT: Errno : 69; /* Disc quota exceeded */
  88. /* Network File System */
  89. ESTALE: Errno : 70; /* Stale NFS file handle */
  90. EREMOTE: Errno : 71; /* Too many levels of remote in path */
  91. EBADRPC: Errno : 72; /* RPC struct is bad */
  92. ERPCMISMATCH: Errno : 73; /* RPC version wrong */
  93. EPROGUNAVAIL: Errno : 74; /* RPC prog. not avail */
  94. EPROGMISMATCH: Errno : 75; /* Program version wrong */
  95. EPROCUNAVAIL: Errno : 76; /* Bad procedure for program */
  96. ENOLCK: Errno : 77; /* No locks available */
  97. ENOSYS: Errno : 78; /* Function not implemented */
  98. EFTYPE: Errno : 79; /* Inappropriate file type or format */
  99. EAUTH: Errno : 80; /* Authentication error */
  100. ENEEDAUTH: Errno : 81; /* Need authenticator */
  101. /* Intelligent device errors */
  102. EPWROFF: Errno : 82; /* Device power is off */
  103. EDEVERR: Errno : 83; /* Device error, e.g. paper out */
  104. EOVERFLOW: Errno : 84; /* Value too large to be stored in data type */
  105. /* Program loading errors */
  106. EBADEXEC: Errno : 85; /* Bad executable */
  107. EBADARCH: Errno : 86; /* Bad CPU type in executable */
  108. ESHLIBVERS: Errno : 87; /* Shared library version mismatch */
  109. EBADMACHO: Errno : 88; /* Malformed Macho file */
  110. ECANCELED: Errno : 89; /* Operation canceled */
  111. EIDRM: Errno : 90; /* Identifier removed */
  112. ENOMSG: Errno : 91; /* No message of desired type */
  113. EILSEQ: Errno : 92; /* Illegal byte sequence */
  114. ENOATTR: Errno : 93; /* Attribute not found */
  115. EBADMSG: Errno : 94; /* Bad message */
  116. EMULTIHOP: Errno : 95; /* Reserved */
  117. ENODATA: Errno : 96; /* No message available on STREAM */
  118. ENOLINK: Errno : 97; /* Reserved */
  119. ENOSR: Errno : 98; /* No STREAM resources */
  120. ENOSTR: Errno : 99; /* Not a STREAM */
  121. EPROTO: Errno : 100; /* Protocol error */
  122. ETIME: Errno : 101; /* STREAM ioctl timeout */
  123. ENOPOLICY: Errno : 103; /* No such policy registered */
  124. ENOTRECOVERABLE: Errno : 104; /* State not recoverable */
  125. EOWNERDEAD: Errno : 105; /* Previous owner died */
  126. EQFULL: Errno : 106; /* Interface output queue is full */
  127. ELAST: Errno : 106; /* Must be equal largest errno */
  128. O_RDONLY :: 0x00000;
  129. O_WRONLY :: 0x00001;
  130. O_RDWR :: 0x00002;
  131. O_CREATE :: 0x00040;
  132. O_EXCL :: 0x00080;
  133. O_NOCTTY :: 0x00100;
  134. O_TRUNC :: 0x00200;
  135. O_NONBLOCK :: 0x00800;
  136. O_APPEND :: 0x00400;
  137. O_SYNC :: 0x01000;
  138. O_ASYNC :: 0x02000;
  139. O_CLOEXEC :: 0x80000;
  140. SEEK_SET :: 0;
  141. SEEK_CUR :: 1;
  142. SEEK_END :: 2;
  143. SEEK_DATA :: 3;
  144. SEEK_HOLE :: 4;
  145. SEEK_MAX :: SEEK_HOLE;
  146. // NOTE(zangent): These are OS specific!
  147. // Do not mix these up!
  148. RTLD_LAZY :: 0x1;
  149. RTLD_NOW :: 0x2;
  150. RTLD_LOCAL :: 0x4;
  151. RTLD_GLOBAL :: 0x8;
  152. RTLD_NODELETE :: 0x80;
  153. RTLD_NOLOAD :: 0x10;
  154. RTLD_FIRST :: 0x100;
  155. // "Argv" arguments converted to Odin strings
  156. args := _alloc_command_line_arguments();
  157. _File_Time :: struct {
  158. seconds: i64,
  159. nanoseconds: i64,
  160. }
  161. Stat :: struct {
  162. device_id: i32, // ID of device containing file
  163. mode: u16, // Mode of the file
  164. nlink: u16, // Number of hard links
  165. serial: u64, // File serial number
  166. uid: u32, // User ID of the file's owner
  167. gid: u32, // Group ID of the file's group
  168. rdev: i32, // Device ID, if device
  169. last_access: File_Time, // Time of last access
  170. modified: File_Time, // Time of last modification
  171. status_change: File_Time, // Time of last status change
  172. created: File_Time, // Time of creation
  173. size: i64, // Size of the file, in bytes
  174. blocks: i64, // Number of blocks allocated for the file
  175. block_size: i32, // Optimal blocksize for I/O
  176. flags: u32, // User-defined flags for the file
  177. gen_num: u32, // File generation number ..?
  178. _spare: i32, // RESERVED
  179. _reserve1,
  180. _reserve2: i64, // RESERVED
  181. };
  182. // File type
  183. S_IFMT :: 0o170000; // Type of file mask
  184. S_IFIFO :: 0o010000; // Named pipe (fifo)
  185. S_IFCHR :: 0o020000; // Character special
  186. S_IFDIR :: 0o040000; // Directory
  187. S_IFBLK :: 0o060000; // Block special
  188. S_IFREG :: 0o100000; // Regular
  189. S_IFLNK :: 0o120000; // Symbolic link
  190. S_IFSOCK :: 0o140000; // Socket
  191. // File mode
  192. // Read, write, execute/search by owner
  193. S_IRWXU :: 0o0700; // RWX mask for owner
  194. S_IRUSR :: 0o0400; // R for owner
  195. S_IWUSR :: 0o0200; // W for owner
  196. S_IXUSR :: 0o0100; // X for owner
  197. // Read, write, execute/search by group
  198. S_IRWXG :: 0o0070; // RWX mask for group
  199. S_IRGRP :: 0o0040; // R for group
  200. S_IWGRP :: 0o0020; // W for group
  201. S_IXGRP :: 0o0010; // X for group
  202. // Read, write, execute/search by others
  203. S_IRWXO :: 0o0007; // RWX mask for other
  204. S_IROTH :: 0o0004; // R for other
  205. S_IWOTH :: 0o0002; // W for other
  206. S_IXOTH :: 0o0001; // X for other
  207. S_ISUID :: 0o4000; // Set user id on execution
  208. S_ISGID :: 0o2000; // Set group id on execution
  209. S_ISVTX :: 0o1000; // Directory restrcted delete
  210. S_ISLNK :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFLNK;
  211. S_ISREG :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFREG;
  212. S_ISDIR :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFDIR;
  213. S_ISCHR :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFCHR;
  214. S_ISBLK :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFBLK;
  215. S_ISFIFO :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFIFO;
  216. S_ISSOCK :: inline proc(m: u32) -> bool do return (m & S_IFMT) == S_IFSOCK;
  217. R_OK :: 4; // Test for read permission
  218. W_OK :: 2; // Test for write permission
  219. X_OK :: 1; // Test for execute permission
  220. F_OK :: 0; // Test for file existance
  221. foreign libc {
  222. @(link_name="open") _unix_open :: proc(path: cstring, flags: int, #c_vararg mode: ..any) -> Handle ---;
  223. @(link_name="close") _unix_close :: proc(handle: Handle) ---;
  224. @(link_name="read") _unix_read :: proc(handle: Handle, buffer: rawptr, count: int) -> int ---;
  225. @(link_name="write") _unix_write :: proc(handle: Handle, buffer: rawptr, count: int) -> int ---;
  226. @(link_name="lseek") _unix_lseek :: proc(fs: Handle, offset: int, whence: int) -> int ---;
  227. @(link_name="gettid") _unix_gettid :: proc() -> u64 ---;
  228. @(link_name="getpagesize") _unix_getpagesize :: proc() -> i32 ---;
  229. @(link_name="stat") _unix_stat :: proc(path: cstring, stat: ^Stat) -> int ---;
  230. @(link_name="access") _unix_access :: proc(path: cstring, mask: int) -> int ---;
  231. @(link_name="malloc") _unix_malloc :: proc(size: int) -> rawptr ---;
  232. @(link_name="calloc") _unix_calloc :: proc(num, size: int) -> rawptr ---;
  233. @(link_name="free") _unix_free :: proc(ptr: rawptr) ---;
  234. @(link_name="realloc") _unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr ---;
  235. @(link_name="getenv") _unix_getenv :: proc(cstring) -> cstring ---;
  236. @(link_name="exit") _unix_exit :: proc(status: int) ---;
  237. }
  238. foreign dl {
  239. @(link_name="dlopen") _unix_dlopen :: proc(filename: cstring, flags: int) -> rawptr ---;
  240. @(link_name="dlsym") _unix_dlsym :: proc(handle: rawptr, symbol: cstring) -> rawptr ---;
  241. @(link_name="dlclose") _unix_dlclose :: proc(handle: rawptr) -> int ---;
  242. @(link_name="dlerror") _unix_dlerror :: proc() -> cstring ---;
  243. }
  244. open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (Handle, Errno) {
  245. cstr := strings.clone_to_cstring(path);
  246. handle := _unix_open(cstr, flags, mode);
  247. delete(cstr);
  248. if handle == -1 {
  249. return INVALID_HANDLE, 1;
  250. }
  251. return handle, 0;
  252. }
  253. close :: proc(fd: Handle) {
  254. _unix_close(fd);
  255. }
  256. write :: proc(fd: Handle, data: []u8) -> (int, Errno) {
  257. assert(fd != -1);
  258. if len(data) == 0 {
  259. return 0, 0;
  260. }
  261. bytes_written := _unix_write(fd, &data[0], len(data));
  262. if(bytes_written == -1) {
  263. return 0, 1;
  264. }
  265. return bytes_written, 0;
  266. }
  267. read :: proc(fd: Handle, data: []u8) -> (int, Errno) {
  268. assert(fd != -1);
  269. bytes_read := _unix_read(fd, &data[0], len(data));
  270. if bytes_read == -1 {
  271. return 0, 1;
  272. }
  273. return bytes_read, 0;
  274. }
  275. seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
  276. assert(fd != -1);
  277. final_offset := i64(_unix_lseek(fd, int(offset), whence));
  278. if final_offset == -1 {
  279. return 0, 1;
  280. }
  281. return final_offset, 0;
  282. }
  283. file_size :: proc(fd: Handle) -> (i64, Errno) {
  284. prev, _ := seek(fd, 0, SEEK_CUR);
  285. size, err := seek(fd, 0, SEEK_END);
  286. seek(fd, prev, SEEK_SET);
  287. return i64(size), err;
  288. }
  289. // NOTE(bill): Uses startup to initialize it
  290. stdin: Handle = 0; // get_std_handle(win32.STD_INPUT_HANDLE);
  291. stdout: Handle = 1; // get_std_handle(win32.STD_OUTPUT_HANDLE);
  292. stderr: Handle = 2; // get_std_handle(win32.STD_ERROR_HANDLE);
  293. /* TODO(zangent): Implement these!
  294. last_write_time :: proc(fd: Handle) -> File_Time {}
  295. last_write_time_by_name :: proc(name: string) -> File_Time {}
  296. */
  297. is_path_separator :: proc(r: rune) -> bool {
  298. return r == '/';
  299. }
  300. stat :: inline proc(path: string) -> (Stat, bool) {
  301. s: Stat;
  302. cstr := strings.clone_to_cstring(path);
  303. defer delete(cstr);
  304. ret_int := _unix_stat(cstr, &s);
  305. return s, ret_int==0;
  306. }
  307. access :: inline proc(path: string, mask: int) -> bool {
  308. cstr := strings.clone_to_cstring(path);
  309. defer delete(cstr);
  310. return _unix_access(cstr, mask) == 0;
  311. }
  312. heap_alloc :: inline proc(size: int) -> rawptr {
  313. assert(size > 0);
  314. return _unix_calloc(1, size);
  315. }
  316. heap_resize :: inline proc(ptr: rawptr, new_size: int) -> rawptr {
  317. return _unix_realloc(ptr, new_size);
  318. }
  319. heap_free :: inline proc(ptr: rawptr) {
  320. _unix_free(ptr);
  321. }
  322. getenv :: proc(name: string) -> (string, bool) {
  323. path_str := strings.clone_to_cstring(name);
  324. defer delete(path_str);
  325. cstr := _unix_getenv(path_str);
  326. if cstr == nil {
  327. return "", false;
  328. }
  329. return string(cstr), true;
  330. }
  331. exit :: inline proc(code: int) -> ! {
  332. _unix_exit(code);
  333. }
  334. current_thread_id :: proc "contextless" () -> int {
  335. // return int(_unix_gettid());
  336. return 0;
  337. }
  338. dlopen :: inline proc(filename: string, flags: int) -> rawptr {
  339. cstr := strings.clone_to_cstring(filename);
  340. defer delete(cstr);
  341. handle := _unix_dlopen(cstr, flags);
  342. return handle;
  343. }
  344. dlsym :: inline proc(handle: rawptr, symbol: string) -> rawptr {
  345. assert(handle != nil);
  346. cstr := strings.clone_to_cstring(symbol);
  347. defer delete(cstr);
  348. proc_handle := _unix_dlsym(handle, cstr);
  349. return proc_handle;
  350. }
  351. dlclose :: inline proc(handle: rawptr) -> bool {
  352. assert(handle != nil);
  353. return _unix_dlclose(handle) == 0;
  354. }
  355. dlerror :: proc() -> string {
  356. return string(_unix_dlerror());
  357. }
  358. get_page_size :: proc() -> int {
  359. // NOTE(tetra): The page size never changes, so why do anything complicated
  360. // if we don't have to.
  361. @static page_size := -1;
  362. if page_size != -1 do return page_size;
  363. page_size = int(_unix_getpagesize());
  364. return page_size;
  365. }
  366. _alloc_command_line_arguments :: proc() -> []string {
  367. res := make([]string, len(runtime.args__));
  368. for arg, i in runtime.args__ {
  369. res[i] = string(arg);
  370. }
  371. return res;
  372. }