1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright © 2022 Keith Packard
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above
14  *    copyright notice, this list of conditions and the following
15  *    disclaimer in the documentation and/or other materials provided
16  *    with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33  * OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #ifndef _STDIO_BUFIO_H_
37 #define _STDIO_BUFIO_H_
38 
39 #include <stdio.h>
40 #include <sys/types.h>
41 #include <sys/lock.h>
42 
43 #define __BALL  0x0001          /* bufio buf is allocated by stdio */
44 #define __BLBF  0x0002          /* bufio is line buffered */
45 
46 struct __file_bufio {
47         struct __file_ext xfile;
48 	int	fd;
49         uint8_t dir;
50         uint8_t bflags;
51         __off_t pos;    /* FD position */
52 	char	*buf;
53         int     size;   /* sizeof buf */
54 	int	len;    /* valid data in buf */
55 	int	off;    /* offset of data in buf */
56         ssize_t (*read)(int fd, void *buf, size_t count);
57         ssize_t (*write)(int fd, const void *buf, size_t count);
58         __off_t (*lseek)(int fd, __off_t offset, int whence);
59         int     (*close)(int fd);
60 #ifndef __SINGLE_THREAD__
61 	_LOCK_T lock;
62 #endif
63 };
64 
65 #define FDEV_SETUP_BUFIO(_fd, _buf, _size, _read, _write, _lseek, _close, _rwflag, _bflags) \
66         {                                                               \
67                 .xfile = FDEV_SETUP_EXT(__bufio_put, __bufio_get,       \
68                                         __bufio_flush, __bufio_close,   \
69                                         __bufio_seek, __bufio_setvbuf,  \
70                                         (_rwflag) | __SBUF),            \
71                 .fd = _fd,                                              \
72                 .dir = 0,                                               \
73                 .bflags = (_bflags),                                    \
74                 .pos = 0,                                               \
75                 .buf = _buf,                                            \
76                 .size = _size,                                          \
77                 .len = 0,                                               \
78                 .off = 0,                                               \
79                 .read = _read,                                          \
80                 .write = _write,                                        \
81                 .lseek = _lseek,                                        \
82                 .close = _close,                                        \
83         }
84 
__bufio_lock_init(FILE * f)85 static inline void __bufio_lock_init(FILE *f) {
86 	(void) f;
87 	__lock_init(((struct __file_bufio *) f)->lock);
88 }
89 
__bufio_lock_close(FILE * f)90 static inline void __bufio_lock_close(FILE *f) {
91 	(void) f;
92         __lock_release(((struct __file_bufio *) f)->lock);
93 	__lock_close(((struct __file_bufio *) f)->lock);
94 }
95 
__bufio_lock(FILE * f)96 static inline void __bufio_lock(FILE *f) {
97 	(void) f;
98 	__lock_acquire(((struct __file_bufio *) f)->lock);
99 }
100 
__bufio_unlock(FILE * f)101 static inline void __bufio_unlock(FILE *f) {
102 	(void) f;
103 	__lock_release(((struct __file_bufio *) f)->lock);
104 }
105 
106 int
107 __bufio_flush_locked(FILE *f);
108 
109 int
110 __bufio_fill_locked(FILE *f);
111 
112 int
113 __bufio_setdir_locked(FILE *f, uint8_t dir);
114 
115 int
116 __bufio_flush(FILE *f);
117 
118 int
119 __bufio_put(char c, FILE *f);
120 
121 int
122 __bufio_get(FILE *f);
123 
124 off_t
125 __bufio_seek(FILE *f, off_t offset, int whence);
126 
127 int
128 __bufio_setvbuf(FILE *f, char *buf, int mode, size_t size);
129 
130 int
131 __bufio_close(FILE *f);
132 
133 #endif /* _STDIO_BUFIO_H_ */
134