arm: protect state after signal handler
[minix.git] / servers / devman / buf.c
bloba6af6aa47266f0469ba8aa7d4693de8e7a5028e4
1 /* buf.c - by Alen Stojanov and David van Moolenbroek, taken from procfs */
2 #define _POSIX_SOURCE 1 /* tell headers to include POSIX stuff */
3 #define _MINIX 1 /* tell headers to include MINIX stuff */
4 #define _SYSTEM 1 /* tell headers that this is the kernel */
5 #define DEVMAN_SERVER 1
7 #include <minix/config.h>
8 #include <errno.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <lib.h>
14 #include <timers.h>
16 #include <minix/callnr.h>
17 #include <minix/type.h>
18 #include <minix/const.h>
19 #include <minix/com.h>
20 #include <minix/syslib.h>
21 #include <minix/sysutil.h>
22 #include <minix/vfsif.h>
23 #include <minix/endpoint.h>
24 #include <minix/sysinfo.h>
25 #include <minix/u64.h>
26 #include <minix/sysinfo.h>
27 #include <minix/type.h>
28 #include <minix/ipc.h>
30 #include <sys/time.h>
31 #include <sys/times.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
35 #include <minix/vtreefs.h>
37 #include <minix/devman.h>
40 #include <stdarg.h>
41 #include <assert.h>
42 #include <string.h>
43 #define BUF_SIZE 4096
45 static char buf[BUF_SIZE + 1];
46 static size_t off, left, used;
47 static off_t skip;
49 /*===========================================================================*
50 * buf_init *
51 *===========================================================================*/
52 void buf_init(off_t start, size_t len)
54 /* Initialize the buffer for fresh use. The first 'start' bytes of the
55 * produced output are to be skipped. After that, up to a total of
56 * 'len' bytes are requested.
59 skip = start;
60 left = MIN(len, BUF_SIZE);
61 off = 0;
62 used = 0;
65 /*===========================================================================*
66 * buf_printf *
67 *===========================================================================*/
68 void buf_printf(char *fmt, ...)
70 /* Add formatted text to the end of the buffer.
72 va_list args;
73 ssize_t len, max;
75 if (left == 0)
76 return;
78 /* There is no way to estimate how much space the result will take, so
79 * we need to produce the string even when skipping part of the start.
80 * If part of the result is to be skipped, do not memcpy; instead, save
81 * the offset of where the result starts within the buffer.
83 * The null terminating character is not part of the result, so room
84 * must be given for it to be stored after completely filling up the
85 * requested part of the buffer.
87 max = MIN(skip + left, BUF_SIZE);
89 va_start(args, fmt);
90 len = vsnprintf(&buf[off + used], max + 1, fmt, args);
91 va_end(args);
93 if (skip > 0) {
94 assert(off == 0);
95 assert(used == 0);
97 if (skip >= len) {
98 skip -= len;
100 return;
103 off = skip;
104 if (left > BUF_SIZE - off)
105 left = BUF_SIZE - off;
106 len -= off;
107 skip = 0;
110 assert(skip == 0);
111 assert(len >= 0);
112 assert((long) left >= 0);
114 if (len > (ssize_t) left)
115 len = left;
117 used += len;
118 left -= len;
121 /*===========================================================================*
122 * buf_append *
123 *===========================================================================*/
124 void buf_append(char *data, size_t len)
126 /* Add arbitrary data to the end of the buffer.
129 if (left == 0)
130 return;
132 if (skip > 0) {
133 if (skip >= (ssize_t) len) {
134 skip -= len;
136 return;
139 data += skip;
140 len -= skip;
141 skip = 0;
144 if (len > left)
145 len = left;
147 memcpy(&buf[off + used], data, len);
149 used += len;
150 left -= len;
153 /*===========================================================================*
154 * buf_get *
155 *===========================================================================*/
156 size_t buf_get(char **ptr)
158 /* Return the buffer's starting address and the length of the used
159 * part, not counting the trailing null character for the latter.
162 *ptr = &buf[off];
164 return used;