Move to kernel style SPDX license identifiers
[lttng-ust.git] / snprintf / various.h
CommitLineData
c0c0989a
MJ
1/*
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (C) 1990 The Regents of the University of California.
a09dac63
PMF
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek.
a09dac63
PMF
9 */
10
bf0d695d
PMF
11#ifndef UST_SNPRINTF_VARIOUS_H
12#define UST_SNPRINTF_VARIOUS_H
13
14#include <stdarg.h>
b4051ad8 15#include <stddef.h>
2786d9d5 16#include <stdio.h>
9f18918b 17#include <wchar.h>
bf0d695d 18
002e1fde 19struct __lttng_ust_sbuf {
bf0d695d
PMF
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 */
002e1fde 48typedef struct __lttng_ust_sFILE {
bf0d695d
PMF
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 */
002e1fde 54 struct __lttng_ust_sbuf _bf; /* the buffer (at least 1 byte, if !NULL) */
bf0d695d
PMF
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 */
002e1fde 65 struct __lttng_ust_sbuf _ext;
bf0d695d
PMF
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 */
002e1fde 75 struct __lttng_ust_sbuf _lb; /* buffer for fgetln() */
bf0d695d
PMF
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 */
002e1fde 80} LTTNG_UST_LFILE;
bf0d695d
PMF
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
002e1fde
MD
101extern int ust_safe_fflush(LTTNG_UST_LFILE *fp);
102extern int ust_safe_vfprintf(LTTNG_UST_LFILE *fp, const char *fmt0, va_list ap);
bf0d695d 103
e5bc3b0f
PMF
104extern size_t ust_safe_mbrtowc(wchar_t *pwc, const char *s, size_t n, mbstate_t *ps);
105
bf0d695d 106#endif /* UST_SNPRINTF_VARIOUS_H */
This page took 0.033402 seconds and 4 git commands to generate.