1
2
3
4
5
6
7
8
9
10
11
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
/* $OpenBSD: ringbuf.c,v 1.7 2005/09/21 23:25:32 djm Exp $ */
/*
* Copyright (c) 2004 Damien Miller
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Simple ringbuffer for lines of text.
*/
#include <sys/types.h>
#include <sys/param.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "syslogd.h"
/* Initialise a ring buffer */
struct ringbuf *
ringbuf_init(size_t len)
{
struct ringbuf *ret;
if (len == 0 || (ret = malloc(sizeof(*ret))) == NULL)
return (NULL);
if ((ret->buf = malloc(len)) == NULL) {
free(ret);
return (NULL);
}
ret->len = len;
ret->start = ret->end = 0;
return (ret);
}
/* Free a ring buffer */
void
ringbuf_free(struct ringbuf *rb)
{
free(rb->buf);
free(rb);
}
/* Clear a ring buffer */
void
ringbuf_clear(struct ringbuf *rb)
{
rb->start = rb->end = 0;
}
/* Return the number of bytes used in a ringbuffer */
size_t
ringbuf_used(struct ringbuf *rb)
{
return ((rb->len + rb->end - rb->start) % rb->len);
}
/*
* Append a line to a ring buffer, will delete lines from start
* of buffer as necessary
*/
int
ringbuf_append_line(struct ringbuf *rb, char *line)
{
size_t llen, used, copy_len;
int overflow = 0;
if (rb == NULL || line == NULL)
return (-1);
llen = strlen(line);
if (llen == 0)
return (-1);
if (line[llen - 1] != '\n')
llen++; /* one extra for appended '\n' */
if (llen >= rb->len)
return (-1);
/*
* If necessary, advance start pointer to make room for appended
* string. Ensure that start pointer is at the beginning of a line
* once we are done (i.e move to after '\n').
*/
used = ringbuf_used(rb);
if (used + llen >= rb->len) {
rb->start = (rb->start + used + llen - rb->len) % rb->len;
/* Find next '\n' */
while (rb->buf[rb->start] != '\n')
rb->start = (rb->start + 1) % rb->len;
/* Skip it */
rb->start = (rb->start + 1) % rb->len;
overflow = 1;
}
/*
* Now append string, starting from last pointer and wrapping if
* necessary
*/
if (rb->end + llen > rb->len) {
copy_len = rb->len - rb->end;
memcpy(rb->buf + rb->end, line, copy_len);
memcpy(rb->buf, line + copy_len, llen - copy_len - 1);
rb->buf[llen - copy_len - 1] = '\n';
} else {
memcpy(rb->buf + rb->end, line, llen - 1);
rb->buf[rb->end + llen - 1] = '\n';
}
rb->end = (rb->end + llen) % rb->len;
return (overflow);
}
/*
* Copy and nul-terminate a ringbuffer to a string.
*/
ssize_t
ringbuf_to_string(char *buf, size_t len, struct ringbuf *rb)
{
size_t copy_len, n;
if (buf == NULL || rb == NULL || len == 0)
return (-1);
copy_len = MIN(len - 1, ringbuf_used(rb));
if (copy_len == 0)
return (copy_len);
if (rb->start < rb->end)
memcpy(buf, rb->buf + rb->start, copy_len);
else {
/* If the buffer is wrapped, copy each hunk separately */
n = rb->len - rb->start;
memcpy(buf, rb->buf + rb->start, MIN(n, copy_len));
if (copy_len > n)
memcpy(buf + n, rb->buf, MIN(rb->end, copy_len - n));
}
buf[copy_len] = '\0';
return (ringbuf_used(rb));
}
|