os_openbsd.odin 19 KB

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