1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Chris Torek. 7 * 8 * %sccs.include.redist.c% 9 * 10 * @(#)stdio.h 5.8 (Berkeley) 01/20/91 11 */ 12 13 #ifndef _STDIO_H_ 14 #define _STDIO_H_ 15 16 /* 17 * NB: to fit things in six character monocase externals, the stdio 18 * code uses the prefix `__s' for stdio objects, typically followed 19 * by a three-character attempt at a mnemonic. 20 */ 21 22 #include <machine/types.h> 23 24 #ifdef _SIZE_T_ 25 typedef _SIZE_T_ size_t; 26 #undef _SIZE_T_ 27 #endif 28 29 #ifndef NULL 30 #define NULL 0 31 #endif 32 33 typedef long fpos_t; /* Must match off_t <sys/types.h> */ 34 35 #define _FSTDIO /* Define for new stdio with functions. */ 36 37 /* stdio buffers */ 38 struct __sbuf { 39 unsigned char *_base; 40 int _size; 41 }; 42 43 /* 44 * stdio state variables. 45 * 46 * The following always hold: 47 * 48 * if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR), 49 * _lbfsize is -_bf._size, else _lbfsize is 0 50 * if _flags&__SRD, _w is 0 51 * if _flags&__SWR, _r is 0 52 * 53 * This ensures that the getc and putc macros (or inline functions) never 54 * try to write or read from a file that is in `read' or `write' mode. 55 * (Moreover, they can, and do, automatically switch from read mode to 56 * write mode, and back, on "r+" and "w+" files.) 57 * 58 * _lbfsize is used only to make the inline line-buffered output stream 59 * code as compact as possible. 60 * 61 * _ub, _up, and _ur are used when ungetc() pushes back more characters 62 * than fit in the current _bf, or when ungetc() pushes back a character 63 * that does not match the previous one in _bf. When this happens, 64 * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff 65 * _ub._base!=NULL) and _up and _ur save the current values of _p and _r. 66 */ 67 typedef struct __sFILE { 68 unsigned char *_p; /* current position in (some) buffer */ 69 int _r; /* read space left for getc() */ 70 int _w; /* write space left for putc() */ 71 short _flags; /* flags, below; this FILE is free if 0 */ 72 short _file; /* fileno, if Unix descriptor, else -1 */ 73 struct __sbuf _bf; /* the buffer (at least 1 byte, if !NULL) */ 74 int _lbfsize; /* 0 or -_bf._size, for inline putc */ 75 76 /* operations */ 77 void *_cookie; /* cookie passed to io functions */ 78 #if __STDC__ || c_plusplus 79 int (*_read)(void *_cookie, char *_buf, int _n); 80 int (*_write)(void *_cookie, const char *_buf, int _n); 81 fpos_t (*_seek)(void *_cookie, fpos_t _offset, int _whence); 82 int (*_close)(void *_cookie); 83 #else 84 int (*_read)(); 85 int (*_write)(); 86 fpos_t (*_seek)(); 87 int (*_close)(); 88 #endif 89 90 /* separate buffer for long sequences of ungetc() */ 91 struct __sbuf _ub; /* ungetc buffer */ 92 unsigned char *_up; /* saved _p when _p is doing ungetc data */ 93 int _ur; /* saved _r when _r is counting ungetc data */ 94 95 /* tricks to meet minimum requirements even when malloc() fails */ 96 unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */ 97 unsigned char _nbuf[1]; /* guarantee a getc() buffer */ 98 99 /* separate buffer for fgetline() when line crosses buffer boundary */ 100 struct __sbuf _lb; /* buffer for fgetline() */ 101 102 /* Unix stdio files get aligned to block boundaries on fseek() */ 103 int _blksize; /* stat.st_blksize (may be != _bf._size) */ 104 int _offset; /* current lseek offset */ 105 } FILE; 106 107 extern FILE __sF[]; 108 109 #define __SLBF 0x0001 /* line buffered */ 110 #define __SNBF 0x0002 /* unbuffered */ 111 #define __SRD 0x0004 /* OK to read */ 112 #define __SWR 0x0008 /* OK to write */ 113 /* RD and WR are never simultaneously asserted */ 114 #define __SRW 0x0010 /* open for reading & writing */ 115 #define __SEOF 0x0020 /* found EOF */ 116 #define __SERR 0x0040 /* found error */ 117 #define __SMBF 0x0080 /* _buf is from malloc */ 118 #define __SAPP 0x0100 /* fdopen()ed in append mode */ 119 #define __SSTR 0x0200 /* this is an sprintf/snprintf string */ 120 #define __SOPT 0x0400 /* do fseek() optimisation */ 121 #define __SNPT 0x0800 /* do not do fseek() optimisation */ 122 #define __SOFF 0x1000 /* set iff _offset is in fact correct */ 123 #define __SMOD 0x2000 /* true => fgetline modified _p text */ 124 125 /* 126 * The following three definitions are for ANSI C, which took them 127 * from System V, which brilliantly took internal interface macros and 128 * made them official arguments to setvbuf(), without renaming them. 129 * Hence, these ugly _IOxxx names are *supposed* to appear in user code. 130 * 131 * Although numbered as their counterparts above, the implementation 132 * does not rely on this. 133 */ 134 #define _IOFBF 0 /* setvbuf should set fully buffered */ 135 #define _IOLBF 1 /* setvbuf should set line buffered */ 136 #define _IONBF 2 /* setvbuf should set unbuffered */ 137 138 #define BUFSIZ 1024 /* size of buffer used by setbuf */ 139 #define EOF (-1) 140 141 /* 142 * FOPEN_MAX is a minimum maximum, and should be the number of descriptors 143 * that the kernel can provide without allocation of a resource that can 144 * fail without the process sleeping. Do not use this for anything. 145 */ 146 #define FOPEN_MAX 20 /* must be <= OPEN_MAX <sys/syslimits.h> */ 147 #define FILENAME_MAX 1024 /* must be <= PATH_MAX <sys/syslimits.h> */ 148 149 /* System V/ANSI C; this is the wrong way to do this, do *not* use these. */ 150 #ifndef _ANSI_SOURCE 151 #define P_tmpdir "/usr/tmp/" 152 #endif 153 #define L_tmpnam 1024 /* XXX must be == PATH_MAX */ 154 #define TMP_MAX 308915776 155 156 #ifndef SEEK_SET 157 #define SEEK_SET 0 /* set file offset to offset */ 158 #endif 159 #ifndef SEEK_CUR 160 #define SEEK_CUR 1 /* set file offset to current plus offset */ 161 #endif 162 #ifndef SEEK_END 163 #define SEEK_END 2 /* set file offset to EOF plus offset */ 164 #endif 165 166 #define stdin (&__sF[0]) 167 #define stdout (&__sF[1]) 168 #define stderr (&__sF[2]) 169 170 /* 171 * Functions defined in ANSI C standard. 172 */ 173 #if __STDC__ || c_plusplus 174 int remove(const char *); 175 int rename(const char *_old, const char *_new); 176 FILE *tmpfile(void); 177 char *tmpnam(char *); 178 int fclose(FILE *); 179 int fflush(FILE *); 180 FILE *fopen(const char *_name, const char *_type); 181 FILE *freopen(const char *_name, const char *_type, FILE *_stream); 182 void setbuf(FILE *, char *); 183 int setvbuf(FILE *, char *, int, size_t); 184 int fprintf(FILE *, const char *, ...); 185 int fscanf(FILE *, const char *, ...); 186 int printf(const char *, ...); 187 int scanf(const char *, ...); 188 int sprintf(char *, const char *, ...); 189 int sscanf(char *, const char *, ...); 190 int vfprintf(FILE *, const char *, _VA_LIST_); 191 int vprintf(const char *, _VA_LIST_); 192 int vsprintf(char *, const char *, _VA_LIST_); 193 int fgetc(FILE *); 194 char *fgets(char *, size_t, FILE *); 195 int fputc(int, FILE *); 196 int fputs(const char *, FILE *); 197 int getc(FILE *); 198 int getchar(void); 199 char *gets(char *); 200 int putc(int, FILE *); 201 int putchar(int); 202 int puts(const char *); 203 int ungetc(int, FILE *); 204 int fread(void *, size_t _size, size_t _n, FILE *); 205 int fwrite(const void *, size_t _size, size_t _n, FILE *); 206 int fgetpos(FILE *, fpos_t *); 207 int fseek(FILE *, long, int); 208 int fsetpos(FILE *, const fpos_t *); 209 long ftell(const FILE *); 210 void rewind(FILE *); 211 void clearerr(FILE *); 212 int feof(FILE *); 213 int ferror(FILE *); 214 void perror(const char *); 215 #else 216 int remove(); 217 int rename(); 218 FILE *tmpfile(); 219 char *tmpnam(); 220 int fclose(); 221 int fflush(); 222 FILE *fopen(); 223 FILE *freopen(); 224 void setbuf(); 225 int setvbuf(); 226 int fprintf(); 227 int fscanf(); 228 int printf(); 229 int scanf(); 230 int sprintf(); 231 int sscanf(); 232 int vfprintf(); 233 int vprintf(); 234 int vsprintf(); 235 int fgetc(); 236 char *fgets(); 237 int fputc(); 238 int fputs(); 239 int getc(); 240 int getchar(); 241 char *gets(); 242 int putc(); 243 int putchar(); 244 int puts(); 245 int ungetc(); 246 int fread(); 247 int fwrite(); 248 int fgetpos(); 249 int fseek(); 250 int fsetpos(); 251 long ftell(); 252 void rewind(); 253 void clearerr(); 254 int feof(); 255 int ferror(); 256 void perror(); 257 #endif 258 259 /* 260 * Functions defined in POSIX 1003.1. 261 */ 262 #ifndef _ANSI_SOURCE 263 #define L_cuserid 9 /* size for cuserid(); UT_NAMESIZE + 1 */ 264 #define L_ctermid 1024 /* size for ctermid(); PATH_MAX */ 265 266 #if __STDC__ || c_plusplus 267 FILE *fdopen(int, const char *); 268 int fileno(FILE *); 269 #else 270 FILE *fdopen(); 271 int fileno(); 272 #endif 273 #endif 274 275 /* 276 * Routines that are purely local. 277 */ 278 #if __STDC__ || c_plusplus 279 char *fgetline(FILE *, size_t *); 280 int fpurge(FILE *); 281 int getw(FILE *); 282 int pclose(FILE *); 283 FILE *popen(const char *_name, const char *_type); 284 int putw(int, FILE *); 285 void setbuffer(FILE *, char *, int); 286 int setlinebuf(FILE *); 287 int snprintf(char *, size_t, const char *, ...); 288 int vsnprintf(char *, size_t, const char *, _VA_LIST_); 289 #else 290 char *fgetline(); 291 int fpurge(); 292 int getw(); 293 int pclose(); 294 FILE *popen(); 295 int putw(); 296 void setbuffer(); 297 int setlinebuf(); 298 int snprintf(); 299 int vsnprintf(); 300 #endif 301 #endif /* _ANSI_SOURCE */ 302 303 #ifndef _ANSI_SOURCE 304 /* 305 * Stdio function-access interface. 306 */ 307 #if __STDC__ || c_plusplus 308 FILE *funopen(const void *_cookie, 309 int (*readfn)(void *_cookie, char *_buf, int _n), 310 int (*writefn)(void *_cookie, const char *_buf, int _n), 311 fpos_t (*seekfn)(void *_cookie, fpos_t _off, int _whence), 312 int (*closefn)(void *_cookie)); 313 #define fropen(cookie, fn) funopen(cookie, fn, 0, 0, 0) 314 #define fwopen(cookie, fn) funopen(cookie, 0, fn, 0, 0) 315 #else 316 FILE *funopen(); 317 #define fropen(cookie, fn) \ 318 funopen(cookie, fn, (int (*)())0, (fpos_t (*)())0, (int (*)())0) 319 #define fwopen(cookie, fn) \ 320 funopen(cookie, (int (*)())0, fn, (fpos_t (*)())0, (int (*)())0) 321 #endif 322 323 /* 324 * Functions internal to the implementation. 325 */ 326 #if __STDC__ || c_plusplus 327 int __srget(FILE *); 328 int __swbuf(int, FILE *); 329 #else 330 int __srget(); 331 int __swbuf(); 332 #endif 333 334 /* 335 * The __sfoo macros are here so that we can 336 * define function versions in the C library. 337 */ 338 #define __sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++)) 339 #ifdef __GNUC__ 340 static __inline int __sputc(int _c, FILE *_p) { 341 if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n')) 342 return (*_p->_p++ = _c); 343 else 344 return (__swbuf(_c, _p)); 345 } 346 #else 347 /* 348 * This has been tuned to generate reasonable code on the vax using pcc. 349 */ 350 #define __sputc(c, p) \ 351 (--(p)->_w < 0 ? \ 352 (p)->_w >= (p)->_lbfsize ? \ 353 (*(p)->_p = (c)), *(p)->_p != '\n' ? \ 354 (int)*(p)->_p++ : \ 355 __swbuf('\n', p) : \ 356 __swbuf((int)(c), p) : \ 357 (*(p)->_p = (c), (int)*(p)->_p++)) 358 #endif 359 360 #define __sfeof(p) (((p)->_flags & __SEOF) != 0) 361 #define __sferror(p) (((p)->_flags & __SERR) != 0) 362 #define __sclearerr(p) ((void)((p)->_flags &= ~(__SERR|__SEOF))) 363 #define __sfileno(p) ((p)->_file) 364 365 #define feof(p) __sfeof(p) 366 #define ferror(p) __sferror(p) 367 #define clearerr(p) __sclearerr(p) 368 369 #ifndef _ANSI_SOURCE 370 #define fileno(p) __sfileno(p) 371 #endif 372 373 #ifndef lint 374 #define getc(fp) __sgetc(fp) 375 #define putc(x, fp) __sputc(x, fp) 376 #endif /* lint */ 377 378 #define getchar() getc(stdin) 379 #define putchar(x) putc(x, stdout) 380 #endif /* _STDIO_H_ */ 381