1 /* $OpenBSD: ringbuf.c,v 1.2 2004/02/26 11:02:32 avsm Exp $ */ 2 3 /* 4 * Copyright (c) 2004 Damien Miller 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 /* 20 * Simple ringbuffer for lines of text. 21 */ 22 23 #include <sys/types.h> 24 #include <sys/param.h> 25 #include <sys/uio.h> 26 #include <errno.h> 27 #include <stdio.h> 28 #include <stdlib.h> 29 #include <string.h> 30 #include <unistd.h> 31 32 #include "syslogd.h" 33 34 /* Initialise a ring buffer */ 35 struct ringbuf * 36 ringbuf_init(size_t len) 37 { 38 struct ringbuf *ret; 39 40 if (len == 0 || (ret = malloc(sizeof(*ret))) == NULL) 41 return (NULL); 42 43 if ((ret->buf = malloc(len)) == NULL) { 44 free(ret); 45 return (NULL); 46 } 47 48 ret->len = len; 49 ret->start = ret->end = 0; 50 51 return (ret); 52 } 53 54 /* Clear a ring buffer */ 55 void 56 ringbuf_clear(struct ringbuf *rb) 57 { 58 rb->start = rb->end = 0; 59 } 60 61 /* Return the number of bytes used in a ringbuffer */ 62 size_t 63 ringbuf_used(struct ringbuf *rb) 64 { 65 return ((rb->len + rb->end - rb->start) % rb->len); 66 } 67 68 /* 69 * Append a line to a ring buffer, will delete lines from start 70 * of buffer as necessary 71 */ 72 int 73 ringbuf_append_line(struct ringbuf *rb, char *line) 74 { 75 size_t llen, used, copy_len; 76 77 if (rb == NULL || line == NULL) 78 return (-1); 79 80 llen = strlen(line); 81 if (line[llen - 1] != '\n') 82 llen++; /* one extra for appended '\n' */ 83 84 if (rb == NULL || llen == 0 || llen >= rb->len) 85 return (-1); 86 87 /* 88 * If necessary, advance start pointer to make room for appended 89 * string. Ensure that start pointer is at the beginning of a line 90 * once we are done (i.e move to after '\n'). 91 */ 92 used = ringbuf_used(rb); 93 if (used + llen >= rb->len) { 94 rb->start = (rb->start + used + llen - rb->len) % rb->len; 95 96 /* Find next '\n' */ 97 while (rb->buf[rb->start] != '\n') 98 rb->start = (rb->start + 1) % rb->len; 99 /* Skip it */ 100 rb->start = (rb->start + 1) % rb->len; 101 } 102 103 /* 104 * Now append string, starting from last pointer and wrapping if 105 * necessary 106 */ 107 if (rb->end + llen > rb->len) { 108 copy_len = rb->len - rb->end; 109 memcpy(rb->buf + rb->end, line, copy_len); 110 memcpy(rb->buf, line + copy_len, llen - copy_len - 1); 111 rb->buf[llen - copy_len - 1] = '\n'; 112 } else { 113 memcpy(rb->buf + rb->end, line, llen - 1); 114 rb->buf[rb->end + llen - 1] = '\n'; 115 } 116 117 rb->end = (rb->end + llen) % rb->len; 118 119 return (0); 120 } 121 122 /* 123 * Copy and nul-terminate a ringbuffer to a string. 124 */ 125 ssize_t 126 ringbuf_to_string(char *buf, size_t len, struct ringbuf *rb) 127 { 128 ssize_t copy_len, n; 129 130 if (buf == NULL || rb == NULL) 131 return (-1); 132 133 copy_len = MIN(len - 1, ringbuf_used(rb)); 134 135 if (copy_len <= 0) 136 return (copy_len); 137 138 if (rb->start < rb->end) 139 memcpy(buf, rb->buf + rb->start, copy_len); 140 else { 141 /* If the buffer is wrapped, copy each hunk separately */ 142 n = rb->len - rb->start; 143 memcpy(buf, rb->buf + rb->start, MIN(n, copy_len)); 144 if (copy_len - n > 0) 145 memcpy(buf + n, rb->buf, MIN(rb->end, copy_len - n)); 146 } 147 buf[copy_len] = '\0'; 148 149 return (ringbuf_used(rb)); 150 } 151