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