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