posix.nim 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # Until std_arg!!
  10. # done: ipc, pwd, stat, semaphore, sys/types, sys/utsname, pthread, unistd,
  11. # statvfs, mman, time, wait, signal, nl_types, sched, spawn, select, ucontext,
  12. # net/if, sys/socket, sys/uio, netinet/in, netinet/tcp, netdb
  13. ## This is a raw POSIX interface module. It does not not provide any
  14. ## convenience: cstrings are used instead of proper Nim strings and
  15. ## return codes indicate errors. If you want exceptions
  16. ## and a proper Nim-like interface, use the OS module or write a wrapper.
  17. ##
  18. ## For high-level wrappers specialized for Linux and BSDs see:
  19. ## `posix_utils <posix_utils.html>`_
  20. ##
  21. ## Coding conventions:
  22. ## ALL types are named the same as in the POSIX standard except that they start
  23. ## with 'T' or 'P' (if they are pointers) and without the '_t' suffix to be
  24. ## consistent with Nim conventions. If an identifier is a Nim keyword
  25. ## the \`identifier\` notation is used.
  26. ##
  27. ## This library relies on the header files of your C compiler. The
  28. ## resulting C code will just `#include <XYZ.h>` and *not* define the
  29. ## symbols declared here.
  30. # Dead code elimination ensures that we don't accidentally generate #includes
  31. # for files that might not exist on a specific platform! The user will get an
  32. # error only if they actually try to use the missing declaration
  33. when defined(nimHasStyleChecks):
  34. {.push styleChecks: off.}
  35. when defined(nimPreviewSlimSystem):
  36. import std/syncio
  37. # TODO these constants don't seem to be fetched from a header file for unknown
  38. # platforms - where do they come from and why are they here?
  39. when false:
  40. const
  41. C_IRUSR = 0o000400 ## Read by owner.
  42. C_IWUSR = 0o000200 ## Write by owner.
  43. C_IXUSR = 0o000100 ## Execute by owner.
  44. C_IRGRP = 0o000040 ## Read by group.
  45. C_IWGRP = 0o000020 ## Write by group.
  46. C_IXGRP = 0o000010 ## Execute by group.
  47. C_IROTH = 0o000004 ## Read by others.
  48. C_IWOTH = 0o000002 ## Write by others.
  49. C_IXOTH = 0o000001 ## Execute by others.
  50. C_ISUID = 0o004000 ## Set user ID.
  51. C_ISGID = 0o002000 ## Set group ID.
  52. C_ISVTX = 0o001000 ## On directories, restricted deletion flag.
  53. C_ISDIR = 0o040000 ## Directory.
  54. C_ISFIFO = 0o010000 ##FIFO.
  55. C_ISREG = 0o100000 ## Regular file.
  56. C_ISBLK = 0o060000 ## Block special.
  57. C_ISCHR = 0o020000 ## Character special.
  58. C_ISCTG = 0o110000 ## Reserved.
  59. C_ISLNK = 0o120000 ## Symbolic link.</p>
  60. C_ISSOCK = 0o140000 ## Socket.
  61. const
  62. MM_NULLLBL* = nil
  63. MM_NULLSEV* = 0
  64. MM_NULLMC* = 0
  65. MM_NULLTXT* = nil
  66. MM_NULLACT* = nil
  67. MM_NULLTAG* = nil
  68. STDERR_FILENO* = 2 ## File number of stderr;
  69. STDIN_FILENO* = 0 ## File number of stdin;
  70. STDOUT_FILENO* = 1 ## File number of stdout;
  71. DT_UNKNOWN* = 0 ## Unknown file type.
  72. DT_FIFO* = 1 ## Named pipe, or FIFO.
  73. DT_CHR* = 2 ## Character device.
  74. DT_DIR* = 4 ## Directory.
  75. DT_BLK* = 6 ## Block device.
  76. DT_REG* = 8 ## Regular file.
  77. DT_LNK* = 10 ## Symbolic link.
  78. DT_SOCK* = 12 ## UNIX domain socket.
  79. DT_WHT* = 14
  80. # Special types
  81. type Sighandler = proc (a: cint) {.noconv.}
  82. const StatHasNanoseconds* = defined(linux) or defined(freebsd) or
  83. defined(osx) or defined(openbsd) or defined(dragonfly) or defined(haiku) ## \
  84. ## Boolean flag that indicates if the system supports nanosecond time
  85. ## resolution in the fields of `Stat`. Note that the nanosecond based fields
  86. ## (`Stat.st_atim`, `Stat.st_mtim` and `Stat.st_ctim`) can be accessed
  87. ## without checking this flag, because this module defines fallback procs
  88. ## when they are not available.
  89. # Platform specific stuff
  90. when (defined(linux) and not defined(android)) and defined(amd64):
  91. include posix_linux_amd64
  92. elif defined(openbsd) and defined(amd64):
  93. include posix_openbsd_amd64
  94. elif (defined(macos) or defined(macosx) or defined(bsd)) and defined(cpu64):
  95. include posix_macos_amd64
  96. elif defined(nintendoswitch):
  97. include posix_nintendoswitch
  98. elif defined(haiku):
  99. include posix_haiku
  100. else:
  101. include posix_other
  102. # There used to be this name in posix.nim a long time ago, not sure why!
  103. when StatHasNanoseconds:
  104. proc st_atime*(s: Stat): Time {.inline.} =
  105. ## Second-granularity time of last access.
  106. result = s.st_atim.tv_sec
  107. proc st_mtime*(s: Stat): Time {.inline.} =
  108. ## Second-granularity time of last data modification.
  109. result = s.st_mtim.tv_sec
  110. proc st_ctime*(s: Stat): Time {.inline.} =
  111. ## Second-granularity time of last status change.
  112. result = s.st_ctim.tv_sec
  113. else:
  114. proc st_atim*(s: Stat): Timespec {.inline.} =
  115. ## Nanosecond-granularity time of last access.
  116. result.tv_sec = s.st_atime
  117. proc st_mtim*(s: Stat): Timespec {.inline.} =
  118. ## Nanosecond-granularity time of last data modification.
  119. result.tv_sec = s.st_mtime
  120. proc st_ctim*(s: Stat): Timespec {.inline.} =
  121. ## Nanosecond-granularity time of last data modification.
  122. result.tv_sec = s.st_ctime
  123. when hasAioH:
  124. proc aio_cancel*(a1: cint, a2: ptr Taiocb): cint {.importc, header: "<aio.h>".}
  125. proc aio_error*(a1: ptr Taiocb): cint {.importc, header: "<aio.h>".}
  126. proc aio_fsync*(a1: cint, a2: ptr Taiocb): cint {.importc, header: "<aio.h>".}
  127. proc aio_read*(a1: ptr Taiocb): cint {.importc, header: "<aio.h>".}
  128. proc aio_return*(a1: ptr Taiocb): int {.importc, header: "<aio.h>".}
  129. proc aio_suspend*(a1: ptr ptr Taiocb, a2: cint, a3: ptr Timespec): cint {.
  130. importc, header: "<aio.h>".}
  131. proc aio_write*(a1: ptr Taiocb): cint {.importc, header: "<aio.h>".}
  132. proc lio_listio*(a1: cint, a2: ptr ptr Taiocb, a3: cint,
  133. a4: ptr SigEvent): cint {.importc, header: "<aio.h>".}
  134. # arpa/inet.h
  135. proc htonl*(a1: uint32): uint32 {.importc, header: "<arpa/inet.h>".}
  136. proc htons*(a1: uint16): uint16 {.importc, header: "<arpa/inet.h>".}
  137. proc ntohl*(a1: uint32): uint32 {.importc, header: "<arpa/inet.h>".}
  138. proc ntohs*(a1: uint16): uint16 {.importc, header: "<arpa/inet.h>".}
  139. when not defined(zephyr):
  140. proc inet_addr*(a1: cstring): InAddrT {.importc, header: "<arpa/inet.h>".}
  141. proc inet_ntoa*(a1: InAddr): cstring {.importc, header: "<arpa/inet.h>".}
  142. proc inet_ntop*(a1: cint, a2: pointer | ptr InAddr | ptr In6Addr, a3: cstring, a4: int32): cstring {.
  143. importc:"(char *)$1", header: "<arpa/inet.h>".}
  144. proc inet_pton*(a1: cint, a2: cstring, a3: pointer | ptr InAddr | ptr In6Addr): cint {.
  145. importc, header: "<arpa/inet.h>".}
  146. var
  147. in6addr_any* {.importc, header: "<netinet/in.h>".}: In6Addr
  148. in6addr_loopback* {.importc, header: "<netinet/in.h>".}: In6Addr
  149. proc IN6ADDR_ANY_INIT* (): In6Addr {.importc, header: "<netinet/in.h>".}
  150. proc IN6ADDR_LOOPBACK_INIT* (): In6Addr {.importc, header: "<netinet/in.h>".}
  151. # dirent.h
  152. proc closedir*(a1: ptr DIR): cint {.importc, header: "<dirent.h>".}
  153. proc opendir*(a1: cstring): ptr DIR {.importc, header: "<dirent.h>", sideEffect.}
  154. proc readdir*(a1: ptr DIR): ptr Dirent {.importc, header: "<dirent.h>", sideEffect.}
  155. proc readdir_r*(a1: ptr DIR, a2: ptr Dirent, a3: ptr ptr Dirent): cint {.
  156. importc, header: "<dirent.h>", sideEffect.}
  157. proc rewinddir*(a1: ptr DIR) {.importc, header: "<dirent.h>".}
  158. proc seekdir*(a1: ptr DIR, a2: int) {.importc, header: "<dirent.h>".}
  159. proc telldir*(a1: ptr DIR): int {.importc, header: "<dirent.h>".}
  160. # dlfcn.h
  161. proc dlclose*(a1: pointer): cint {.importc, header: "<dlfcn.h>", sideEffect.}
  162. proc dlerror*(): cstring {.importc, header: "<dlfcn.h>", sideEffect.}
  163. proc dlopen*(a1: cstring, a2: cint): pointer {.importc, header: "<dlfcn.h>", sideEffect.}
  164. proc dlsym*(a1: pointer, a2: cstring): pointer {.importc, header: "<dlfcn.h>", sideEffect.}
  165. proc creat*(a1: cstring, a2: Mode): cint {.importc, header: "<fcntl.h>", sideEffect.}
  166. proc fcntl*(a1: cint | SocketHandle, a2: cint): cint {.varargs, importc, header: "<fcntl.h>", sideEffect.}
  167. proc openImpl(a1: cstring, a2: cint): cint {.varargs, importc: "open", header: "<fcntl.h>", sideEffect.}
  168. proc open*(a1: cstring, a2: cint, mode: Mode | cint = 0.Mode): cint {.inline.} =
  169. # prevents bug #17888
  170. openImpl(a1, a2, mode)
  171. proc posix_fadvise*(a1: cint, a2, a3: Off, a4: cint): cint {.
  172. importc, header: "<fcntl.h>".}
  173. proc ftruncate*(a1: cint, a2: Off): cint {.importc, header: "<unistd.h>".}
  174. when defined(osx): # 2001 POSIX evidently does not concern Apple
  175. type FStore {.importc: "fstore_t", header: "<fcntl.h>", bycopy.} = object
  176. fst_flags: uint32 ## IN: flags word
  177. fst_posmode: cint ## IN: indicates offset field
  178. fst_offset, ## IN: start of the region
  179. fst_length, ## IN: size of the region
  180. fst_bytesalloc: Off ## OUT: number of bytes allocated
  181. var F_PEOFPOSMODE {.importc, header: "<fcntl.h>".}: cint
  182. var F_ALLOCATEALL {.importc, header: "<fcntl.h>".}: uint32
  183. var F_PREALLOCATE {.importc, header: "<fcntl.h>".}: cint
  184. proc posix_fallocate*(a1: cint, a2, a3: Off): cint =
  185. var fst = FStore(fst_flags: F_ALLOCATEALL, fst_posmode: F_PEOFPOSMODE,
  186. fst_offset: a2, fst_length: a3)
  187. # Must also call ftruncate to match what POSIX does. Unlike posix_fallocate,
  188. # this can shrink files. Could guard w/getFileSize, but caller likely knows
  189. # present size & has no good reason to call this unless it is growing.
  190. if fcntl(a1, F_PREALLOCATE, fst.addr) != cint(-1): ftruncate(a1, a2 + a3)
  191. else: cint(-1)
  192. else:
  193. proc posix_fallocate*(a1: cint, a2, a3: Off): cint {.
  194. importc, header: "<fcntl.h>".}
  195. when not defined(haiku) and not defined(openbsd):
  196. proc fmtmsg*(a1: int, a2: cstring, a3: cint,
  197. a4, a5, a6: cstring): cint {.importc, header: "<fmtmsg.h>".}
  198. proc fnmatch*(a1, a2: cstring, a3: cint): cint {.importc, header: "<fnmatch.h>".}
  199. proc ftw*(a1: cstring,
  200. a2: proc (x1: cstring, x2: ptr Stat, x3: cint): cint {.noconv.},
  201. a3: cint): cint {.importc, header: "<ftw.h>".}
  202. when not (defined(linux) and defined(amd64)) and not defined(nintendoswitch):
  203. proc nftw*(a1: cstring,
  204. a2: proc (x1: cstring, x2: ptr Stat,
  205. x3: cint, x4: ptr FTW): cint {.noconv.},
  206. a3: cint,
  207. a4: cint): cint {.importc, header: "<ftw.h>".}
  208. proc glob*(a1: cstring, a2: cint,
  209. a3: proc (x1: cstring, x2: cint): cint {.noconv.},
  210. a4: ptr Glob): cint {.importc, header: "<glob.h>", sideEffect.}
  211. ## Filename globbing. Use `os.walkPattern() <os.html#glob_1>`_ and similar.
  212. proc globfree*(a1: ptr Glob) {.importc, header: "<glob.h>".}
  213. proc getgrgid*(a1: Gid): ptr Group {.importc, header: "<grp.h>".}
  214. proc getgrnam*(a1: cstring): ptr Group {.importc, header: "<grp.h>".}
  215. proc getgrgid_r*(a1: Gid, a2: ptr Group, a3: cstring, a4: int,
  216. a5: ptr ptr Group): cint {.importc, header: "<grp.h>".}
  217. proc getgrnam_r*(a1: cstring, a2: ptr Group, a3: cstring,
  218. a4: int, a5: ptr ptr Group): cint {.
  219. importc, header: "<grp.h>".}
  220. proc getgrent*(): ptr Group {.importc, header: "<grp.h>".}
  221. proc endgrent*() {.importc, header: "<grp.h>".}
  222. proc setgrent*() {.importc, header: "<grp.h>".}
  223. proc iconv_open*(a1, a2: cstring): Iconv {.importc, header: "<iconv.h>".}
  224. proc iconv*(a1: Iconv, a2: var cstring, a3: var int, a4: var cstring,
  225. a5: var int): int {.importc, header: "<iconv.h>".}
  226. proc iconv_close*(a1: Iconv): cint {.importc, header: "<iconv.h>".}
  227. proc nl_langinfo*(a1: Nl_item): cstring {.importc, header: "<langinfo.h>".}
  228. proc basename*(a1: cstring): cstring {.importc, header: "<libgen.h>".}
  229. proc dirname*(a1: cstring): cstring {.importc, header: "<libgen.h>".}
  230. proc localeconv*(): ptr Lconv {.importc, header: "<locale.h>".}
  231. proc setlocale*(a1: cint, a2: cstring): cstring {.
  232. importc, header: "<locale.h>", sideEffect.}
  233. proc strfmon*(a1: cstring, a2: int, a3: cstring): int {.varargs,
  234. importc, header: "<monetary.h>".}
  235. when not (defined(nintendoswitch) or defined(macos) or defined(macosx)):
  236. proc mq_notify*(mqdes: Mqd, event: ptr SigEvent): cint {.
  237. importc, header: "<mqueue.h>".}
  238. proc mq_open*(name: cstring, flags: cint): Mqd {.
  239. varargs, importc, header: "<mqueue.h>".}
  240. proc mq_close*(mqdes: Mqd): cint {.importc, header: "<mqueue.h>".}
  241. proc mq_receive*(
  242. mqdes: Mqd,
  243. buffer: cstring,
  244. length: csize_t,
  245. priority: var cuint
  246. ): int {.importc, header: "<mqueue.h>".}
  247. proc mq_timedreceive*(
  248. mqdes: Mqd,
  249. buffer: cstring,
  250. length: csize_t,
  251. priority: cuint,
  252. timeout: ptr Timespec
  253. ): int {.importc, header: "<mqueue.h>".}
  254. proc mq_send*(
  255. mqdes: Mqd,
  256. buffer: cstring,
  257. length: csize_t,
  258. priority: cuint
  259. ): cint {.importc, header: "<mqueue.h>".}
  260. proc mq_timedsend*(
  261. mqdes: Mqd,
  262. buffer: cstring,
  263. length: csize_t,
  264. priority: cuint,
  265. timeout: ptr Timespec
  266. ): cint {.importc, header: "<mqueue.h>".}
  267. proc mq_getattr*(mqdes: Mqd, attribute: ptr MqAttr): cint {.
  268. importc, header: "<mqueue.h>".}
  269. proc mq_setattr*(mqdes: Mqd, newAttribute, oldAttribute: ptr MqAttr): cint {.
  270. importc, header: "<mqueue.h>".}
  271. proc mq_unlink*(mqdes: cstring): cint {.importc, header: "<mqueue.h>".}
  272. proc getpwnam*(a1: cstring): ptr Passwd {.importc, header: "<pwd.h>".}
  273. proc getpwuid*(a1: Uid): ptr Passwd {.importc, header: "<pwd.h>".}
  274. proc getpwnam_r*(a1: cstring, a2: ptr Passwd, a3: cstring, a4: int,
  275. a5: ptr ptr Passwd): cint {.importc, header: "<pwd.h>".}
  276. proc getpwuid_r*(a1: Uid, a2: ptr Passwd, a3: cstring,
  277. a4: int, a5: ptr ptr Passwd): cint {.importc, header: "<pwd.h>".}
  278. proc endpwent*() {.importc, header: "<pwd.h>".}
  279. proc getpwent*(): ptr Passwd {.importc, header: "<pwd.h>".}
  280. proc setpwent*() {.importc, header: "<pwd.h>".}
  281. proc uname*(a1: var Utsname): cint {.importc, header: "<sys/utsname.h>".}
  282. proc strerror*(errnum: cint): cstring {.importc, header: "<string.h>".}
  283. proc pthread_atfork*(a1, a2, a3: proc () {.noconv.}): cint {.
  284. importc, header: "<pthread.h>".}
  285. proc pthread_attr_destroy*(a1: ptr Pthread_attr): cint {.
  286. importc, header: "<pthread.h>".}
  287. proc pthread_attr_getdetachstate*(a1: ptr Pthread_attr, a2: cint): cint {.
  288. importc, header: "<pthread.h>".}
  289. proc pthread_attr_getguardsize*(a1: ptr Pthread_attr, a2: var cint): cint {.
  290. importc, header: "<pthread.h>".}
  291. proc pthread_attr_getinheritsched*(a1: ptr Pthread_attr,
  292. a2: var cint): cint {.importc, header: "<pthread.h>".}
  293. proc pthread_attr_getschedparam*(a1: ptr Pthread_attr,
  294. a2: ptr Sched_param): cint {.importc, header: "<pthread.h>".}
  295. proc pthread_attr_getschedpolicy*(a1: ptr Pthread_attr,
  296. a2: var cint): cint {.importc, header: "<pthread.h>".}
  297. proc pthread_attr_getscope*(a1: ptr Pthread_attr,
  298. a2: var cint): cint {.importc, header: "<pthread.h>".}
  299. proc pthread_attr_getstack*(a1: ptr Pthread_attr,
  300. a2: var pointer, a3: var int): cint {.importc, header: "<pthread.h>".}
  301. proc pthread_attr_getstackaddr*(a1: ptr Pthread_attr,
  302. a2: var pointer): cint {.importc, header: "<pthread.h>".}
  303. proc pthread_attr_getstacksize*(a1: ptr Pthread_attr,
  304. a2: var int): cint {.importc, header: "<pthread.h>".}
  305. proc pthread_attr_init*(a1: ptr Pthread_attr): cint {.
  306. importc, header: "<pthread.h>".}
  307. proc pthread_attr_setdetachstate*(a1: ptr Pthread_attr, a2: cint): cint {.
  308. importc, header: "<pthread.h>".}
  309. proc pthread_attr_setguardsize*(a1: ptr Pthread_attr, a2: int): cint {.
  310. importc, header: "<pthread.h>".}
  311. proc pthread_attr_setinheritsched*(a1: ptr Pthread_attr, a2: cint): cint {.
  312. importc, header: "<pthread.h>".}
  313. proc pthread_attr_setschedparam*(a1: ptr Pthread_attr,
  314. a2: ptr Sched_param): cint {.importc, header: "<pthread.h>".}
  315. proc pthread_attr_setschedpolicy*(a1: ptr Pthread_attr, a2: cint): cint {.
  316. importc, header: "<pthread.h>".}
  317. proc pthread_attr_setscope*(a1: ptr Pthread_attr, a2: cint): cint {.importc,
  318. header: "<pthread.h>".}
  319. proc pthread_attr_setstack*(a1: ptr Pthread_attr, a2: pointer, a3: int): cint {.
  320. importc, header: "<pthread.h>".}
  321. proc pthread_attr_setstackaddr*(a1: ptr Pthread_attr, a2: pointer): cint {.
  322. importc, header: "<pthread.h>".}
  323. proc pthread_attr_setstacksize*(a1: ptr Pthread_attr, a2: int): cint {.
  324. importc, header: "<pthread.h>".}
  325. proc pthread_barrier_destroy*(a1: ptr Pthread_barrier): cint {.
  326. importc, header: "<pthread.h>".}
  327. proc pthread_barrier_init*(a1: ptr Pthread_barrier,
  328. a2: ptr Pthread_barrierattr, a3: cint): cint {.
  329. importc, header: "<pthread.h>".}
  330. proc pthread_barrier_wait*(a1: ptr Pthread_barrier): cint {.
  331. importc, header: "<pthread.h>".}
  332. proc pthread_barrierattr_destroy*(a1: ptr Pthread_barrierattr): cint {.
  333. importc, header: "<pthread.h>".}
  334. proc pthread_barrierattr_getpshared*(
  335. a1: ptr Pthread_barrierattr, a2: var cint): cint {.
  336. importc, header: "<pthread.h>".}
  337. proc pthread_barrierattr_init*(a1: ptr Pthread_barrierattr): cint {.
  338. importc, header: "<pthread.h>".}
  339. proc pthread_barrierattr_setpshared*(a1: ptr Pthread_barrierattr,
  340. a2: cint): cint {.importc, header: "<pthread.h>".}
  341. proc pthread_cancel*(a1: Pthread): cint {.importc, header: "<pthread.h>".}
  342. proc pthread_cleanup_push*(a1: proc (x: pointer) {.noconv.}, a2: pointer) {.
  343. importc, header: "<pthread.h>".}
  344. proc pthread_cleanup_pop*(a1: cint) {.importc, header: "<pthread.h>".}
  345. proc pthread_cond_broadcast*(a1: ptr Pthread_cond): cint {.
  346. importc, header: "<pthread.h>".}
  347. proc pthread_cond_destroy*(a1: ptr Pthread_cond): cint {.importc, header: "<pthread.h>".}
  348. proc pthread_cond_init*(a1: ptr Pthread_cond,
  349. a2: ptr Pthread_condattr): cint {.importc, header: "<pthread.h>".}
  350. proc pthread_cond_signal*(a1: ptr Pthread_cond): cint {.importc, header: "<pthread.h>".}
  351. proc pthread_cond_timedwait*(a1: ptr Pthread_cond,
  352. a2: ptr Pthread_mutex, a3: ptr Timespec): cint {.importc, header: "<pthread.h>".}
  353. proc pthread_cond_wait*(a1: ptr Pthread_cond,
  354. a2: ptr Pthread_mutex): cint {.importc, header: "<pthread.h>".}
  355. proc pthread_condattr_destroy*(a1: ptr Pthread_condattr): cint {.importc, header: "<pthread.h>".}
  356. proc pthread_condattr_getclock*(a1: ptr Pthread_condattr,
  357. a2: var ClockId): cint {.importc, header: "<pthread.h>".}
  358. proc pthread_condattr_getpshared*(a1: ptr Pthread_condattr,
  359. a2: var cint): cint {.importc, header: "<pthread.h>".}
  360. proc pthread_condattr_init*(a1: ptr Pthread_condattr): cint {.importc, header: "<pthread.h>".}
  361. proc pthread_condattr_setclock*(a1: ptr Pthread_condattr,a2: ClockId): cint {.importc, header: "<pthread.h>".}
  362. proc pthread_condattr_setpshared*(a1: ptr Pthread_condattr, a2: cint): cint {.importc, header: "<pthread.h>".}
  363. proc pthread_create*(a1: ptr Pthread, a2: ptr Pthread_attr,
  364. a3: proc (x: pointer): pointer {.noconv.}, a4: pointer): cint {.importc, header: "<pthread.h>".}
  365. proc pthread_detach*(a1: Pthread): cint {.importc, header: "<pthread.h>".}
  366. proc pthread_equal*(a1, a2: Pthread): cint {.importc, header: "<pthread.h>".}
  367. proc pthread_exit*(a1: pointer) {.importc, header: "<pthread.h>".}
  368. proc pthread_getconcurrency*(): cint {.importc, header: "<pthread.h>".}
  369. proc pthread_getcpuclockid*(a1: Pthread, a2: var ClockId): cint {.importc, header: "<pthread.h>".}
  370. proc pthread_getschedparam*(a1: Pthread, a2: var cint,
  371. a3: ptr Sched_param): cint {.importc, header: "<pthread.h>".}
  372. proc pthread_getspecific*(a1: Pthread_key): pointer {.importc, header: "<pthread.h>".}
  373. proc pthread_join*(a1: Pthread, a2: ptr pointer): cint {.importc, header: "<pthread.h>".}
  374. proc pthread_key_create*(a1: ptr Pthread_key, a2: proc (x: pointer) {.noconv.}): cint {.importc, header: "<pthread.h>".}
  375. proc pthread_key_delete*(a1: Pthread_key): cint {.importc, header: "<pthread.h>".}
  376. proc pthread_mutex_destroy*(a1: ptr Pthread_mutex): cint {.importc, header: "<pthread.h>".}
  377. proc pthread_mutex_getprioceiling*(a1: ptr Pthread_mutex,
  378. a2: var cint): cint {.importc, header: "<pthread.h>".}
  379. proc pthread_mutex_init*(a1: ptr Pthread_mutex,
  380. a2: ptr Pthread_mutexattr): cint {.importc, header: "<pthread.h>".}
  381. proc pthread_mutex_lock*(a1: ptr Pthread_mutex): cint {.importc, header: "<pthread.h>".}
  382. proc pthread_mutex_setprioceiling*(a1: ptr Pthread_mutex,a2: cint,
  383. a3: var cint): cint {.importc, header: "<pthread.h>".}
  384. proc pthread_mutex_timedlock*(a1: ptr Pthread_mutex,
  385. a2: ptr Timespec): cint {.importc, header: "<pthread.h>".}
  386. proc pthread_mutex_trylock*(a1: ptr Pthread_mutex): cint {.importc, header: "<pthread.h>".}
  387. proc pthread_mutex_unlock*(a1: ptr Pthread_mutex): cint {.importc, header: "<pthread.h>".}
  388. proc pthread_mutexattr_destroy*(a1: ptr Pthread_mutexattr): cint {.importc, header: "<pthread.h>".}
  389. proc pthread_mutexattr_getprioceiling*(
  390. a1: ptr Pthread_mutexattr, a2: var cint): cint {.importc, header: "<pthread.h>".}
  391. proc pthread_mutexattr_getprotocol*(a1: ptr Pthread_mutexattr,
  392. a2: var cint): cint {.importc, header: "<pthread.h>".}
  393. proc pthread_mutexattr_getpshared*(a1: ptr Pthread_mutexattr,
  394. a2: var cint): cint {.importc, header: "<pthread.h>".}
  395. proc pthread_mutexattr_gettype*(a1: ptr Pthread_mutexattr,
  396. a2: var cint): cint {.importc, header: "<pthread.h>".}
  397. proc pthread_mutexattr_init*(a1: ptr Pthread_mutexattr): cint {.importc, header: "<pthread.h>".}
  398. proc pthread_mutexattr_setprioceiling*(a1: ptr Pthread_mutexattr, a2: cint): cint {.importc, header: "<pthread.h>".}
  399. proc pthread_mutexattr_setprotocol*(a1: ptr Pthread_mutexattr, a2: cint): cint {.importc, header: "<pthread.h>".}
  400. proc pthread_mutexattr_setpshared*(a1: ptr Pthread_mutexattr, a2: cint): cint {.importc, header: "<pthread.h>".}
  401. proc pthread_mutexattr_settype*(a1: ptr Pthread_mutexattr, a2: cint): cint {.importc, header: "<pthread.h>".}
  402. proc pthread_once*(a1: ptr Pthread_once, a2: proc () {.noconv.}): cint {.importc, header: "<pthread.h>".}
  403. proc pthread_rwlock_destroy*(a1: ptr Pthread_rwlock): cint {.importc, header: "<pthread.h>".}
  404. proc pthread_rwlock_init*(a1: ptr Pthread_rwlock,
  405. a2: ptr Pthread_rwlockattr): cint {.importc, header: "<pthread.h>".}
  406. proc pthread_rwlock_rdlock*(a1: ptr Pthread_rwlock): cint {.importc, header: "<pthread.h>".}
  407. proc pthread_rwlock_timedrdlock*(a1: ptr Pthread_rwlock,
  408. a2: ptr Timespec): cint {.importc, header: "<pthread.h>".}
  409. proc pthread_rwlock_timedwrlock*(a1: ptr Pthread_rwlock,
  410. a2: ptr Timespec): cint {.importc, header: "<pthread.h>".}
  411. proc pthread_rwlock_tryrdlock*(a1: ptr Pthread_rwlock): cint {.importc, header: "<pthread.h>".}
  412. proc pthread_rwlock_trywrlock*(a1: ptr Pthread_rwlock): cint {.importc, header: "<pthread.h>".}
  413. proc pthread_rwlock_unlock*(a1: ptr Pthread_rwlock): cint {.importc, header: "<pthread.h>".}
  414. proc pthread_rwlock_wrlock*(a1: ptr Pthread_rwlock): cint {.importc, header: "<pthread.h>".}
  415. proc pthread_rwlockattr_destroy*(a1: ptr Pthread_rwlockattr): cint {.importc, header: "<pthread.h>".}
  416. proc pthread_rwlockattr_getpshared*(
  417. a1: ptr Pthread_rwlockattr, a2: var cint): cint {.importc, header: "<pthread.h>".}
  418. proc pthread_rwlockattr_init*(a1: ptr Pthread_rwlockattr): cint {.importc, header: "<pthread.h>".}
  419. proc pthread_rwlockattr_setpshared*(a1: ptr Pthread_rwlockattr, a2: cint): cint {.importc, header: "<pthread.h>".}
  420. proc pthread_self*(): Pthread {.importc, header: "<pthread.h>".}
  421. proc pthread_setcancelstate*(a1: cint, a2: var cint): cint {.importc, header: "<pthread.h>".}
  422. proc pthread_setcanceltype*(a1: cint, a2: var cint): cint {.importc, header: "<pthread.h>".}
  423. proc pthread_setconcurrency*(a1: cint): cint {.importc, header: "<pthread.h>".}
  424. proc pthread_setschedparam*(a1: Pthread, a2: cint,
  425. a3: ptr Sched_param): cint {.importc, header: "<pthread.h>".}
  426. proc pthread_setschedprio*(a1: Pthread, a2: cint): cint {.
  427. importc, header: "<pthread.h>".}
  428. proc pthread_setspecific*(a1: Pthread_key, a2: pointer): cint {.
  429. importc, header: "<pthread.h>".}
  430. proc pthread_spin_destroy*(a1: ptr Pthread_spinlock): cint {.
  431. importc, header: "<pthread.h>".}
  432. proc pthread_spin_init*(a1: ptr Pthread_spinlock, a2: cint): cint {.
  433. importc, header: "<pthread.h>".}
  434. proc pthread_spin_lock*(a1: ptr Pthread_spinlock): cint {.
  435. importc, header: "<pthread.h>".}
  436. proc pthread_spin_trylock*(a1: ptr Pthread_spinlock): cint{.
  437. importc, header: "<pthread.h>".}
  438. proc pthread_spin_unlock*(a1: ptr Pthread_spinlock): cint {.
  439. importc, header: "<pthread.h>".}
  440. proc pthread_testcancel*() {.importc, header: "<pthread.h>".}
  441. proc exitnow*(code: int) {.importc: "_exit", header: "<unistd.h>".}
  442. proc access*(a1: cstring, a2: cint): cint {.importc, header: "<unistd.h>".}
  443. proc alarm*(a1: cint): cint {.importc, header: "<unistd.h>".}
  444. proc chdir*(a1: cstring): cint {.importc, header: "<unistd.h>".}
  445. proc chown*(a1: cstring, a2: Uid, a3: Gid): cint {.importc, header: "<unistd.h>".}
  446. proc close*(a1: cint | SocketHandle): cint {.importc, header: "<unistd.h>".}
  447. proc confstr*(a1: cint, a2: cstring, a3: int): int {.importc, header: "<unistd.h>".}
  448. proc crypt*(a1, a2: cstring): cstring {.importc, header: "<unistd.h>".}
  449. proc ctermid*(a1: cstring): cstring {.importc, header: "<unistd.h>".}
  450. proc dup*(a1: cint): cint {.importc, header: "<unistd.h>".}
  451. proc dup2*(a1, a2: cint): cint {.importc, header: "<unistd.h>".}
  452. proc encrypt*(a1: array[0..63, char], a2: cint) {.importc, header: "<unistd.h>".}
  453. proc execl*(a1, a2: cstring): cint {.varargs, importc, header: "<unistd.h>", sideEffect.}
  454. proc execle*(a1, a2: cstring): cint {.varargs, importc, header: "<unistd.h>", sideEffect.}
  455. proc execlp*(a1, a2: cstring): cint {.varargs, importc, header: "<unistd.h>", sideEffect.}
  456. proc execv*(a1: cstring, a2: cstringArray): cint {.importc, header: "<unistd.h>", sideEffect.}
  457. proc execve*(a1: cstring, a2, a3: cstringArray): cint {.
  458. importc, header: "<unistd.h>", sideEffect.}
  459. proc execvp*(a1: cstring, a2: cstringArray): cint {.importc, header: "<unistd.h>", sideEffect.}
  460. proc execvpe*(a1: cstring, a2: cstringArray, a3: cstringArray): cint {.importc, header: "<unistd.h>", sideEffect.}
  461. proc fchown*(a1: cint, a2: Uid, a3: Gid): cint {.importc, header: "<unistd.h>", sideEffect.}
  462. proc fchdir*(a1: cint): cint {.importc, header: "<unistd.h>", sideEffect.}
  463. proc fdatasync*(a1: cint): cint {.importc, header: "<unistd.h>".}
  464. proc fork*(): Pid {.importc, header: "<unistd.h>", sideEffect.}
  465. proc fpathconf*(a1, a2: cint): int {.importc, header: "<unistd.h>".}
  466. proc fsync*(a1: cint): cint {.importc, header: "<unistd.h>".}
  467. ## synchronize a file's buffer cache to the storage device
  468. proc getcwd*(a1: cstring, a2: int): cstring {.importc, header: "<unistd.h>", sideEffect.}
  469. proc getuid*(): Uid {.importc, header: "<unistd.h>", sideEffect.}
  470. ## returns the real user ID of the calling process
  471. proc geteuid*(): Uid {.importc, header: "<unistd.h>", sideEffect.}
  472. ## returns the effective user ID of the calling process
  473. proc getgid*(): Gid {.importc, header: "<unistd.h>", sideEffect.}
  474. ## returns the real group ID of the calling process
  475. proc getegid*(): Gid {.importc, header: "<unistd.h>", sideEffect.}
  476. ## returns the effective group ID of the calling process
  477. proc getgroups*(a1: cint, a2: ptr array[0..255, Gid]): cint {.
  478. importc, header: "<unistd.h>".}
  479. proc gethostid*(): int {.importc, header: "<unistd.h>", sideEffect.}
  480. proc gethostname*(a1: cstring, a2: int): cint {.importc, header: "<unistd.h>", sideEffect.}
  481. proc getlogin*(): cstring {.importc, header: "<unistd.h>", sideEffect.}
  482. proc getlogin_r*(a1: cstring, a2: int): cint {.importc, header: "<unistd.h>", sideEffect.}
  483. proc getopt*(a1: cint, a2: cstringArray, a3: cstring): cint {.
  484. importc, header: "<unistd.h>".}
  485. proc getpgid*(a1: Pid): Pid {.importc, header: "<unistd.h>".}
  486. proc getpgrp*(): Pid {.importc, header: "<unistd.h>".}
  487. proc getpid*(): Pid {.importc, header: "<unistd.h>", sideEffect.}
  488. ## returns the process ID (PID) of the calling process
  489. proc getppid*(): Pid {.importc, header: "<unistd.h>", sideEffect.}
  490. ## returns the process ID of the parent of the calling process
  491. proc getsid*(a1: Pid): Pid {.importc, header: "<unistd.h>", sideEffect.}
  492. ## returns the session ID of the calling process
  493. proc getwd*(a1: cstring): cstring {.importc, header: "<unistd.h>".}
  494. proc isatty*(a1: cint): cint {.importc, header: "<unistd.h>".}
  495. proc lchown*(a1: cstring, a2: Uid, a3: Gid): cint {.importc, header: "<unistd.h>".}
  496. proc link*(a1, a2: cstring): cint {.importc, header: "<unistd.h>".}
  497. proc lockf*(a1, a2: cint, a3: Off): cint {.importc, header: "<unistd.h>".}
  498. proc lseek*(a1: cint, a2: Off, a3: cint): Off {.importc, header: "<unistd.h>".}
  499. proc nice*(a1: cint): cint {.importc, header: "<unistd.h>".}
  500. proc pathconf*(a1: cstring, a2: cint): int {.importc, header: "<unistd.h>".}
  501. proc pause*(): cint {.importc, header: "<unistd.h>".}
  502. proc pclose*(a: File): cint {.importc, header: "<stdio.h>".}
  503. proc pipe*(a: array[0..1, cint]): cint {.importc, header: "<unistd.h>".}
  504. proc popen*(a1, a2: cstring): File {.importc, header: "<stdio.h>".}
  505. proc pread*(a1: cint, a2: pointer, a3: int, a4: Off): int {.
  506. importc, header: "<unistd.h>".}
  507. proc pwrite*(a1: cint, a2: pointer, a3: int, a4: Off): int {.
  508. importc, header: "<unistd.h>".}
  509. proc read*(a1: cint, a2: pointer, a3: int): int {.importc, header: "<unistd.h>".}
  510. when not defined(nintendoswitch):
  511. proc readlink*(a1, a2: cstring, a3: int): int {.importc, header: "<unistd.h>".}
  512. proc ioctl*(f: FileHandle, device: uint): int {.importc: "ioctl",
  513. header: "<sys/ioctl.h>", varargs, tags: [WriteIOEffect].}
  514. ## A system call for device-specific input/output operations and other
  515. ## operations which cannot be expressed by regular system calls
  516. proc rmdir*(a1: cstring): cint {.importc, header: "<unistd.h>".}
  517. proc setegid*(a1: Gid): cint {.importc, header: "<unistd.h>".}
  518. proc seteuid*(a1: Uid): cint {.importc, header: "<unistd.h>".}
  519. proc setgid*(a1: Gid): cint {.importc, header: "<unistd.h>".}
  520. proc setpgid*(a1, a2: Pid): cint {.importc, header: "<unistd.h>".}
  521. proc setpgrp*(): Pid {.importc, header: "<unistd.h>".}
  522. proc setregid*(a1, a2: Gid): cint {.importc, header: "<unistd.h>".}
  523. proc setreuid*(a1, a2: Uid): cint {.importc, header: "<unistd.h>".}
  524. proc setsid*(): Pid {.importc, header: "<unistd.h>".}
  525. proc setuid*(a1: Uid): cint {.importc, header: "<unistd.h>".}
  526. proc sleep*(a1: cint): cint {.importc, header: "<unistd.h>".}
  527. proc swab*(a1, a2: pointer, a3: int) {.importc, header: "<unistd.h>".}
  528. when not defined(nintendoswitch):
  529. proc symlink*(a1, a2: cstring): cint {.importc, header: "<unistd.h>".}
  530. else:
  531. proc symlink*(a1, a2: cstring): cint = -1
  532. proc sync*() {.importc, header: "<unistd.h>".}
  533. proc sysconf*(a1: cint): int {.importc, header: "<unistd.h>".}
  534. proc tcgetpgrp*(a1: cint): Pid {.importc, header: "<unistd.h>".}
  535. proc tcsetpgrp*(a1: cint, a2: Pid): cint {.importc, header: "<unistd.h>".}
  536. proc truncate*(a1: cstring, a2: Off): cint {.importc, header: "<unistd.h>".}
  537. proc ttyname*(a1: cint): cstring {.importc, header: "<unistd.h>".}
  538. proc ttyname_r*(a1: cint, a2: cstring, a3: int): cint {.
  539. importc, header: "<unistd.h>".}
  540. proc ualarm*(a1, a2: Useconds): Useconds {.importc, header: "<unistd.h>".}
  541. proc unlink*(a1: cstring): cint {.importc, header: "<unistd.h>".}
  542. proc usleep*(a1: Useconds): cint {.importc, header: "<unistd.h>".}
  543. proc vfork*(): Pid {.importc, header: "<unistd.h>".}
  544. proc write*(a1: cint, a2: pointer, a3: int): int {.importc, header: "<unistd.h>".}
  545. proc sem_close*(a1: ptr Sem): cint {.importc, header: "<semaphore.h>".}
  546. proc sem_destroy*(a1: ptr Sem): cint {.importc, header: "<semaphore.h>".}
  547. proc sem_getvalue*(a1: ptr Sem, a2: var cint): cint {.
  548. importc, header: "<semaphore.h>".}
  549. proc sem_init*(a1: ptr Sem, a2: cint, a3: cint): cint {.
  550. importc, header: "<semaphore.h>".}
  551. proc sem_open*(a1: cstring, a2: cint): ptr Sem {.
  552. varargs, importc, header: "<semaphore.h>".}
  553. proc sem_post*(a1: ptr Sem): cint {.importc, header: "<semaphore.h>".}
  554. proc sem_timedwait*(a1: ptr Sem, a2: ptr Timespec): cint {.
  555. importc, header: "<semaphore.h>".}
  556. proc sem_trywait*(a1: ptr Sem): cint {.importc, header: "<semaphore.h>".}
  557. proc sem_unlink*(a1: cstring): cint {.importc, header: "<semaphore.h>".}
  558. proc sem_wait*(a1: ptr Sem): cint {.importc, header: "<semaphore.h>".}
  559. proc ftok*(a1: cstring, a2: cint): Key {.importc, header: "<sys/ipc.h>".}
  560. proc statvfs*(a1: cstring, a2: var Statvfs): cint {.
  561. importc, header: "<sys/statvfs.h>".}
  562. proc fstatvfs*(a1: cint, a2: var Statvfs): cint {.
  563. importc, header: "<sys/statvfs.h>".}
  564. proc chmod*(a1: cstring, a2: Mode): cint {.importc, header: "<sys/stat.h>", sideEffect.}
  565. when defined(osx) or defined(freebsd):
  566. proc lchmod*(a1: cstring, a2: Mode): cint {.importc, header: "<sys/stat.h>", sideEffect.}
  567. proc fchmod*(a1: cint, a2: Mode): cint {.importc, header: "<sys/stat.h>", sideEffect.}
  568. proc fstat*(a1: cint, a2: var Stat): cint {.importc, header: "<sys/stat.h>", sideEffect.}
  569. proc lstat*(a1: cstring, a2: var Stat): cint {.importc, header: "<sys/stat.h>", sideEffect.}
  570. proc mkdir*(a1: cstring, a2: Mode): cint {.importc, header: "<sys/stat.h>", sideEffect.}
  571. ## Use `os.createDir() <os.html#createDir,string>`_ and similar.
  572. proc mkfifo*(a1: cstring, a2: Mode): cint {.importc, header: "<sys/stat.h>".}
  573. proc mknod*(a1: cstring, a2: Mode, a3: Dev): cint {.
  574. importc, header: "<sys/stat.h>".}
  575. proc stat*(a1: cstring, a2: var Stat): cint {.importc, header: "<sys/stat.h>".}
  576. proc umask*(a1: Mode): Mode {.importc, header: "<sys/stat.h>".}
  577. proc S_ISBLK*(m: Mode): bool {.importc, header: "<sys/stat.h>".}
  578. ## Test for a block special file.
  579. proc S_ISCHR*(m: Mode): bool {.importc, header: "<sys/stat.h>".}
  580. ## Test for a character special file.
  581. proc S_ISDIR*(m: Mode): bool {.importc, header: "<sys/stat.h>".}
  582. ## Test for a directory.
  583. proc S_ISFIFO*(m: Mode): bool {.importc, header: "<sys/stat.h>".}
  584. ## Test for a pipe or FIFO special file.
  585. proc S_ISREG*(m: Mode): bool {.importc, header: "<sys/stat.h>".}
  586. ## Test for a regular file.
  587. proc S_ISLNK*(m: Mode): bool {.importc, header: "<sys/stat.h>".}
  588. ## Test for a symbolic link.
  589. proc S_ISSOCK*(m: Mode): bool {.importc, header: "<sys/stat.h>".}
  590. ## Test for a socket.
  591. proc S_TYPEISMQ*(buf: var Stat): bool {.importc, header: "<sys/stat.h>".}
  592. ## Test for a message queue.
  593. proc S_TYPEISSEM*(buf: var Stat): bool {.importc, header: "<sys/stat.h>".}
  594. ## Test for a semaphore.
  595. proc S_TYPEISSHM*(buf: var Stat): bool {.importc, header: "<sys/stat.h>".}
  596. ## Test for a shared memory object.
  597. proc S_TYPEISTMO*(buf: var Stat): bool {.importc, header: "<sys/stat.h>".}
  598. ## Test macro for a typed memory object.
  599. proc mlock*(a1: pointer, a2: int): cint {.importc, header: "<sys/mman.h>".}
  600. proc mlockall*(a1: cint): cint {.importc, header: "<sys/mman.h>".}
  601. proc mmap*(a1: pointer, a2: int, a3, a4, a5: cint, a6: Off): pointer {.
  602. importc, header: "<sys/mman.h>".}
  603. proc mprotect*(a1: pointer, a2: int, a3: cint): cint {.
  604. importc, header: "<sys/mman.h>".}
  605. proc msync*(a1: pointer, a2: int, a3: cint): cint {.importc, header: "<sys/mman.h>".}
  606. proc munlock*(a1: pointer, a2: int): cint {.importc, header: "<sys/mman.h>".}
  607. proc munlockall*(): cint {.importc, header: "<sys/mman.h>".}
  608. proc munmap*(a1: pointer, a2: int): cint {.importc, header: "<sys/mman.h>".}
  609. proc posix_madvise*(a1: pointer, a2: int, a3: cint): cint {.
  610. importc, header: "<sys/mman.h>".}
  611. proc posix_mem_offset*(a1: pointer, a2: int, a3: var Off,
  612. a4: var int, a5: var cint): cint {.importc, header: "<sys/mman.h>".}
  613. when not (defined(linux) and defined(amd64)) and not defined(nintendoswitch) and
  614. not defined(haiku):
  615. proc posix_typed_mem_get_info*(a1: cint,
  616. a2: var Posix_typed_mem_info): cint {.importc, header: "<sys/mman.h>".}
  617. proc posix_typed_mem_open*(a1: cstring, a2, a3: cint): cint {.
  618. importc, header: "<sys/mman.h>".}
  619. proc shm_open*(a1: cstring, a2: cint, a3: Mode): cint {.
  620. importc, header: "<sys/mman.h>".}
  621. proc shm_unlink*(a1: cstring): cint {.importc, header: "<sys/mman.h>".}
  622. proc asctime*(a1: var Tm): cstring{.importc, header: "<time.h>".}
  623. proc asctime_r*(a1: var Tm, a2: cstring): cstring {.importc, header: "<time.h>".}
  624. proc clock*(): Clock {.importc, header: "<time.h>", sideEffect.}
  625. proc clock_getcpuclockid*(a1: Pid, a2: var ClockId): cint {.
  626. importc, header: "<time.h>", sideEffect.}
  627. proc clock_getres*(a1: ClockId, a2: var Timespec): cint {.
  628. importc, header: "<time.h>", sideEffect.}
  629. proc clock_gettime*(a1: ClockId, a2: var Timespec): cint {.
  630. importc, header: "<time.h>", sideEffect.}
  631. proc clock_nanosleep*(a1: ClockId, a2: cint, a3: var Timespec,
  632. a4: var Timespec): cint {.importc, header: "<time.h>", sideEffect.}
  633. proc clock_settime*(a1: ClockId, a2: var Timespec): cint {.
  634. importc, header: "<time.h>", sideEffect.}
  635. proc `==`*(a, b: Time): bool {.borrow.}
  636. proc `-`*(a, b: Time): Time {.borrow.}
  637. proc ctime*(a1: var Time): cstring {.importc, header: "<time.h>".}
  638. proc ctime_r*(a1: var Time, a2: cstring): cstring {.importc, header: "<time.h>".}
  639. proc difftime*(a1, a2: Time): cdouble {.importc, header: "<time.h>".}
  640. proc getdate*(a1: cstring): ptr Tm {.importc, header: "<time.h>".}
  641. proc gmtime*(a1: var Time): ptr Tm {.importc, header: "<time.h>".}
  642. proc gmtime_r*(a1: var Time, a2: var Tm): ptr Tm {.importc, header: "<time.h>".}
  643. proc localtime*(a1: var Time): ptr Tm {.importc, header: "<time.h>".}
  644. proc localtime_r*(a1: var Time, a2: var Tm): ptr Tm {.importc, header: "<time.h>".}
  645. proc mktime*(a1: var Tm): Time {.importc, header: "<time.h>".}
  646. proc timegm*(a1: var Tm): Time {.importc, header: "<time.h>".}
  647. proc nanosleep*(a1, a2: var Timespec): cint {.importc, header: "<time.h>", sideEffect.}
  648. proc strftime*(a1: cstring, a2: int, a3: cstring,
  649. a4: var Tm): int {.importc, header: "<time.h>".}
  650. proc strptime*(a1, a2: cstring, a3: var Tm): cstring {.importc, header: "<time.h>".}
  651. proc time*(a1: var Time): Time {.importc, header: "<time.h>", sideEffect.}
  652. proc timer_create*(a1: ClockId, a2: var SigEvent,
  653. a3: var Timer): cint {.importc, header: "<time.h>".}
  654. proc timer_delete*(a1: Timer): cint {.importc, header: "<time.h>".}
  655. proc timer_gettime*(a1: Timer, a2: var Itimerspec): cint {.
  656. importc, header: "<time.h>".}
  657. proc timer_getoverrun*(a1: Timer): cint {.importc, header: "<time.h>".}
  658. proc timer_settime*(a1: Timer, a2: cint, a3: var Itimerspec,
  659. a4: var Itimerspec): cint {.importc, header: "<time.h>".}
  660. proc tzset*() {.importc, header: "<time.h>".}
  661. proc wait*(a1: ptr cint): Pid {.importc, discardable, header: "<sys/wait.h>", sideEffect.}
  662. proc waitid*(a1: cint, a2: Id, a3: var SigInfo, a4: cint): cint {.
  663. importc, header: "<sys/wait.h>", sideEffect.}
  664. proc waitpid*(a1: Pid, a2: var cint, a3: cint): Pid {.
  665. importc, header: "<sys/wait.h>", sideEffect.}
  666. type Rusage* {.importc: "struct rusage", header: "<sys/resource.h>",
  667. bycopy.} = object
  668. ru_utime*, ru_stime*: Timeval # User and system time
  669. ru_maxrss*, ru_ixrss*, ru_idrss*, ru_isrss*, # memory sizes
  670. ru_minflt*, ru_majflt*, ru_nswap*, # paging activity
  671. ru_inblock*, ru_oublock*, ru_msgsnd*, ru_msgrcv*, # IO activity
  672. ru_nsignals*, ru_nvcsw*, ru_nivcsw*: clong # switching activity
  673. proc wait4*(pid: Pid, status: ptr cint, options: cint, rusage: ptr Rusage): Pid
  674. {.importc, header: "<sys/wait.h>", sideEffect.}
  675. const
  676. RUSAGE_SELF* = cint(0)
  677. RUSAGE_CHILDREN* = cint(-1)
  678. RUSAGE_THREAD* = cint(1) # This one is less std; Linux, BSD agree though.
  679. # This can only fail if `who` is invalid or `rusage` ptr is invalid.
  680. proc getrusage*(who: cint, rusage: ptr Rusage): cint
  681. {.importc, header: "<sys/resource.h>", discardable.}
  682. proc bsd_signal*(a1: cint, a2: proc (x: pointer) {.noconv.}) {.
  683. importc, header: "<signal.h>".}
  684. proc kill*(a1: Pid, a2: cint): cint {.importc, header: "<signal.h>", sideEffect.}
  685. proc killpg*(a1: Pid, a2: cint): cint {.importc, header: "<signal.h>", sideEffect.}
  686. proc pthread_kill*(a1: Pthread, a2: cint): cint {.importc, header: "<signal.h>".}
  687. proc pthread_sigmask*(a1: cint, a2, a3: var Sigset): cint {.
  688. importc, header: "<signal.h>".}
  689. proc `raise`*(a1: cint): cint {.importc, header: "<signal.h>".}
  690. proc sigaction*(a1: cint, a2, a3: var Sigaction): cint {.
  691. importc, header: "<signal.h>".}
  692. proc sigaction*(a1: cint, a2: var Sigaction; a3: ptr Sigaction = nil): cint {.
  693. importc, header: "<signal.h>".}
  694. proc sigaddset*(a1: var Sigset, a2: cint): cint {.importc, header: "<signal.h>".}
  695. proc sigaltstack*(a1, a2: var Stack): cint {.importc, header: "<signal.h>".}
  696. proc sigdelset*(a1: var Sigset, a2: cint): cint {.importc, header: "<signal.h>".}
  697. proc sigemptyset*(a1: var Sigset): cint {.importc, header: "<signal.h>".}
  698. proc sigfillset*(a1: var Sigset): cint {.importc, header: "<signal.h>".}
  699. proc sighold*(a1: cint): cint {.importc, header: "<signal.h>".}
  700. proc sigignore*(a1: cint): cint {.importc, header: "<signal.h>".}
  701. proc siginterrupt*(a1, a2: cint): cint {.importc, header: "<signal.h>".}
  702. proc sigismember*(a1: var Sigset, a2: cint): cint {.importc, header: "<signal.h>".}
  703. proc signal*(a1: cint, a2: Sighandler) {.
  704. importc, header: "<signal.h>".}
  705. proc sigpause*(a1: cint): cint {.importc, header: "<signal.h>".}
  706. proc sigpending*(a1: var Sigset): cint {.importc, header: "<signal.h>".}
  707. proc sigprocmask*(a1: cint, a2, a3: var Sigset): cint {.
  708. importc, header: "<signal.h>".}
  709. proc sigqueue*(a1: Pid, a2: cint, a3: SigVal): cint {.
  710. importc, header: "<signal.h>".}
  711. proc sigrelse*(a1: cint): cint {.importc, header: "<signal.h>".}
  712. proc sigset*(a1: int, a2: proc (x: cint) {.noconv.}) {.
  713. importc, header: "<signal.h>".}
  714. proc sigsuspend*(a1: var Sigset): cint {.importc, header: "<signal.h>".}
  715. when defined(android):
  716. proc syscall(arg: clong): clong {.varargs, importc: "syscall", header: "<unistd.h>".}
  717. var NR_rt_sigtimedwait {.importc: "__NR_rt_sigtimedwait", header: "<sys/syscall.h>".}: clong
  718. var NSIGMAX {.importc: "NSIG", header: "<signal.h>".}: clong
  719. proc sigtimedwait*(a1: var Sigset, a2: var SigInfo, a3: var Timespec): cint =
  720. result = cint(syscall(NR_rt_sigtimedwait, addr(a1), addr(a2), addr(a3), NSIGMAX div 8))
  721. else:
  722. proc sigtimedwait*(a1: var Sigset, a2: var SigInfo,
  723. a3: var Timespec): cint {.importc, header: "<signal.h>".}
  724. when defined(sunos) or defined(solaris):
  725. # The following compile time flag is needed on Illumos/Solaris to use the POSIX
  726. # `sigwait` implementation. See the documentation here:
  727. # https://docs.oracle.com/cd/E19455-01/806-5257/6je9h033k/index.html
  728. # https://www.illumos.org/man/2/sigwait
  729. {.passc: "-D_POSIX_PTHREAD_SEMANTICS".}
  730. proc sigwait*(a1: var Sigset, a2: var cint): cint {.
  731. importc, header: "<signal.h>".}
  732. proc sigwaitinfo*(a1: var Sigset, a2: var SigInfo): cint {.
  733. importc, header: "<signal.h>".}
  734. when not defined(nintendoswitch):
  735. proc catclose*(a1: Nl_catd): cint {.importc, header: "<nl_types.h>".}
  736. proc catgets*(a1: Nl_catd, a2, a3: cint, a4: cstring): cstring {.
  737. importc, header: "<nl_types.h>".}
  738. proc catopen*(a1: cstring, a2: cint): Nl_catd {.
  739. importc, header: "<nl_types.h>".}
  740. proc sched_get_priority_max*(a1: cint): cint {.importc, header: "<sched.h>".}
  741. proc sched_get_priority_min*(a1: cint): cint {.importc, header: "<sched.h>".}
  742. proc sched_getparam*(a1: Pid, a2: var Sched_param): cint {.
  743. importc, header: "<sched.h>".}
  744. proc sched_getscheduler*(a1: Pid): cint {.importc, header: "<sched.h>".}
  745. proc sched_rr_get_interval*(a1: Pid, a2: var Timespec): cint {.
  746. importc, header: "<sched.h>".}
  747. proc sched_setparam*(a1: Pid, a2: var Sched_param): cint {.
  748. importc, header: "<sched.h>".}
  749. proc sched_setscheduler*(a1: Pid, a2: cint, a3: var Sched_param): cint {.
  750. importc, header: "<sched.h>".}
  751. proc sched_yield*(): cint {.importc, header: "<sched.h>".}
  752. proc hstrerror*(herrnum: cint): cstring {.importc:"(char *)$1", header: "<netdb.h>".}
  753. proc FD_CLR*(a1: cint, a2: var TFdSet) {.importc, header: "<sys/select.h>".}
  754. proc FD_ISSET*(a1: cint | SocketHandle, a2: var TFdSet): cint {.
  755. importc, header: "<sys/select.h>".}
  756. proc FD_SET*(a1: cint | SocketHandle, a2: var TFdSet) {.
  757. importc: "FD_SET", header: "<sys/select.h>".}
  758. proc FD_ZERO*(a1: var TFdSet) {.importc, header: "<sys/select.h>".}
  759. proc pselect*(a1: cint, a2, a3, a4: ptr TFdSet, a5: ptr Timespec,
  760. a6: var Sigset): cint {.importc, header: "<sys/select.h>".}
  761. proc select*(a1: cint | SocketHandle, a2, a3, a4: ptr TFdSet, a5: ptr Timeval): cint {.
  762. importc, header: "<sys/select.h>".}
  763. when hasSpawnH:
  764. proc posix_spawn*(a1: var Pid, a2: cstring,
  765. a3: var Tposix_spawn_file_actions,
  766. a4: var Tposix_spawnattr,
  767. a5, a6: cstringArray): cint {.importc, header: "<spawn.h>".}
  768. proc posix_spawn_file_actions_addclose*(a1: var Tposix_spawn_file_actions,
  769. a2: cint): cint {.importc, header: "<spawn.h>".}
  770. proc posix_spawn_file_actions_adddup2*(a1: var Tposix_spawn_file_actions,
  771. a2, a3: cint): cint {.importc, header: "<spawn.h>".}
  772. proc posix_spawn_file_actions_addopen*(a1: var Tposix_spawn_file_actions,
  773. a2: cint, a3: cstring, a4: cint, a5: Mode): cint {.
  774. importc, header: "<spawn.h>".}
  775. proc posix_spawn_file_actions_destroy*(
  776. a1: var Tposix_spawn_file_actions): cint {.importc, header: "<spawn.h>".}
  777. proc posix_spawn_file_actions_init*(
  778. a1: var Tposix_spawn_file_actions): cint {.importc, header: "<spawn.h>".}
  779. proc posix_spawnattr_destroy*(a1: var Tposix_spawnattr): cint {.
  780. importc, header: "<spawn.h>".}
  781. proc posix_spawnattr_getsigdefault*(a1: var Tposix_spawnattr,
  782. a2: var Sigset): cint {.importc, header: "<spawn.h>".}
  783. proc posix_spawnattr_getflags*(a1: var Tposix_spawnattr,
  784. a2: var cshort): cint {.importc, header: "<spawn.h>".}
  785. proc posix_spawnattr_getpgroup*(a1: var Tposix_spawnattr,
  786. a2: var Pid): cint {.importc, header: "<spawn.h>".}
  787. proc posix_spawnattr_getschedparam*(a1: var Tposix_spawnattr,
  788. a2: var Sched_param): cint {.importc, header: "<spawn.h>".}
  789. proc posix_spawnattr_getschedpolicy*(a1: var Tposix_spawnattr,
  790. a2: var cint): cint {.importc, header: "<spawn.h>".}
  791. proc posix_spawnattr_getsigmask*(a1: var Tposix_spawnattr,
  792. a2: var Sigset): cint {.importc, header: "<spawn.h>".}
  793. proc posix_spawnattr_init*(a1: var Tposix_spawnattr): cint {.
  794. importc, header: "<spawn.h>".}
  795. proc posix_spawnattr_setsigdefault*(a1: var Tposix_spawnattr,
  796. a2: var Sigset): cint {.importc, header: "<spawn.h>".}
  797. proc posix_spawnattr_setflags*(a1: var Tposix_spawnattr, a2: cint): cint {.
  798. importc, header: "<spawn.h>".}
  799. proc posix_spawnattr_setpgroup*(a1: var Tposix_spawnattr, a2: Pid): cint {.
  800. importc, header: "<spawn.h>".}
  801. proc posix_spawnattr_setschedparam*(a1: var Tposix_spawnattr,
  802. a2: var Sched_param): cint {.importc, header: "<spawn.h>".}
  803. proc posix_spawnattr_setschedpolicy*(a1: var Tposix_spawnattr,
  804. a2: cint): cint {.
  805. importc, header: "<spawn.h>".}
  806. proc posix_spawnattr_setsigmask*(a1: var Tposix_spawnattr,
  807. a2: var Sigset): cint {.importc, header: "<spawn.h>".}
  808. proc posix_spawnp*(a1: var Pid, a2: cstring,
  809. a3: var Tposix_spawn_file_actions,
  810. a4: var Tposix_spawnattr,
  811. a5, a6: cstringArray): cint {.importc, header: "<spawn.h>".}
  812. when not defined(nintendoswitch):
  813. proc getcontext*(a1: var Ucontext): cint {.importc, header: "<ucontext.h>".}
  814. proc makecontext*(a1: var Ucontext, a4: proc (){.noconv.}, a3: cint) {.
  815. varargs, importc, header: "<ucontext.h>".}
  816. proc setcontext*(a1: var Ucontext): cint {.importc, header: "<ucontext.h>".}
  817. proc swapcontext*(a1, a2: var Ucontext): cint {.importc, header: "<ucontext.h>".}
  818. proc readv*(a1: cint, a2: ptr IOVec, a3: cint): int {.
  819. importc, header: "<sys/uio.h>".}
  820. proc writev*(a1: cint, a2: ptr IOVec, a3: cint): int {.
  821. importc, header: "<sys/uio.h>".}
  822. proc CMSG_DATA*(cmsg: ptr Tcmsghdr): cstring {.
  823. importc, header: "<sys/socket.h>".}
  824. proc CMSG_NXTHDR*(mhdr: ptr Tmsghdr, cmsg: ptr Tcmsghdr): ptr Tcmsghdr {.
  825. importc, header: "<sys/socket.h>".}
  826. proc CMSG_FIRSTHDR*(mhdr: ptr Tmsghdr): ptr Tcmsghdr {.
  827. importc, header: "<sys/socket.h>".}
  828. proc CMSG_SPACE*(len: csize_t): csize_t {.
  829. importc, header: "<sys/socket.h>".}
  830. proc CMSG_LEN*(len: csize_t): csize_t {.
  831. importc, header: "<sys/socket.h>".}
  832. const
  833. INVALID_SOCKET* = SocketHandle(-1)
  834. proc `==`*(x, y: SocketHandle): bool {.borrow.}
  835. proc accept*(a1: SocketHandle, a2: ptr SockAddr, a3: ptr SockLen): SocketHandle {.
  836. importc, header: "<sys/socket.h>", sideEffect.}
  837. when defined(linux) or defined(bsd) or defined(nuttx):
  838. proc accept4*(a1: SocketHandle, a2: ptr SockAddr, a3: ptr SockLen,
  839. flags: cint): SocketHandle {.importc, header: "<sys/socket.h>".}
  840. proc bindSocket*(a1: SocketHandle, a2: ptr SockAddr, a3: SockLen): cint {.
  841. importc: "bind", header: "<sys/socket.h>".}
  842. ## is Posix's `bind`, because `bind` is a reserved word
  843. proc connect*(a1: SocketHandle, a2: ptr SockAddr, a3: SockLen): cint {.
  844. importc, header: "<sys/socket.h>".}
  845. proc getpeername*(a1: SocketHandle, a2: ptr SockAddr, a3: ptr SockLen): cint {.
  846. importc, header: "<sys/socket.h>".}
  847. proc getsockname*(a1: SocketHandle, a2: ptr SockAddr, a3: ptr SockLen): cint {.
  848. importc, header: "<sys/socket.h>".}
  849. proc getsockopt*(a1: SocketHandle, a2, a3: cint, a4: pointer, a5: ptr SockLen): cint {.
  850. importc, header: "<sys/socket.h>".}
  851. proc listen*(a1: SocketHandle, a2: cint): cint {.
  852. importc, header: "<sys/socket.h>", sideEffect.}
  853. proc recv*(a1: SocketHandle, a2: pointer, a3: int, a4: cint): int {.
  854. importc, header: "<sys/socket.h>", sideEffect.}
  855. proc recvfrom*(a1: SocketHandle, a2: pointer, a3: int, a4: cint,
  856. a5: ptr SockAddr, a6: ptr SockLen): int {.
  857. importc, header: "<sys/socket.h>", sideEffect.}
  858. proc recvmsg*(a1: SocketHandle, a2: ptr Tmsghdr, a3: cint): int {.
  859. importc, header: "<sys/socket.h>", sideEffect.}
  860. proc send*(a1: SocketHandle, a2: pointer, a3: int, a4: cint): int {.
  861. importc, header: "<sys/socket.h>", sideEffect.}
  862. proc sendmsg*(a1: SocketHandle, a2: ptr Tmsghdr, a3: cint): int {.
  863. importc, header: "<sys/socket.h>", sideEffect.}
  864. proc sendto*(a1: SocketHandle, a2: pointer, a3: int, a4: cint, a5: ptr SockAddr,
  865. a6: SockLen): int {.
  866. importc, header: "<sys/socket.h>", sideEffect.}
  867. proc setsockopt*(a1: SocketHandle, a2, a3: cint, a4: pointer, a5: SockLen): cint {.
  868. importc, header: "<sys/socket.h>".}
  869. proc shutdown*(a1: SocketHandle, a2: cint): cint {.
  870. importc, header: "<sys/socket.h>".}
  871. proc socket*(a1, a2, a3: cint): SocketHandle {.
  872. importc, header: "<sys/socket.h>".}
  873. proc sockatmark*(a1: cint): cint {.
  874. importc, header: "<sys/socket.h>".}
  875. proc socketpair*(a1, a2, a3: cint, a4: var array[0..1, cint]): cint {.
  876. importc, header: "<sys/socket.h>".}
  877. proc if_nametoindex*(a1: cstring): cint {.importc, header: "<net/if.h>".}
  878. proc if_indextoname*(a1: cint, a2: cstring): cstring {.
  879. importc, header: "<net/if.h>".}
  880. proc if_nameindex*(): ptr Tif_nameindex {.importc, header: "<net/if.h>".}
  881. proc if_freenameindex*(a1: ptr Tif_nameindex) {.importc, header: "<net/if.h>".}
  882. proc IN6_IS_ADDR_UNSPECIFIED* (a1: ptr In6Addr): cint {.
  883. importc, header: "<netinet/in.h>".}
  884. ## Unspecified address.
  885. proc IN6_IS_ADDR_LOOPBACK* (a1: ptr In6Addr): cint {.
  886. importc, header: "<netinet/in.h>".}
  887. ## Loopback address.
  888. proc IN6_IS_ADDR_MULTICAST* (a1: ptr In6Addr): cint {.
  889. importc, header: "<netinet/in.h>".}
  890. ## Multicast address.
  891. proc IN6_IS_ADDR_LINKLOCAL* (a1: ptr In6Addr): cint {.
  892. importc, header: "<netinet/in.h>".}
  893. ## Unicast link-local address.
  894. proc IN6_IS_ADDR_SITELOCAL* (a1: ptr In6Addr): cint {.
  895. importc, header: "<netinet/in.h>".}
  896. ## Unicast site-local address.
  897. when defined(lwip):
  898. proc IN6_IS_ADDR_V4MAPPED*(ipv6_address: ptr In6Addr): cint =
  899. var bits32: ptr array[4, uint32] = cast[ptr array[4, uint32]](ipv6_address)
  900. return (bits32[1] == 0'u32 and bits32[2] == htonl(0x0000FFFF)).cint
  901. else:
  902. proc IN6_IS_ADDR_V4MAPPED* (a1: ptr In6Addr): cint {.
  903. importc, header: "<netinet/in.h>".}
  904. ## IPv4 mapped address.
  905. proc IN6_IS_ADDR_V4COMPAT* (a1: ptr In6Addr): cint {.
  906. importc, header: "<netinet/in.h>".}
  907. ## IPv4-compatible address.
  908. proc IN6_IS_ADDR_MC_NODELOCAL* (a1: ptr In6Addr): cint {.
  909. importc, header: "<netinet/in.h>".}
  910. ## Multicast node-local address.
  911. proc IN6_IS_ADDR_MC_LINKLOCAL* (a1: ptr In6Addr): cint {.
  912. importc, header: "<netinet/in.h>".}
  913. ## Multicast link-local address.
  914. proc IN6_IS_ADDR_MC_SITELOCAL* (a1: ptr In6Addr): cint {.
  915. importc, header: "<netinet/in.h>".}
  916. ## Multicast site-local address.
  917. proc IN6_IS_ADDR_MC_ORGLOCAL* (a1: ptr In6Addr): cint {.
  918. importc, header: "<netinet/in.h>".}
  919. ## Multicast organization-local address.
  920. proc IN6_IS_ADDR_MC_GLOBAL* (a1: ptr In6Addr): cint {.
  921. importc, header: "<netinet/in.h>".}
  922. ## Multicast global address.
  923. proc endhostent*() {.importc, header: "<netdb.h>".}
  924. proc endnetent*() {.importc, header: "<netdb.h>".}
  925. proc endprotoent*() {.importc, header: "<netdb.h>".}
  926. proc endservent*() {.importc, header: "<netdb.h>".}
  927. proc freeAddrInfo*(a1: ptr AddrInfo) {.importc: "freeaddrinfo", header: "<netdb.h>".}
  928. proc gai_strerror*(a1: cint): cstring {.importc:"(char *)$1", header: "<netdb.h>".}
  929. proc getaddrinfo*(a1, a2: cstring, a3: ptr AddrInfo,
  930. a4: var ptr AddrInfo): cint {.importc, header: "<netdb.h>".}
  931. when not defined(android4):
  932. proc gethostbyaddr*(a1: pointer, a2: SockLen, a3: cint): ptr Hostent {.
  933. importc, header: "<netdb.h>".}
  934. else:
  935. proc gethostbyaddr*(a1: cstring, a2: cint, a3: cint): ptr Hostent {.
  936. importc, header: "<netdb.h>".}
  937. proc gethostbyname*(a1: cstring): ptr Hostent {.importc, header: "<netdb.h>".}
  938. proc gethostent*(): ptr Hostent {.importc, header: "<netdb.h>".}
  939. proc getnameinfo*(a1: ptr SockAddr, a2: SockLen,
  940. a3: cstring, a4: SockLen, a5: cstring,
  941. a6: SockLen, a7: cint): cint {.importc, header: "<netdb.h>".}
  942. proc getnetbyaddr*(a1: int32, a2: cint): ptr Tnetent {.importc, header: "<netdb.h>".}
  943. proc getnetbyname*(a1: cstring): ptr Tnetent {.importc, header: "<netdb.h>".}
  944. proc getnetent*(): ptr Tnetent {.importc, header: "<netdb.h>".}
  945. proc getprotobyname*(a1: cstring): ptr Protoent {.importc, header: "<netdb.h>".}
  946. proc getprotobynumber*(a1: cint): ptr Protoent {.importc, header: "<netdb.h>".}
  947. proc getprotoent*(): ptr Protoent {.importc, header: "<netdb.h>".}
  948. proc getservbyname*(a1, a2: cstring): ptr Servent {.importc, header: "<netdb.h>".}
  949. proc getservbyport*(a1: cint, a2: cstring): ptr Servent {.
  950. importc, header: "<netdb.h>".}
  951. proc getservent*(): ptr Servent {.importc, header: "<netdb.h>".}
  952. proc sethostent*(a1: cint) {.importc, header: "<netdb.h>".}
  953. proc setnetent*(a1: cint) {.importc, header: "<netdb.h>".}
  954. proc setprotoent*(a1: cint) {.importc, header: "<netdb.h>".}
  955. proc setservent*(a1: cint) {.importc, header: "<netdb.h>".}
  956. when not defined(lwip):
  957. # Linux and Haiku emulate SVR4, which used unsigned long.
  958. # Meanwhile, BSD derivatives had used unsigned int; we will use this
  959. # for the else case, because it is more widely cloned than SVR4's
  960. # behavior.
  961. when defined(linux) or defined(haiku):
  962. type
  963. Tnfds* {.importc: "nfds_t", header: "<poll.h>".} = culong
  964. elif defined(zephyr):
  965. type
  966. Tnfds* = distinct cint
  967. else:
  968. type
  969. Tnfds* {.importc: "nfds_t", header: "<poll.h>".} = cuint
  970. proc poll*(a1: ptr TPollfd, a2: Tnfds, a3: cint): cint {.
  971. importc, header: "<poll.h>", sideEffect.}
  972. proc realpath*(name, resolved: cstring): cstring {.
  973. importc: "realpath", header: "<stdlib.h>".}
  974. proc mkstemp*(tmpl: cstring): cint {.importc, header: "<stdlib.h>", sideEffect.}
  975. ## Creates a unique temporary file.
  976. ##
  977. ## .. warning:: The `tmpl` argument is written to by `mkstemp` and thus
  978. ## can't be a string literal. If in doubt make a copy of the cstring before
  979. ## passing it in.
  980. proc mkstemps*(tmpl: cstring, suffixlen: int): cint {.importc, header: "<stdlib.h>", sideEffect.}
  981. ## Creates a unique temporary file.
  982. ##
  983. ## .. warning:: The `tmpl` argument is written to by `mkstemps` and thus
  984. ## can't be a string literal. If in doubt make a copy of the cstring before
  985. ## passing it in.
  986. proc mkdtemp*(tmpl: cstring): pointer {.importc, header: "<stdlib.h>", sideEffect.}
  987. when defined(linux) or defined(bsd) or defined(osx):
  988. proc mkostemp*(tmpl: cstring, oflags: cint): cint {.importc, header: "<stdlib.h>", sideEffect.}
  989. proc mkostemps*(tmpl: cstring, suffixlen: cint, oflags: cint): cint {.importc, header: "<stdlib.h>", sideEffect.}
  990. proc posix_memalign*(memptr: pointer, alignment: csize_t, size: csize_t): cint {.importc, header: "<stdlib.h>".}
  991. proc utimes*(path: cstring, times: ptr array[2, Timeval]): int {.
  992. importc: "utimes", header: "<sys/time.h>", sideEffect.}
  993. ## Sets file access and modification times.
  994. ##
  995. ## Pass the filename and an array of times to set the access and modification
  996. ## times respectively. If you pass nil as the array both attributes will be
  997. ## set to the current time.
  998. ##
  999. ## Returns zero on success.
  1000. ##
  1001. ## For more information read https://www.unix.com/man-page/posix/3/utimes/.
  1002. proc handle_signal(sig: cint, handler: proc (a: cint) {.noconv.}) {.importc: "signal", header: "<signal.h>".}
  1003. template onSignal*(signals: varargs[cint], body: untyped) =
  1004. ## Setup code to be executed when Unix signals are received. The
  1005. ## currently handled signal is injected as `sig` into the calling
  1006. ## scope.
  1007. ##
  1008. ## Example:
  1009. ## ```Nim
  1010. ## from std/posix import SIGINT, SIGTERM, onSignal
  1011. ## onSignal(SIGINT, SIGTERM):
  1012. ## echo "bye from signal ", sig
  1013. ## ```
  1014. for s in signals:
  1015. handle_signal(s,
  1016. proc (signal: cint) {.noconv.} =
  1017. let sig {.inject.} = signal
  1018. body
  1019. )
  1020. type
  1021. RLimit* {.importc: "struct rlimit",
  1022. header: "<sys/resource.h>", pure, final.} = object
  1023. rlim_cur*: int
  1024. rlim_max*: int
  1025. ## The getrlimit() and setrlimit() system calls get and set resource limits respectively.
  1026. ## Each resource has an associated soft and hard limit, as defined by the RLimit structure
  1027. proc setrlimit*(resource: cint, rlp: var RLimit): cint {.
  1028. importc: "setrlimit", header: "<sys/resource.h>".}
  1029. ## The setrlimit() system calls sets resource limits.
  1030. proc getrlimit*(resource: cint, rlp: var RLimit): cint {.
  1031. importc: "getrlimit", header: "<sys/resource.h>".}
  1032. ## The getrlimit() system call gets resource limits.
  1033. when defined(nimHasStyleChecks):
  1034. {.pop.} # {.push styleChecks: off.}