stdio_libc.odin 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #+build linux, windows, linux, darwin, netbsd, openbsd, freebsd
  2. package posix
  3. import "core:c"
  4. import "core:c/libc"
  5. when ODIN_OS == .Windows {
  6. foreign import lib {
  7. "system:libucrt.lib",
  8. "system:legacy_stdio_definitions.lib",
  9. }
  10. } else when ODIN_OS == .Darwin {
  11. foreign import lib "system:System.framework"
  12. } else {
  13. foreign import lib "system:c"
  14. }
  15. // stdio.h - standard buffered input/output
  16. when ODIN_OS == .Windows {
  17. @(private) LGETC_UNLOCKED :: "_getc_nolock"
  18. @(private) LGETCHAR_UNLOCKED :: "_getchar_nolock"
  19. @(private) LPUTC_UNLOCKED :: "_putc_nolock"
  20. @(private) LPUTCHAR_UNLOCKED :: "_putchar_nolock"
  21. @(private) LTEMPNAM :: "_tempnam"
  22. @(private) LPOPEN :: "_popen"
  23. @(private) LPCLOSE :: "_pclose"
  24. } else {
  25. @(private) LGETC_UNLOCKED :: "getc_unlocked"
  26. @(private) LGETCHAR_UNLOCKED :: "getchar_unlocked"
  27. @(private) LPUTC_UNLOCKED :: "putc_unlocked"
  28. @(private) LPUTCHAR_UNLOCKED :: "putchar_unlocked"
  29. @(private) LTEMPNAM :: "tempnam"
  30. @(private) LPOPEN :: "popen"
  31. @(private) LPCLOSE :: "pclose"
  32. }
  33. foreign lib {
  34. /*
  35. Equivalent to fprintf but output is written to s, it is the user's responsibility to
  36. ensure there is enough space.
  37. Return: number of bytes written, negative (setting errno) on failure
  38. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/dprintf.html ]]
  39. */
  40. sprintf :: proc(s: [^]byte, format: cstring, #c_vararg args: ..any) -> c.int ---
  41. /*
  42. Equivalent to getc but unaffected by locks.
  43. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/getc_unlocked.html ]]
  44. */
  45. @(link_name=LGETC_UNLOCKED)
  46. getc_unlocked :: proc(stream: ^FILE) -> c.int ---
  47. /*
  48. Equivalent to getchar but unaffected by locks.
  49. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/getc_unlocked.html ]]
  50. */
  51. @(link_name=LGETCHAR_UNLOCKED)
  52. getchar_unlocked :: proc() -> c.int ---
  53. /*
  54. Equivalent to putc but unaffected by locks.
  55. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/getc_unlocked.html ]]
  56. */
  57. @(link_name=LPUTC_UNLOCKED)
  58. putc_unlocked :: proc(ch: c.int, stream: ^FILE) -> c.int ---
  59. /*
  60. Equivalent to putchar but unaffected by locks.
  61. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/getc_unlocked.html ]]
  62. */
  63. @(link_name=LPUTCHAR_UNLOCKED)
  64. putchar_unlocked :: proc(ch: c.int) -> c.int ---
  65. /*
  66. Get a string from the stdin stream.
  67. It is up to the user to make sure s is big enough.
  68. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/gets.html ]]
  69. */
  70. gets :: proc(s: [^]byte) -> cstring ---
  71. /*
  72. Create a name for a temporary file.
  73. Returns: an allocated cstring that needs to be freed, nil on failure
  74. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/tempnam.html ]]
  75. */
  76. @(link_name=LTEMPNAM)
  77. tempnam :: proc(dir: cstring, pfx: cstring) -> cstring ---
  78. /*
  79. Executes the command specified, creating a pipe and returning a pointer to a stream that can
  80. read or write from/to the pipe.
  81. Returns: nil (setting errno) on failure or a pointer to the stream
  82. Example:
  83. fp := posix.popen("ls *", "r")
  84. if fp == nil {
  85. /* Handle error */
  86. }
  87. path: [1024]byte
  88. for posix.fgets(raw_data(path[:]), len(path), fp) != nil {
  89. posix.printf("%s", &path)
  90. }
  91. status := posix.pclose(fp)
  92. if status == -1 {
  93. /* Error reported by pclose() */
  94. } else {
  95. /* Use functions described under wait() to inspect `status` in order
  96. to determine success/failure of the command executed by popen() */
  97. }
  98. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/popen.html ]]
  99. */
  100. @(link_name=LPOPEN)
  101. popen :: proc(command: cstring, mode: cstring) -> ^FILE ---
  102. /*
  103. Closes a pipe stream to or from a process.
  104. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/pclose.html ]]
  105. */
  106. @(link_name=LPCLOSE)
  107. pclose :: proc(stream: ^FILE) -> c.int ---
  108. }
  109. clearerr :: libc.clearerr
  110. fclose :: libc.fclose
  111. feof :: libc.feof
  112. ferror :: libc.ferror
  113. fflush :: libc.fflush
  114. fgetc :: libc.fgetc
  115. fgetpos :: libc.fgetpos
  116. fgets :: libc.fgets
  117. fopen :: libc.fopen
  118. fprintf :: libc.fprintf
  119. fputc :: libc.fputc
  120. fread :: libc.fread
  121. freopen :: libc.freopen
  122. fscanf :: libc.fscanf
  123. fseek :: libc.fseek
  124. fsetpos :: libc.fsetpos
  125. ftell :: libc.ftell
  126. fwrite :: libc.fwrite
  127. getc :: libc.getc
  128. getchar :: libc.getchar
  129. perror :: libc.perror
  130. printf :: libc.printf
  131. putc :: libc.puts
  132. putchar :: libc.putchar
  133. puts :: libc.puts
  134. remove :: libc.remove
  135. rename :: libc.rename
  136. rewind :: libc.rewind
  137. scanf :: libc.scanf
  138. setbuf :: libc.setbuf
  139. setvbuf :: libc.setvbuf
  140. snprintf :: libc.snprintf
  141. sscanf :: libc.sscanf
  142. tmpfile :: libc.tmpfile
  143. tmpnam :: libc.tmpnam
  144. vfprintf :: libc.vfprintf
  145. vfscanf :: libc.vfscanf
  146. vprintf :: libc.vprintf
  147. vscanf :: libc.vscanf
  148. vsnprintf :: libc.vsnprintf
  149. vsprintf :: libc.vsprintf
  150. vsscanf :: libc.vsscanf
  151. ungetc :: libc.ungetc
  152. to_stream :: libc.to_stream
  153. Whence :: libc.Whence
  154. FILE :: libc.FILE
  155. fpos_t :: libc.fpos_t
  156. BUFSIZ :: libc.BUFSIZ
  157. _IOFBF :: libc._IOFBF
  158. _IOLBF :: libc._IOLBF
  159. _IONBF :: libc._IONBF
  160. SEEK_CUR :: libc.SEEK_CUR
  161. SEEK_END :: libc.SEEK_END
  162. SEEK_SET :: libc.SEEK_SET
  163. FILENAME_MAX :: libc.FILENAME_MAX
  164. FOPEN_MAX :: libc.FOPEN_MAX
  165. TMP_MAX :: libc.TMP_MAX
  166. EOF :: libc.EOF
  167. stderr := libc.stderr
  168. stdin := libc.stdin
  169. stdout := libc.stdout