aboutsummaryrefslogtreecommitdiff
path: root/termkey.c
diff options
context:
space:
mode:
authorPaul LeoNerd Evans <leonerd@leonerd.org.uk>2012-01-18 10:07:36 +0000
committerPaul LeoNerd Evans <leonerd@leonerd.org.uk>2012-01-18 10:07:36 +0000
commit3474a45b14a131e92e52960dd90e63c12fda275d (patch)
tree9eaf5f424d3c8edf886d9aa86c7ca06b073e4fe7 /termkey.c
parent9534330003bdfdbc6ac47ad08fdf15636e0d9518 (diff)
downloadtermo-3474a45b14a131e92e52960dd90e63c12fda275d.tar.gz
termo-3474a45b14a131e92e52960dd90e63c12fda275d.tar.xz
termo-3474a45b14a131e92e52960dd90e63c12fda275d.zip
Avoid push_bytes() entirely by read()ing directly into tk->buffer
Diffstat (limited to 'termkey.c')
-rw-r--r--termkey.c26
1 files changed, 7 insertions, 19 deletions
diff --git a/termkey.c b/termkey.c
index e50ff38..7770d83 100644
--- a/termkey.c
+++ b/termkey.c
@@ -940,29 +940,17 @@ retry:
/* UNREACHABLE */
}
-static void push_bytes(TermKey *tk, const unsigned char *input, size_t inputlen)
-{
- if(tk->buffstart + tk->buffcount + inputlen > tk->buffsize) {
- while(tk->buffstart + tk->buffcount + inputlen > tk->buffsize)
- tk->buffsize *= 2;
-
- unsigned char *newbuffer = realloc(tk->buffer, tk->buffsize);
- // TODO: Handle realloc() failure
- tk->buffer = newbuffer;
- }
-
- // Not strcpy just in case of NUL bytes
- memcpy(tk->buffer + tk->buffstart + tk->buffcount, input, inputlen);
- tk->buffcount += inputlen;
-}
-
TermKeyResult termkey_advisereadable(TermKey *tk)
{
- unsigned char buffer[64]; // Smaller than the default size
ssize_t len;
+ if(tk->buffstart) {
+ memmove(tk->buffer, tk->buffer + tk->buffstart, tk->buffcount);
+ tk->buffstart = 0;
+ }
+
retry:
- len = read(tk->fd, buffer, sizeof buffer);
+ len = read(tk->fd, tk->buffer + tk->buffcount, tk->buffsize - tk->buffcount);
if(len == -1) {
if(errno == EAGAIN)
@@ -977,7 +965,7 @@ retry:
return TERMKEY_RES_NONE;
}
else {
- push_bytes(tk, buffer, len);
+ tk->buffcount += len;
return TERMKEY_RES_AGAIN;
}
}