1 /* ProcFS - buf.c - by Alen Stojanov and David van Moolenbroek */
8 static char buf
[BUF_SIZE
+ 1];
9 static size_t off
, left
, used
;
12 /*===========================================================================*
14 *===========================================================================*/
15 void buf_init(off_t start
, size_t len
)
17 /* Initialize the buffer for fresh use. The first 'start' bytes of the
18 * produced output are to be skipped. After that, up to a total of
19 * 'len' bytes are requested.
23 left
= MIN(len
, BUF_SIZE
);
28 /*===========================================================================*
30 *===========================================================================*/
31 void buf_printf(char *fmt
, ...)
33 /* Add formatted text to the end of the buffer.
41 /* There is no way to estimate how much space the result will take, so
42 * we need to produce the string even when skipping part of the start.
43 * If part of the result is to be skipped, do not memcpy; instead, save
44 * the offset of where the result starts within the buffer.
46 * The null terminating character is not part of the result, so room
47 * must be given for it to be stored after completely filling up the
48 * requested part of the buffer.
50 max
= MIN(skip
+ left
, BUF_SIZE
);
53 len
= vsnprintf(&buf
[off
+ used
], max
+ 1, fmt
, args
);
67 if (left
> BUF_SIZE
- off
)
68 left
= BUF_SIZE
- off
;
75 assert((long) left
>= 0);
77 if (len
> (ssize_t
) left
)
84 /*===========================================================================*
86 *===========================================================================*/
87 void buf_append(char *data
, size_t len
)
89 /* Add arbitrary data to the end of the buffer.
96 if (skip
>= (ssize_t
) len
) {
110 memcpy(&buf
[off
+ used
], data
, len
);
116 /*===========================================================================*
118 *===========================================================================*/
119 size_t buf_get(char **ptr
)
121 /* Return the buffer's starting address and the length of the used
122 * part, not counting the trailing null character for the latter.