stdio.h 4.5 KB

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