Repository navigation
Expand file tree
/
Copy pathbuf.c
More file actions
128 lines (101 loc) · 2.82 KB
/
Copy pathbuf.c
File metadata and controls
128 lines (101 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
8000
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* ProcFS - buf.c - by Alen Stojanov and David van Moolenbroek */
#include "inc.h"
#include <stdarg.h>
#define BUF_SIZE 4096
static char buf[BUF_SIZE + 1];
static size_t off, left, used;
static off_t skip;
/*===========================================================================*
* buf_init *
*===========================================================================*/
void buf_init(off_t start, size_t len)
{
/* Initialize the buffer for fresh use. The first 'start' bytes of the
* produced output are to be skipped. After that, up to a total of
* 'len' bytes are requested.
*/
skip = start;
left = MIN(len, BUF_SIZE);
off = 0;
used = 0;
}
/*===========================================================================*
* buf_printf *
*===========================================================================*/
void buf_printf(char *fmt, ...)
{
/* Add formatted text to the end of the buffer.
*/
va_list args;
ssize_t len, max;
if (left == 0)
return;
/* There is no way to estimate how much space the result will take, so
* we need to produce the string even when skipping part of the start.
* If part of the result is to be skipped, do not memcpy; instead, save
* the offset of where the result starts within the buffer.
*
* The null terminating character is not part of the result, so room
* must be given for it to be stored after completely filling up the
* requested part of the buffer.
*/
max = MIN(skip + left, BUF_SIZE);
va_start(args, fmt);
len = vsnprintf(&buf[off + used], max + 1, fmt, args);
va_end(args);
if (skip > 0) {
assert(off == 0);
assert(used == 0);
if (skip >= len) {
skip -= len;
return;
}
off = skip;
if (left > BUF_SIZE - off)
left = BUF_SIZE - off;
len -= off;
skip = 0;
}
assert(skip == 0);
assert(len >= 0);
assert((long) left >= 0);
if (len > (ssize_t) left)
len = left;
used += len;
left -= len;
}
/*==========================
5789
=================================================*
* buf_append *
*===========================================================================*/
void buf_append(char *data, size_t len)
{
/* Add arbitrary data to the end of the buffer.
*/
if (left == 0)
return;
if (skip > 0) {
if (skip >= (ssize_t) len) {
skip -= len;
return;
}
data += skip;
len -= skip;
skip = 0;
}
if (len > left)
len = left;
memcpy(&buf[off + used], data, len);
used += len;
left -= len;
}
/*===========================================================================*
* buf_get *
*===========================================================================*/
size_t buf_get(char **ptr)
{
/* Return the buffer's starting address and the length of the used
* part, not counting the trailing null character for the latter.
*/
*ptr = &buf[off];
return used;
}