commit 6442aecfc08e934d65b38ec83c76bd8a040370a0
parent e8214e1e8d0394f4569e5049f162794d69019a03
Author: Quentin Rameau <quinq@fifth.space>
Date: Thu, 22 Jun 2017 17:13:24 +0200
Improve a little getrawitem
Diffstat:
sacc.c | | | 23 | +++++++++++------------ |
1 file changed, 11 insertions(+), 12 deletions(-)
diff --git a/sacc.c b/sacc.c
@@ -223,30 +223,29 @@ molddiritem(char *raw)
char *
getrawitem(int sock)
{
- char *item, *buf;
- size_t is, bs, ns;
+ char *raw, *buf;
+ size_t bn, bs;
ssize_t n;
- is = bs = BUFSIZ;
- item = buf = xmalloc(BUFSIZ+1);
+ raw = buf = NULL;
+ bn = bs = n = 0;
- while ((n = read(sock, buf, bs)) > 0) {
+ do {
bs -= n;
buf += n;
- if (bs <= 0) {
- ns = is + BUFSIZ;
- item = xreallocarray(item, ns+1, 1);
- is = ns;
- buf = item + is-BUFSIZ;
+ if (bs <= 1) {
+ raw = xreallocarray(raw, ++bn, BUFSIZ);
+ buf = raw + (bn-1) * BUFSIZ;
bs = BUFSIZ;
}
- }
+ } while ((n = read(sock, buf, bs)) > 0);
+
if (n < 0)
die("Can't read socket: %s", strerror(errno));
*buf = '\0';
- return item;
+ return raw;
}
int