os_netbsd.odin 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. package os
  2. foreign import dl "system:dl"
  3. foreign import libc "system:c"
  4. import "base: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 /* No error */
  12. EPERM: Errno : 1 /* Operation not permitted */
  13. ENOENT: Errno : 2 /* No such file or directory */
  14. EINTR: Errno : 4 /* Interrupted system call */
  15. ESRCH: Errno : 3 /* No such process */
  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. 11 was EAGAIN */
  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 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 or too small */
  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 option not available */
  58. EPROTONOSUPPORT: Errno : 43 /* Protocol not supported */
  59. ESOCKTNOSUPPORT: Errno : 44 /* Socket type not supported */
  60. EOPNOTSUPP: 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. /* SystemV IPC */
  102. EIDRM: Errno : 82 /* Identifier removed */
  103. ENOMSG: Errno : 83 /* No message of desired type */
  104. EOVERFLOW: Errno : 84 /* Value too large to be stored in data type */
  105. /* Wide/multibyte-character handling, ISO/IEC 9899/AMD1:1995 */
  106. EILSEQ: Errno : 85 /* Illegal byte sequence */
  107. /* From IEEE Std 1003.1-2001 */
  108. /* Base, Realtime, Threads or Thread Priority Scheduling option errors */
  109. ENOTSUP: Errno : 86 /* Not supported */
  110. /* Realtime option errors */
  111. ECANCELED: Errno : 87 /* Operation canceled */
  112. /* Realtime, XSI STREAMS option errors */
  113. EBADMSG: Errno : 88 /* Bad or Corrupt message */
  114. /* XSI STREAMS option errors */
  115. ENODATA: Errno : 89 /* No message available */
  116. ENOSR: Errno : 90 /* No STREAM resources */
  117. ENOSTR: Errno : 91 /* Not a STREAM */
  118. ETIME: Errno : 92 /* STREAM ioctl timeout */
  119. /* File system extended attribute errors */
  120. ENOATTR: Errno : 93 /* Attribute not found */
  121. /* Realtime, XSI STREAMS option errors */
  122. EMULTIHOP: Errno : 94 /* Multihop attempted */
  123. ENOLINK: Errno : 95 /* Link has been severed */
  124. EPROTO: Errno : 96 /* Protocol error */
  125. /* Robust mutexes */
  126. EOWNERDEAD: Errno : 97 /* Previous owner died */
  127. ENOTRECOVERABLE: Errno : 98 /* State not recoverable */
  128. ELAST: Errno : 98 /* Must equal largest errno */
  129. /* end of errno */
  130. O_RDONLY :: 0x000000000
  131. O_WRONLY :: 0x000000001
  132. O_RDWR :: 0x000000002
  133. O_CREATE :: 0x000000200
  134. O_EXCL :: 0x000000800
  135. O_NOCTTY :: 0x000008000
  136. O_TRUNC :: 0x000000400
  137. O_NONBLOCK :: 0x000000004
  138. O_APPEND :: 0x000000008
  139. O_SYNC :: 0x000000080
  140. O_ASYNC :: 0x000000040
  141. O_CLOEXEC :: 0x000400000
  142. RTLD_LAZY :: 0x001
  143. RTLD_NOW :: 0x002
  144. RTLD_GLOBAL :: 0x100
  145. RTLD_LOCAL :: 0x200
  146. RTLD_TRACE :: 0x200
  147. RTLD_NODELETE :: 0x01000
  148. RTLD_NOLOAD :: 0x02000
  149. F_GETPATH :: 15
  150. MAX_PATH :: 1024
  151. MAXNAMLEN :: 511
  152. args := _alloc_command_line_arguments()
  153. Unix_File_Time :: struct {
  154. seconds: time_t,
  155. nanoseconds: c.long,
  156. }
  157. dev_t :: u64
  158. ino_t :: u64
  159. nlink_t :: u32
  160. off_t :: i64
  161. mode_t :: u16
  162. pid_t :: u32
  163. uid_t :: u32
  164. gid_t :: u32
  165. blkcnt_t :: i64
  166. blksize_t :: i32
  167. fflags_t :: u32
  168. time_t :: i64
  169. OS_Stat :: struct {
  170. device_id: dev_t,
  171. mode: mode_t,
  172. _padding0: i16,
  173. ino: ino_t,
  174. nlink: nlink_t,
  175. uid: uid_t,
  176. gid: gid_t,
  177. _padding1: i32,
  178. rdev: dev_t,
  179. last_access: Unix_File_Time,
  180. modified: Unix_File_Time,
  181. status_change: Unix_File_Time,
  182. birthtime: Unix_File_Time,
  183. size: off_t,
  184. blocks: blkcnt_t,
  185. block_size: blksize_t,
  186. flags: fflags_t,
  187. gen: u32,
  188. lspare: [2]u32,
  189. }
  190. Dirent :: struct {
  191. ino: ino_t,
  192. reclen: u16,
  193. namlen: u16,
  194. type: u8,
  195. name: [MAXNAMLEN + 1]byte,
  196. }
  197. Dir :: distinct rawptr // DIR*
  198. // File type
  199. S_IFMT :: 0o170000 // Type of file mask
  200. S_IFIFO :: 0o010000 // Named pipe (fifo)
  201. S_IFCHR :: 0o020000 // Character special
  202. S_IFDIR :: 0o040000 // Directory
  203. S_IFBLK :: 0o060000 // Block special
  204. S_IFREG :: 0o100000 // Regular
  205. S_IFLNK :: 0o120000 // Symbolic link
  206. S_IFSOCK :: 0o140000 // Socket
  207. // File mode
  208. // Read, write, execute/search by owner
  209. S_IRWXU :: 0o0700 // RWX mask for owner
  210. S_IRUSR :: 0o0400 // R for owner
  211. S_IWUSR :: 0o0200 // W for owner
  212. S_IXUSR :: 0o0100 // X for owner
  213. // Read, write, execute/search by group
  214. S_IRWXG :: 0o0070 // RWX mask for group
  215. S_IRGRP :: 0o0040 // R for group
  216. S_IWGRP :: 0o0020 // W for group
  217. S_IXGRP :: 0o0010 // X for group
  218. // Read, write, execute/search by others
  219. S_IRWXO :: 0o0007 // RWX mask for other
  220. S_IROTH :: 0o0004 // R for other
  221. S_IWOTH :: 0o0002 // W for other
  222. S_IXOTH :: 0o0001 // X for other
  223. S_ISUID :: 0o4000 // Set user id on execution
  224. S_ISGID :: 0o2000 // Set group id on execution
  225. S_ISVTX :: 0o1000 // Directory restrcted delete
  226. S_ISLNK :: #force_inline proc "contextless" (m: mode_t) -> bool { return (m & S_IFMT) == S_IFLNK }
  227. S_ISREG :: #force_inline proc "contextless" (m: mode_t) -> bool { return (m & S_IFMT) == S_IFREG }
  228. S_ISDIR :: #force_inline proc "contextless" (m: mode_t) -> bool { return (m & S_IFMT) == S_IFDIR }
  229. S_ISCHR :: #force_inline proc "contextless" (m: mode_t) -> bool { return (m & S_IFMT) == S_IFCHR }
  230. S_ISBLK :: #force_inline proc "contextless" (m: mode_t) -> bool { return (m & S_IFMT) == S_IFBLK }
  231. S_ISFIFO :: #force_inline proc "contextless" (m: mode_t) -> bool { return (m & S_IFMT) == S_IFIFO }
  232. S_ISSOCK :: #force_inline proc "contextless" (m: mode_t) -> bool { return (m & S_IFMT) == S_IFSOCK }
  233. F_OK :: 0 // Test for file existance
  234. X_OK :: 1 // Test for execute permission
  235. W_OK :: 2 // Test for write permission
  236. R_OK :: 4 // Test for read permission
  237. foreign libc {
  238. @(link_name="__errno") __errno_location :: proc() -> ^c.int ---
  239. @(link_name="open") _unix_open :: proc(path: cstring, flags: c.int, mode: c.int) -> Handle ---
  240. @(link_name="close") _unix_close :: proc(fd: Handle) -> c.int ---
  241. @(link_name="read") _unix_read :: proc(fd: Handle, buf: rawptr, size: c.size_t) -> c.ssize_t ---
  242. @(link_name="write") _unix_write :: proc(fd: Handle, buf: rawptr, size: c.size_t) -> c.ssize_t ---
  243. @(link_name="lseek") _unix_seek :: proc(fd: Handle, offset: i64, whence: c.int) -> i64 ---
  244. @(link_name="getpagesize") _unix_getpagesize :: proc() -> c.int ---
  245. @(link_name="stat") _unix_stat :: proc(path: cstring, stat: ^OS_Stat) -> c.int ---
  246. @(link_name="__lstat50") _unix_lstat :: proc(path: cstring, sb: ^OS_Stat) -> c.int ---
  247. @(link_name="__fstat50") _unix_fstat :: proc(fd: Handle, stat: ^OS_Stat) -> c.int ---
  248. @(link_name="readlink") _unix_readlink :: proc(path: cstring, buf: ^byte, bufsiz: c.size_t) -> c.ssize_t ---
  249. @(link_name="access") _unix_access :: proc(path: cstring, mask: c.int) -> c.int ---
  250. @(link_name="getcwd") _unix_getcwd :: proc(buf: cstring, len: c.size_t) -> cstring ---
  251. @(link_name="chdir") _unix_chdir :: proc(buf: cstring) -> c.int ---
  252. @(link_name="rename") _unix_rename :: proc(old, new: cstring) -> c.int ---
  253. @(link_name="unlink") _unix_unlink :: proc(path: cstring) -> c.int ---
  254. @(link_name="rmdir") _unix_rmdir :: proc(path: cstring) -> c.int ---
  255. @(link_name="mkdir") _unix_mkdir :: proc(path: cstring, mode: mode_t) -> c.int ---
  256. @(link_name="fcntl") _unix_fcntl :: proc(fd: Handle, cmd: c.int, arg: uintptr) -> c.int ---
  257. @(link_name="fdopendir") _unix_fdopendir :: proc(fd: Handle) -> Dir ---
  258. @(link_name="closedir") _unix_closedir :: proc(dirp: Dir) -> c.int ---
  259. @(link_name="rewinddir") _unix_rewinddir :: proc(dirp: Dir) ---
  260. @(link_name="readdir_r") _unix_readdir_r :: proc(dirp: Dir, entry: ^Dirent, result: ^^Dirent) -> c.int ---
  261. @(link_name="malloc") _unix_malloc :: proc(size: c.size_t) -> rawptr ---
  262. @(link_name="calloc") _unix_calloc :: proc(num, size: c.size_t) -> rawptr ---
  263. @(link_name="free") _unix_free :: proc(ptr: rawptr) ---
  264. @(link_name="realloc") _unix_realloc :: proc(ptr: rawptr, size: c.size_t) -> rawptr ---
  265. @(link_name="getenv") _unix_getenv :: proc(cstring) -> cstring ---
  266. @(link_name="realpath") _unix_realpath :: proc(path: cstring, resolved_path: rawptr) -> rawptr ---
  267. @(link_name="sysctlbyname") _sysctlbyname :: proc(path: cstring, oldp: rawptr, oldlenp: rawptr, newp: rawptr, newlen: int) -> c.int ---
  268. @(link_name="exit") _unix_exit :: proc(status: c.int) -> ! ---
  269. }
  270. foreign dl {
  271. @(link_name="dlopen") _unix_dlopen :: proc(filename: cstring, flags: c.int) -> rawptr ---
  272. @(link_name="dlsym") _unix_dlsym :: proc(handle: rawptr, symbol: cstring) -> rawptr ---
  273. @(link_name="dlclose") _unix_dlclose :: proc(handle: rawptr) -> c.int ---
  274. @(link_name="dlerror") _unix_dlerror :: proc() -> cstring ---
  275. }
  276. @(private)
  277. foreign libc {
  278. _lwp_self :: proc() -> i32 ---
  279. }
  280. // NOTE(phix): Perhaps share the following functions with FreeBSD if they turn out to be the same in the end.
  281. is_path_separator :: proc(r: rune) -> bool {
  282. return r == '/'
  283. }
  284. get_last_error :: proc "contextless" () -> int {
  285. return int(__errno_location()^)
  286. }
  287. open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (Handle, Errno) {
  288. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  289. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  290. handle := _unix_open(cstr, c.int(flags), c.int(mode))
  291. if handle == -1 {
  292. return INVALID_HANDLE, Errno(get_last_error())
  293. }
  294. return handle, ERROR_NONE
  295. }
  296. close :: proc(fd: Handle) -> Errno {
  297. result := _unix_close(fd)
  298. if result == -1 {
  299. return Errno(get_last_error())
  300. }
  301. return ERROR_NONE
  302. }
  303. // We set a max of 1GB to keep alignment and to be safe.
  304. @(private)
  305. MAX_RW :: 1 << 30
  306. read :: proc(fd: Handle, data: []byte) -> (int, Errno) {
  307. to_read := min(c.size_t(len(data)), MAX_RW)
  308. bytes_read := _unix_read(fd, &data[0], to_read)
  309. if bytes_read == -1 {
  310. return -1, Errno(get_last_error())
  311. }
  312. return int(bytes_read), ERROR_NONE
  313. }
  314. write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
  315. if len(data) == 0 {
  316. return 0, ERROR_NONE
  317. }
  318. to_write := min(c.size_t(len(data)), MAX_RW)
  319. bytes_written := _unix_write(fd, &data[0], to_write)
  320. if bytes_written == -1 {
  321. return -1, Errno(get_last_error())
  322. }
  323. return int(bytes_written), ERROR_NONE
  324. }
  325. seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
  326. res := _unix_seek(fd, offset, c.int(whence))
  327. if res == -1 {
  328. return -1, Errno(get_last_error())
  329. }
  330. return res, ERROR_NONE
  331. }
  332. file_size :: proc(fd: Handle) -> (i64, Errno) {
  333. s, err := _fstat(fd)
  334. if err != ERROR_NONE {
  335. return -1, err
  336. }
  337. return s.size, ERROR_NONE
  338. }
  339. rename :: proc(old_path, new_path: string) -> Errno {
  340. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  341. old_path_cstr := strings.clone_to_cstring(old_path, context.temp_allocator)
  342. new_path_cstr := strings.clone_to_cstring(new_path, context.temp_allocator)
  343. res := _unix_rename(old_path_cstr, new_path_cstr)
  344. if res == -1 {
  345. return Errno(get_last_error())
  346. }
  347. return ERROR_NONE
  348. }
  349. remove :: proc(path: string) -> Errno {
  350. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  351. path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
  352. res := _unix_unlink(path_cstr)
  353. if res == -1 {
  354. return Errno(get_last_error())
  355. }
  356. return ERROR_NONE
  357. }
  358. make_directory :: proc(path: string, mode: mode_t = 0o775) -> Errno {
  359. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  360. path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
  361. res := _unix_mkdir(path_cstr, mode)
  362. if res == -1 {
  363. return Errno(get_last_error())
  364. }
  365. return ERROR_NONE
  366. }
  367. remove_directory :: proc(path: string) -> Errno {
  368. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  369. path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
  370. res := _unix_rmdir(path_cstr)
  371. if res == -1 {
  372. return Errno(get_last_error())
  373. }
  374. return ERROR_NONE
  375. }
  376. is_file_handle :: proc(fd: Handle) -> bool {
  377. s, err := _fstat(fd)
  378. if err != ERROR_NONE {
  379. return false
  380. }
  381. return S_ISREG(s.mode)
  382. }
  383. is_file_path :: proc(path: string, follow_links: bool = true) -> bool {
  384. s: OS_Stat
  385. err: Errno
  386. if follow_links {
  387. s, err = _stat(path)
  388. } else {
  389. s, err = _lstat(path)
  390. }
  391. if err != ERROR_NONE {
  392. return false
  393. }
  394. return S_ISREG(s.mode)
  395. }
  396. is_dir_handle :: proc(fd: Handle) -> bool {
  397. s, err := _fstat(fd)
  398. if err != ERROR_NONE {
  399. return false
  400. }
  401. return S_ISDIR(s.mode)
  402. }
  403. is_dir_path :: proc(path: string, follow_links: bool = true) -> bool {
  404. s: OS_Stat
  405. err: Errno
  406. if follow_links {
  407. s, err = _stat(path)
  408. } else {
  409. s, err = _lstat(path)
  410. }
  411. if err != ERROR_NONE {
  412. return false
  413. }
  414. return S_ISDIR(s.mode)
  415. }
  416. is_file :: proc {is_file_path, is_file_handle}
  417. is_dir :: proc {is_dir_path, is_dir_handle}
  418. exists :: proc(path: string) -> bool {
  419. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  420. cpath := strings.clone_to_cstring(path, context.temp_allocator)
  421. res := _unix_access(cpath, O_RDONLY)
  422. return res == 0
  423. }
  424. fcntl :: proc(fd: int, cmd: int, arg: int) -> (int, Errno) {
  425. result := _unix_fcntl(Handle(fd), c.int(cmd), uintptr(arg))
  426. if result < 0 {
  427. return 0, Errno(get_last_error())
  428. }
  429. return int(result), ERROR_NONE
  430. }
  431. // NOTE(bill): Uses startup to initialize it
  432. stdin: Handle = 0
  433. stdout: Handle = 1
  434. stderr: Handle = 2
  435. last_write_time :: proc(fd: Handle) -> (File_Time, Errno) {
  436. s, err := _fstat(fd)
  437. if err != ERROR_NONE {
  438. return 0, err
  439. }
  440. modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
  441. return File_Time(modified), ERROR_NONE
  442. }
  443. last_write_time_by_name :: proc(name: string) -> (File_Time, Errno) {
  444. s, err := _stat(name)
  445. if err != ERROR_NONE {
  446. return 0, err
  447. }
  448. modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
  449. return File_Time(modified), ERROR_NONE
  450. }
  451. @private
  452. _stat :: proc(path: string) -> (OS_Stat, Errno) {
  453. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  454. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  455. s: OS_Stat = ---
  456. result := _unix_lstat(cstr, &s)
  457. if result == -1 {
  458. return s, Errno(get_last_error())
  459. }
  460. return s, ERROR_NONE
  461. }
  462. @private
  463. _lstat :: proc(path: string) -> (OS_Stat, Errno) {
  464. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  465. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  466. // deliberately uninitialized
  467. s: OS_Stat = ---
  468. res := _unix_lstat(cstr, &s)
  469. if res == -1 {
  470. return s, Errno(get_last_error())
  471. }
  472. return s, ERROR_NONE
  473. }
  474. @private
  475. _fstat :: proc(fd: Handle) -> (OS_Stat, Errno) {
  476. s: OS_Stat = ---
  477. result := _unix_fstat(fd, &s)
  478. if result == -1 {
  479. return s, Errno(get_last_error())
  480. }
  481. return s, ERROR_NONE
  482. }
  483. @private
  484. _fdopendir :: proc(fd: Handle) -> (Dir, Errno) {
  485. dirp := _unix_fdopendir(fd)
  486. if dirp == cast(Dir)nil {
  487. return nil, Errno(get_last_error())
  488. }
  489. return dirp, ERROR_NONE
  490. }
  491. @private
  492. _closedir :: proc(dirp: Dir) -> Errno {
  493. rc := _unix_closedir(dirp)
  494. if rc != 0 {
  495. return Errno(get_last_error())
  496. }
  497. return ERROR_NONE
  498. }
  499. @private
  500. _rewinddir :: proc(dirp: Dir) {
  501. _unix_rewinddir(dirp)
  502. }
  503. @private
  504. _readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Errno, end_of_stream: bool) {
  505. result: ^Dirent
  506. rc := _unix_readdir_r(dirp, &entry, &result)
  507. if rc != 0 {
  508. err = Errno(get_last_error())
  509. return
  510. }
  511. err = ERROR_NONE
  512. if result == nil {
  513. end_of_stream = true
  514. return
  515. }
  516. return
  517. }
  518. @private
  519. _readlink :: proc(path: string) -> (string, Errno) {
  520. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == context.allocator)
  521. path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
  522. bufsz : uint = MAX_PATH
  523. buf := make([]byte, MAX_PATH)
  524. for {
  525. rc := _unix_readlink(path_cstr, &(buf[0]), bufsz)
  526. if rc == -1 {
  527. delete(buf)
  528. return "", Errno(get_last_error())
  529. } else if rc == int(bufsz) {
  530. bufsz += MAX_PATH
  531. delete(buf)
  532. buf = make([]byte, bufsz)
  533. } else {
  534. return strings.string_from_ptr(&buf[0], rc), ERROR_NONE
  535. }
  536. }
  537. return "", Errno{}
  538. }
  539. absolute_path_from_handle :: proc(fd: Handle) -> (string, Errno) {
  540. buf: [MAX_PATH]byte
  541. _, err := fcntl(int(fd), F_GETPATH, int(uintptr(&buf[0])))
  542. if err != ERROR_NONE {
  543. return "", err
  544. }
  545. path := strings.clone_from_cstring(cstring(&buf[0]))
  546. return path, err
  547. }
  548. absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
  549. rel := rel
  550. if rel == "" {
  551. rel = "."
  552. }
  553. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == context.allocator)
  554. rel_cstr := strings.clone_to_cstring(rel, context.temp_allocator)
  555. path_ptr := _unix_realpath(rel_cstr, nil)
  556. if path_ptr == nil {
  557. return "", Errno(get_last_error())
  558. }
  559. defer _unix_free(path_ptr)
  560. path = strings.clone(string(cstring(path_ptr)))
  561. return path, ERROR_NONE
  562. }
  563. access :: proc(path: string, mask: int) -> (bool, Errno) {
  564. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  565. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  566. result := _unix_access(cstr, c.int(mask))
  567. if result == -1 {
  568. return false, Errno(get_last_error())
  569. }
  570. return true, ERROR_NONE
  571. }
  572. lookup_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) {
  573. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == allocator)
  574. path_str := strings.clone_to_cstring(key, context.temp_allocator)
  575. cstr := _unix_getenv(path_str)
  576. if cstr == nil {
  577. return "", false
  578. }
  579. return strings.clone(string(cstr), allocator), true
  580. }
  581. get_env :: proc(key: string, allocator := context.allocator) -> (value: string) {
  582. value, _ = lookup_env(key, allocator)
  583. return
  584. }
  585. get_current_directory :: proc() -> string {
  586. // NOTE(tetra): I would use PATH_MAX here, but I was not able to find
  587. // an authoritative value for it across all systems.
  588. // The largest value I could find was 4096, so might as well use the page size.
  589. page_size := get_page_size()
  590. buf := make([dynamic]u8, page_size)
  591. #no_bounds_check for {
  592. cwd := _unix_getcwd(cstring(&buf[0]), c.size_t(len(buf)))
  593. if cwd != nil {
  594. return string(cwd)
  595. }
  596. if Errno(get_last_error()) != ERANGE {
  597. delete(buf)
  598. return ""
  599. }
  600. resize(&buf, len(buf)+page_size)
  601. }
  602. unreachable()
  603. }
  604. set_current_directory :: proc(path: string) -> (err: Errno) {
  605. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  606. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  607. res := _unix_chdir(cstr)
  608. if res == -1 {
  609. return Errno(get_last_error())
  610. }
  611. return ERROR_NONE
  612. }
  613. exit :: proc "contextless" (code: int) -> ! {
  614. runtime._cleanup_runtime_contextless()
  615. _unix_exit(c.int(code))
  616. }
  617. current_thread_id :: proc "contextless" () -> int {
  618. return int(_lwp_self())
  619. }
  620. dlopen :: proc(filename: string, flags: int) -> rawptr {
  621. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  622. cstr := strings.clone_to_cstring(filename, context.temp_allocator)
  623. handle := _unix_dlopen(cstr, c.int(flags))
  624. return handle
  625. }
  626. dlsym :: proc(handle: rawptr, symbol: string) -> rawptr {
  627. assert(handle != nil)
  628. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  629. cstr := strings.clone_to_cstring(symbol, context.temp_allocator)
  630. proc_handle := _unix_dlsym(handle, cstr)
  631. return proc_handle
  632. }
  633. dlclose :: proc(handle: rawptr) -> bool {
  634. assert(handle != nil)
  635. return _unix_dlclose(handle) == 0
  636. }
  637. dlerror :: proc() -> string {
  638. return string(_unix_dlerror())
  639. }
  640. get_page_size :: proc() -> int {
  641. // NOTE(tetra): The page size never changes, so why do anything complicated
  642. // if we don't have to.
  643. @static page_size := -1
  644. if page_size != -1 {
  645. return page_size
  646. }
  647. page_size = int(_unix_getpagesize())
  648. return page_size
  649. }
  650. @(private)
  651. _processor_core_count :: proc() -> int {
  652. count : int = 0
  653. count_size := size_of(count)
  654. if _sysctlbyname("hw.logicalcpu", &count, &count_size, nil, 0) == 0 {
  655. if count > 0 {
  656. return count
  657. }
  658. }
  659. return 1
  660. }
  661. _alloc_command_line_arguments :: proc() -> []string {
  662. res := make([]string, len(runtime.args__))
  663. for arg, i in runtime.args__ {
  664. res[i] = string(arg)
  665. }
  666. return res
  667. }