2
0

os_darwin.odin 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. package os
  2. foreign import dl "system:dl"
  3. foreign import libc "System.framework"
  4. foreign import pthread "System.framework"
  5. import "core:runtime"
  6. import "core:strings"
  7. import "core:strconv"
  8. import "core:c"
  9. Handle :: distinct i32;
  10. File_Time :: distinct u64;
  11. Errno :: distinct int;
  12. INVALID_HANDLE :: ~Handle(0);
  13. ERROR_NONE: Errno : 0;
  14. EPERM: Errno : 1; /* Operation not permitted */
  15. ENOENT: Errno : 2; /* No such file or directory */
  16. ESRCH: Errno : 3; /* No such process */
  17. EINTR: Errno : 4; /* Interrupted system call */
  18. EIO: Errno : 5; /* Input/output error */
  19. ENXIO: Errno : 6; /* Device not configured */
  20. E2BIG: Errno : 7; /* Argument list too long */
  21. ENOEXEC: Errno : 8; /* Exec format error */
  22. EBADF: Errno : 9; /* Bad file descriptor */
  23. ECHILD: Errno : 10; /* No child processes */
  24. EDEADLK: Errno : 11; /* Resource deadlock avoided */
  25. ENOMEM: Errno : 12; /* Cannot allocate memory */
  26. EACCES: Errno : 13; /* Permission denied */
  27. EFAULT: Errno : 14; /* Bad address */
  28. ENOTBLK: Errno : 15; /* Block device required */
  29. EBUSY: Errno : 16; /* Device / Resource busy */
  30. EEXIST: Errno : 17; /* File exists */
  31. EXDEV: Errno : 18; /* Cross-device link */
  32. ENODEV: Errno : 19; /* Operation not supported by device */
  33. ENOTDIR: Errno : 20; /* Not a directory */
  34. EISDIR: Errno : 21; /* Is a directory */
  35. EINVAL: Errno : 22; /* Invalid argument */
  36. ENFILE: Errno : 23; /* Too many open files in system */
  37. EMFILE: Errno : 24; /* Too many open files */
  38. ENOTTY: Errno : 25; /* Inappropriate ioctl for device */
  39. ETXTBSY: Errno : 26; /* Text file busy */
  40. EFBIG: Errno : 27; /* File too large */
  41. ENOSPC: Errno : 28; /* No space left on device */
  42. ESPIPE: Errno : 29; /* Illegal seek */
  43. EROFS: Errno : 30; /* Read-only file system */
  44. EMLINK: Errno : 31; /* Too many links */
  45. EPIPE: Errno : 32; /* Broken pipe */
  46. /* math software */
  47. EDOM: Errno : 33; /* Numerical argument out of domain */
  48. ERANGE: Errno : 34; /* Result too large */
  49. /* non-blocking and interrupt i/o */
  50. EAGAIN: Errno : 35; /* Resource temporarily unavailable */
  51. EWOULDBLOCK: Errno : EAGAIN; /* Operation would block */
  52. EINPROGRESS: Errno : 36; /* Operation now in progress */
  53. EALREADY: Errno : 37; /* Operation already in progress */
  54. /* ipc/network software -- argument errors */
  55. ENOTSOCK: Errno : 38; /* Socket operation on non-socket */
  56. EDESTADDRREQ: Errno : 39; /* Destination address required */
  57. EMSGSIZE: Errno : 40; /* Message too long */
  58. EPROTOTYPE: Errno : 41; /* Protocol wrong type for socket */
  59. ENOPROTOOPT: Errno : 42; /* Protocol not available */
  60. EPROTONOSUPPORT: Errno : 43; /* Protocol not supported */
  61. ESOCKTNOSUPPORT: Errno : 44; /* Socket type not supported */
  62. ENOTSUP: Errno : 45; /* Operation not supported */
  63. EPFNOSUPPORT: Errno : 46; /* Protocol family not supported */
  64. EAFNOSUPPORT: Errno : 47; /* Address family not supported by protocol family */
  65. EADDRINUSE: Errno : 48; /* Address already in use */
  66. EADDRNOTAVAIL: Errno : 49; /* Can't assign requested address */
  67. /* ipc/network software -- operational errors */
  68. ENETDOWN: Errno : 50; /* Network is down */
  69. ENETUNREACH: Errno : 51; /* Network is unreachable */
  70. ENETRESET: Errno : 52; /* Network dropped connection on reset */
  71. ECONNABORTED: Errno : 53; /* Software caused connection abort */
  72. ECONNRESET: Errno : 54; /* Connection reset by peer */
  73. ENOBUFS: Errno : 55; /* No buffer space available */
  74. EISCONN: Errno : 56; /* Socket is already connected */
  75. ENOTCONN: Errno : 57; /* Socket is not connected */
  76. ESHUTDOWN: Errno : 58; /* Can't send after socket shutdown */
  77. ETOOMANYREFS: Errno : 59; /* Too many references: can't splice */
  78. ETIMEDOUT: Errno : 60; /* Operation timed out */
  79. ECONNREFUSED: Errno : 61; /* Connection refused */
  80. ELOOP: Errno : 62; /* Too many levels of symbolic links */
  81. ENAMETOOLONG: Errno : 63; /* File name too long */
  82. /* should be rearranged */
  83. EHOSTDOWN: Errno : 64; /* Host is down */
  84. EHOSTUNREACH: Errno : 65; /* No route to host */
  85. ENOTEMPTY: Errno : 66; /* Directory not empty */
  86. /* quotas & mush */
  87. EPROCLIM: Errno : 67; /* Too many processes */
  88. EUSERS: Errno : 68; /* Too many users */
  89. EDQUOT: Errno : 69; /* Disc quota exceeded */
  90. /* Network File System */
  91. ESTALE: Errno : 70; /* Stale NFS file handle */
  92. EREMOTE: Errno : 71; /* Too many levels of remote in path */
  93. EBADRPC: Errno : 72; /* RPC struct is bad */
  94. ERPCMISMATCH: Errno : 73; /* RPC version wrong */
  95. EPROGUNAVAIL: Errno : 74; /* RPC prog. not avail */
  96. EPROGMISMATCH: Errno : 75; /* Program version wrong */
  97. EPROCUNAVAIL: Errno : 76; /* Bad procedure for program */
  98. ENOLCK: Errno : 77; /* No locks available */
  99. ENOSYS: Errno : 78; /* Function not implemented */
  100. EFTYPE: Errno : 79; /* Inappropriate file type or format */
  101. EAUTH: Errno : 80; /* Authentication error */
  102. ENEEDAUTH: Errno : 81; /* Need authenticator */
  103. /* Intelligent device errors */
  104. EPWROFF: Errno : 82; /* Device power is off */
  105. EDEVERR: Errno : 83; /* Device error, e.g. paper out */
  106. EOVERFLOW: Errno : 84; /* Value too large to be stored in data type */
  107. /* Program loading errors */
  108. EBADEXEC: Errno : 85; /* Bad executable */
  109. EBADARCH: Errno : 86; /* Bad CPU type in executable */
  110. ESHLIBVERS: Errno : 87; /* Shared library version mismatch */
  111. EBADMACHO: Errno : 88; /* Malformed Macho file */
  112. ECANCELED: Errno : 89; /* Operation canceled */
  113. EIDRM: Errno : 90; /* Identifier removed */
  114. ENOMSG: Errno : 91; /* No message of desired type */
  115. EILSEQ: Errno : 92; /* Illegal byte sequence */
  116. ENOATTR: Errno : 93; /* Attribute not found */
  117. EBADMSG: Errno : 94; /* Bad message */
  118. EMULTIHOP: Errno : 95; /* Reserved */
  119. ENODATA: Errno : 96; /* No message available on STREAM */
  120. ENOLINK: Errno : 97; /* Reserved */
  121. ENOSR: Errno : 98; /* No STREAM resources */
  122. ENOSTR: Errno : 99; /* Not a STREAM */
  123. EPROTO: Errno : 100; /* Protocol error */
  124. ETIME: Errno : 101; /* STREAM ioctl timeout */
  125. ENOPOLICY: Errno : 103; /* No such policy registered */
  126. ENOTRECOVERABLE: Errno : 104; /* State not recoverable */
  127. EOWNERDEAD: Errno : 105; /* Previous owner died */
  128. EQFULL: Errno : 106; /* Interface output queue is full */
  129. ELAST: Errno : 106; /* Must be equal largest errno */
  130. O_RDONLY :: 0x0000;
  131. O_WRONLY :: 0x0001;
  132. O_RDWR :: 0x0002;
  133. O_CREATE :: 0x0200;
  134. O_EXCL :: 0x0800;
  135. O_NOCTTY :: 0;
  136. O_TRUNC :: 0x0400;
  137. O_NONBLOCK :: 0x0004;
  138. O_APPEND :: 0x0008;
  139. O_SYNC :: 0x0080;
  140. O_ASYNC :: 0x0040;
  141. O_CLOEXEC :: 0x1000000;
  142. SEEK_SET :: 0;
  143. SEEK_CUR :: 1;
  144. SEEK_END :: 2;
  145. SEEK_DATA :: 3;
  146. SEEK_HOLE :: 4;
  147. SEEK_MAX :: SEEK_HOLE;
  148. // NOTE(zangent): These are OS specific!
  149. // Do not mix these up!
  150. RTLD_LAZY :: 0x1;
  151. RTLD_NOW :: 0x2;
  152. RTLD_LOCAL :: 0x4;
  153. RTLD_GLOBAL :: 0x8;
  154. RTLD_NODELETE :: 0x80;
  155. RTLD_NOLOAD :: 0x10;
  156. RTLD_FIRST :: 0x100;
  157. // "Argv" arguments converted to Odin strings
  158. args := _alloc_command_line_arguments();
  159. Unix_File_Time :: struct {
  160. seconds: i64,
  161. nanoseconds: i64,
  162. }
  163. OS_Stat :: struct {
  164. device_id: i32, // ID of device containing file
  165. mode: u16, // Mode of the file
  166. nlink: u16, // Number of hard links
  167. serial: u64, // File serial number
  168. uid: u32, // User ID of the file's owner
  169. gid: u32, // Group ID of the file's group
  170. rdev: i32, // Device ID, if device
  171. last_access: Unix_File_Time, // Time of last access
  172. modified: Unix_File_Time, // Time of last modification
  173. status_change: Unix_File_Time, // Time of last status change
  174. created: Unix_File_Time, // Time of creation
  175. size: i64, // Size of the file, in bytes
  176. blocks: i64, // Number of blocks allocated for the file
  177. block_size: i32, // Optimal blocksize for I/O
  178. flags: u32, // User-defined flags for the file
  179. gen_num: u32, // File generation number ..?
  180. _spare: i32, // RESERVED
  181. _reserve1,
  182. _reserve2: i64, // RESERVED
  183. };
  184. // NOTE(laleksic, 2021-01-21): Comment and rename these to match OS_Stat above
  185. Dirent :: struct {
  186. ino: u64,
  187. off: u64,
  188. reclen: u16,
  189. type: u8,
  190. name: [256]byte,
  191. };
  192. Dir :: distinct rawptr; // DIR*
  193. // File type
  194. S_IFMT :: 0o170000; // Type of file mask
  195. S_IFIFO :: 0o010000; // Named pipe (fifo)
  196. S_IFCHR :: 0o020000; // Character special
  197. S_IFDIR :: 0o040000; // Directory
  198. S_IFBLK :: 0o060000; // Block special
  199. S_IFREG :: 0o100000; // Regular
  200. S_IFLNK :: 0o120000; // Symbolic link
  201. S_IFSOCK :: 0o140000; // Socket
  202. // File mode
  203. // Read, write, execute/search by owner
  204. S_IRWXU :: 0o0700; // RWX mask for owner
  205. S_IRUSR :: 0o0400; // R for owner
  206. S_IWUSR :: 0o0200; // W for owner
  207. S_IXUSR :: 0o0100; // X for owner
  208. // Read, write, execute/search by group
  209. S_IRWXG :: 0o0070; // RWX mask for group
  210. S_IRGRP :: 0o0040; // R for group
  211. S_IWGRP :: 0o0020; // W for group
  212. S_IXGRP :: 0o0010; // X for group
  213. // Read, write, execute/search by others
  214. S_IRWXO :: 0o0007; // RWX mask for other
  215. S_IROTH :: 0o0004; // R for other
  216. S_IWOTH :: 0o0002; // W for other
  217. S_IXOTH :: 0o0001; // X for other
  218. S_ISUID :: 0o4000; // Set user id on execution
  219. S_ISGID :: 0o2000; // Set group id on execution
  220. S_ISVTX :: 0o1000; // Directory restrcted delete
  221. S_ISLNK :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFLNK; }
  222. S_ISREG :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFREG; }
  223. S_ISDIR :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFDIR; }
  224. S_ISCHR :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFCHR; }
  225. S_ISBLK :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFBLK; }
  226. S_ISFIFO :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFIFO; }
  227. S_ISSOCK :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFSOCK; }
  228. R_OK :: 4; // Test for read permission
  229. W_OK :: 2; // Test for write permission
  230. X_OK :: 1; // Test for execute permission
  231. F_OK :: 0; // Test for file existance
  232. foreign libc {
  233. @(link_name="__error") __error :: proc() -> ^int ---;
  234. @(link_name="open") _unix_open :: proc(path: cstring, flags: i32, mode: u16) -> Handle ---;
  235. @(link_name="close") _unix_close :: proc(handle: Handle) ---;
  236. @(link_name="read") _unix_read :: proc(handle: Handle, buffer: rawptr, count: int) -> int ---;
  237. @(link_name="write") _unix_write :: proc(handle: Handle, buffer: rawptr, count: int) -> int ---;
  238. @(link_name="lseek") _unix_lseek :: proc(fs: Handle, offset: int, whence: int) -> int ---;
  239. @(link_name="gettid") _unix_gettid :: proc() -> u64 ---;
  240. @(link_name="getpagesize") _unix_getpagesize :: proc() -> i32 ---;
  241. @(link_name="stat64") _unix_stat :: proc(path: cstring, stat: ^OS_Stat) -> c.int ---;
  242. @(link_name="lstat") _unix_lstat :: proc(path: cstring, stat: ^OS_Stat) -> c.int ---;
  243. @(link_name="fstat") _unix_fstat :: proc(fd: Handle, stat: ^OS_Stat) -> c.int ---;
  244. @(link_name="readlink") _unix_readlink :: proc(path: cstring, buf: ^byte, bufsiz: c.size_t) -> c.ssize_t ---;
  245. @(link_name="access") _unix_access :: proc(path: cstring, mask: int) -> int ---;
  246. @(link_name="fdopendir") _unix_fdopendir :: proc(fd: Handle) -> Dir ---;
  247. @(link_name="closedir") _unix_closedir :: proc(dirp: Dir) -> c.int ---;
  248. @(link_name="rewinddir") _unix_rewinddir :: proc(dirp: Dir) ---;
  249. @(link_name="readdir_r") _unix_readdir_r :: proc(dirp: Dir, entry: ^Dirent, result: ^^Dirent) -> c.int ---;
  250. @(link_name="malloc") _unix_malloc :: proc(size: int) -> rawptr ---;
  251. @(link_name="calloc") _unix_calloc :: proc(num, size: int) -> rawptr ---;
  252. @(link_name="free") _unix_free :: proc(ptr: rawptr) ---;
  253. @(link_name="realloc") _unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr ---;
  254. @(link_name="getenv") _unix_getenv :: proc(cstring) -> cstring ---;
  255. @(link_name="getcwd") _unix_getcwd :: proc(buf: cstring, len: c.size_t) -> cstring ---;
  256. @(link_name="chdir") _unix_chdir :: proc(buf: cstring) -> c.int ---;
  257. @(link_name="realpath") _unix_realpath :: proc(path: cstring, resolved_path: rawptr) -> rawptr ---;
  258. @(link_name="exit") _unix_exit :: proc(status: int) -> ! ---;
  259. }
  260. foreign dl {
  261. @(link_name="dlopen") _unix_dlopen :: proc(filename: cstring, flags: int) -> rawptr ---;
  262. @(link_name="dlsym") _unix_dlsym :: proc(handle: rawptr, symbol: cstring) -> rawptr ---;
  263. @(link_name="dlclose") _unix_dlclose :: proc(handle: rawptr) -> int ---;
  264. @(link_name="dlerror") _unix_dlerror :: proc() -> cstring ---;
  265. }
  266. get_last_error :: proc() -> int {
  267. return __error()^;
  268. }
  269. open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (Handle, Errno) {
  270. cstr := strings.clone_to_cstring(path);
  271. handle := _unix_open(cstr, i32(flags), u16(mode));
  272. delete(cstr);
  273. if handle == -1 {
  274. return INVALID_HANDLE, 1;
  275. }
  276. return handle, 0;
  277. }
  278. close :: proc(fd: Handle) {
  279. _unix_close(fd);
  280. }
  281. write :: proc(fd: Handle, data: []u8) -> (int, Errno) {
  282. assert(fd != -1);
  283. if len(data) == 0 {
  284. return 0, 0;
  285. }
  286. bytes_written := _unix_write(fd, raw_data(data), len(data));
  287. if bytes_written == -1 {
  288. return 0, 1;
  289. }
  290. return bytes_written, 0;
  291. }
  292. read :: proc(fd: Handle, data: []u8) -> (int, Errno) {
  293. assert(fd != -1);
  294. bytes_read := _unix_read(fd, raw_data(data), len(data));
  295. if bytes_read == -1 {
  296. return 0, 1;
  297. }
  298. return bytes_read, 0;
  299. }
  300. seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
  301. assert(fd != -1);
  302. final_offset := i64(_unix_lseek(fd, int(offset), whence));
  303. if final_offset == -1 {
  304. return 0, 1;
  305. }
  306. return final_offset, 0;
  307. }
  308. file_size :: proc(fd: Handle) -> (i64, Errno) {
  309. prev, _ := seek(fd, 0, SEEK_CUR);
  310. size, err := seek(fd, 0, SEEK_END);
  311. seek(fd, prev, SEEK_SET);
  312. return i64(size), err;
  313. }
  314. // NOTE(bill): Uses startup to initialize it
  315. stdin: Handle = 0; // get_std_handle(win32.STD_INPUT_HANDLE);
  316. stdout: Handle = 1; // get_std_handle(win32.STD_OUTPUT_HANDLE);
  317. stderr: Handle = 2; // get_std_handle(win32.STD_ERROR_HANDLE);
  318. /* TODO(zangent): Implement these!
  319. last_write_time :: proc(fd: Handle) -> File_Time {}
  320. last_write_time_by_name :: proc(name: string) -> File_Time {}
  321. */
  322. is_path_separator :: proc(r: rune) -> bool {
  323. return r == '/';
  324. }
  325. @private
  326. _stat :: proc(path: string) -> (OS_Stat, Errno) {
  327. cstr := strings.clone_to_cstring(path, context.temp_allocator);
  328. s: OS_Stat;
  329. result := _unix_stat(cstr, &s);
  330. if result == -1 {
  331. return s, Errno(get_last_error());
  332. }
  333. return s, ERROR_NONE;
  334. }
  335. @private
  336. _lstat :: proc(path: string) -> (OS_Stat, Errno) {
  337. cstr := strings.clone_to_cstring(path, context.temp_allocator);
  338. s: OS_Stat;
  339. result := _unix_lstat(cstr, &s);
  340. if result == -1 {
  341. return s, Errno(get_last_error());
  342. }
  343. return s, ERROR_NONE;
  344. }
  345. @private
  346. _fstat :: proc(fd: Handle) -> (OS_Stat, Errno) {
  347. s: OS_Stat;
  348. result := _unix_fstat(fd, &s);
  349. if result == -1 {
  350. return s, Errno(get_last_error());
  351. }
  352. return s, ERROR_NONE;
  353. }
  354. @private
  355. _fdopendir :: proc(fd: Handle) -> (Dir, Errno) {
  356. dirp := _unix_fdopendir(fd);
  357. if dirp == cast(Dir)nil {
  358. return nil, Errno(get_last_error());
  359. }
  360. return dirp, ERROR_NONE;
  361. }
  362. @private
  363. _closedir :: proc(dirp: Dir) -> Errno {
  364. rc := _unix_closedir(dirp);
  365. if rc != 0 {
  366. return Errno(get_last_error());
  367. }
  368. return ERROR_NONE;
  369. }
  370. @private
  371. _rewinddir :: proc(dirp: Dir) {
  372. _unix_rewinddir(dirp);
  373. }
  374. @private
  375. _readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Errno, end_of_stream: bool) {
  376. result: ^Dirent;
  377. rc := _unix_readdir_r(dirp, &entry, &result);
  378. if rc != 0 {
  379. err = Errno(get_last_error());
  380. return;
  381. }
  382. err = ERROR_NONE;
  383. if result == nil {
  384. end_of_stream = true;
  385. return;
  386. }
  387. end_of_stream = false;
  388. return;
  389. }
  390. @private
  391. _readlink :: proc(path: string) -> (string, Errno) {
  392. path_cstr := strings.clone_to_cstring(path, context.temp_allocator);
  393. bufsz : uint = 256;
  394. buf := make([]byte, bufsz);
  395. for {
  396. rc := _unix_readlink(path_cstr, &(buf[0]), bufsz);
  397. if rc == -1 {
  398. delete(buf);
  399. return "", Errno(get_last_error());
  400. } else if rc == int(bufsz) {
  401. // NOTE(laleksic, 2021-01-21): Any cleaner way to resize the slice?
  402. bufsz *= 2;
  403. delete(buf);
  404. buf = make([]byte, bufsz);
  405. } else {
  406. return strings.string_from_ptr(&buf[0], rc), ERROR_NONE;
  407. }
  408. }
  409. }
  410. absolute_path_from_handle :: proc(fd: Handle) -> (string, Errno) {
  411. buf : [256]byte;
  412. fd_str := strconv.itoa( buf[:], cast(int)fd );
  413. procfs_path := strings.concatenate( []string{ "/proc/self/fd/", fd_str } );
  414. defer delete(procfs_path);
  415. return _readlink(procfs_path);
  416. }
  417. absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
  418. rel := rel;
  419. if rel == "" {
  420. rel = ".";
  421. }
  422. rel_cstr := strings.clone_to_cstring(rel, context.temp_allocator);
  423. path_ptr := _unix_realpath(rel_cstr, nil);
  424. if path_ptr == nil {
  425. return "", Errno(get_last_error());
  426. }
  427. defer _unix_free(path_ptr);
  428. path_cstr := transmute(cstring)path_ptr;
  429. path = strings.clone( string(path_cstr) );
  430. return path, ERROR_NONE;
  431. }
  432. access :: proc(path: string, mask: int) -> bool {
  433. cstr := strings.clone_to_cstring(path, context.temp_allocator);
  434. return _unix_access(cstr, mask) == 0;
  435. }
  436. heap_alloc :: proc(size: int) -> rawptr {
  437. assert(size > 0);
  438. return _unix_calloc(1, size);
  439. }
  440. heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
  441. return _unix_realloc(ptr, new_size);
  442. }
  443. heap_free :: proc(ptr: rawptr) {
  444. _unix_free(ptr);
  445. }
  446. getenv :: proc(name: string) -> (string, bool) {
  447. path_str := strings.clone_to_cstring(name, context.temp_allocator);
  448. cstr := _unix_getenv(path_str);
  449. if cstr == nil {
  450. return "", false;
  451. }
  452. return string(cstr), true;
  453. }
  454. get_current_directory :: proc() -> string {
  455. page_size := get_page_size(); // NOTE(tetra): See note in os_linux.odin/get_current_directory.
  456. buf := make([dynamic]u8, page_size);
  457. for {
  458. cwd := _unix_getcwd(cstring(raw_data(buf)), c.size_t(len(buf)));
  459. if cwd != nil {
  460. return string(cwd);
  461. }
  462. if Errno(get_last_error()) != ERANGE {
  463. return "";
  464. }
  465. resize(&buf, len(buf)+page_size);
  466. }
  467. unreachable();
  468. }
  469. set_current_directory :: proc(path: string) -> (err: Errno) {
  470. cstr := strings.clone_to_cstring(path, context.temp_allocator);
  471. res := _unix_chdir(cstr);
  472. if res == -1 {
  473. return Errno(get_last_error());
  474. }
  475. return ERROR_NONE;
  476. }
  477. exit :: proc "contextless" (code: int) -> ! {
  478. _unix_exit(code);
  479. }
  480. current_thread_id :: proc "contextless" () -> int {
  481. tid: u64;
  482. // NOTE(Oskar): available from OSX 10.6 and iOS 3.2.
  483. // For older versions there is `syscall(SYS_thread_selfid)`, but not really
  484. // the same thing apparently.
  485. foreign pthread { pthread_threadid_np :: proc "c" (rawptr, ^u64) -> c.int ---; }
  486. pthread_threadid_np(nil, &tid);
  487. return int(tid);
  488. }
  489. dlopen :: proc(filename: string, flags: int) -> rawptr {
  490. cstr := strings.clone_to_cstring(filename, context.temp_allocator);
  491. handle := _unix_dlopen(cstr, flags);
  492. return handle;
  493. }
  494. dlsym :: proc(handle: rawptr, symbol: string) -> rawptr {
  495. assert(handle != nil);
  496. cstr := strings.clone_to_cstring(symbol, context.temp_allocator);
  497. proc_handle := _unix_dlsym(handle, cstr);
  498. return proc_handle;
  499. }
  500. dlclose :: proc(handle: rawptr) -> bool {
  501. assert(handle != nil);
  502. return _unix_dlclose(handle) == 0;
  503. }
  504. dlerror :: proc() -> string {
  505. return string(_unix_dlerror());
  506. }
  507. get_page_size :: proc() -> int {
  508. // NOTE(tetra): The page size never changes, so why do anything complicated
  509. // if we don't have to.
  510. @static page_size := -1;
  511. if page_size != -1 {
  512. return page_size;
  513. }
  514. page_size = int(_unix_getpagesize());
  515. return page_size;
  516. }
  517. _alloc_command_line_arguments :: proc() -> []string {
  518. res := make([]string, len(runtime.args__));
  519. for arg, i in runtime.args__ {
  520. res[i] = string(arg);
  521. }
  522. return res;
  523. }