stdio.h 5.5 KB

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