os_darwin.odin 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. package os
  2. foreign import dl "system:dl"
  3. foreign import libc "system:System.framework"
  4. foreign import pthread "system:System.framework"
  5. import "base:runtime"
  6. import "core:strings"
  7. import "core:c"
  8. Handle :: distinct i32
  9. File_Time :: distinct u64
  10. Errno :: distinct int
  11. INVALID_HANDLE :: ~Handle(0)
  12. ERROR_NONE: Errno : 0
  13. EPERM: Errno : 1 /* Operation not permitted */
  14. ENOENT: Errno : 2 /* No such file or directory */
  15. ESRCH: Errno : 3 /* No such process */
  16. EINTR: Errno : 4 /* Interrupted system call */
  17. EIO: Errno : 5 /* Input/output error */
  18. ENXIO: Errno : 6 /* Device not configured */
  19. E2BIG: Errno : 7 /* Argument list too long */
  20. ENOEXEC: Errno : 8 /* Exec format error */
  21. EBADF: Errno : 9 /* Bad file descriptor */
  22. ECHILD: Errno : 10 /* No child processes */
  23. EDEADLK: Errno : 11 /* Resource deadlock avoided */
  24. ENOMEM: Errno : 12 /* Cannot allocate memory */
  25. EACCES: Errno : 13 /* Permission denied */
  26. EFAULT: Errno : 14 /* Bad address */
  27. ENOTBLK: Errno : 15 /* Block device required */
  28. EBUSY: Errno : 16 /* Device / Resource busy */
  29. EEXIST: Errno : 17 /* File exists */
  30. EXDEV: Errno : 18 /* Cross-device link */
  31. ENODEV: Errno : 19 /* Operation not supported by device */
  32. ENOTDIR: Errno : 20 /* Not a directory */
  33. EISDIR: Errno : 21 /* Is a directory */
  34. EINVAL: Errno : 22 /* Invalid argument */
  35. ENFILE: Errno : 23 /* Too many open files in system */
  36. EMFILE: Errno : 24 /* Too many open files */
  37. ENOTTY: Errno : 25 /* Inappropriate ioctl for device */
  38. ETXTBSY: Errno : 26 /* Text file busy */
  39. EFBIG: Errno : 27 /* File too large */
  40. ENOSPC: Errno : 28 /* No space left on device */
  41. ESPIPE: Errno : 29 /* Illegal seek */
  42. EROFS: Errno : 30 /* Read-only file system */
  43. EMLINK: Errno : 31 /* Too many links */
  44. EPIPE: Errno : 32 /* Broken pipe */
  45. /* math software */
  46. EDOM: Errno : 33 /* Numerical argument out of domain */
  47. ERANGE: Errno : 34 /* Result too large */
  48. /* non-blocking and interrupt i/o */
  49. EAGAIN: Errno : 35 /* Resource temporarily unavailable */
  50. EWOULDBLOCK: Errno : EAGAIN /* Operation would block */
  51. EINPROGRESS: Errno : 36 /* Operation now in progress */
  52. EALREADY: Errno : 37 /* Operation already in progress */
  53. /* ipc/network software -- argument errors */
  54. ENOTSOCK: Errno : 38 /* Socket operation on non-socket */
  55. EDESTADDRREQ: Errno : 39 /* Destination address required */
  56. EMSGSIZE: Errno : 40 /* Message too long */
  57. EPROTOTYPE: Errno : 41 /* Protocol wrong type for socket */
  58. ENOPROTOOPT: Errno : 42 /* Protocol not available */
  59. EPROTONOSUPPORT: Errno : 43 /* Protocol not supported */
  60. ESOCKTNOSUPPORT: Errno : 44 /* Socket type not supported */
  61. ENOTSUP: Errno : 45 /* Operation not supported */
  62. EOPNOTSUPP:: ENOTSUP
  63. EPFNOSUPPORT: Errno : 46 /* Protocol family not supported */
  64. EAFNOSUPPORT: Errno : 47 /* Address family not supported by protocol family */
  65. EADDRINUSE: Errno : 48 /* Address already in use */
  66. EADDRNOTAVAIL: Errno : 49 /* Can't assign requested address */
  67. /* ipc/network software -- operational errors */
  68. ENETDOWN: Errno : 50 /* Network is down */
  69. ENETUNREACH: Errno : 51 /* Network is unreachable */
  70. ENETRESET: Errno : 52 /* Network dropped connection on reset */
  71. ECONNABORTED: Errno : 53 /* Software caused connection abort */
  72. ECONNRESET: Errno : 54 /* Connection reset by peer */
  73. ENOBUFS: Errno : 55 /* No buffer space available */
  74. EISCONN: Errno : 56 /* Socket is already connected */
  75. ENOTCONN: Errno : 57 /* Socket is not connected */
  76. ESHUTDOWN: Errno : 58 /* Can't send after socket shutdown */
  77. ETOOMANYREFS: Errno : 59 /* Too many references: can't splice */
  78. ETIMEDOUT: Errno : 60 /* Operation timed out */
  79. ECONNREFUSED: Errno : 61 /* Connection refused */
  80. ELOOP: Errno : 62 /* Too many levels of symbolic links */
  81. ENAMETOOLONG: Errno : 63 /* File name too long */
  82. /* should be rearranged */
  83. EHOSTDOWN: Errno : 64 /* Host is down */
  84. EHOSTUNREACH: Errno : 65 /* No route to host */
  85. ENOTEMPTY: Errno : 66 /* Directory not empty */
  86. /* quotas & mush */
  87. EPROCLIM: Errno : 67 /* Too many processes */
  88. EUSERS: Errno : 68 /* Too many users */
  89. EDQUOT: Errno : 69 /* Disc quota exceeded */
  90. /* Network File System */
  91. ESTALE: Errno : 70 /* Stale NFS file handle */
  92. EREMOTE: Errno : 71 /* Too many levels of remote in path */
  93. EBADRPC: Errno : 72 /* RPC struct is bad */
  94. ERPCMISMATCH: Errno : 73 /* RPC version wrong */
  95. EPROGUNAVAIL: Errno : 74 /* RPC prog. not avail */
  96. EPROGMISMATCH: Errno : 75 /* Program version wrong */
  97. EPROCUNAVAIL: Errno : 76 /* Bad procedure for program */
  98. ENOLCK: Errno : 77 /* No locks available */
  99. ENOSYS: Errno : 78 /* Function not implemented */
  100. EFTYPE: Errno : 79 /* Inappropriate file type or format */
  101. EAUTH: Errno : 80 /* Authentication error */
  102. ENEEDAUTH: Errno : 81 /* Need authenticator */
  103. /* Intelligent device errors */
  104. EPWROFF: Errno : 82 /* Device power is off */
  105. EDEVERR: Errno : 83 /* Device error, e.g. paper out */
  106. EOVERFLOW: Errno : 84 /* Value too large to be stored in data type */
  107. /* Program loading errors */
  108. EBADEXEC: Errno : 85 /* Bad executable */
  109. EBADARCH: Errno : 86 /* Bad CPU type in executable */
  110. ESHLIBVERS: Errno : 87 /* Shared library version mismatch */
  111. EBADMACHO: Errno : 88 /* Malformed Macho file */
  112. ECANCELED: Errno : 89 /* Operation canceled */
  113. EIDRM: Errno : 90 /* Identifier removed */
  114. ENOMSG: Errno : 91 /* No message of desired type */
  115. EILSEQ: Errno : 92 /* Illegal byte sequence */
  116. ENOATTR: Errno : 93 /* Attribute not found */
  117. EBADMSG: Errno : 94 /* Bad message */
  118. EMULTIHOP: Errno : 95 /* Reserved */
  119. ENODATA: Errno : 96 /* No message available on STREAM */
  120. ENOLINK: Errno : 97 /* Reserved */
  121. ENOSR: Errno : 98 /* No STREAM resources */
  122. ENOSTR: Errno : 99 /* Not a STREAM */
  123. EPROTO: Errno : 100 /* Protocol error */
  124. ETIME: Errno : 101 /* STREAM ioctl timeout */
  125. ENOPOLICY: Errno : 103 /* No such policy registered */
  126. ENOTRECOVERABLE: Errno : 104 /* State not recoverable */
  127. EOWNERDEAD: Errno : 105 /* Previous owner died */
  128. EQFULL: Errno : 106 /* Interface output queue is full */
  129. ELAST: Errno : 106 /* Must be equal largest errno */
  130. O_RDONLY :: 0x0000
  131. O_WRONLY :: 0x0001
  132. O_RDWR :: 0x0002
  133. O_CREATE :: 0x0200
  134. O_EXCL :: 0x0800
  135. O_NOCTTY :: 0
  136. O_TRUNC :: 0x0400
  137. O_NONBLOCK :: 0x0004
  138. O_APPEND :: 0x0008
  139. O_SYNC :: 0x0080
  140. O_ASYNC :: 0x0040
  141. O_CLOEXEC :: 0x1000000
  142. SEEK_DATA :: 3
  143. SEEK_HOLE :: 4
  144. SEEK_MAX :: SEEK_HOLE
  145. // NOTE(zangent): These are OS specific!
  146. // Do not mix these up!
  147. RTLD_LAZY :: 0x1
  148. RTLD_NOW :: 0x2
  149. RTLD_LOCAL :: 0x4
  150. RTLD_GLOBAL :: 0x8
  151. RTLD_NODELETE :: 0x80
  152. RTLD_NOLOAD :: 0x10
  153. RTLD_FIRST :: 0x100
  154. SOL_SOCKET :: 0xFFFF
  155. SOCK_STREAM :: 1
  156. SOCK_DGRAM :: 2
  157. SOCK_RAW :: 3
  158. SOCK_RDM :: 4
  159. SOCK_SEQPACKET :: 5
  160. SO_DEBUG :: 0x0001
  161. SO_ACCEPTCONN :: 0x0002
  162. SO_REUSEADDR :: 0x0004
  163. SO_KEEPALIVE :: 0x0008
  164. SO_DONTROUTE :: 0x0010
  165. SO_BROADCAST :: 0x0020
  166. SO_USELOOPBACK :: 0x0040
  167. SO_LINGER :: 0x0080
  168. SO_OOBINLINE :: 0x0100
  169. SO_REUSEPORT :: 0x0200
  170. SO_TIMESTAMP :: 0x0400
  171. SO_DONTTRUNC :: 0x2000
  172. SO_WANTMORE :: 0x4000
  173. SO_WANTOOBFLAG :: 0x8000
  174. SO_SNDBUF :: 0x1001
  175. SO_RCVBUF :: 0x1002
  176. SO_SNDLOWAT :: 0x1003
  177. SO_RCVLOWAT :: 0x1004
  178. SO_SNDTIMEO :: 0x1005
  179. SO_RCVTIMEO :: 0x1006
  180. SO_ERROR :: 0x1007
  181. SO_TYPE :: 0x1008
  182. SO_PRIVSTATE :: 0x1009
  183. SO_NREAD :: 0x1020
  184. SO_NKE :: 0x1021
  185. AF_UNSPEC :: 0
  186. AF_LOCAL :: 1
  187. AF_UNIX :: AF_LOCAL
  188. AF_INET :: 2
  189. AF_IMPLINK :: 3
  190. AF_PUP :: 4
  191. AF_CHAOS :: 5
  192. AF_NS :: 6
  193. AF_ISO :: 7
  194. AF_OSI :: AF_ISO
  195. AF_ECMA :: 8
  196. AF_DATAKIT :: 9
  197. AF_CCITT :: 10
  198. AF_SNA :: 11
  199. AF_DECnet :: 12
  200. AF_DLI :: 13
  201. AF_LAT :: 14
  202. AF_HYLINK :: 15
  203. AF_APPLETALK :: 16
  204. AF_ROUTE :: 17
  205. AF_LINK :: 18
  206. pseudo_AF_XTP :: 19
  207. AF_COIP :: 20
  208. AF_CNT :: 21
  209. pseudo_AF_RTIP :: 22
  210. AF_IPX :: 23
  211. AF_SIP :: 24
  212. pseudo_AF_PIP :: 25
  213. pseudo_AF_BLUE :: 26
  214. AF_NDRV :: 27
  215. AF_ISDN :: 28
  216. AF_E164 :: AF_ISDN
  217. pseudo_AF_KEY :: 29
  218. AF_INET6 :: 30
  219. AF_NATM :: 31
  220. AF_SYSTEM :: 32
  221. AF_NETBIOS :: 33
  222. AF_PPP :: 34
  223. TCP_NODELAY :: 0x01
  224. TCP_MAXSEG :: 0x02
  225. TCP_NOPUSH :: 0x04
  226. TCP_NOOPT :: 0x08
  227. IPPROTO_ICMP :: 1
  228. IPPROTO_TCP :: 6
  229. IPPROTO_UDP :: 17
  230. SHUT_RD :: 0
  231. SHUT_WR :: 1
  232. SHUT_RDWR :: 2
  233. F_GETFL: int : 3 /* Get file flags */
  234. F_SETFL: int : 4 /* Set file flags */
  235. // "Argv" arguments converted to Odin strings
  236. args := _alloc_command_line_arguments()
  237. Unix_File_Time :: struct {
  238. seconds: i64,
  239. nanoseconds: i64,
  240. }
  241. OS_Stat :: struct {
  242. device_id: i32, // ID of device containing file
  243. mode: u16, // Mode of the file
  244. nlink: u16, // Number of hard links
  245. serial: u64, // File serial number
  246. uid: u32, // User ID of the file's owner
  247. gid: u32, // Group ID of the file's group
  248. rdev: i32, // Device ID, if device
  249. last_access: Unix_File_Time, // Time of last access
  250. modified: Unix_File_Time, // Time of last modification
  251. status_change: Unix_File_Time, // Time of last status change
  252. created: Unix_File_Time, // Time of creation
  253. size: i64, // Size of the file, in bytes
  254. blocks: i64, // Number of blocks allocated for the file
  255. block_size: i32, // Optimal blocksize for I/O
  256. flags: u32, // User-defined flags for the file
  257. gen_num: u32, // File generation number ..?
  258. _spare: i32, // RESERVED
  259. _reserve1,
  260. _reserve2: i64, // RESERVED
  261. }
  262. DARWIN_MAXPATHLEN :: 1024
  263. Dirent :: struct {
  264. ino: u64,
  265. off: u64,
  266. reclen: u16,
  267. namlen: u16,
  268. type: u8,
  269. name: [DARWIN_MAXPATHLEN]byte,
  270. }
  271. Dir :: distinct rawptr // DIR*
  272. ADDRESS_FAMILY :: c.char
  273. SOCKADDR :: struct #packed {
  274. len: c.char,
  275. family: ADDRESS_FAMILY,
  276. sa_data: [14]c.char,
  277. }
  278. SOCKADDR_STORAGE_LH :: struct #packed {
  279. len: c.char,
  280. family: ADDRESS_FAMILY,
  281. __ss_pad1: [6]c.char,
  282. __ss_align: i64,
  283. __ss_pad2: [112]c.char,
  284. }
  285. sockaddr_in :: struct #packed {
  286. sin_len: c.char,
  287. sin_family: ADDRESS_FAMILY,
  288. sin_port: u16be,
  289. sin_addr: in_addr,
  290. sin_zero: [8]c.char,
  291. }
  292. sockaddr_in6 :: struct #packed {
  293. sin6_len: c.char,
  294. sin6_family: ADDRESS_FAMILY,
  295. sin6_port: u16be,
  296. sin6_flowinfo: c.uint,
  297. sin6_addr: in6_addr,
  298. sin6_scope_id: c.uint,
  299. }
  300. in_addr :: struct #packed {
  301. s_addr: u32,
  302. }
  303. in6_addr :: struct #packed {
  304. s6_addr: [16]u8,
  305. }
  306. SIOCGIFFLAG :: enum c.int {
  307. UP = 0, /* Interface is up. */
  308. BROADCAST = 1, /* Broadcast address valid. */
  309. DEBUG = 2, /* Turn on debugging. */
  310. LOOPBACK = 3, /* Is a loopback net. */
  311. POINT_TO_POINT = 4, /* Interface is point-to-point link. */
  312. NO_TRAILERS = 5, /* Avoid use of trailers. */
  313. RUNNING = 6, /* Resources allocated. */
  314. NOARP = 7, /* No address resolution protocol. */
  315. PROMISC = 8, /* Receive all packets. */
  316. ALL_MULTI = 9, /* Receive all multicast packets. Unimplemented. */
  317. }
  318. SIOCGIFFLAGS :: bit_set[SIOCGIFFLAG; c.int]
  319. ifaddrs :: struct {
  320. next: ^ifaddrs,
  321. name: cstring,
  322. flags: SIOCGIFFLAGS,
  323. address: ^SOCKADDR,
  324. netmask: ^SOCKADDR,
  325. broadcast_or_dest: ^SOCKADDR, // Broadcast or Point-to-Point address
  326. data: rawptr, // Address-specific data.
  327. }
  328. Timeval :: struct {
  329. seconds: i64,
  330. microseconds: int,
  331. }
  332. Linger :: struct {
  333. onoff: int,
  334. linger: int,
  335. }
  336. Socket :: distinct int
  337. socklen_t :: c.int
  338. // File type
  339. S_IFMT :: 0o170000 // Type of file mask
  340. S_IFIFO :: 0o010000 // Named pipe (fifo)
  341. S_IFCHR :: 0o020000 // Character special
  342. S_IFDIR :: 0o040000 // Directory
  343. S_IFBLK :: 0o060000 // Block special
  344. S_IFREG :: 0o100000 // Regular
  345. S_IFLNK :: 0o120000 // Symbolic link
  346. S_IFSOCK :: 0o140000 // Socket
  347. // File mode
  348. // Read, write, execute/search by owner
  349. S_IRWXU :: 0o0700 // RWX mask for owner
  350. S_IRUSR :: 0o0400 // R for owner
  351. S_IWUSR :: 0o0200 // W for owner
  352. S_IXUSR :: 0o0100 // X for owner
  353. // Read, write, execute/search by group
  354. S_IRWXG :: 0o0070 // RWX mask for group
  355. S_IRGRP :: 0o0040 // R for group
  356. S_IWGRP :: 0o0020 // W for group
  357. S_IXGRP :: 0o0010 // X for group
  358. // Read, write, execute/search by others
  359. S_IRWXO :: 0o0007 // RWX mask for other
  360. S_IROTH :: 0o0004 // R for other
  361. S_IWOTH :: 0o0002 // W for other
  362. S_IXOTH :: 0o0001 // X for other
  363. S_ISUID :: 0o4000 // Set user id on execution
  364. S_ISGID :: 0o2000 // Set group id on execution
  365. S_ISVTX :: 0o1000 // Directory restrcted delete
  366. S_ISLNK :: #force_inline proc(m: u16) -> bool { return (m & S_IFMT) == S_IFLNK }
  367. S_ISREG :: #force_inline proc(m: u16) -> bool { return (m & S_IFMT) == S_IFREG }
  368. S_ISDIR :: #force_inline proc(m: u16) -> bool { return (m & S_IFMT) == S_IFDIR }
  369. S_ISCHR :: #force_inline proc(m: u16) -> bool { return (m & S_IFMT) == S_IFCHR }
  370. S_ISBLK :: #force_inline proc(m: u16) -> bool { return (m & S_IFMT) == S_IFBLK }
  371. S_ISFIFO :: #force_inline proc(m: u16) -> bool { return (m & S_IFMT) == S_IFIFO }
  372. S_ISSOCK :: #force_inline proc(m: u16) -> bool { return (m & S_IFMT) == S_IFSOCK }
  373. R_OK :: 4 // Test for read permission
  374. W_OK :: 2 // Test for write permission
  375. X_OK :: 1 // Test for execute permission
  376. F_OK :: 0 // Test for file existance
  377. F_GETPATH :: 50 // return the full path of the fd
  378. foreign libc {
  379. @(link_name="__error") __error :: proc() -> ^c.int ---
  380. @(link_name="open") _unix_open :: proc(path: cstring, flags: i32, #c_vararg args: ..any) -> Handle ---
  381. @(link_name="close") _unix_close :: proc(handle: Handle) -> c.int ---
  382. @(link_name="read") _unix_read :: proc(handle: Handle, buffer: rawptr, count: c.size_t) -> int ---
  383. @(link_name="write") _unix_write :: proc(handle: Handle, buffer: rawptr, count: c.size_t) -> int ---
  384. @(link_name="pread") _unix_pread :: proc(handle: Handle, buffer: rawptr, count: c.size_t, offset: i64) -> int ---
  385. @(link_name="pwrite") _unix_pwrite :: proc(handle: Handle, buffer: rawptr, count: c.size_t, offset: i64) -> int ---
  386. @(link_name="lseek") _unix_lseek :: proc(fs: Handle, offset: int, whence: c.int) -> int ---
  387. @(link_name="gettid") _unix_gettid :: proc() -> u64 ---
  388. @(link_name="getpagesize") _unix_getpagesize :: proc() -> i32 ---
  389. @(link_name="stat64") _unix_stat :: proc(path: cstring, stat: ^OS_Stat) -> c.int ---
  390. @(link_name="lstat64") _unix_lstat :: proc(path: cstring, stat: ^OS_Stat) -> c.int ---
  391. @(link_name="fstat64") _unix_fstat :: proc(fd: Handle, stat: ^OS_Stat) -> c.int ---
  392. @(link_name="readlink") _unix_readlink :: proc(path: cstring, buf: ^byte, bufsiz: c.size_t) -> c.ssize_t ---
  393. @(link_name="access") _unix_access :: proc(path: cstring, mask: c.int) -> c.int ---
  394. @(link_name="fsync") _unix_fsync :: proc(handle: Handle) -> c.int ---
  395. @(link_name="fdopendir$INODE64") _unix_fdopendir_amd64 :: proc(fd: Handle) -> Dir ---
  396. @(link_name="readdir_r$INODE64") _unix_readdir_r_amd64 :: proc(dirp: Dir, entry: ^Dirent, result: ^^Dirent) -> c.int ---
  397. @(link_name="fdopendir") _unix_fdopendir_arm64 :: proc(fd: Handle) -> Dir ---
  398. @(link_name="readdir_r") _unix_readdir_r_arm64 :: proc(dirp: Dir, entry: ^Dirent, result: ^^Dirent) -> c.int ---
  399. @(link_name="closedir") _unix_closedir :: proc(dirp: Dir) -> c.int ---
  400. @(link_name="rewinddir") _unix_rewinddir :: proc(dirp: Dir) ---
  401. @(link_name="__fcntl") _unix__fcntl :: proc(fd: Handle, cmd: c.int, arg: uintptr) -> c.int ---
  402. @(link_name="rename") _unix_rename :: proc(old: cstring, new: cstring) -> c.int ---
  403. @(link_name="remove") _unix_remove :: proc(path: cstring) -> c.int ---
  404. @(link_name="fchmod") _unix_fchmod :: proc(fd: Handle, mode: u16) -> c.int ---
  405. @(link_name="malloc") _unix_malloc :: proc(size: int) -> rawptr ---
  406. @(link_name="calloc") _unix_calloc :: proc(num, size: int) -> rawptr ---
  407. @(link_name="free") _unix_free :: proc(ptr: rawptr) ---
  408. @(link_name="realloc") _unix_realloc :: proc(ptr: rawptr, size: int) -> rawptr ---
  409. @(link_name="getenv") _unix_getenv :: proc(cstring) -> cstring ---
  410. @(link_name="unsetenv") _unix_unsetenv :: proc(cstring) -> c.int ---
  411. @(link_name="setenv") _unix_setenv :: proc(key: cstring, value: cstring, overwrite: c.int) -> c.int ---
  412. @(link_name="getcwd") _unix_getcwd :: proc(buf: cstring, len: c.size_t) -> cstring ---
  413. @(link_name="chdir") _unix_chdir :: proc(buf: cstring) -> c.int ---
  414. @(link_name="mkdir") _unix_mkdir :: proc(buf: cstring, mode: u16) -> c.int ---
  415. @(link_name="realpath") _unix_realpath :: proc(path: cstring, resolved_path: rawptr) -> rawptr ---
  416. @(link_name="strerror") _darwin_string_error :: proc(num : c.int) -> cstring ---
  417. @(link_name="sysctlbyname") _sysctlbyname :: proc(path: cstring, oldp: rawptr, oldlenp: rawptr, newp: rawptr, newlen: int) -> c.int ---
  418. @(link_name="socket") _unix_socket :: proc(domain: int, type: int, protocol: int) -> int ---
  419. @(link_name="listen") _unix_listen :: proc(socket: int, backlog: int) -> int ---
  420. @(link_name="accept") _unix_accept :: proc(socket: int, addr: rawptr, addr_len: rawptr) -> int ---
  421. @(link_name="connect") _unix_connect :: proc(socket: int, addr: rawptr, addr_len: socklen_t) -> int ---
  422. @(link_name="bind") _unix_bind :: proc(socket: int, addr: rawptr, addr_len: socklen_t) -> int ---
  423. @(link_name="setsockopt") _unix_setsockopt :: proc(socket: int, level: int, opt_name: int, opt_val: rawptr, opt_len: socklen_t) -> int ---
  424. @(link_name="getsockopt") _unix_getsockopt :: proc(socket: int, level: int, opt_name: int, opt_val: rawptr, opt_len: socklen_t) -> int ---
  425. @(link_name="recvfrom") _unix_recvfrom :: proc(socket: int, buffer: rawptr, buffer_len: c.size_t, flags: int, addr: rawptr, addr_len: ^socklen_t) -> c.ssize_t ---
  426. @(link_name="recv") _unix_recv :: proc(socket: int, buffer: rawptr, buffer_len: c.size_t, flags: int) -> c.ssize_t ---
  427. @(link_name="sendto") _unix_sendto :: proc(socket: int, buffer: rawptr, buffer_len: c.size_t, flags: int, addr: rawptr, addr_len: socklen_t) -> c.ssize_t ---
  428. @(link_name="send") _unix_send :: proc(socket: int, buffer: rawptr, buffer_len: c.size_t, flags: int) -> c.ssize_t ---
  429. @(link_name="shutdown") _unix_shutdown :: proc(socket: int, how: int) -> int ---
  430. @(link_name="getifaddrs") _getifaddrs :: proc(ifap: ^^ifaddrs) -> (c.int) ---
  431. @(link_name="freeifaddrs") _freeifaddrs :: proc(ifa: ^ifaddrs) ---
  432. @(link_name="exit") _unix_exit :: proc(status: c.int) -> ! ---
  433. }
  434. when ODIN_ARCH != .arm64 {
  435. _unix_fdopendir :: proc {_unix_fdopendir_amd64}
  436. _unix_readdir_r :: proc {_unix_readdir_r_amd64}
  437. } else {
  438. _unix_fdopendir :: proc {_unix_fdopendir_arm64}
  439. _unix_readdir_r :: proc {_unix_readdir_r_arm64}
  440. }
  441. foreign dl {
  442. @(link_name="dlopen") _unix_dlopen :: proc(filename: cstring, flags: int) -> rawptr ---
  443. @(link_name="dlsym") _unix_dlsym :: proc(handle: rawptr, symbol: cstring) -> rawptr ---
  444. @(link_name="dlclose") _unix_dlclose :: proc(handle: rawptr) -> int ---
  445. @(link_name="dlerror") _unix_dlerror :: proc() -> cstring ---
  446. }
  447. get_last_error :: proc "contextless" () -> int {
  448. return int(__error()^)
  449. }
  450. get_last_error_string :: proc() -> string {
  451. return cast(string)_darwin_string_error(cast(c.int)get_last_error())
  452. }
  453. open :: proc(path: string, flags: int = O_RDWR, mode: int = 0) -> (Handle, Errno) {
  454. isDir := is_dir_path(path)
  455. flags := flags
  456. if isDir {
  457. /*
  458. @INFO(Platin): To make it impossible to use the wrong flag for dir's
  459. as you can't write to a dir only read which makes it fail to open
  460. */
  461. flags = O_RDONLY
  462. }
  463. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  464. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  465. handle := _unix_open(cstr, i32(flags), u16(mode))
  466. if handle == -1 {
  467. return INVALID_HANDLE, cast(Errno)get_last_error()
  468. }
  469. /*
  470. @INFO(Platin): this is only done because O_CREATE for some reason fails to apply mode
  471. should not happen if the handle is a directory
  472. */
  473. if mode != 0 && !isDir {
  474. err := fchmod(handle, cast(u16)mode)
  475. if err != 0 {
  476. _unix_close(handle)
  477. return INVALID_HANDLE, err
  478. }
  479. }
  480. return handle, 0
  481. }
  482. fchmod :: proc(fd: Handle, mode: u16) -> Errno {
  483. return cast(Errno)_unix_fchmod(fd, mode)
  484. }
  485. close :: proc(fd: Handle) -> Errno {
  486. return cast(Errno)_unix_close(fd)
  487. }
  488. // If you read or write more than `SSIZE_MAX` bytes, most darwin implementations will return `EINVAL`
  489. // but it is really implementation defined. `SSIZE_MAX` is also implementation defined but usually
  490. // the max of an i32 on Darwin.
  491. // In practice a read/write call would probably never read/write these big buffers all at once,
  492. // which is why the number of bytes is returned and why there are procs that will call this in a
  493. // loop for you.
  494. // We set a max of 1GB to keep alignment and to be safe.
  495. @(private)
  496. MAX_RW :: 1 << 30
  497. write :: proc(fd: Handle, data: []byte) -> (int, Errno) {
  498. if len(data) == 0 {
  499. return 0, ERROR_NONE
  500. }
  501. to_write := min(c.size_t(len(data)), MAX_RW)
  502. bytes_written := _unix_write(fd, raw_data(data), to_write)
  503. if bytes_written < 0 {
  504. return -1, Errno(get_last_error())
  505. }
  506. return bytes_written, ERROR_NONE
  507. }
  508. read :: proc(fd: Handle, data: []u8) -> (int, Errno) {
  509. if len(data) == 0 {
  510. return 0, ERROR_NONE
  511. }
  512. to_read := min(c.size_t(len(data)), MAX_RW)
  513. bytes_read := _unix_read(fd, raw_data(data), to_read)
  514. if bytes_read < 0 {
  515. return -1, Errno(get_last_error())
  516. }
  517. return bytes_read, ERROR_NONE
  518. }
  519. read_at :: proc(fd: Handle, data: []byte, offset: i64) -> (int, Errno) {
  520. if len(data) == 0 {
  521. return 0, ERROR_NONE
  522. }
  523. to_read := min(c.size_t(len(data)), MAX_RW)
  524. bytes_read := _unix_pread(fd, raw_data(data), to_read, offset)
  525. if bytes_read < 0 {
  526. return -1, Errno(get_last_error())
  527. }
  528. return bytes_read, ERROR_NONE
  529. }
  530. write_at :: proc(fd: Handle, data: []byte, offset: i64) -> (int, Errno) {
  531. if len(data) == 0 {
  532. return 0, ERROR_NONE
  533. }
  534. to_write := min(c.size_t(len(data)), MAX_RW)
  535. bytes_written := _unix_pwrite(fd, raw_data(data), to_write, offset)
  536. if bytes_written < 0 {
  537. return -1, Errno(get_last_error())
  538. }
  539. return bytes_written, ERROR_NONE
  540. }
  541. seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
  542. assert(fd != -1)
  543. final_offset := i64(_unix_lseek(fd, int(offset), c.int(whence)))
  544. if final_offset == -1 {
  545. return 0, 1
  546. }
  547. return final_offset, 0
  548. }
  549. file_size :: proc(fd: Handle) -> (i64, Errno) {
  550. prev, _ := seek(fd, 0, SEEK_CUR)
  551. size, err := seek(fd, 0, SEEK_END)
  552. seek(fd, prev, SEEK_SET)
  553. return i64(size), err
  554. }
  555. // NOTE(bill): Uses startup to initialize it
  556. stdin: Handle = 0 // get_std_handle(win32.STD_INPUT_HANDLE);
  557. stdout: Handle = 1 // get_std_handle(win32.STD_OUTPUT_HANDLE);
  558. stderr: Handle = 2 // get_std_handle(win32.STD_ERROR_HANDLE);
  559. last_write_time :: proc(fd: Handle) -> (File_Time, Errno) {
  560. s, err := _fstat(fd)
  561. if err != ERROR_NONE {
  562. return 0, err
  563. }
  564. modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
  565. return File_Time(modified), ERROR_NONE
  566. }
  567. last_write_time_by_name :: proc(name: string) -> (File_Time, Errno) {
  568. s, err := _stat(name)
  569. if err != ERROR_NONE {
  570. return 0, err
  571. }
  572. modified := s.modified.seconds * 1_000_000_000 + s.modified.nanoseconds
  573. return File_Time(modified), ERROR_NONE
  574. }
  575. is_path_separator :: proc(r: rune) -> bool {
  576. return r == '/'
  577. }
  578. is_file_handle :: proc(fd: Handle) -> bool {
  579. s, err := _fstat(fd)
  580. if err != ERROR_NONE {
  581. return false
  582. }
  583. return S_ISREG(s.mode)
  584. }
  585. is_file_path :: proc(path: string, follow_links: bool = true) -> bool {
  586. s: OS_Stat
  587. err: Errno
  588. if follow_links {
  589. s, err = _stat(path)
  590. } else {
  591. s, err = _lstat(path)
  592. }
  593. if err != ERROR_NONE {
  594. return false
  595. }
  596. return S_ISREG(s.mode)
  597. }
  598. is_dir_handle :: proc(fd: Handle) -> bool {
  599. s, err := _fstat(fd)
  600. if err != ERROR_NONE {
  601. return false
  602. }
  603. return S_ISDIR(s.mode)
  604. }
  605. is_dir_path :: proc(path: string, follow_links: bool = true) -> bool {
  606. s: OS_Stat
  607. err: Errno
  608. if follow_links {
  609. s, err = _stat(path)
  610. } else {
  611. s, err = _lstat(path)
  612. }
  613. if err != ERROR_NONE {
  614. return false
  615. }
  616. return S_ISDIR(s.mode)
  617. }
  618. is_file :: proc {is_file_path, is_file_handle}
  619. is_dir :: proc {is_dir_path, is_dir_handle}
  620. exists :: proc(path: string) -> bool {
  621. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  622. cpath := strings.clone_to_cstring(path, context.temp_allocator)
  623. res := _unix_access(cpath, O_RDONLY)
  624. return res == 0
  625. }
  626. rename :: proc(old: string, new: string) -> bool {
  627. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  628. old_cstr := strings.clone_to_cstring(old, context.temp_allocator)
  629. new_cstr := strings.clone_to_cstring(new, context.temp_allocator)
  630. return _unix_rename(old_cstr, new_cstr) != -1
  631. }
  632. remove :: proc(path: string) -> Errno {
  633. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  634. path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
  635. res := _unix_remove(path_cstr)
  636. if res == -1 {
  637. return Errno(get_last_error())
  638. }
  639. return ERROR_NONE
  640. }
  641. @private
  642. _stat :: proc(path: string) -> (OS_Stat, Errno) {
  643. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  644. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  645. s: OS_Stat
  646. result := _unix_stat(cstr, &s)
  647. if result == -1 {
  648. return s, Errno(get_last_error())
  649. }
  650. return s, ERROR_NONE
  651. }
  652. @private
  653. _lstat :: proc(path: string) -> (OS_Stat, Errno) {
  654. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  655. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  656. s: OS_Stat
  657. result := _unix_lstat(cstr, &s)
  658. if result == -1 {
  659. return s, Errno(get_last_error())
  660. }
  661. return s, ERROR_NONE
  662. }
  663. @private
  664. _fstat :: proc(fd: Handle) -> (OS_Stat, Errno) {
  665. s: OS_Stat
  666. result := _unix_fstat(fd, &s)
  667. if result == -1 {
  668. return s, Errno(get_last_error())
  669. }
  670. return s, ERROR_NONE
  671. }
  672. @private
  673. _fdopendir :: proc(fd: Handle) -> (Dir, Errno) {
  674. dirp := _unix_fdopendir(fd)
  675. if dirp == cast(Dir)nil {
  676. return nil, Errno(get_last_error())
  677. }
  678. return dirp, ERROR_NONE
  679. }
  680. @private
  681. _closedir :: proc(dirp: Dir) -> Errno {
  682. rc := _unix_closedir(dirp)
  683. if rc != 0 {
  684. return Errno(get_last_error())
  685. }
  686. return ERROR_NONE
  687. }
  688. @private
  689. _rewinddir :: proc(dirp: Dir) {
  690. _unix_rewinddir(dirp)
  691. }
  692. @private
  693. _readdir :: proc(dirp: Dir) -> (entry: Dirent, err: Errno, end_of_stream: bool) {
  694. result: ^Dirent
  695. rc := _unix_readdir_r(dirp, &entry, &result)
  696. if rc != 0 {
  697. err = Errno(get_last_error())
  698. return
  699. }
  700. err = ERROR_NONE
  701. if result == nil {
  702. end_of_stream = true
  703. return
  704. }
  705. end_of_stream = false
  706. return
  707. }
  708. @private
  709. _readlink :: proc(path: string) -> (string, Errno) {
  710. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == context.allocator)
  711. path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
  712. bufsz : uint = 256
  713. buf := make([]byte, bufsz)
  714. for {
  715. rc := _unix_readlink(path_cstr, &(buf[0]), bufsz)
  716. if rc == -1 {
  717. delete(buf)
  718. return "", Errno(get_last_error())
  719. } else if rc == int(bufsz) {
  720. // NOTE(laleksic, 2021-01-21): Any cleaner way to resize the slice?
  721. bufsz *= 2
  722. delete(buf)
  723. buf = make([]byte, bufsz)
  724. } else {
  725. return strings.string_from_ptr(&buf[0], rc), ERROR_NONE
  726. }
  727. }
  728. }
  729. absolute_path_from_handle :: proc(fd: Handle) -> (string, Errno) {
  730. buf: [DARWIN_MAXPATHLEN]byte
  731. _, err := fcntl(int(fd), F_GETPATH, int(uintptr(&buf[0])))
  732. if err != ERROR_NONE {
  733. return "", err
  734. }
  735. path := strings.clone_from_cstring(cstring(&buf[0]))
  736. return path, err
  737. }
  738. absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Errno) {
  739. rel := rel
  740. if rel == "" {
  741. rel = "."
  742. }
  743. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == context.allocator)
  744. rel_cstr := strings.clone_to_cstring(rel, context.temp_allocator)
  745. path_ptr := _unix_realpath(rel_cstr, nil)
  746. if path_ptr == nil {
  747. return "", Errno(get_last_error())
  748. }
  749. defer _unix_free(path_ptr)
  750. path_cstr := cast(cstring)path_ptr
  751. path = strings.clone(string(path_cstr))
  752. return path, ERROR_NONE
  753. }
  754. access :: proc(path: string, mask: int) -> bool {
  755. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  756. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  757. return _unix_access(cstr, c.int(mask)) == 0
  758. }
  759. flush :: proc(fd: Handle) -> Errno {
  760. return cast(Errno)_unix_fsync(fd)
  761. }
  762. lookup_env :: proc(key: string, allocator := context.allocator) -> (value: string, found: bool) {
  763. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == allocator)
  764. path_str := strings.clone_to_cstring(key, context.temp_allocator)
  765. cstr := _unix_getenv(path_str)
  766. if cstr == nil {
  767. return "", false
  768. }
  769. return strings.clone(string(cstr), allocator), true
  770. }
  771. get_env :: proc(key: string, allocator := context.allocator) -> (value: string) {
  772. value, _ = lookup_env(key, allocator)
  773. return
  774. }
  775. set_env :: proc(key, value: string) -> Errno {
  776. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  777. key_cstring := strings.clone_to_cstring(key, context.temp_allocator)
  778. value_cstring := strings.clone_to_cstring(value, context.temp_allocator)
  779. res := _unix_setenv(key_cstring, value_cstring, 1)
  780. if res < 0 {
  781. return Errno(get_last_error())
  782. }
  783. return ERROR_NONE
  784. }
  785. unset_env :: proc(key: string) -> Errno {
  786. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  787. s := strings.clone_to_cstring(key, context.temp_allocator)
  788. res := _unix_unsetenv(s)
  789. if res < 0 {
  790. return Errno(get_last_error())
  791. }
  792. return ERROR_NONE
  793. }
  794. get_current_directory :: proc() -> string {
  795. page_size := get_page_size() // NOTE(tetra): See note in os_linux.odin/get_current_directory.
  796. buf := make([dynamic]u8, page_size)
  797. for {
  798. cwd := _unix_getcwd(cstring(raw_data(buf)), c.size_t(len(buf)))
  799. if cwd != nil {
  800. return string(cwd)
  801. }
  802. if Errno(get_last_error()) != ERANGE {
  803. delete(buf)
  804. return ""
  805. }
  806. resize(&buf, len(buf)+page_size)
  807. }
  808. unreachable()
  809. }
  810. set_current_directory :: proc(path: string) -> (err: Errno) {
  811. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  812. cstr := strings.clone_to_cstring(path, context.temp_allocator)
  813. res := _unix_chdir(cstr)
  814. if res == -1 {
  815. return Errno(get_last_error())
  816. }
  817. return ERROR_NONE
  818. }
  819. make_directory :: proc(path: string, mode: u16 = 0o775) -> Errno {
  820. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  821. path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
  822. res := _unix_mkdir(path_cstr, mode)
  823. if res == -1 {
  824. return Errno(get_last_error())
  825. }
  826. return ERROR_NONE
  827. }
  828. exit :: proc "contextless" (code: int) -> ! {
  829. runtime._cleanup_runtime_contextless()
  830. _unix_exit(i32(code))
  831. }
  832. current_thread_id :: proc "contextless" () -> int {
  833. tid: u64
  834. // NOTE(Oskar): available from OSX 10.6 and iOS 3.2.
  835. // For older versions there is `syscall(SYS_thread_selfid)`, but not really
  836. // the same thing apparently.
  837. foreign pthread { pthread_threadid_np :: proc "c" (rawptr, ^u64) -> c.int --- }
  838. pthread_threadid_np(nil, &tid)
  839. return int(tid)
  840. }
  841. dlopen :: proc(filename: string, flags: int) -> rawptr {
  842. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  843. cstr := strings.clone_to_cstring(filename, context.temp_allocator)
  844. handle := _unix_dlopen(cstr, flags)
  845. return handle
  846. }
  847. dlsym :: proc(handle: rawptr, symbol: string) -> rawptr {
  848. assert(handle != nil)
  849. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
  850. cstr := strings.clone_to_cstring(symbol, context.temp_allocator)
  851. proc_handle := _unix_dlsym(handle, cstr)
  852. return proc_handle
  853. }
  854. dlclose :: proc(handle: rawptr) -> bool {
  855. assert(handle != nil)
  856. return _unix_dlclose(handle) == 0
  857. }
  858. dlerror :: proc() -> string {
  859. return string(_unix_dlerror())
  860. }
  861. get_page_size :: proc() -> int {
  862. // NOTE(tetra): The page size never changes, so why do anything complicated
  863. // if we don't have to.
  864. @static page_size := -1
  865. if page_size != -1 {
  866. return page_size
  867. }
  868. page_size = int(_unix_getpagesize())
  869. return page_size
  870. }
  871. @(private)
  872. _processor_core_count :: proc() -> int {
  873. count : int = 0
  874. count_size := size_of(count)
  875. if _sysctlbyname("hw.logicalcpu", &count, &count_size, nil, 0) == 0 {
  876. if count > 0 {
  877. return count
  878. }
  879. }
  880. return 1
  881. }
  882. _alloc_command_line_arguments :: proc() -> []string {
  883. res := make([]string, len(runtime.args__))
  884. for _, i in res {
  885. res[i] = string(runtime.args__[i])
  886. }
  887. return res
  888. }
  889. socket :: proc(domain: int, type: int, protocol: int) -> (Socket, Errno) {
  890. result := _unix_socket(domain, type, protocol)
  891. if result < 0 {
  892. return 0, Errno(get_last_error())
  893. }
  894. return Socket(result), ERROR_NONE
  895. }
  896. connect :: proc(sd: Socket, addr: ^SOCKADDR, len: socklen_t) -> (Errno) {
  897. result := _unix_connect(int(sd), addr, len)
  898. if result < 0 {
  899. return Errno(get_last_error())
  900. }
  901. return ERROR_NONE
  902. }
  903. bind :: proc(sd: Socket, addr: ^SOCKADDR, len: socklen_t) -> (Errno) {
  904. result := _unix_bind(int(sd), addr, len)
  905. if result < 0 {
  906. return Errno(get_last_error())
  907. }
  908. return ERROR_NONE
  909. }
  910. accept :: proc(sd: Socket, addr: ^SOCKADDR, len: rawptr) -> (Socket, Errno) {
  911. result := _unix_accept(int(sd), rawptr(addr), len)
  912. if result < 0 {
  913. return 0, Errno(get_last_error())
  914. }
  915. return Socket(result), ERROR_NONE
  916. }
  917. listen :: proc(sd: Socket, backlog: int) -> (Errno) {
  918. result := _unix_listen(int(sd), backlog)
  919. if result < 0 {
  920. return Errno(get_last_error())
  921. }
  922. return ERROR_NONE
  923. }
  924. setsockopt :: proc(sd: Socket, level: int, optname: int, optval: rawptr, optlen: socklen_t) -> (Errno) {
  925. result := _unix_setsockopt(int(sd), level, optname, optval, optlen)
  926. if result < 0 {
  927. return Errno(get_last_error())
  928. }
  929. return ERROR_NONE
  930. }
  931. getsockopt :: proc(sd: Socket, level: int, optname: int, optval: rawptr, optlen: socklen_t) -> Errno {
  932. result := _unix_getsockopt(int(sd), level, optname, optval, optlen)
  933. if result < 0 {
  934. return Errno(get_last_error())
  935. }
  936. return ERROR_NONE
  937. }
  938. recvfrom :: proc(sd: Socket, data: []byte, flags: int, addr: ^SOCKADDR, addr_size: ^socklen_t) -> (u32, Errno) {
  939. result := _unix_recvfrom(int(sd), raw_data(data), len(data), flags, addr, addr_size)
  940. if result < 0 {
  941. return 0, Errno(get_last_error())
  942. }
  943. return u32(result), ERROR_NONE
  944. }
  945. recv :: proc(sd: Socket, data: []byte, flags: int) -> (u32, Errno) {
  946. result := _unix_recv(int(sd), raw_data(data), len(data), flags)
  947. if result < 0 {
  948. return 0, Errno(get_last_error())
  949. }
  950. return u32(result), ERROR_NONE
  951. }
  952. sendto :: proc(sd: Socket, data: []u8, flags: int, addr: ^SOCKADDR, addrlen: socklen_t) -> (u32, Errno) {
  953. result := _unix_sendto(int(sd), raw_data(data), len(data), flags, addr, addrlen)
  954. if result < 0 {
  955. return 0, Errno(get_last_error())
  956. }
  957. return u32(result), ERROR_NONE
  958. }
  959. send :: proc(sd: Socket, data: []byte, flags: int) -> (u32, Errno) {
  960. result := _unix_send(int(sd), raw_data(data), len(data), 0)
  961. if result < 0 {
  962. return 0, Errno(get_last_error())
  963. }
  964. return u32(result), ERROR_NONE
  965. }
  966. shutdown :: proc(sd: Socket, how: int) -> (Errno) {
  967. result := _unix_shutdown(int(sd), how)
  968. if result < 0 {
  969. return Errno(get_last_error())
  970. }
  971. return ERROR_NONE
  972. }
  973. fcntl :: proc(fd: int, cmd: int, arg: int) -> (int, Errno) {
  974. result := _unix__fcntl(Handle(fd), c.int(cmd), uintptr(arg))
  975. if result < 0 {
  976. return 0, Errno(get_last_error())
  977. }
  978. return int(result), ERROR_NONE
  979. }