stdio.odin 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #+build linux, darwin, netbsd, openbsd, freebsd
  2. package posix
  3. import "core:c"
  4. when ODIN_OS == .Darwin {
  5. foreign import lib "system:System.framework"
  6. } else {
  7. foreign import lib "system:c"
  8. }
  9. // stdio.h - standard buffered input/output
  10. foreign lib {
  11. /*
  12. Generates a string that, when used as a pathname,
  13. refers to the current controlling terminal for the current process.
  14. If s is nil, the returned string might be static and overwritten by subsequent calls or other factors.
  15. If s is not nil, s is assumed len(s) >= L_ctermid.
  16. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/ctermid.html ]]
  17. */
  18. ctermid :: proc(s: [^]byte) -> cstring ---
  19. /*
  20. Equivalent to fprintf but output is written to the file descriptor.
  21. Return: number of bytes written, negative (setting errno) on failure
  22. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/dprintf.html ]]
  23. */
  24. dprintf :: proc(fildse: FD, format: cstring, #c_vararg args: ..any) -> c.int ---
  25. /*
  26. Associate a stream with a file descriptor.
  27. Returns: nil (setting errno) on failure, the stream on success
  28. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/fdopen.html ]]
  29. */
  30. fdopen :: proc(fildes: FD, mode: cstring) -> ^FILE ---
  31. /*
  32. Map a stream pointer to a file descriptor.
  33. Returns: the file descriptor or -1 (setting errno) on failure
  34. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/fileno.html ]]
  35. */
  36. fileno :: proc(stream: ^FILE) -> FD ---
  37. /*
  38. Locks a file.
  39. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/flockfile.html ]]
  40. */
  41. flockfile :: proc(file: ^FILE) ---
  42. /*
  43. Tries to lock a file.
  44. Returns: 0 if it could be locked, non-zero if it couldn't
  45. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/flockfile.html ]]
  46. */
  47. ftrylockfile :: proc(file: ^FILE) -> c.int ---
  48. /*
  49. Unlocks a file.
  50. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/flockfile.html ]]
  51. */
  52. funlockfile :: proc(file: ^FILE) ---
  53. /*
  54. Open a memory buffer stream.
  55. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/fmemopen.html ]]
  56. */
  57. fmemopen :: proc(buf: [^]byte, size: c.size_t, mode: cstring) -> ^FILE ---
  58. /*
  59. Reposition a file-position indicator in a stream.
  60. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/fseeko.html ]]
  61. */
  62. fseeko :: proc(stream: ^FILE, offset: off_t, whence: Whence) -> result ---
  63. /*
  64. Return the file offset in a stream.
  65. Returns: the current file offset, -1 (setting errno) on error
  66. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/ftello.html ]]
  67. */
  68. ftello :: proc(^FILE) -> off_t ---
  69. /*
  70. Open a dynamic memory buffer stream.
  71. Returns: nil (setting errno) on failure, the stream on success
  72. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/open_memstream.html ]]
  73. */
  74. open_memstream :: proc(bufp: ^[^]byte, sizep: ^c.size_t) -> ^FILE ---
  75. /*
  76. Read a delimited record from the stream.
  77. Returns: the number of bytes written or -1 on failure/EOF
  78. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/getdelim.html ]]
  79. */
  80. getdelim :: proc(lineptr: ^cstring, n: ^c.size_t, delimiter: c.int, stream: ^FILE) -> c.ssize_t ---
  81. /*
  82. Read a line delimited record from the stream.
  83. Returns: the number of bytes written or -1 on failure/EOF
  84. Example:
  85. fp := posix.fopen(#file, "r")
  86. if fp == nil {
  87. posix.exit(1)
  88. }
  89. line: cstring
  90. length: uint
  91. for {
  92. read := posix.getline(&line, &length, fp)
  93. if read == -1 do break
  94. posix.printf("Retrieved line of length %zu :\n", read)
  95. posix.printf("%s", line)
  96. }
  97. if posix.ferror(fp) != 0 {
  98. /* handle error */
  99. }
  100. posix.free(rawptr(line))
  101. posix.fclose(fp)
  102. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/getdelim.html ]]
  103. */
  104. getline :: proc(lineptr: ^cstring, n: ^c.size_t, stream: ^FILE) -> c.ssize_t ---
  105. /*
  106. Equivalent to rename but relative directories are resolved from their respective fds.
  107. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/renameat.html ]]
  108. */
  109. renameat :: proc(oldfd: FD, old: cstring, newfd: FD, new: cstring) -> result ---
  110. }
  111. when ODIN_OS == .Darwin {
  112. L_ctermid :: 1024
  113. L_tmpnam :: 1024
  114. P_tmpdir :: "/var/tmp/"
  115. } else when ODIN_OS == .FreeBSD || ODIN_OS == .NetBSD || ODIN_OS == .OpenBSD {
  116. L_ctermid :: 1024
  117. L_tmpnam :: 1024
  118. P_tmpdir :: "/tmp/"
  119. } else when ODIN_OS == .Linux {
  120. L_ctermid :: 20 // 20 on musl, 9 on glibc
  121. L_tmpnam :: 20
  122. P_tmpdir :: "/tmp/"
  123. }