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