commit f53778c44deacd990d4447564de134ce4f2dd6ce
parent 0747827b3067eea950b8feaece8658b318d6d784
Author: sin <sin@2f30.org>
Date: Sat, 26 Dec 2015 16:35:37 +0000
make fillbuf() separate
Diffstat:
1 file changed, 13 insertions(+), 10 deletions(-)
diff --git a/tracker.c b/tracker.c
@@ -14,7 +14,7 @@
struct buf {
char *p;
- size_t size;
+ size_t n;
};
static void
@@ -31,16 +31,19 @@ freebuf(struct buf *b)
}
static size_t
-writefn(void *ptr, size_t size, size_t nmemb, struct buf *buf)
+fillbuf(struct buf *b, void *ptr, size_t n)
{
- size_t newsz;
-
- newsz = buf->size + size * nmemb;
- if (!(buf->p = realloc(buf->p, newsz + 1)))
+ if (!(b->p = realloc(b->p, b->n + n)))
err(1, "realloc");
- memcpy(buf->p + buf->size, ptr, size * nmemb);
- buf->size = newsz;
- return size * nmemb;
+ memcpy(b->p + b->n, ptr, n);
+ b->n += n;
+ return b->n;
+}
+
+static size_t
+writefn(void *ptr, size_t size, size_t nmemb, struct buf *b)
+{
+ return fillbuf(b, ptr, size * nmemb);
}
static int
@@ -51,7 +54,7 @@ parsepeers(struct torrent *t, struct buf *b)
struct peer *peer;
char *p;
- if (!bdecode(b->p, b->p + b->size, &reply))
+ if (!bdecode(b->p, b->p + b->n, &reply))
return -1;
if (!(peers = dlookstr(reply, "peers"))) {