Blame snprintf/various.h

Packit c04fcb
/*-
Packit c04fcb
 * Copyright (c) 1990 The Regents of the University of California.
Packit c04fcb
 * All rights reserved.
Packit c04fcb
 *
Packit c04fcb
 * This code is derived from software contributed to Berkeley by
Packit c04fcb
 * Chris Torek.
Packit c04fcb
 *
Packit c04fcb
 * Redistribution and use in source and binary forms, with or without
Packit c04fcb
 * modification, are permitted provided that the following conditions
Packit c04fcb
 * are met:
Packit c04fcb
 * 1. Redistributions of source code must retain the above copyright
Packit c04fcb
 *    notice, this list of conditions and the following disclaimer.
Packit c04fcb
 * 2. Redistributions in binary form must reproduce the above copyright
Packit c04fcb
 *    notice, this list of conditions and the following disclaimer in the
Packit c04fcb
 *    documentation and/or other materials provided with the distribution.
Packit c04fcb
 * 3. Neither the name of the University nor the names of its contributors
Packit c04fcb
 *    may be used to endorse or promote products derived from this software
Packit c04fcb
 *    without specific prior written permission.
Packit c04fcb
 *
Packit c04fcb
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit c04fcb
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit c04fcb
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit c04fcb
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit c04fcb
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit c04fcb
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit c04fcb
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit c04fcb
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit c04fcb
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit c04fcb
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit c04fcb
 * SUCH DAMAGE.
Packit c04fcb
 */
Packit c04fcb
Packit c04fcb
#ifndef UST_SNPRINTF_VARIOUS_H
Packit c04fcb
#define UST_SNPRINTF_VARIOUS_H
Packit c04fcb
Packit c04fcb
#include <stdarg.h>
Packit c04fcb
Packit c04fcb
struct __lttng_ust_sbuf {
Packit c04fcb
        unsigned char *_base;
Packit c04fcb
        int     _size;
Packit c04fcb
};
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * stdio state variables.
Packit c04fcb
 *
Packit c04fcb
 * The following always hold:
Packit c04fcb
 *
Packit c04fcb
 *      if (_flags&(__SLBF|__SWR)) == (__SLBF|__SWR),
Packit c04fcb
 *              _lbfsize is -_bf._size, else _lbfsize is 0
Packit c04fcb
 *      if _flags&__SRD, _w is 0
Packit c04fcb
 *      if _flags&__SWR, _r is 0
Packit c04fcb
 *
Packit c04fcb
 * This ensures that the getc and putc macros (or inline functions) never
Packit c04fcb
 * try to write or read from a file that is in `read' or `write' mode.
Packit c04fcb
 * (Moreover, they can, and do, automatically switch from read mode to
Packit c04fcb
 * write mode, and back, on "r+" and "w+" files.)
Packit c04fcb
 *
Packit c04fcb
 * _lbfsize is used only to make the inline line-buffered output stream
Packit c04fcb
 * code as compact as possible.
Packit c04fcb
 *
Packit c04fcb
 * _ub, _up, and _ur are used when ungetc() pushes back more characters
Packit c04fcb
 * than fit in the current _bf, or when ungetc() pushes back a character
Packit c04fcb
 * that does not match the previous one in _bf.  When this happens,
Packit c04fcb
 * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
Packit c04fcb
 * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
Packit c04fcb
 */
Packit c04fcb
typedef struct __lttng_ust_sFILE {
Packit c04fcb
        unsigned char *_p;      /* current position in (some) buffer */
Packit c04fcb
        int     _r;             /* read space left for getc() */
Packit c04fcb
        int     _w;             /* write space left for putc() */
Packit c04fcb
        short   _flags;         /* flags, below; this FILE is free if 0 */
Packit c04fcb
        short   _file;          /* fileno, if Unix descriptor, else -1 */
Packit c04fcb
        struct  __lttng_ust_sbuf _bf;     /* the buffer (at least 1 byte, if !NULL) */
Packit c04fcb
        int     _lbfsize;       /* 0 or -_bf._size, for inline putc */
Packit c04fcb
Packit c04fcb
        /* operations */
Packit c04fcb
        void    *_cookie;       /* cookie passed to io functions */
Packit c04fcb
        int     (*_close)(void *);
Packit c04fcb
        int     (*_read)(void *, char *, int);
Packit c04fcb
        fpos_t  (*_seek)(void *, fpos_t, int);
Packit c04fcb
        int     (*_write)(void *, const char *, int);
Packit c04fcb
Packit c04fcb
        /* extension data, to avoid further ABI breakage */
Packit c04fcb
        struct  __lttng_ust_sbuf _ext;
Packit c04fcb
        /* data for long sequences of ungetc() */
Packit c04fcb
        unsigned char *_up;     /* saved _p when _p is doing ungetc data */
Packit c04fcb
        int     _ur;            /* saved _r when _r is counting ungetc data */
Packit c04fcb
Packit c04fcb
        /* tricks to meet minimum requirements even when malloc() fails */
Packit c04fcb
        unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */
Packit c04fcb
        unsigned char _nbuf[1]; /* guarantee a getc() buffer */
Packit c04fcb
Packit c04fcb
        /* separate buffer for fgetln() when line crosses buffer boundary */
Packit c04fcb
        struct  __lttng_ust_sbuf _lb;     /* buffer for fgetln() */
Packit c04fcb
Packit c04fcb
        /* Unix stdio files get aligned to block boundaries on fseek() */
Packit c04fcb
        int     _blksize;       /* stat.st_blksize (may be != _bf._size) */
Packit c04fcb
        fpos_t  _offset;        /* current lseek offset */
Packit c04fcb
} LTTNG_UST_LFILE;
Packit c04fcb
Packit c04fcb
#define __SLBF  0x0001          /* line buffered */
Packit c04fcb
#define __SNBF  0x0002          /* unbuffered */
Packit c04fcb
#define __SRD   0x0004          /* OK to read */
Packit c04fcb
#define __SWR   0x0008          /* OK to write */
Packit c04fcb
        /* RD and WR are never simultaneously asserted */
Packit c04fcb
#define __SRW   0x0010          /* open for reading & writing */
Packit c04fcb
#define __SEOF  0x0020          /* found EOF */
Packit c04fcb
#define __SERR  0x0040          /* found error */
Packit c04fcb
#define __SMBF  0x0080          /* _buf is from malloc */
Packit c04fcb
#define __SAPP  0x0100          /* fdopen()ed in append mode */
Packit c04fcb
#define __SSTR  0x0200          /* this is an sprintf/snprintf string */
Packit c04fcb
#define __SOPT  0x0400          /* do fseek() optimisation */
Packit c04fcb
#define __SNPT  0x0800          /* do not do fseek() optimisation */
Packit c04fcb
#define __SOFF  0x1000          /* set iff _offset is in fact correct */
Packit c04fcb
#define __SMOD  0x2000          /* true => fgetln modified _p text */
Packit c04fcb
#define __SALC  0x4000          /* allocate string space dynamically */
Packit c04fcb
Packit c04fcb
#define __sferror(p)    (((p)->_flags & __SERR) != 0)
Packit c04fcb
Packit c04fcb
extern int ust_safe_fflush(LTTNG_UST_LFILE *fp);
Packit c04fcb
extern int ust_safe_vfprintf(LTTNG_UST_LFILE *fp, const char *fmt0, va_list ap);
Packit c04fcb
Packit c04fcb
extern size_t ust_safe_mbrtowc(wchar_t *pwc, const char *s, size_t n, mbstate_t *ps);
Packit c04fcb
Packit c04fcb
#endif /* UST_SNPRINTF_VARIOUS_H */