xref: /dragonfly/lib/libc/stdio/priv_stdio.h (revision 49781055)
1 /*-
2  * Copyright (c) 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 2005 Joerg Sonnenberger <joerg@bec.de>.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Chris Torek.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $DragonFly: src/lib/libc/stdio/priv_stdio.h,v 1.3 2005/08/02 00:44:39 joerg Exp $
34  */
35 
36 #ifndef _LIBC_PRIV_STDIO_H_
37 #define _LIBC_PRIV_STDIO_H_
38 
39 #include <pthread.h>
40 
41 #include "wcio.h"
42 
43 /* stdio buffers */
44 struct __sbuf {
45 	unsigned char *_base;
46 	int	_size;
47 };
48 
49 /*
50  * _ub, _up, and _ur are used when ungetc() pushes back more characters
51  * than fit in the current _bf, or when ungetc() pushes back a character
52  * that does not match the previous one in _bf.  When this happens,
53  * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff
54  * _ub._base!=NULL) and _up and _ur save the current values of _p and _r.
55  */
56 
57 struct __FILE {
58 	struct __FILE_public	pub;
59 	struct	__sbuf _bf;	/* the buffer (at least 1 byte, if !NULL) */
60 
61 	/* operations */
62 	void	*_cookie;	/* cookie passed to io functions */
63 	int	(*_close) (void *);
64 	int	(*_read)  (void *, char *, int);
65 	fpos_t	(*_seek)  (void *, fpos_t, int);
66 	int	(*_write) (void *, const char *, int);
67 
68 	/* separate buffer for long sequences of ungetc() */
69 	struct	__sbuf _ub;	/* ungetc buffer */
70 	int	_ur;		/* saved _r when _r is counting ungetc data */
71 
72 	/* tricks to meet minimum requirements even when malloc() fails */
73 	unsigned char _ubuf[3];	/* guarantee an ungetc() buffer */
74 	unsigned char _nbuf[1];	/* guarantee a getc() buffer */
75 
76 	/* separate buffer for fgetln() when line crosses buffer boundary */
77 	struct	__sbuf _lb;	/* buffer for fgetln() */
78 
79 	/* Unix stdio files get aligned to block boundaries on fseek() */
80 	int	_blksize;	/* stat.st_blksize (may be != _bf._size) */
81 	fpos_t	_offset;	/* current lseek offset (see WARNING) */
82 
83 	unsigned char	*_up;	/* saved _p when _p is doing ungetc data */
84 	pthread_mutex_t	fl_mutex;	/* used for MT-safety */
85 	pthread_t	fl_owner;	/* current owner */
86 	int		fl_count;	/* recursive lock count */
87 
88 	struct wchar_io_data _wcio;
89 };
90 
91 /*
92  * I/O descriptors for __sfvwrite().
93  */
94 struct __siov {
95 	void	*iov_base;
96 	size_t	iov_len;
97 };
98 struct __suio {
99 	struct	__siov *uio_iov;
100 	int	uio_iovcnt;
101 	int	uio_resid;
102 };
103 
104 /*
105  * The first few FILEs are statically allocated; others are dynamically
106  * allocated and linked in via this glue structure.
107  */
108 struct glue {
109 	struct	glue *next;
110 	int	niobs;
111 	FILE	*iobs;
112 };
113 
114 extern struct glue __sglue;
115 
116 __BEGIN_DECLS
117 int	__sfvwrite(FILE *, struct __suio *);
118 int	__fflush(FILE *);
119 int	__ungetc(int, FILE *);
120 __END_DECLS
121 
122 #endif /* _LIBC_PRIV_STDIO_H_ */
123