diff options
author | Yousong Zhou <yszhou4tech@gmail.com> | 2015-01-21 16:21:25 +0300 |
---|---|---|
committer | Felix Fietkau <nbd@openwrt.org> | 2015-01-21 22:00:39 +0300 |
commit | 60236c485387d33e1d02398a953a0834ad125161 (patch) | |
tree | ca3c355aad62de5fc0041dcb4a003924adf829cc /ustream.c | |
parent | f1c794b29e2dac52ec25869bf8c37b3e230fb2a2 (diff) |
ustream: add function ustream_read().
It can be used to fill caller-specified buffer with data already in
ustream read buffer. Useful in the following use pattern.
int available = ustream_pending_data(s, false);
if (available >= sizeof(struct msghdr)) {
struct msghdr h;
ustream_read(s, &h, sizeof(h));
}
Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
Diffstat (limited to 'ustream.c')
-rw-r--r-- | ustream.c | 20 |
1 files changed, 20 insertions, 0 deletions
@@ -333,6 +333,26 @@ char *ustream_get_read_buf(struct ustream *s, int *buflen) return data; } +int ustream_read(struct ustream *s, char *buf, int buflen) +{ + char *chunk; + int chunk_len; + int len = 0; + + do { + chunk = ustream_get_read_buf(s, &chunk_len); + if (!chunk) + break; + if (chunk_len > buflen - len) + chunk_len = buflen - len; + memcpy(buf + len, chunk, chunk_len); + ustream_consume(s, chunk_len); + len += chunk_len; + } while (len < buflen); + + return len; +} + static void ustream_write_error(struct ustream *s) { if (!s->write_error) |