| 58 | /* multiple add */ |
| 59 | |
| 60 | int |
| 61 | cirbuf_add_buf_tail(struct cirbuf *cbuf, const char *c, unsigned int n) |
| 62 | { |
| 63 | unsigned int e; |
| 64 | |
| 65 | if (!cbuf || !c || !n || n > CIRBUF_GET_FREELEN(cbuf)) |
| 66 | return -EINVAL; |
| 67 | |
| 68 | e = CIRBUF_IS_EMPTY(cbuf) ? 1 : 0; |
| 69 | |
| 70 | if (n < cbuf->maxlen - cbuf->end - 1 + e) { |
| 71 | dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->end + !e, n); |
| 72 | memcpy(cbuf->buf + cbuf->end + !e, c, n); |
| 73 | } |
| 74 | else { |
| 75 | dprintf("s[%d] -> d[%d] (%d)\n", cbuf->end + !e, 0, |
| 76 | cbuf->maxlen - cbuf->end - 1 + e); |
| 77 | dprintf("s[%d] -> d[%d] (%d)\n", cbuf->maxlen - cbuf->end - 1 + |
| 78 | e, 0, n - cbuf->maxlen + cbuf->end + 1 - e); |
| 79 | memcpy(cbuf->buf + cbuf->end + !e, c, cbuf->maxlen - |
| 80 | cbuf->end - 1 + e); |
| 81 | memcpy(cbuf->buf, c + cbuf->maxlen - cbuf->end - 1 + e, |
| 82 | n - cbuf->maxlen + cbuf->end + 1 - e); |
| 83 | } |
| 84 | cbuf->len += n; |
| 85 | cbuf->end += n - e; |
| 86 | cbuf->end %= cbuf->maxlen; |
| 87 | return n; |
| 88 | } |
| 89 | |
| 90 | /* add at head */ |
| 91 | |