_stdio.h (4180B)
1 #ifndef _STDIO_H 2 #define _STDIO_H 3 4 #include <sys/types.h> 5 #include <stddef.h> 6 #include <stdarg.h> 7 8 9 typedef struct { 10 int fd; // Underlying file descriptor 11 12 unsigned char bufmode; // Buffering mode 13 unsigned char eofflag; // End-of-file indicator 14 unsigned char errflag; // Error indicator 15 16 unsigned char *obuf; // Output buffer 17 unsigned opos; // Write position in output buffer 18 unsigned omax; // Size of output buffer 19 20 unsigned char *ibuf; // Input buffer 21 unsigned ipos; // Read position in input buffer 22 unsigned ilim; // Read limit in input buffer 23 unsigned imax; // Size of input buffer 24 25 int append; // Always append to end of file 26 int isstring; // is constant string 27 unsigned ioffset; // offset of last read 28 } FILE; 29 30 typedef signed long long fpos_t; 31 32 #define EOF (-1) // End-of-file return value. 33 34 #ifndef SEEK_SET 35 #define SEEK_SET 0 // Seek relative to start-of-file. 36 #define SEEK_CUR 1 // Seek relative to current position. 37 #define SEEK_END 2 // Seek relative to end-of-file. 38 #endif 39 40 #define BUFSIZ 1024 // Standard I/O buffer size 41 42 #define _IOFBF 0 // Input/output fully buffered. 43 #define _IOLBF 1 // Input/output line buffered. 44 #define _IONBF 2 // Input/output unbuffered. 45 46 47 // Standard I/O streams 48 extern FILE __stdin, __stdout, __stderr; 49 #define stdin (&__stdin) 50 #define stdout (&__stdout) 51 #define stderr (&__stderr) 52 53 54 #define L_tmpnam 20 55 56 57 // File handling 58 FILE *fopen(const char *path, const char *mode); 59 FILE *fdopen(int fildes, const char *mode); 60 FILE *freopen(const char *path, const char *mode, FILE *stream); 61 int fclose(FILE *fp); 62 63 // Temporary files 64 FILE *tmpfile(void); 65 char *tmpnam(char *buf); 66 67 // Character output 68 int fputc(int c, FILE *f); 69 int putc(int c, FILE *f); 70 int putchar(int c); 71 #define putc(c,f) ((f)->opos < (f)->omax && (c) != '\n' \ 72 ? (int)((f)->obuf[(f)->opos++] = (c)) \ 73 : fputc(c,f)) 74 #define putchar(c) putc(c, stdout) 75 76 // Unformatted output 77 int fputs(const char *s, FILE *f); 78 int puts(const char *s); 79 size_t fwrite(const void *__restrict buf, size_t eltsize, size_t nelts, 80 FILE *__restrict f); 81 82 // Formatted output 83 int printf(const char *format, ...); 84 int fprintf(FILE *f, const char *fmt, ...); 85 int sprintf(char *buf, const char *fmt, ...); 86 int snprintf(char *buf, int n, const char *fmt, ...); 87 88 int vprintf(const char *format, va_list ap); 89 int vfprintf(FILE *f, const char *fmt, va_list ap); 90 int vsprintf(char *buf, const char *fmt, va_list ap); 91 int vsnprintf(char *buf, int n, const char *fmt, va_list ap); 92 93 // Character input 94 int fgetc(FILE *f); 95 int getc(FILE *f); 96 int getchar(void); 97 int ungetc(int c, FILE *f); 98 #define getc(f) ((f)->ipos < (f)->ilim \ 99 ? (int)((f)->ibuf[(f)->ipos++]) \ 100 : fgetc(f)) 101 #define getchar() getc(stdin) 102 int ungetc(int, FILE*); 103 104 // Unformatted input 105 char *gets(char *s); 106 char *fgets(char *s, int size, FILE *stream); 107 size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); 108 109 // Formatted input 110 int scanf(const char *__restrict format, ... ); 111 int fscanf(FILE *__restrict f, const char *__restrict format, ... ); 112 int sscanf(const char *__restrict str, const char *__restrict format, ... ); 113 114 int vscanf(const char *__restrict format, va_list); 115 int vfscanf(FILE *__restrict f, const char *__restrict format, va_list); 116 int vsscanf(const char *__restrict str, const char *__restrict format, va_list); 117 118 // Seek position 119 int fseek(FILE *stream, long offset, int whence); 120 long ftell(FILE *stream); 121 void rewind(FILE *stream); 122 int fgetpos(FILE *__restrict f, fpos_t *__restrict pos); 123 int fsetpos(FILE *f, const fpos_t *pos); 124 125 off_t ftello(FILE *stream); 126 127 // Error/EOF handling 128 int feof(FILE *f); 129 int ferror(FILE *f); 130 void clearerr(FILE *f); 131 #define feof(f) ((int)(f)->eofflag) 132 #define ferror(f) ((int)(f)->errflag) 133 #define clearerror(f) ((void)((f)->errflag = 0)) 134 135 // Buffer management 136 int fflush(FILE *f); 137 void setbuf(FILE *__restrict f, char *__restrict buf); 138 int setvbuf(FILE *__restrict f, char *__restrict buf, int type, size_t size); 139 140 // Misc 141 #define fileno(f) ((f)->fd) 142 143 void perror(const char*); 144 int remove(const char*); 145 146 // File management 147 int remove(const char *path); 148 int rename(const char *from, const char *to); 149 150 #define FILENAME_MAX 1024 151 152 #endif // _STDIO_H