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