| 1 | #ifndef UST_SNPRINTF_VARIOUS_H |
| 2 | #define UST_SNPRINTF_VARIOUS_H |
| 3 | |
| 4 | #include <stdarg.h> |
| 5 | |
| 6 | struct __sbuf { |
| 7 | unsigned char *_base; |
| 8 | int _size; |
| 9 | }; |
| 10 | |
| 11 | /* |
| 12 | * stdio state variables. |
| 13 | * |
| 14 | * The following always hold: |
| 15 | * |
| 16 | * if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR), |
| 17 | * _lbfsize is -_bf._size, else _lbfsize is 0 |
| 18 | * if _flags&__SRD, _w is 0 |
| 19 | * if _flags&__SWR, _r is 0 |
| 20 | * |
| 21 | * This ensures that the getc and putc macros (or inline functions) never |
| 22 | * try to write or read from a file that is in `read' or `write' mode. |
| 23 | * (Moreover, they can, and do, automatically switch from read mode to |
| 24 | * write mode, and back, on "r+" and "w+" files.) |
| 25 | * |
| 26 | * _lbfsize is used only to make the inline line-buffered output stream |
| 27 | * code as compact as possible. |
| 28 | * |
| 29 | * _ub, _up, and _ur are used when ungetc() pushes back more characters |
| 30 | * than fit in the current _bf, or when ungetc() pushes back a character |
| 31 | * that does not match the previous one in _bf. When this happens, |
| 32 | * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff |
| 33 | * _ub._base!=NULL) and _up and _ur save the current values of _p and _r. |
| 34 | */ |
| 35 | typedef struct __sFILE { |
| 36 | unsigned char *_p; /* current position in (some) buffer */ |
| 37 | int _r; /* read space left for getc() */ |
| 38 | int _w; /* write space left for putc() */ |
| 39 | short _flags; /* flags, below; this FILE is free if 0 */ |
| 40 | short _file; /* fileno, if Unix descriptor, else -1 */ |
| 41 | struct __sbuf _bf; /* the buffer (at least 1 byte, if !NULL) */ |
| 42 | int _lbfsize; /* 0 or -_bf._size, for inline putc */ |
| 43 | |
| 44 | /* operations */ |
| 45 | void *_cookie; /* cookie passed to io functions */ |
| 46 | int (*_close)(void *); |
| 47 | int (*_read)(void *, char *, int); |
| 48 | fpos_t (*_seek)(void *, fpos_t, int); |
| 49 | int (*_write)(void *, const char *, int); |
| 50 | |
| 51 | /* extension data, to avoid further ABI breakage */ |
| 52 | struct __sbuf _ext; |
| 53 | /* data for long sequences of ungetc() */ |
| 54 | unsigned char *_up; /* saved _p when _p is doing ungetc data */ |
| 55 | int _ur; /* saved _r when _r is counting ungetc data */ |
| 56 | |
| 57 | /* tricks to meet minimum requirements even when malloc() fails */ |
| 58 | unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */ |
| 59 | unsigned char _nbuf[1]; /* guarantee a getc() buffer */ |
| 60 | |
| 61 | /* separate buffer for fgetln() when line crosses buffer boundary */ |
| 62 | struct __sbuf _lb; /* buffer for fgetln() */ |
| 63 | |
| 64 | /* Unix stdio files get aligned to block boundaries on fseek() */ |
| 65 | int _blksize; /* stat.st_blksize (may be != _bf._size) */ |
| 66 | fpos_t _offset; /* current lseek offset */ |
| 67 | } LFILE; |
| 68 | |
| 69 | #define __SLBF 0x0001 /* line buffered */ |
| 70 | #define __SNBF 0x0002 /* unbuffered */ |
| 71 | #define __SRD 0x0004 /* OK to read */ |
| 72 | #define __SWR 0x0008 /* OK to write */ |
| 73 | /* RD and WR are never simultaneously asserted */ |
| 74 | #define __SRW 0x0010 /* open for reading & writing */ |
| 75 | #define __SEOF 0x0020 /* found EOF */ |
| 76 | #define __SERR 0x0040 /* found error */ |
| 77 | #define __SMBF 0x0080 /* _buf is from malloc */ |
| 78 | #define __SAPP 0x0100 /* fdopen()ed in append mode */ |
| 79 | #define __SSTR 0x0200 /* this is an sprintf/snprintf string */ |
| 80 | #define __SOPT 0x0400 /* do fseek() optimisation */ |
| 81 | #define __SNPT 0x0800 /* do not do fseek() optimisation */ |
| 82 | #define __SOFF 0x1000 /* set iff _offset is in fact correct */ |
| 83 | #define __SMOD 0x2000 /* true => fgetln modified _p text */ |
| 84 | #define __SALC 0x4000 /* allocate string space dynamically */ |
| 85 | |
| 86 | #define __sferror(p) (((p)->_flags & __SERR) != 0) |
| 87 | |
| 88 | extern int ust_safe_fflush(LFILE *fp); |
| 89 | extern int ust_safe_vfprintf(LFILE *fp, const char *fmt0, va_list ap); |
| 90 | |
| 91 | extern size_t ust_safe_mbrtowc(wchar_t *pwc, const char *s, size_t n, mbstate_t *ps); |
| 92 | |
| 93 | #endif /* UST_SNPRINTF_VARIOUS_H */ |