os_freebsd.odin 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. package os
  2. foreign import dl "system:dl"
  3. foreign import libc "system:c"
  4. import "core:runtime"
  5. import "core:strings"
  6. import "core:c"
  7. Handle :: distinct i32
  8. File_Time :: distinct u64
  9. Errno :: distinct i32
  10. INVALID_HANDLE :: ~Handle(0)
  11. ERROR_NONE: Errno : 0
  12. EPERM: Errno : 1
  13. ENOENT: Errno : 2
  14. ESRCH: Errno : 3
  15. EINTR: Errno : 4
  16. EIO: Errno : 5
  17. ENXIO: Errno : 6
  18. E2BIG: Errno : 7
  19. ENOEXEC: Errno : 8
  20. EBADF: Errno : 9
  21. ECHILD: Errno : 10
  22. EBEADLK: Errno : 11
  23. ENOMEM: Errno : 12
  24. EACCESS: Errno : 13
  25. EFAULT: Errno : 14
  26. ENOTBLK: Errno : 15
  27. EBUSY: Errno : 16
  28. EEXIST: Errno : 17
  29. EXDEV: Errno : 18
  30. ENODEV: Errno : 19
  31. ENOTDIR: Errno : 20
  32. EISDIR: Errno : 21
  33. EINVAL: Errno : 22
  34. ENFILE: Errno : 23
  35. EMFILE: Errno : 24
  36. ENOTTY: Errno : 25
  37. ETXTBSY: Errno : 26
  38. EFBIG: Errno : 27
  39. ENOSPC: Errno : 28
  40. ESPIPE: Errno : 29
  41. EROFS: Errno : 30
  42. EMLINK: Errno : 31
  43. EPIPE: Errno : 32
  44. EDOM: Errno : 33
  45. ERANGE: Errno : 34 /* Result too large */
  46. EAGAIN: Errno : 35
  47. EINPROGRESS: Errno : 36
  48. EALREADY: Errno : 37
  49. ENOTSOCK: Errno : 38
  50. EDESTADDRREQ: Errno : 39
  51. EMSGSIZE: Errno : 40
  52. EPROTOTYPE: Errno : 41
  53. ENOPROTOOPT: Errno : 42
  54. EPROTONOSUPPORT: Errno : 43
  55. ESOCKTNOSUPPORT: Errno : 44
  56. EOPNOTSUPP: Errno : 45
  57. EPFNOSUPPORT: Errno : 46
  58. EAFNOSUPPORT: Errno : 47
  59. EADDRINUSE: Errno : 48
  60. EADDRNOTAVAIL: Errno : 49
  61. ENETDOWN: Errno : 50
  62. ENETUNREACH: Errno : 51
  63. ENETRESET: Errno : 52
  64. ECONNABORTED: Errno : 53
  65. ECONNRESET: Errno : 54
  66. ENOBUFS: Errno : 55
  67. EISCONN: Errno : 56
  68. ENOTCONN: Errno : 57
  69. ESHUTDOWN: Errno : 58
  70. ETIMEDOUT: Errno : 60
  71. ECONNREFUSED: Errno : 61
  72. ELOOP: Errno : 62
  73. ENAMETOOLING: Errno : 63
  74. EHOSTDOWN: Errno : 64
  75. EHOSTUNREACH: Errno : 65
  76. ENOTEMPTY: Errno : 66
  77. EPROCLIM: Errno : 67
  78. EUSERS: Errno : 68
  79. EDQUOT: Errno : 69
  80. ESTALE: Errno : 70
  81. EBADRPC: Errno : 72
  82. ERPCMISMATCH: Errno : 73
  83. EPROGUNAVAIL: Errno : 74
  84. EPROGMISMATCH: Errno : 75
  85. EPROCUNAVAIL: Errno : 76
  86. ENOLCK: Errno : 77
  87. ENOSYS: Errno : 78
  88. EFTYPE: Errno : 79
  89. EAUTH: Errno : 80
  90. ENEEDAUTH: Errno : 81
  91. EIDRM: Errno : 82
  92. ENOMSG: Errno : 83
  93. EOVERFLOW: Errno : 84
  94. ECANCELED: Errno : 85
  95. EILSEQ: Errno : 86
  96. ENOATTR: Errno : 87
  97. EDOOFUS: Errno : 88
  98. EBADMSG: Errno : 89
  99. EMULTIHOP: Errno : 90
  100. ENOLINK: Errno : 91
  101. EPROTO: Errno : 92
  102. ENOTCAPABLE: Errno : 93
  103. ECAPMODE: Errno : 94
  104. ENOTRECOVERABLE: Errno : 95
  105. EOWNERDEAD: Errno : 96
  106. O_RDONLY :: 0x00000
  107. O_WRONLY :: 0x00001
  108. O_RDWR :: 0x00002
  109. O_CREATE :: 0x00040
  110. O_EXCL :: 0x00080
  111. O_NOCTTY :: 0x00100
  112. O_TRUNC :: 0x00200
  113. O_NONBLOCK :: 0x00800
  114. O_APPEND :: 0x00400
  115. O_SYNC :: 0x01000
  116. O_ASYNC :: 0x02000
  117. O_CLOEXEC :: 0x80000
  118. SEEK_SET :: 0
  119. SEEK_CUR :: 1
  120. SEEK_END :: 2
  121. SEEK_DATA :: 3
  122. SEEK_HOLE :: 4
  123. SEEK_MAX :: SEEK_HOLE
  124. // NOTE: These are OS specific!
  125. // Do not mix these up!
  126. RTLD_LAZY :: 0x001
  127. RTLD_NOW :: 0x002
  128. //RTLD_BINDING_MASK :: 0x3 // Called MODEMASK in dlfcn.h
  129. RTLD_GLOBAL :: 0x100
  130. RTLD_LOCAL :: 0x000
  131. RTLD_TRACE :: 0x200
  132. RTLD_NODELETE :: 0x01000
  133. RTLD_NOLOAD :: 0x02000
  134. MAX_PATH :: 1024
  135. args := _alloc_command_line_arguments()
  136. Unix_File_Time :: struct {
  137. seconds: time_t,
  138. nanoseconds: c.long,
  139. }
  140. dev_t :: u64
  141. ino_t :: u64
  142. nlink_t :: u64
  143. off_t :: i64
  144. mode_t :: u16
  145. pid_t :: u32
  146. uid_t :: u32
  147. gid_t :: u32
  148. blkcnt_t :: i64
  149. blksize_t :: i32
  150. fflags_t :: u32
  151. when ODIN_ARCH == .amd64 /* LP64 */ {
  152. time_t :: i64
  153. } else {
  154. time_t :: i32
  155. }
  156. OS_Stat :: struct {
  157. device_id: dev_t,
  158. serial: ino_t,
  159. nlink: nlink_t,
  160. mode: mode_t,
  161. _padding0: i16,
  162. uid: uid_t,
  163. gid: gid_t,
  164. _padding1: i32,
  165. rdev: dev_t,
  166. last_access: Unix_File_Time,
  167. modified: Unix_File_Time,
  168. status_change: Unix_File_Time,
  169. birthtime: Unix_File_Time,
  170. size: off_t,
  171. blocks: blkcnt_t,
  172. block_size: blksize_t,
  173. flags: fflags_t,
  174. gen: u64,
  175. lspare: [10]u64,
  176. }
  177. // since FreeBSD v12
  178. Dirent :: struct {
  179. ino: ino_t,
  180. off: off_t,
  181. reclen: u16,
  182. type: u8,
  183. _pad0: u8,
  184. namlen: u16,
  185. _pad1: u16,
  186. name: [256]byte,
  187. }
  188. Dir :: distinct rawptr // DIR*
  189. // File type
  190. S_IFMT :: 0o170000 // Type of file mask
  191. S_IFIFO :: 0o010000 // Named pipe (fifo)
  192. S_IFCHR :: 0o020000 // Character special
  193. S_IFDIR :: 0o040000 // Directory
  194. S_IFBLK :: 0o060000 // Block special
  195. S_IFREG :: 0o100000 // Regular
  196. S_IFLNK :: 0o120000 // Symbolic link
  197. S_IFSOCK :: 0o140000 // Socket
  198. //S_ISVTX :: 0o001000 // Save swapped text even after use
  199. // File mode
  200. // Read, write, execute/search by owner
  201. S_IRWXU :: 0o0700 // RWX mask for owner
  202. S_IRUSR :: 0o0400 // R for owner
  203. S_IWUSR :: 0o0200 // W for owner
  204. S_IXUSR :: 0o0100 // X for owner
  205. // Read, write, execute/search by group
  206. S_IRWXG :: 0o0070 // RWX mask for group
  207. S_IRGRP :: 0o0040 // R for group
  208. S_IWGRP :: 0o0020 // W for group
  209. S_IXGRP :: 0o0010 // X for group
  210. // Read, write, execute/search by others
  211. S_IRWXO :: 0o0007 // RWX mask for other
  212. S_IROTH :: 0o0004 // R for other
  213. S_IWOTH :: 0o0002 // W for other
  214. S_IXOTH :: 0o0001 // X for other
  215. S_ISUID :: 0o4000 // Set user id on execution
  216. S_ISGID :: 0o2000 // Set group id on execution
  217. S_ISVTX :: 0o1000 // Directory restrcted delete
  218. S_ISLNK :: #force_inline proc(m: mode_t) -> bool do return (m & S_IFMT) == S_IFLNK
  219. S_ISREG :: #force_inline proc(m: mode_t) -> bool do return (m & S_IFMT) == S_IFREG
  220. S_ISDIR :: #force_inline proc(m: mode_t) -> bool do return (m & S_IFMT) == S_IFDIR
  221. S_ISCHR :: #force_inline proc(m: mode_t) -> bool do return (m & S_IFMT) == S_IFCHR
  222. S_ISBLK :: #force_inline proc(m: mode_t) -> bool do return (m & S_IFMT) == S_IFBLK
  223. S_ISFIFO :: #force_inline proc(m: mode_t) -> bool do return (m & S_IFMT) == S_IFIFO
  224. S_ISSOCK :: #force_inline proc(m: mode_t) -> bool do return (m & S_IFMT) == S_IFSOCK
  225. F_OK :: 0 // Test for file existance
  226. X_OK :: 1 // Test for execute permission
  227. W_OK :: 2 // Test for write permission
  228. R_OK :: 4 // Test for read permission
  229. foreign libc {
  230. @(link_name="__error") __errno_location :: proc() -> ^int ---
  231. @(link_name="open") _unix_open :: proc(path: cstring, flags: c.int, mode: c.int) -> Handle ---
  232. @(link_name="close") _unix_close :: proc(fd: Handle) -> c.int ---
  233. @(link_name="read") _unix_read :: proc(fd: Handle, buf: rawptr, size: c.size_t) -> c.ssize_t ---
  234. @(link_name="write") _unix_write :: proc(fd: Handle, buf: rawptr, size: c.size_t) -> c.ssize_t ---
  235. @(link_name="lseek") _unix_seek :: proc(fd: Handle, offset: i64, whence: c.int) -> i64 ---
  236. @(link_name="getpagesize") _unix_getpagesize :: proc() -> c.int ---
  237. @(link_name="stat") _unix_stat :: proc(path: cstring, stat: ^OS_Stat) -> c.int ---
  238. @(link_name="lstat") _unix_lstat :: proc(path: cstring, sb: ^OS_Stat) -> c.int ---
  239. @(link_name="fstat") _unix_fstat :: proc(fd: Handle, stat: ^OS_Stat) -> c.int ---
  240. @(link_name="readlink") _unix_readlink :: proc(path: cstring, buf: ^byte, bufsiz: c.size_t) -> c.ssize_t ---
  241. @(link_name="access") _unix_access :: proc(path: cstring, mask: c.int) -> c.int ---
  242. @(link_name="getcwd") _unix_getcwd :: proc(buf: cstring, len: c.size_t) -> cstring ---
  243. @(link_name="chdir") _unix_chdir :: proc(buf: cstring) -> c.int ---
  244. @(link_name="rename") _unix_rename :: proc(old, new: cstring) -> c.int ---
  245. @(link_name="unlink") _unix_unlink :: proc(path: cstring) -> c.int ---
  246. @(link_name="rmdir") _unix_rmdir :: proc(path: cstring) -> c.int ---
  247. @(link_name="mkdir") _unix_mkdir :: proc(path: cstring, mode: mode_t) -> c.int ---
  248. @(link_name="fdopendir") _unix_fdopendir :: proc(fd: Handle) -> Dir ---
  249. @(link_name="closedir") _unix_closedir :: proc(dirp: Dir) -> c.int ---
  250. @(link_name="rewinddir") _unix_rewinddir :: proc(dirp: Dir) ---
  251. @(link_name="readdir_r") _unix_readdir_r :: proc(dirp: Dir, entry: ^Dirent, result: ^^Dirent) -> c.int ---
  252. @(link_name="malloc") _unix_malloc :: proc(size: c.size_t) -> rawptr ---
  253. @(link_name="calloc") _unix_calloc :: proc(num, size: c.size_t) -> rawptr ---
  254. @(link_name="free") _unix_free :: proc(ptr: rawptr) ---
  255. @(link_name="realloc") _unix_realloc :: proc(ptr: rawptr, size: c.size_t) -> rawptr ---
  256. @(link_name="getenv") _unix_getenv :: proc(cstring) -> cstring ---
  257. @(link_name="realpath") _unix_realpath :: proc(path: cstring, resolved_path: rawptr) -> rawptr ---
  258. @(link_name="exit") _unix_exit :: proc(status: c.int) -> ! ---
  259. }
  260. foreign dl {
  261. @(link_name="dlopen") _unix_dlopen :: proc(filename: cstring, flags: c.int) -> rawptr ---
  262. @(link_name="dlsym") _unix_dlsym :: proc(handle: rawptr, symbol: cstring) -> rawptr ---
  263. @(link_name="dlclose") _unix_dlclose :: proc(handle: rawptr) -> c.int ---
  264. @(link_name="dlerror") _unix_dlerror :: proc() -> cstring ---
  265. @(link_name="pthread_getthreadid_np") pthread_getthreadid_np :: proc() -> c.int ---
  266. }
  267. is_path_separator :: proc(r: rune) -> bool {
  268. return r == '/'
  269. }
  270. get_last_error :: proc() -> int {
  271. return __errno_location()^
  272. }
  273. open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (Handle, Errno) {
  274. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  275. handle := _unix_open(cstr, c.int(flags), c.int(mode))
  276. if handle == -1 {
  277. return INVALID_HANDLE, Errno(get_last_error())
  278. }
  279. return handle, ERROR_NONE
  280. }
  281. close :: proc(fd: Handle) -> Errno {
  282. result := _unix_close(fd)
  283. if result == -1 {
  284. return Errno(get_last_error())
  285. }
  286. return ERROR_NONE
  287. }
  288. read :: proc(fd: Handle, data: []byte) -> (int, Errno) {
  289. bytes_read := _unix_read(fd, &data[0], c.size_t(len(data)))
  290. if bytes_read == -1 {
  291. return -1, Errno(get_last_error())
  292. }
  293. return int(bytes_read), ERROR_NONE
  294. }
  295. write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
  296. if len(data) == 0 {
  297. return 0, ERROR_NONE
  298. }
  299. bytes_written := _unix_write(fd, &data[0], c.size_t(len(data)))
  300. if bytes_written == -1 {
  301. return -1, Errno(get_last_error())
  302. }
  303. return int(bytes_written), ERROR_NONE
  304. }
  305. seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
  306. res := _unix_seek(fd, offset, c.int(whence))
  307. if res == -1 {
  308. return -1, Errno(get_last_error())
  309. }
  310. return res, ERROR_NONE
  311. }
  312. file_size :: proc(fd: Handle) -> (i64, Errno) {
  313. s, err := fstat(fd)
  314. if err != ERROR_NONE {
  315. return -1, err
  316. }
  317. return s.size, ERROR_NONE
  318. }
  319. rename :: proc(old_path, new_path: string) -> Errno {
  320. old_path_cstr := strings.clone_to_cstring(old_path, context.temp_allocator)
  321. new_path_cstr := strings.clone_to_cstring(new_path, context.temp_allocator)
  322. res := _unix_rename(old_path_cstr, new_path_cstr)
  323. if res == -1 {
  324. return Errno(get_last_error())
  325. }
  326. return ERROR_NONE
  327. }
  328. remove :: proc(path: string) -> Errno {
  329. path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
  330. res := _unix_unlink(path_cstr)
  331. if res == -1 {
  332. return Errno(get_last_error())
  333. }
  334. return ERROR_NONE
  335. }
  336. make_directory :: proc(path: string, mode: mode_t = 0o775) -> Errno {
  337. path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
  338. res := _unix_mkdir(path_cstr, mode)
  339. if res == -1 {
  340. return Errno(get_last_error())
  341. }
  342. return ERROR_NONE
  343. }
  344. remove_directory :: proc(path: string) -> Errno {
  345. path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
  346. res := _unix_rmdir(path_cstr)
  347. if res == -1 {
  348. return Errno(get_last_error())
  349. }
  350. return ERROR_NONE
  351. }
  352. is_file_handle :: proc(fd: Handle) -> bool {
  353. s, err := _fstat(fd)
  354. if err != ERROR_NONE {
  355. return false
  356. }
  357. return S_ISREG(s.mode)
  358. }
  359. is_file_path :: proc(path: string, follow_links: bool = true) -> bool {
  360. s: OS_Stat
  361. err: Errno
  362. if follow_links {
  363. s, err = _stat(path)
  364. } else {
  365. s, err = _lstat(path)
  366. }
  367. if err != ERROR_NONE {
  368. return false
  369. }
  370. return S_ISREG(s.mode)
  371. }
  372. is_dir_handle :: proc(fd: Handle) -> bool {
  373. s, err := _fstat(fd)
  374. if err != ERROR_NONE {
  375. return false
  376. }
  377. return S_ISDIR(s.mode)
  378. }
  379. is_dir_path :: proc(path: string, follow_links: bool = true) -> bool {
  380. s: OS_Stat
  381. err: Errno
  382. if follow_links {
  383. s, err = _stat(path)
  384. } else {
  385. s, err = _lstat(path)
  386. }
  387. if err != ERROR_NONE {
  388. return false
  389. }
  390. return S_ISDIR(s.mode)
  391. }
  392. is_file :: proc {is_file_path, is_file_handle}
  393. is_dir :: proc {is_dir_path, is_dir_handle}
  394. // NOTE(bill): Uses startup to initialize it
  395. stdin: Handle = 0
  396. stdout: Handle = 1
  397. stderr: Handle = 2
  398. /* TODO(zangent): Implement these!
  399. last_write_time :: proc(fd: Handle) -> File_Time {}
  400. last_write_time_by_name :: proc(name: string) -> File_Time {}
  401. */
  402. last_write_time :: proc(fd: Handle) -> (File_Time, Errno) {
  403. s, err := _fstat(fd)
  404. if err != ERROR_NONE {
  405. return 0, err
  406. }
  407. modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
  408. return File_Time(modified), ERROR_NONE
  409. }
  410. last_write_time_by_name :: proc(name: string) -> (File_Time, Errno) {
  411. s, err := _stat(name)
  412. if err != ERROR_NONE {
  413. return 0, err
  414. }
  415. modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
  416. return File_Time(modified), ERROR_NONE
  417. }
  418. @private
  419. _stat :: proc(path: string) -> (OS_Stat, Errno) {
  420. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  421. s: OS_Stat = ---
  422. result := _unix_lstat(cstr, &s)
  423. if result == -1 {
  424. return s, Errno(get_last_error())
  425. }
  426. return s, ERROR_NONE
  427. }
  428. @private
  429. _lstat :: proc(path: string) -> (OS_Stat, Errno) {
  430. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  431. // deliberately uninitialized
  432. s: OS_Stat = ---
  433. res := _unix_lstat(cstr, &s)
  434. if res == -1 {
  435. return s, Errno(get_last_error())
  436. }
  437. return s, ERROR_NONE
  438. }
  439. @private
  440. _fstat :: proc(fd: Handle) -> (OS_Stat, Errno) {
  441. s: OS_Stat = ---
  442. result := _unix_fstat(fd, &s)
  443. if result == -1 {
  444. return s, Errno(get_last_error())
  445. }
  446. return s, ERROR_NONE
  447. }
  448. @private
  449. _fdopendir :: proc(fd: Handle) -> (Dir, Errno) {
  450. dirp := _unix_fdopendir(fd)
  451. if dirp == cast(Dir)nil {
  452. return nil, Errno(get_last_error())
  453. }
  454. return dirp, ERROR_NONE
  455. }
  456. @private
  457. _closedir :: proc(dirp: Dir) -> Errno {
  458. rc := _unix_closedir(dirp)
  459. if rc != 0 {
  460. return Errno(get_last_error())
  461. }
  462. return ERROR_NONE
  463. }
  464. @private
  465. _rewinddir :: proc(dirp: Dir) {
  466. _unix_rewinddir(dirp)
  467. }
  468. @private
  469. _readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Errno, end_of_stream: bool) {
  470. result: ^Dirent
  471. rc := _unix_readdir_r(dirp, &entry, &result)
  472. if rc != 0 {
  473. err = Errno(get_last_error())
  474. return
  475. }
  476. err = ERROR_NONE
  477. if result == nil {
  478. end_of_stream = true
  479. return
  480. }
  481. return
  482. }
  483. @private
  484. _readlink :: proc(path: string) -> (string, Errno) {
  485. path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
  486. bufsz : uint = MAX_PATH
  487. buf := make([]byte, MAX_PATH)
  488. for {
  489. rc := _unix_readlink(path_cstr, &(buf[0]), bufsz)
  490. if rc == -1 {
  491. delete(buf)
  492. return "", Errno(get_last_error())
  493. } else if rc == int(bufsz) {
  494. bufsz += MAX_PATH
  495. delete(buf)
  496. buf = make([]byte, bufsz)
  497. } else {
  498. return strings.string_from_ptr(&buf[0], rc), ERROR_NONE
  499. }
  500. }
  501. unreachable()
  502. }
  503. // XXX FreeBSD
  504. absolute_path_from_handle :: proc(fd: Handle) -> (string, Errno) {
  505. return "", Errno(ENOSYS)
  506. }
  507. absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
  508. rel := rel
  509. if rel == "" {
  510. rel = "."
  511. }
  512. rel_cstr := strings.clone_to_cstring(rel, context.temp_allocator)
  513. path_ptr := _unix_realpath(rel_cstr, nil)
  514. if path_ptr == nil {
  515. return "", Errno(get_last_error())
  516. }
  517. defer _unix_free(path_ptr)
  518. path_cstr := transmute(cstring)path_ptr
  519. path = strings.clone( string(path_cstr) )
  520. return path, ERROR_NONE
  521. }
  522. access :: proc(path: string, mask: int) -> (bool, Errno) {
  523. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  524. result := _unix_access(cstr, c.int(mask))
  525. if result == -1 {
  526. return false, Errno(get_last_error())
  527. }
  528. return true, ERROR_NONE
  529. }
  530. heap_alloc :: proc(size: int) -> rawptr {
  531. assert(size >= 0)
  532. return _unix_calloc(1, c.size_t(size))
  533. }
  534. heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
  535. // NOTE: _unix_realloc doesn't guarantee new memory will be zeroed on
  536. // POSIX platforms. Ensure your caller takes this into account.
  537. return _unix_realloc(ptr, c.size_t(new_size))
  538. }
  539. heap_free :: proc(ptr: rawptr) {
  540. _unix_free(ptr)
  541. }
  542. getenv :: proc(name: string) -> (string, bool) {
  543. path_str := strings.clone_to_cstring(name, context.temp_allocator)
  544. cstr := _unix_getenv(path_str)
  545. if cstr == nil {
  546. return "", false
  547. }
  548. return string(cstr), true
  549. }
  550. get_current_directory :: proc() -> string {
  551. // NOTE(tetra): I would use PATH_MAX here, but I was not able to find
  552. // an authoritative value for it across all systems.
  553. // The largest value I could find was 4096, so might as well use the page size.
  554. page_size := get_page_size()
  555. buf := make([dynamic]u8, page_size)
  556. #no_bounds_check for {
  557. cwd := _unix_getcwd(cstring(&buf[0]), c.size_t(len(buf)))
  558. if cwd != nil {
  559. return string(cwd)
  560. }
  561. if Errno(get_last_error()) != ERANGE {
  562. return ""
  563. }
  564. resize(&buf, len(buf)+page_size)
  565. }
  566. unreachable()
  567. }
  568. set_current_directory :: proc(path: string) -> (err: Errno) {
  569. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  570. res := _unix_chdir(cstr)
  571. if res == -1 do return Errno(get_last_error())
  572. return ERROR_NONE
  573. }
  574. exit :: proc "contextless" (code: int) -> ! {
  575. runtime._cleanup_runtime_contextless()
  576. _unix_exit(c.int(code))
  577. }
  578. current_thread_id :: proc "contextless" () -> int {
  579. return cast(int) pthread_getthreadid_np()
  580. }
  581. dlopen :: proc(filename: string, flags: int) -> rawptr {
  582. cstr := strings.clone_to_cstring(filename, context.temp_allocator)
  583. handle := _unix_dlopen(cstr, c.int(flags))
  584. return handle
  585. }
  586. dlsym :: proc(handle: rawptr, symbol: string) -> rawptr {
  587. assert(handle != nil)
  588. cstr := strings.clone_to_cstring(symbol, context.temp_allocator)
  589. proc_handle := _unix_dlsym(handle, cstr)
  590. return proc_handle
  591. }
  592. dlclose :: proc(handle: rawptr) -> bool {
  593. assert(handle != nil)
  594. return _unix_dlclose(handle) == 0
  595. }
  596. dlerror :: proc() -> string {
  597. return string(_unix_dlerror())
  598. }
  599. get_page_size :: proc() -> int {
  600. // NOTE(tetra): The page size never changes, so why do anything complicated
  601. // if we don't have to.
  602. @static page_size := -1
  603. if page_size != -1 do return page_size
  604. page_size = int(_unix_getpagesize())
  605. return page_size
  606. }
  607. _alloc_command_line_arguments :: proc() -> []string {
  608. res := make([]string, len(runtime.args__))
  609. for arg, i in runtime.args__ {
  610. res[i] = string(arg)
  611. }
  612. return res
  613. }