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>
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>
31 #include <sys/times.h>
32 #include <sys/types.h>
35 #include <minix/vtreefs.h>
37 #include <minix/devman.h>
45 static char buf
[BUF_SIZE
+ 1];
46 static size_t off
, left
, used
;
49 /*===========================================================================*
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.
60 left
= MIN(len
, BUF_SIZE
);
65 /*===========================================================================*
67 *===========================================================================*/
68 void buf_printf(char *fmt
, ...)
70 /* Add formatted text to the end of the buffer.
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
);
90 len
= vsnprintf(&buf
[off
+ used
], max
+ 1, fmt
, args
);
104 if (left
> BUF_SIZE
- off
)
105 left
= BUF_SIZE
- off
;
112 assert((long) left
>= 0);
114 if (len
> (ssize_t
) left
)
121 /*===========================================================================*
123 *===========================================================================*/
124 void buf_append(char *data
, size_t len
)
126 /* Add arbitrary data to the end of the buffer.
133 if (skip
>= (ssize_t
) len
) {
147 memcpy(&buf
[off
+ used
], data
, len
);
153 /*===========================================================================*
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.