os_openbsd.odin 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. package os
  2. foreign import libc "system:c"
  3. import "core:strings"
  4. import "core:c"
  5. import "base:runtime"
  6. Handle :: distinct i32
  7. Pid :: distinct i32
  8. File_Time :: distinct u64
  9. INVALID_HANDLE :: ~Handle(0)
  10. _Platform_Error :: enum i32 {
  11. NONE = 0,
  12. EPERM = 1,
  13. ENOENT = 2,
  14. ESRCH = 3,
  15. EINTR = 4,
  16. EIO = 5,
  17. ENXIO = 6,
  18. E2BIG = 7,
  19. ENOEXEC = 8,
  20. EBADF = 9,
  21. ECHILD = 10,
  22. EDEADLK = 11,
  23. ENOMEM = 12,
  24. EACCES = 13,
  25. EFAULT = 14,
  26. ENOTBLK = 15,
  27. EBUSY = 16,
  28. EEXIST = 17,
  29. EXDEV = 18,
  30. ENODEV = 19,
  31. ENOTDIR = 20,
  32. EISDIR = 21,
  33. EINVAL = 22,
  34. ENFILE = 23,
  35. EMFILE = 24,
  36. ENOTTY = 25,
  37. ETXTBSY = 26,
  38. EFBIG = 27,
  39. ENOSPC = 28,
  40. ESPIPE = 29,
  41. EROFS = 30,
  42. EMLINK = 31,
  43. EPIPE = 32,
  44. EDOM = 33,
  45. ERANGE = 34,
  46. EAGAIN = 35,
  47. EWOULDBLOCK = EAGAIN,
  48. EINPROGRESS = 36,
  49. EALREADY = 37,
  50. ENOTSOCK = 38,
  51. EDESTADDRREQ = 39,
  52. EMSGSIZE = 40,
  53. EPROTOTYPE = 41,
  54. ENOPROTOOPT = 42,
  55. EPROTONOSUPPORT = 43,
  56. ESOCKTNOSUPPORT = 44,
  57. EOPNOTSUPP = 45,
  58. EPFNOSUPPORT = 46,
  59. EAFNOSUPPORT = 47,
  60. EADDRINUSE = 48,
  61. EADDRNOTAVAIL = 49,
  62. ENETDOWN = 50,
  63. ENETUNREACH = 51,
  64. ENETRESET = 52,
  65. ECONNABORTED = 53,
  66. ECONNRESET = 54,
  67. ENOBUFS = 55,
  68. EISCONN = 56,
  69. ENOTCONN = 57,
  70. ESHUTDOWN = 58,
  71. ETOOMANYREFS = 59,
  72. ETIMEDOUT = 60,
  73. ECONNREFUSED = 61,
  74. ELOOP = 62,
  75. ENAMETOOLONG = 63,
  76. EHOSTDOWN = 64,
  77. EHOSTUNREACH = 65,
  78. ENOTEMPTY = 66,
  79. EPROCLIM = 67,
  80. EUSERS = 68,
  81. EDQUOT = 69,
  82. ESTALE = 70,
  83. EREMOTE = 71,
  84. EBADRPC = 72,
  85. ERPCMISMATCH = 73,
  86. EPROGUNAVAIL = 74,
  87. EPROGMISMATCH = 75,
  88. EPROCUNAVAIL = 76,
  89. ENOLCK = 77,
  90. ENOSYS = 78,
  91. EFTYPE = 79,
  92. EAUTH = 80,
  93. ENEEDAUTH = 81,
  94. EIPSEC = 82,
  95. ENOATTR = 83,
  96. EILSEQ = 84,
  97. ENOMEDIUM = 85,
  98. EMEDIUMTYPE = 86,
  99. EOVERFLOW = 87,
  100. ECANCELED = 88,
  101. EIDRM = 89,
  102. ENOMSG = 90,
  103. ENOTSUP = 91,
  104. EBADMSG = 92,
  105. ENOTRECOVERABLE = 93,
  106. EOWNERDEAD = 94,
  107. EPROTO = 95,
  108. }
  109. EPERM :: Platform_Error.EPERM
  110. ENOENT :: Platform_Error.ENOENT
  111. ESRCH :: Platform_Error.ESRCH
  112. EINTR :: Platform_Error.EINTR
  113. EIO :: Platform_Error.EIO
  114. ENXIO :: Platform_Error.ENXIO
  115. E2BIG :: Platform_Error.E2BIG
  116. ENOEXEC :: Platform_Error.ENOEXEC
  117. EBADF :: Platform_Error.EBADF
  118. ECHILD :: Platform_Error.ECHILD
  119. EDEADLK :: Platform_Error.EDEADLK
  120. ENOMEM :: Platform_Error.ENOMEM
  121. EACCES :: Platform_Error.EACCES
  122. EFAULT :: Platform_Error.EFAULT
  123. ENOTBLK :: Platform_Error.ENOTBLK
  124. EBUSY :: Platform_Error.EBUSY
  125. EEXIST :: Platform_Error.EEXIST
  126. EXDEV :: Platform_Error.EXDEV
  127. ENODEV :: Platform_Error.ENODEV
  128. ENOTDIR :: Platform_Error.ENOTDIR
  129. EISDIR :: Platform_Error.EISDIR
  130. EINVAL :: Platform_Error.EINVAL
  131. ENFILE :: Platform_Error.ENFILE
  132. EMFILE :: Platform_Error.EMFILE
  133. ENOTTY :: Platform_Error.ENOTTY
  134. ETXTBSY :: Platform_Error.ETXTBSY
  135. EFBIG :: Platform_Error.EFBIG
  136. ENOSPC :: Platform_Error.ENOSPC
  137. ESPIPE :: Platform_Error.ESPIPE
  138. EROFS :: Platform_Error.EROFS
  139. EMLINK :: Platform_Error.EMLINK
  140. EPIPE :: Platform_Error.EPIPE
  141. EDOM :: Platform_Error.EDOM
  142. ERANGE :: Platform_Error.ERANGE
  143. EAGAIN :: Platform_Error.EAGAIN
  144. EWOULDBLOCK :: Platform_Error.EWOULDBLOCK
  145. EINPROGRESS :: Platform_Error.EINPROGRESS
  146. EALREADY :: Platform_Error.EALREADY
  147. ENOTSOCK :: Platform_Error.ENOTSOCK
  148. EDESTADDRREQ :: Platform_Error.EDESTADDRREQ
  149. EMSGSIZE :: Platform_Error.EMSGSIZE
  150. EPROTOTYPE :: Platform_Error.EPROTOTYPE
  151. ENOPROTOOPT :: Platform_Error.ENOPROTOOPT
  152. EPROTONOSUPPORT :: Platform_Error.EPROTONOSUPPORT
  153. ESOCKTNOSUPPORT :: Platform_Error.ESOCKTNOSUPPORT
  154. EOPNOTSUPP :: Platform_Error.EOPNOTSUPP
  155. EPFNOSUPPORT :: Platform_Error.EPFNOSUPPORT
  156. EAFNOSUPPORT :: Platform_Error.EAFNOSUPPORT
  157. EADDRINUSE :: Platform_Error.EADDRINUSE
  158. EADDRNOTAVAIL :: Platform_Error.EADDRNOTAVAIL
  159. ENETDOWN :: Platform_Error.ENETDOWN
  160. ENETUNREACH :: Platform_Error.ENETUNREACH
  161. ENETRESET :: Platform_Error.ENETRESET
  162. ECONNABORTED :: Platform_Error.ECONNABORTED
  163. ECONNRESET :: Platform_Error.ECONNRESET
  164. ENOBUFS :: Platform_Error.ENOBUFS
  165. EISCONN :: Platform_Error.EISCONN
  166. ENOTCONN :: Platform_Error.ENOTCONN
  167. ESHUTDOWN :: Platform_Error.ESHUTDOWN
  168. ETOOMANYREFS :: Platform_Error.ETOOMANYREFS
  169. ETIMEDOUT :: Platform_Error.ETIMEDOUT
  170. ECONNREFUSED :: Platform_Error.ECONNREFUSED
  171. ELOOP :: Platform_Error.ELOOP
  172. ENAMETOOLONG :: Platform_Error.ENAMETOOLONG
  173. EHOSTDOWN :: Platform_Error.EHOSTDOWN
  174. EHOSTUNREACH :: Platform_Error.EHOSTUNREACH
  175. ENOTEMPTY :: Platform_Error.ENOTEMPTY
  176. EPROCLIM :: Platform_Error.EPROCLIM
  177. EUSERS :: Platform_Error.EUSERS
  178. EDQUOT :: Platform_Error.EDQUOT
  179. ESTALE :: Platform_Error.ESTALE
  180. EREMOTE :: Platform_Error.EREMOTE
  181. EBADRPC :: Platform_Error.EBADRPC
  182. ERPCMISMATCH :: Platform_Error.ERPCMISMATCH
  183. EPROGUNAVAIL :: Platform_Error.EPROGUNAVAIL
  184. EPROGMISMATCH :: Platform_Error.EPROGMISMATCH
  185. EPROCUNAVAIL :: Platform_Error.EPROCUNAVAIL
  186. ENOLCK :: Platform_Error.ENOLCK
  187. ENOSYS :: Platform_Error.ENOSYS
  188. EFTYPE :: Platform_Error.EFTYPE
  189. EAUTH :: Platform_Error.EAUTH
  190. ENEEDAUTH :: Platform_Error.ENEEDAUTH
  191. EIPSEC :: Platform_Error.EIPSEC
  192. ENOATTR :: Platform_Error.ENOATTR
  193. EILSEQ :: Platform_Error.EILSEQ
  194. ENOMEDIUM :: Platform_Error.ENOMEDIUM
  195. EMEDIUMTYPE :: Platform_Error.EMEDIUMTYPE
  196. EOVERFLOW :: Platform_Error.EOVERFLOW
  197. ECANCELED :: Platform_Error.ECANCELED
  198. EIDRM :: Platform_Error.EIDRM
  199. ENOMSG :: Platform_Error.ENOMSG
  200. ENOTSUP :: Platform_Error.ENOTSUP
  201. EBADMSG :: Platform_Error.EBADMSG
  202. ENOTRECOVERABLE :: Platform_Error.ENOTRECOVERABLE
  203. EOWNERDEAD :: Platform_Error.EOWNERDEAD
  204. EPROTO :: Platform_Error.EPROTO
  205. O_RDONLY :: 0x00000
  206. O_WRONLY :: 0x00001
  207. O_RDWR :: 0x00002
  208. O_NONBLOCK :: 0x00004
  209. O_APPEND :: 0x00008
  210. O_ASYNC :: 0x00040
  211. O_SYNC :: 0x00080
  212. O_CREATE :: 0x00200
  213. O_TRUNC :: 0x00400
  214. O_EXCL :: 0x00800
  215. O_NOCTTY :: 0x08000
  216. O_CLOEXEC :: 0x10000
  217. RTLD_LAZY :: 0x001
  218. RTLD_NOW :: 0x002
  219. RTLD_LOCAL :: 0x000
  220. RTLD_GLOBAL :: 0x100
  221. RTLD_TRACE :: 0x200
  222. RTLD_NODELETE :: 0x400
  223. MAX_PATH :: 1024
  224. // "Argv" arguments converted to Odin strings
  225. args := _alloc_command_line_arguments()
  226. pid_t :: i32
  227. time_t :: i64
  228. mode_t :: u32
  229. dev_t :: i32
  230. ino_t :: u64
  231. nlink_t :: u32
  232. uid_t :: u32
  233. gid_t :: u32
  234. off_t :: i64
  235. blkcnt_t :: u64
  236. blksize_t :: i32
  237. Unix_File_Time :: struct {
  238. seconds: time_t,
  239. nanoseconds: c.long,
  240. }
  241. OS_Stat :: struct {
  242. mode: mode_t, // inode protection mode
  243. device_id: dev_t, // inode's device
  244. serial: ino_t, // inode's number
  245. nlink: nlink_t, // number of hard links
  246. uid: uid_t, // user ID of the file's owner
  247. gid: gid_t, // group ID of the file's group
  248. rdev: dev_t, // device type
  249. last_access: Unix_File_Time, // time of last access
  250. modified: Unix_File_Time, // time of last data modification
  251. status_change: Unix_File_Time, // time of last file status change
  252. size: off_t, // file size, in bytes
  253. blocks: blkcnt_t, // blocks allocated for file
  254. block_size: blksize_t, // optimal blocksize for I/O
  255. flags: u32, // user defined flags for file
  256. gen: u32, // file generation number
  257. birthtime: Unix_File_Time, // time of file creation
  258. }
  259. MAXNAMLEN :: 255
  260. // NOTE(laleksic, 2021-01-21): Comment and rename these to match OS_Stat above
  261. Dirent :: struct {
  262. ino: ino_t, // file number of entry
  263. off: off_t, // offset after this entry
  264. reclen: u16, // length of this record
  265. type: u8, // file type
  266. namlen: u8, // length of string in name
  267. _padding: [4]u8,
  268. name: [MAXNAMLEN + 1]byte, // name
  269. }
  270. Dir :: distinct rawptr // DIR*
  271. // File type
  272. S_IFMT :: 0o170000 // Type of file mask
  273. S_IFIFO :: 0o010000 // Named pipe (fifo)
  274. S_IFCHR :: 0o020000 // Character special
  275. S_IFDIR :: 0o040000 // Directory
  276. S_IFBLK :: 0o060000 // Block special
  277. S_IFREG :: 0o100000 // Regular
  278. S_IFLNK :: 0o120000 // Symbolic link
  279. S_IFSOCK :: 0o140000 // Socket
  280. S_ISVTX :: 0o001000 // Save swapped text even after use
  281. // File mode
  282. // Read, write, execute/search by owner
  283. S_IRWXU :: 0o0700 // RWX mask for owner
  284. S_IRUSR :: 0o0400 // R for owner
  285. S_IWUSR :: 0o0200 // W for owner
  286. S_IXUSR :: 0o0100 // X for owner
  287. // Read, write, execute/search by group
  288. S_IRWXG :: 0o0070 // RWX mask for group
  289. S_IRGRP :: 0o0040 // R for group
  290. S_IWGRP :: 0o0020 // W for group
  291. S_IXGRP :: 0o0010 // X for group
  292. // Read, write, execute/search by others
  293. S_IRWXO :: 0o0007 // RWX mask for other
  294. S_IROTH :: 0o0004 // R for other
  295. S_IWOTH :: 0o0002 // W for other
  296. S_IXOTH :: 0o0001 // X for other
  297. S_ISUID :: 0o4000 // Set user id on execution
  298. S_ISGID :: 0o2000 // Set group id on execution
  299. S_ISTXT :: 0o1000 // Sticky bit
  300. @(require_results) S_ISLNK :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFLNK }
  301. @(require_results) S_ISREG :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFREG }
  302. @(require_results) S_ISDIR :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFDIR }
  303. @(require_results) S_ISCHR :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFCHR }
  304. @(require_results) S_ISBLK :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFBLK }
  305. @(require_results) S_ISFIFO :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFIFO }
  306. @(require_results) S_ISSOCK :: #force_inline proc(m: u32) -> bool { return (m & S_IFMT) == S_IFSOCK }
  307. F_OK :: 0x00 // Test for file existance
  308. X_OK :: 0x01 // Test for execute permission
  309. W_OK :: 0x02 // Test for write permission
  310. R_OK :: 0x04 // Test for read permission
  311. AT_FDCWD :: -100
  312. AT_EACCESS :: 0x01
  313. AT_SYMLINK_NOFOLLOW :: 0x02
  314. AT_SYMLINK_FOLLOW :: 0x04
  315. AT_REMOVEDIR :: 0x08
  316. @(default_calling_convention="c")
  317. foreign libc {
  318. @(link_name="__error") __error :: proc() -> ^c.int ---
  319. @(link_name="fork") _unix_fork :: proc() -> pid_t ---
  320. @(link_name="getthrid") _unix_getthrid :: proc() -> int ---
  321. @(link_name="open") _unix_open :: proc(path: cstring, flags: c.int, #c_vararg mode: ..u32) -> Handle ---
  322. @(link_name="close") _unix_close :: proc(fd: Handle) -> c.int ---
  323. @(link_name="read") _unix_read :: proc(fd: Handle, buf: rawptr, size: c.size_t) -> c.ssize_t ---
  324. @(link_name="pread") _unix_pread :: proc(fd: Handle, buf: rawptr, size: c.size_t, offset: i64) -> c.ssize_t ---
  325. @(link_name="write") _unix_write :: proc(fd: Handle, buf: rawptr, size: c.size_t) -> c.ssize_t ---
  326. @(link_name="pwrite") _unix_pwrite :: proc(fd: Handle, buf: rawptr, size: c.size_t, offset: i64) -> c.ssize_t ---
  327. @(link_name="lseek") _unix_seek :: proc(fd: Handle, offset: off_t, whence: c.int) -> off_t ---
  328. @(link_name="stat") _unix_stat :: proc(path: cstring, sb: ^OS_Stat) -> c.int ---
  329. @(link_name="fstat") _unix_fstat :: proc(fd: Handle, sb: ^OS_Stat) -> c.int ---
  330. @(link_name="lstat") _unix_lstat :: proc(path: cstring, sb: ^OS_Stat) -> c.int ---
  331. @(link_name="readlink") _unix_readlink :: proc(path: cstring, buf: ^byte, bufsiz: c.size_t) -> c.ssize_t ---
  332. @(link_name="access") _unix_access :: proc(path: cstring, mask: c.int) -> c.int ---
  333. @(link_name="getcwd") _unix_getcwd :: proc(buf: cstring, len: c.size_t) -> cstring ---
  334. @(link_name="chdir") _unix_chdir :: proc(path: cstring) -> c.int ---
  335. @(link_name="rename") _unix_rename :: proc(old, new: cstring) -> c.int ---
  336. @(link_name="unlink") _unix_unlink :: proc(path: cstring) -> c.int ---
  337. @(link_name="rmdir") _unix_rmdir :: proc(path: cstring) -> c.int ---
  338. @(link_name="mkdir") _unix_mkdir :: proc(path: cstring, mode: mode_t) -> c.int ---
  339. @(link_name="fsync") _unix_fsync :: proc(fd: Handle) -> c.int ---
  340. @(link_name="dup") _unix_dup :: proc(fd: Handle) -> Handle ---
  341. @(link_name="getpagesize") _unix_getpagesize :: proc() -> c.int ---
  342. @(link_name="sysconf") _sysconf :: proc(name: c.int) -> c.long ---
  343. @(link_name="fdopendir") _unix_fdopendir :: proc(fd: Handle) -> Dir ---
  344. @(link_name="closedir") _unix_closedir :: proc(dirp: Dir) -> c.int ---
  345. @(link_name="rewinddir") _unix_rewinddir :: proc(dirp: Dir) ---
  346. @(link_name="readdir_r") _unix_readdir_r :: proc(dirp: Dir, entry: ^Dirent, result: ^^Dirent) -> c.int ---
  347. @(link_name="malloc") _unix_malloc :: proc(size: c.size_t) -> rawptr ---
  348. @(link_name="calloc") _unix_calloc :: proc(num, size: c.size_t) -> rawptr ---
  349. @(link_name="free") _unix_free :: proc(ptr: rawptr) ---
  350. @(link_name="realloc") _unix_realloc :: proc(ptr: rawptr, size: c.size_t) -> rawptr ---
  351. @(link_name="getenv") _unix_getenv :: proc(cstring) -> cstring ---
  352. @(link_name="realpath") _unix_realpath :: proc(path: cstring, resolved_path: [^]byte = nil) -> cstring ---
  353. @(link_name="exit") _unix_exit :: proc(status: c.int) -> ! ---
  354. @(link_name="dlopen") _unix_dlopen :: proc(filename: cstring, flags: c.int) -> rawptr ---
  355. @(link_name="dlsym") _unix_dlsym :: proc(handle: rawptr, symbol: cstring) -> rawptr ---
  356. @(link_name="dlclose") _unix_dlclose :: proc(handle: rawptr) -> c.int ---
  357. @(link_name="dlerror") _unix_dlerror :: proc() -> cstring ---
  358. }
  359. @(require_results)
  360. is_path_separator :: proc(r: rune) -> bool {
  361. return r == '/'
  362. }
  363. @(require_results, no_instrumentation)
  364. get_last_error :: proc "contextless" () -> Error {
  365. return Platform_Error(__error()^)
  366. }
  367. @(require_results)
  368. fork :: proc() -> (Pid, Error) {
  369. pid := _unix_fork()
  370. if pid == -1 {
  371. return Pid(-1), get_last_error()
  372. }
  373. return Pid(pid), nil
  374. }
  375. @(require_results)
  376. open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (Handle, Error) {
  377. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  378. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  379. handle := _unix_open(cstr, c.int(flags), c.uint(mode))
  380. if handle == -1 {
  381. return INVALID_HANDLE, get_last_error()
  382. }
  383. return handle, nil
  384. }
  385. close :: proc(fd: Handle) -> Error {
  386. result := _unix_close(fd)
  387. if result == -1 {
  388. return get_last_error()
  389. }
  390. return nil
  391. }
  392. flush :: proc(fd: Handle) -> Error {
  393. result := _unix_fsync(fd)
  394. if result == -1 {
  395. return get_last_error()
  396. }
  397. return nil
  398. }
  399. // If you read or write more than `SSIZE_MAX` bytes, OpenBSD returns `EINVAL`.
  400. // In practice a read/write call would probably never read/write these big buffers all at once,
  401. // which is why the number of bytes is returned and why there are procs that will call this in a
  402. // loop for you.
  403. // We set a max of 1GB to keep alignment and to be safe.
  404. @(private)
  405. MAX_RW :: 1 << 30
  406. read :: proc(fd: Handle, data: []byte) -> (int, Error) {
  407. to_read := min(c.size_t(len(data)), MAX_RW)
  408. bytes_read := _unix_read(fd, &data[0], to_read)
  409. if bytes_read == -1 {
  410. return -1, get_last_error()
  411. }
  412. return int(bytes_read), nil
  413. }
  414. write :: proc(fd: Handle, data: []byte) -> (int, Error) {
  415. if len(data) == 0 {
  416. return 0, nil
  417. }
  418. to_write := min(c.size_t(len(data)), MAX_RW)
  419. bytes_written := _unix_write(fd, &data[0], to_write)
  420. if bytes_written == -1 {
  421. return -1, get_last_error()
  422. }
  423. return int(bytes_written), nil
  424. }
  425. read_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) {
  426. if len(data) == 0 {
  427. return 0, nil
  428. }
  429. to_read := min(uint(len(data)), MAX_RW)
  430. bytes_read := _unix_pread(fd, raw_data(data), to_read, offset)
  431. if bytes_read < 0 {
  432. return -1, get_last_error()
  433. }
  434. return bytes_read, nil
  435. }
  436. write_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) {
  437. if len(data) == 0 {
  438. return 0, nil
  439. }
  440. to_write := min(uint(len(data)), MAX_RW)
  441. bytes_written := _unix_pwrite(fd, raw_data(data), to_write, offset)
  442. if bytes_written < 0 {
  443. return -1, get_last_error()
  444. }
  445. return bytes_written, nil
  446. }
  447. seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Error) {
  448. switch whence {
  449. case SEEK_SET, SEEK_CUR, SEEK_END:
  450. break
  451. case:
  452. return 0, .Invalid_Whence
  453. }
  454. res := _unix_seek(fd, offset, c.int(whence))
  455. if res == -1 {
  456. errno := get_last_error()
  457. switch errno {
  458. case .EINVAL:
  459. return 0, .Invalid_Offset
  460. }
  461. return 0, errno
  462. }
  463. return res, nil
  464. }
  465. @(require_results)
  466. file_size :: proc(fd: Handle) -> (size: i64, err: Error) {
  467. size = -1
  468. s := _fstat(fd) or_return
  469. size = s.size
  470. return
  471. }
  472. rename :: proc(old_path, new_path: string) -> Error {
  473. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  474. old_path_cstr := strings.clone_to_cstring(old_path, context.temp_allocator)
  475. new_path_cstr := strings.clone_to_cstring(new_path, context.temp_allocator)
  476. res := _unix_rename(old_path_cstr, new_path_cstr)
  477. if res == -1 {
  478. return get_last_error()
  479. }
  480. return nil
  481. }
  482. remove :: proc(path: string) -> Error {
  483. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  484. path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
  485. res := _unix_unlink(path_cstr)
  486. if res == -1 {
  487. return get_last_error()
  488. }
  489. return nil
  490. }
  491. make_directory :: proc(path: string, mode: mode_t = 0o775) -> Error {
  492. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  493. path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
  494. res := _unix_mkdir(path_cstr, mode)
  495. if res == -1 {
  496. return get_last_error()
  497. }
  498. return nil
  499. }
  500. remove_directory :: proc(path: string) -> Error {
  501. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  502. path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
  503. res := _unix_rmdir(path_cstr)
  504. if res == -1 {
  505. return get_last_error()
  506. }
  507. return nil
  508. }
  509. @(require_results)
  510. is_file_handle :: proc(fd: Handle) -> bool {
  511. s, err := _fstat(fd)
  512. if err != nil {
  513. return false
  514. }
  515. return S_ISREG(s.mode)
  516. }
  517. @(require_results)
  518. is_file_path :: proc(path: string, follow_links: bool = true) -> bool {
  519. s: OS_Stat
  520. err: Error
  521. if follow_links {
  522. s, err = _stat(path)
  523. } else {
  524. s, err = _lstat(path)
  525. }
  526. if err != nil {
  527. return false
  528. }
  529. return S_ISREG(s.mode)
  530. }
  531. @(require_results)
  532. is_dir_handle :: proc(fd: Handle) -> bool {
  533. s, err := _fstat(fd)
  534. if err != nil {
  535. return false
  536. }
  537. return S_ISDIR(s.mode)
  538. }
  539. @(require_results)
  540. is_dir_path :: proc(path: string, follow_links: bool = true) -> bool {
  541. s: OS_Stat
  542. err: Error
  543. if follow_links {
  544. s, err = _stat(path)
  545. } else {
  546. s, err = _lstat(path)
  547. }
  548. if err != nil {
  549. return false
  550. }
  551. return S_ISDIR(s.mode)
  552. }
  553. is_file :: proc {is_file_path, is_file_handle}
  554. is_dir :: proc {is_dir_path, is_dir_handle}
  555. // NOTE(bill): Uses startup to initialize it
  556. stdin: Handle = 0
  557. stdout: Handle = 1
  558. stderr: Handle = 2
  559. /* TODO(zangent): Implement these!
  560. last_write_time :: proc(fd: Handle) -> File_Time {}
  561. last_write_time_by_name :: proc(name: string) -> File_Time {}
  562. */
  563. @(require_results)
  564. last_write_time :: proc(fd: Handle) -> (time: File_Time, err: Error) {
  565. s := _fstat(fd) or_return
  566. modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
  567. return File_Time(modified), nil
  568. }
  569. @(require_results)
  570. last_write_time_by_name :: proc(name: string) -> (time: File_Time, err: Error) {
  571. s := _stat(name) or_return
  572. modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
  573. return File_Time(modified), nil
  574. }
  575. @(private, require_results)
  576. _stat :: proc(path: string) -> (OS_Stat, Error) {
  577. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  578. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  579. // deliberately uninitialized
  580. s: OS_Stat = ---
  581. res := _unix_stat(cstr, &s)
  582. if res == -1 {
  583. return s, get_last_error()
  584. }
  585. return s, nil
  586. }
  587. @(private, require_results)
  588. _lstat :: proc(path: string) -> (OS_Stat, Error) {
  589. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  590. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  591. // deliberately uninitialized
  592. s: OS_Stat = ---
  593. res := _unix_lstat(cstr, &s)
  594. if res == -1 {
  595. return s, get_last_error()
  596. }
  597. return s, nil
  598. }
  599. @(private, require_results)
  600. _fstat :: proc(fd: Handle) -> (OS_Stat, Error) {
  601. // deliberately uninitialized
  602. s: OS_Stat = ---
  603. res := _unix_fstat(fd, &s)
  604. if res == -1 {
  605. return s, get_last_error()
  606. }
  607. return s, nil
  608. }
  609. @(private, require_results)
  610. _fdopendir :: proc(fd: Handle) -> (Dir, Error) {
  611. dirp := _unix_fdopendir(fd)
  612. if dirp == cast(Dir)nil {
  613. return nil, get_last_error()
  614. }
  615. return dirp, nil
  616. }
  617. @(private)
  618. _closedir :: proc(dirp: Dir) -> Error {
  619. rc := _unix_closedir(dirp)
  620. if rc != 0 {
  621. return get_last_error()
  622. }
  623. return nil
  624. }
  625. @(private)
  626. _rewinddir :: proc(dirp: Dir) {
  627. _unix_rewinddir(dirp)
  628. }
  629. @(private, require_results)
  630. _readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Error, end_of_stream: bool) {
  631. result: ^Dirent
  632. rc := _unix_readdir_r(dirp, &entry, &result)
  633. if rc != 0 {
  634. err = get_last_error()
  635. return
  636. }
  637. err = nil
  638. if result == nil {
  639. end_of_stream = true
  640. return
  641. }
  642. return
  643. }
  644. @(private, require_results)
  645. _readlink :: proc(path: string) -> (string, Error) {
  646. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == context.allocator)
  647. path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
  648. bufsz : uint = MAX_PATH
  649. buf := make([]byte, MAX_PATH)
  650. for {
  651. rc := _unix_readlink(path_cstr, &(buf[0]), bufsz)
  652. if rc == -1 {
  653. delete(buf)
  654. return "", get_last_error()
  655. } else if rc == int(bufsz) {
  656. bufsz += MAX_PATH
  657. delete(buf)
  658. buf = make([]byte, bufsz)
  659. } else {
  660. return strings.string_from_ptr(&buf[0], rc), nil
  661. }
  662. }
  663. }
  664. @(private, require_results)
  665. _dup :: proc(fd: Handle) -> (Handle, Error) {
  666. dup := _unix_dup(fd)
  667. if dup == -1 {
  668. return INVALID_HANDLE, get_last_error()
  669. }
  670. return dup, nil
  671. }
  672. // XXX OpenBSD
  673. @(require_results)
  674. absolute_path_from_handle :: proc(fd: Handle) -> (string, Error) {
  675. return "", Error(ENOSYS)
  676. }
  677. @(require_results)
  678. absolute_path_from_relative :: proc(rel: string, allocator := context.allocator) -> (path: string, err: Error) {
  679. rel := rel
  680. if rel == "" {
  681. rel = "."
  682. }
  683. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == context.allocator)
  684. rel_cstr := strings.clone_to_cstring(rel, context.temp_allocator)
  685. path_ptr := _unix_realpath(rel_cstr, nil)
  686. if path_ptr == nil {
  687. return "", get_last_error()
  688. }
  689. defer _unix_free(rawptr(path_ptr))
  690. return strings.clone(string(path_ptr), allocator)
  691. }
  692. access :: proc(path: string, mask: int) -> (bool, Error) {
  693. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  694. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  695. res := _unix_access(cstr, c.int(mask))
  696. if res == -1 {
  697. return false, get_last_error()
  698. }
  699. return true, nil
  700. }
  701. @(require_results)
  702. lookup_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) {
  703. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == allocator)
  704. path_str := strings.clone_to_cstring(key, context.temp_allocator)
  705. cstr := _unix_getenv(path_str)
  706. if cstr == nil {
  707. return "", false
  708. }
  709. return strings.clone(string(cstr), allocator), true
  710. }
  711. @(require_results)
  712. get_env :: proc(key: string, allocator := context.allocator) -> (value: string) {
  713. value, _ = lookup_env(key, allocator)
  714. return
  715. }
  716. @(require_results)
  717. get_current_directory :: proc(allocator := context.allocator) -> string {
  718. context.allocator = allocator
  719. buf := make([dynamic]u8, MAX_PATH)
  720. for {
  721. cwd := _unix_getcwd(cstring(raw_data(buf)), c.size_t(len(buf)))
  722. if cwd != nil {
  723. return string(cwd)
  724. }
  725. if get_last_error() != ERANGE {
  726. delete(buf)
  727. return ""
  728. }
  729. resize(&buf, len(buf) + MAX_PATH)
  730. }
  731. unreachable()
  732. }
  733. set_current_directory :: proc(path: string) -> (err: Error) {
  734. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  735. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  736. res := _unix_chdir(cstr)
  737. if res == -1 {
  738. return get_last_error()
  739. }
  740. return nil
  741. }
  742. exit :: proc "contextless" (code: int) -> ! {
  743. runtime._cleanup_runtime_contextless()
  744. _unix_exit(c.int(code))
  745. }
  746. @(require_results)
  747. current_thread_id :: proc "contextless" () -> int {
  748. return _unix_getthrid()
  749. }
  750. @(require_results)
  751. dlopen :: proc(filename: string, flags: int) -> rawptr {
  752. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  753. cstr := strings.clone_to_cstring(filename, context.temp_allocator)
  754. handle := _unix_dlopen(cstr, c.int(flags))
  755. return handle
  756. }
  757. @(require_results)
  758. dlsym :: proc(handle: rawptr, symbol: string) -> rawptr {
  759. assert(handle != nil)
  760. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  761. cstr := strings.clone_to_cstring(symbol, context.temp_allocator)
  762. proc_handle := _unix_dlsym(handle, cstr)
  763. return proc_handle
  764. }
  765. dlclose :: proc(handle: rawptr) -> bool {
  766. assert(handle != nil)
  767. return _unix_dlclose(handle) == 0
  768. }
  769. @(require_results)
  770. dlerror :: proc() -> string {
  771. return string(_unix_dlerror())
  772. }
  773. @(require_results)
  774. get_page_size :: proc() -> int {
  775. // NOTE(tetra): The page size never changes, so why do anything complicated
  776. // if we don't have to.
  777. @static page_size := -1
  778. if page_size != -1 {
  779. return page_size
  780. }
  781. page_size = int(_unix_getpagesize())
  782. return page_size
  783. }
  784. _SC_NPROCESSORS_ONLN :: 503
  785. @(private, require_results)
  786. _processor_core_count :: proc() -> int {
  787. return int(_sysconf(_SC_NPROCESSORS_ONLN))
  788. }
  789. @(require_results)
  790. _alloc_command_line_arguments :: proc() -> []string {
  791. res := make([]string, len(runtime.args__))
  792. for arg, i in runtime.args__ {
  793. res[i] = string(arg)
  794. }
  795. return res
  796. }