1 /* ProcFS - buf.c - output buffer management for read calls */
7 static size_t left
, used
;
11 * Initialize the buffer for fresh use. The output is to be stored into 'ptr'
12 * which is BUF_SIZE bytes in size, since that is the size we requested. Due
13 * to the way vsnprintf works, we cannot use the last byte of this buffer. The
14 * first 'start' bytes of the produced output are to be skipped. After that, a
15 * total of 'len' bytes are requested.
18 buf_init(char * ptr
, size_t len
, off_t start
)
23 left
= MIN(len
, BUF_SIZE
- 1);
28 * Add formatted text to the end of the buffer.
31 buf_printf(char * fmt
, ...)
40 * There is no way to estimate how much space the result will take, so
41 * we need to produce the string even when skipping part of the start.
42 * The null terminating character is not part of the result, so room
43 * must be given for it to be stored after completely filling up the
44 * requested part of the buffer.
46 max
= MIN(skip
+ left
+ 1, BUF_SIZE
);
49 len
= vsnprintf(&buf
[used
], max
, fmt
, args
);
53 * The snprintf family returns the number of bytes that would be stored
54 * if the buffer were large enough, excluding the null terminator.
68 memmove(buf
, &buf
[skip
], len
- skip
);
76 assert((ssize_t
) left
>= 0);
78 if (len
> (ssize_t
)left
)
86 * Add arbitrary data to the end of the buffer.
89 buf_append(char * data
, size_t len
)
96 if (skip
>= (ssize_t
)len
) {
110 memcpy(&buf
[used
], data
, len
);
117 * Return the resulting number of bytes produced, not counting the trailing
118 * null character in the buffer.