stdio.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #pragma src "/sys/src/libstdio"
  10. #pragma lib "libstdio.a"
  11. /*
  12. * pANS astdio.h
  13. */
  14. /*
  15. * According to X3J11, there is only one i/o buffer
  16. * and it must not be occupied by both input and output data.
  17. * If rp<wp, we must have state==RD and
  18. * if wp<rp, we must have state==WR, so that getc and putc work correctly.
  19. * On open, rp, wp and buf are set to 0, so first getc or putc will call _IO_getc
  20. * or _IO_putc, which will allocate the buffer.
  21. * If setvbuf(., ., _IONBF, .) is called, bufl is set to 0 and
  22. * buf, rp and wp are pointed at unbuf.
  23. * If setvbuf(., ., _IOLBF, .) is called, _IO_putc leaves wp and rp pointed at the
  24. * end of the buffer so that it can be called on each putc to check whether it's got
  25. * a newline. This nonsense is in order to avoid impacting performance of the other
  26. * buffering modes more than necessary -- putting the test in putc adds many
  27. * instructions that are wasted in non-_IOLBF mode:
  28. * #define putc(c, f) (_IO_ctmp=(c),\
  29. * (f)->wp>=(f)->rp || (f)->flags&LINEBUF && _IO_ctmp=='\n'\
  30. * ?_IO_putc(_IO_ctmp, f)\
  31. * :*(f)->wp++=_IO_ctmp)
  32. *
  33. */
  34. typedef struct{
  35. int fd; /* UNIX file pointer */
  36. char flags; /* bits for must free buffer on close, line-buffered */
  37. char state; /* last operation was read, write, position, error, eof */
  38. char *buf; /* pointer to i/o buffer */
  39. char *rp; /* read pointer (or write end-of-buffer) */
  40. char *wp; /* write pointer (or read end-of-buffer) */
  41. char *lp; /* actual write pointer used when line-buffering */
  42. int32_t bufl; /* actual length of buffer */
  43. char unbuf[1]; /* tiny buffer for unbuffered io (used for ungetc?) */
  44. }FILE;
  45. typedef int32_t fpos_t;
  46. #ifndef NULL
  47. #define NULL ((void*)0)
  48. #endif
  49. /*
  50. * Third arg of setvbuf
  51. */
  52. #define _IOFBF 1 /* block-buffered */
  53. #define _IOLBF 2 /* line-buffered */
  54. #define _IONBF 3 /* unbuffered */
  55. #define BUFSIZ 4096 /* size of setbuf buffer */
  56. #define EOF (-1) /* returned on end of file */
  57. #define FOPEN_MAX 100 /* max files open */
  58. #define FILENAME_MAX BUFSIZ /* silly filename length */
  59. #define L_tmpnam 20 /* sizeof "/tmp/abcdefghij9999 */
  60. #ifndef SEEK_SET /* also defined in unistd.h */
  61. #define SEEK_CUR 1
  62. #define SEEK_END 2
  63. #define SEEK_SET 0
  64. #endif
  65. #define TMP_MAX 64 /* very hard to set correctly */
  66. #define stderr (&_IO_stream[2])
  67. #define stdin (&_IO_stream[0])
  68. #define stdout (&_IO_stream[1])
  69. #define _IO_CHMASK 0377 /* mask for 8 bit characters */
  70. FILE *tmpfile(void);
  71. char *tmpnam(char *);
  72. int fclose(FILE *);
  73. int fflush(FILE *);
  74. FILE *fopen(const char *, const char *);
  75. FILE *fdopen(const int, const char *);
  76. FILE *freopen(const char *, const char *, FILE *);
  77. void setbuf(FILE *, char *);
  78. int setvbuf(FILE *, char *, int, int32_t);
  79. int fprintf(FILE *, const char *, ...);
  80. int fscanf(FILE *, const char *, ...);
  81. int printf(const char *, ...);
  82. int scanf(const char *, ...);
  83. int sprintf(char *, const char *, ...);
  84. int snprintf(char *, int, const char *, ...);
  85. int sscanf(const char *, const char *, ...);
  86. int vfprintf(FILE *, const char *, va_list);
  87. int vprintf(const char *, va_list);
  88. int vsprintf(char *, const char *, va_list);
  89. int vsnprintf(char *, int, const char *, va_list);
  90. int vfscanf(FILE *, const char *, va_list);
  91. int fgetc(FILE *);
  92. char *fgets(char *, int, FILE *);
  93. int fputc(int, FILE *);
  94. int fputs(const char *, FILE *);
  95. int getc(FILE *);
  96. #define getc(f) ((f)->rp>=(f)->wp?_IO_getc(f):*(f)->rp++&_IO_CHMASK)
  97. int _IO_getc(FILE *f);
  98. int getchar(void);
  99. #define getchar() getc(stdin)
  100. char *gets(char *);
  101. int putc(int, FILE *);
  102. #define putc(c, f) ((f)->wp>=(f)->rp?_IO_putc(c, f):(*(f)->wp++=c)&_IO_CHMASK)
  103. int _IO_putc(int, FILE *);
  104. int putchar(int);
  105. #define putchar(c) putc(c, stdout)
  106. int puts(const char *);
  107. int ungetc(int, FILE *);
  108. int32_t fread(void *, int32_t, int32_t, FILE *);
  109. int32_t fwrite(const void *, int32_t, int32_t, FILE *);
  110. int fgetpos(FILE *, fpos_t *);
  111. int fseek(FILE *, int32_t, int);
  112. int fseeko(FILE *, long long, int);
  113. int fsetpos(FILE *, const fpos_t *);
  114. int32_t ftell(FILE *);
  115. long long ftello(FILE *);
  116. void rewind(FILE *);
  117. void clearerr(FILE *);
  118. int feof(FILE *);
  119. int ferror(FILE *);
  120. void perror(const char *);
  121. extern FILE _IO_stream[FOPEN_MAX];
  122. FILE *sopenr(const char *);
  123. FILE *sopenw(void);
  124. char *sclose(FILE *);
  125. int fileno(FILE *);