diff options
author | Stef Walter <stef@memberwebs.com> | 2004-09-18 02:59:30 +0000 |
---|---|---|
committer | Stef Walter <stef@memberwebs.com> | 2004-09-18 02:59:30 +0000 |
commit | d1395329541644273d15f7c9b11aa396da308512 (patch) | |
tree | dfcdc681c91f2a17e400d8c169d48f6ba825a12d | |
parent | 2dd8af96f9714d4729c8a2a3f4c599ee0d4175e0 (diff) |
buffer read fixes.
-rw-r--r-- | common/spio.c | 38 |
1 files changed, 21 insertions, 17 deletions
diff --git a/common/spio.c b/common/spio.c index 6677746..2d42eac 100644 --- a/common/spio.c +++ b/common/spio.c @@ -229,7 +229,7 @@ unsigned int spio_select(spctx_t* ctx, ...) int read_raw(spctx_t* ctx, spio_t* io, int opts) { - int len, x; + int len, x, count; char* at; char* p; @@ -239,17 +239,19 @@ int read_raw(spctx_t* ctx, spio_t* io, int opts) * _nx: Extra data read on last read. * _ln: Length of that extra data. * - * _nx should never be equal to line when entering this - * function. And _ln should always be less than a full - * buffer. + * _nx should never be equal to line when entering this function. + * And _ln should always be less than a full buffer. */ + count = 0; + io->line[0] = 0; + /* Remaining data in the buffer */ if(io->_nx && io->_ln > 0) { ASSERT(!io->_nx || io->_nx > io->line); ASSERT(io->_ln < SP_LINE_LENGTH); - ASSERT(io->_nx + io->_ln < io->line + SP_LINE_LENGTH); + ASSERT(io->_nx + io->_ln <= io->line + SP_LINE_LENGTH); /* Check for a return in the current buffer */ if((p = (char*)memchr(io->_nx, '\n', io->_ln)) != NULL) @@ -267,12 +269,13 @@ int read_raw(spctx_t* ctx, spio_t* io, int opts) io->_nx += x; /* A double check on the return value */ - ASSERT(strlen(io->line) == x); - return x; + count += x; + return count; } /* Otherwise move all old data to front */ memmove(io->line, io->_nx, io->_ln); + count += io->_ln; /* We always leave space for a null terminator */ len = (SP_LINE_LENGTH - io->_ln) - 1; @@ -329,9 +332,7 @@ int read_raw(spctx_t* ctx, spio_t* io, int opts) io->_nx = NULL; io->_ln = 0; - /* A double check on the return value */ - ASSERT(strlen(io->line) == at - io->line); - return at - io->line; + return count; } /* Check for a new line */ @@ -339,21 +340,25 @@ int read_raw(spctx_t* ctx, spio_t* io, int opts) if(p != NULL) { p++; - len = x - (p - at); + count += (p - at); /* Insert the null terminator */ - memmove(p, p + 1, len); + len = x - (p - at); + memmove(p + 1, p, len); *p = 0; /* Do maintenence for remaining data */ io->_nx = p + 1; io->_ln = len; - /* A double check on the return value */ - ASSERT(strlen(io->line) == p - io->line); - return p - io->line; + return count; } + /* Move the buffer pointer along */ + at += x; + len -= x; + count += x; + if(len <= 0) { /* Keep reading until we hit a new line */ @@ -381,8 +386,7 @@ int read_raw(spctx_t* ctx, spio_t* io, int opts) io->line[SP_LINE_LENGTH] = 0; /* A double check on the return value */ - ASSERT(strlen(io->line) == p - io->line); - return SP_LINE_LENGTH; + return count; } } } |