size_t len;
int ret;
- while ((buf = shl_ring_peek(pty->msgbuf, &len))) {
+ while ((buf = shl_ring_peek(pty->msgbuf, &len, 0))) {
ret = write(pty->fd, buf, len);
if (ret > 0) {
shl_ring_drop(pty->msgbuf, ret);
return 0;
}
-static inline const char *shl_ring_peek(struct shl_ring *ring, size_t *len)
+static inline const char *shl_ring_peek(struct shl_ring *ring, size_t *len,
+ size_t offset)
{
- if (!ring || !ring->first)
+ struct shl_ring_entry *iter;
+
+ if (!ring || !ring->first || !len) {
+ if (len)
+ *len = 0;
return NULL;
+ }
+
+ iter = ring->first;
+ while (iter->len <= offset) {
+ if (!iter->next) {
+ *len = 0;
+ return NULL;
+ }
+
+ offset -= iter->len;
+ iter = iter->next;
+ }
- *len = ring->first->len;
- return ring->first->buf;
+ *len = ring->first->len - offset;
+ return &ring->first->buf[offset];
}
static inline void shl_ring_drop(struct shl_ring *ring, size_t len)