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 #define MIN(x,y) (x<y?x:y)
51 /*===========================================================================*
53 *===========================================================================*/
54 void buf_init(off_t start
, size_t len
)
56 /* Initialize the buffer for fresh use. The first 'start' bytes of the
57 * produced output are to be skipped. After that, up to a total of
58 * 'len' bytes are requested.
62 left
= MIN(len
, BUF_SIZE
);
67 /*===========================================================================*
69 *===========================================================================*/
70 void buf_printf(char *fmt
, ...)
72 /* Add formatted text to the end of the buffer.
80 /* There is no way to estimate how much space the result will take, so
81 * we need to produce the string even when skipping part of the start.
82 * If part of the result is to be skipped, do not memcpy; instead, save
83 * the offset of where the result starts within the buffer.
85 * The null terminating character is not part of the result, so room
86 * must be given for it to be stored after completely filling up the
87 * requested part of the buffer.
89 max
= MIN(skip
+ left
, BUF_SIZE
);
92 len
= vsnprintf(&buf
[off
+ used
], max
+ 1, fmt
, args
);
106 if (left
> BUF_SIZE
- off
)
107 left
= BUF_SIZE
- off
;
114 assert((long) left
>= 0);
116 if (len
> (ssize_t
) left
)
123 /*===========================================================================*
125 *===========================================================================*/
126 void buf_append(char *data
, size_t len
)
128 /* Add arbitrary data to the end of the buffer.
135 if (skip
>= (ssize_t
) len
) {
149 memcpy(&buf
[off
+ used
], data
, len
);
155 /*===========================================================================*
157 *===========================================================================*/
158 size_t buf_get(char **ptr
)
160 /* Return the buffer's starting address and the length of the used
161 * part, not counting the trailing null character for the latter.