diff --git a/src/pty.c b/src/pty.c index 61d6b6c..bcd7e16 100644 --- a/src/pty.c +++ b/src/pty.c @@ -367,7 +367,7 @@ static int send_buf(struct kmscon_pty *pty) 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); diff --git a/src/shl_ring.h b/src/shl_ring.h index d668ab6..5e6622f 100644 --- a/src/shl_ring.h +++ b/src/shl_ring.h @@ -131,13 +131,30 @@ next: 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) - return NULL; + struct shl_ring_entry *iter; - *len = ring->first->len; - return ring->first->buf; + 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 - offset; + return &ring->first->buf[offset]; } static inline void shl_ring_drop(struct shl_ring *ring, size_t len)