Commit | Line | Data |
---|---|---|
a09dac63 PMF |
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 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * 1. Redistributions of source code must retain the above copyright | |
12 | * notice, this list of conditions and the following disclaimer. | |
13 | * 2. Redistributions in binary form must reproduce the above copyright | |
14 | * notice, this list of conditions and the following disclaimer in the | |
15 | * documentation and/or other materials provided with the distribution. | |
16 | * 3. Neither the name of the University nor the names of its contributors | |
17 | * may be used to endorse or promote products derived from this software | |
18 | * without specific prior written permission. | |
19 | * | |
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
30 | * SUCH DAMAGE. | |
31 | */ | |
32 | ||
bf0d695d PMF |
33 | #ifndef UST_SNPRINTF_VARIOUS_H |
34 | #define UST_SNPRINTF_VARIOUS_H | |
35 | ||
36 | #include <stdarg.h> | |
37 | ||
18d0b33a | 38 | struct __lttng_ust_sbuf { |
bf0d695d PMF |
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 | */ | |
18d0b33a | 67 | typedef struct __lttng_ust_sFILE { |
bf0d695d PMF |
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 */ | |
18d0b33a | 73 | struct __lttng_ust_sbuf _bf; /* the buffer (at least 1 byte, if !NULL) */ |
bf0d695d PMF |
74 | int _lbfsize; /* 0 or -_bf._size, for inline putc */ |
75 | ||
76 | /* operations */ | |
77 | void *_cookie; /* cookie passed to io functions */ | |
78 | int (*_close)(void *); | |
79 | int (*_read)(void *, char *, int); | |
80 | fpos_t (*_seek)(void *, fpos_t, int); | |
81 | int (*_write)(void *, const char *, int); | |
82 | ||
83 | /* extension data, to avoid further ABI breakage */ | |
18d0b33a | 84 | struct __lttng_ust_sbuf _ext; |
bf0d695d PMF |
85 | /* data for long sequences of ungetc() */ |
86 | unsigned char *_up; /* saved _p when _p is doing ungetc data */ | |
87 | int _ur; /* saved _r when _r is counting ungetc data */ | |
88 | ||
89 | /* tricks to meet minimum requirements even when malloc() fails */ | |
90 | unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */ | |
91 | unsigned char _nbuf[1]; /* guarantee a getc() buffer */ | |
92 | ||
93 | /* separate buffer for fgetln() when line crosses buffer boundary */ | |
18d0b33a | 94 | struct __lttng_ust_sbuf _lb; /* buffer for fgetln() */ |
bf0d695d PMF |
95 | |
96 | /* Unix stdio files get aligned to block boundaries on fseek() */ | |
97 | int _blksize; /* stat.st_blksize (may be != _bf._size) */ | |
98 | fpos_t _offset; /* current lseek offset */ | |
18d0b33a | 99 | } LTTNG_UST_LFILE; |
bf0d695d PMF |
100 | |
101 | #define __SLBF 0x0001 /* line buffered */ | |
102 | #define __SNBF 0x0002 /* unbuffered */ | |
103 | #define __SRD 0x0004 /* OK to read */ | |
104 | #define __SWR 0x0008 /* OK to write */ | |
105 | /* RD and WR are never simultaneously asserted */ | |
106 | #define __SRW 0x0010 /* open for reading & writing */ | |
107 | #define __SEOF 0x0020 /* found EOF */ | |
108 | #define __SERR 0x0040 /* found error */ | |
109 | #define __SMBF 0x0080 /* _buf is from malloc */ | |
110 | #define __SAPP 0x0100 /* fdopen()ed in append mode */ | |
111 | #define __SSTR 0x0200 /* this is an sprintf/snprintf string */ | |
112 | #define __SOPT 0x0400 /* do fseek() optimisation */ | |
113 | #define __SNPT 0x0800 /* do not do fseek() optimisation */ | |
114 | #define __SOFF 0x1000 /* set iff _offset is in fact correct */ | |
115 | #define __SMOD 0x2000 /* true => fgetln modified _p text */ | |
116 | #define __SALC 0x4000 /* allocate string space dynamically */ | |
117 | ||
118 | #define __sferror(p) (((p)->_flags & __SERR) != 0) | |
119 | ||
18d0b33a MD |
120 | extern int ust_safe_fflush(LTTNG_UST_LFILE *fp); |
121 | extern int ust_safe_vfprintf(LTTNG_UST_LFILE *fp, const char *fmt0, va_list ap); | |
bf0d695d | 122 | |
e5bc3b0f PMF |
123 | extern size_t ust_safe_mbrtowc(wchar_t *pwc, const char *s, size_t n, mbstate_t *ps); |
124 | ||
bf0d695d | 125 | #endif /* UST_SNPRINTF_VARIOUS_H */ |