2009-04-20 13:00:56 +00:00
|
|
|
#include "cache.h"
|
2012-03-14 15:29:29 +00:00
|
|
|
#include <linux/kernel.h>
|
2009-04-20 13:00:56 +00:00
|
|
|
|
|
|
|
int prefixcmp(const char *str, const char *prefix)
|
|
|
|
{
|
|
|
|
for (; ; str++, prefix++)
|
|
|
|
if (!*prefix)
|
|
|
|
return 0;
|
|
|
|
else if (*str != *prefix)
|
|
|
|
return (unsigned char)*prefix - (unsigned char)*str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Used as the default ->buf value, so that people can always assume
|
|
|
|
* buf is non NULL and ->buf is NUL terminated even for a freshly
|
|
|
|
* initialized strbuf.
|
|
|
|
*/
|
|
|
|
char strbuf_slopbuf[1];
|
|
|
|
|
2009-07-01 10:37:06 +00:00
|
|
|
void strbuf_init(struct strbuf *sb, ssize_t hint)
|
2009-04-20 13:00:56 +00:00
|
|
|
{
|
|
|
|
sb->alloc = sb->len = 0;
|
|
|
|
sb->buf = strbuf_slopbuf;
|
|
|
|
if (hint)
|
|
|
|
strbuf_grow(sb, hint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void strbuf_release(struct strbuf *sb)
|
|
|
|
{
|
|
|
|
if (sb->alloc) {
|
2013-12-27 19:55:14 +00:00
|
|
|
zfree(&sb->buf);
|
2009-04-20 13:00:56 +00:00
|
|
|
strbuf_init(sb, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char *strbuf_detach(struct strbuf *sb, size_t *sz)
|
|
|
|
{
|
|
|
|
char *res = sb->alloc ? sb->buf : NULL;
|
|
|
|
if (sz)
|
|
|
|
*sz = sb->len;
|
|
|
|
strbuf_init(sb, 0);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void strbuf_grow(struct strbuf *sb, size_t extra)
|
|
|
|
{
|
|
|
|
if (sb->len + extra + 1 <= sb->len)
|
|
|
|
die("you want to use way too much memory");
|
|
|
|
if (!sb->alloc)
|
|
|
|
sb->buf = NULL;
|
|
|
|
ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void strbuf_add(struct strbuf *sb, const void *data, size_t len)
|
|
|
|
{
|
|
|
|
strbuf_grow(sb, len);
|
|
|
|
memcpy(sb->buf + sb->len, data, len);
|
|
|
|
strbuf_setlen(sb, sb->len + len);
|
|
|
|
}
|
|
|
|
|
2015-10-24 15:49:27 +00:00
|
|
|
void strbuf_addv(struct strbuf *sb, const char *fmt, va_list ap)
|
2009-04-20 13:00:56 +00:00
|
|
|
{
|
|
|
|
int len;
|
2015-10-24 15:49:27 +00:00
|
|
|
va_list ap_saved;
|
2009-04-20 13:00:56 +00:00
|
|
|
|
|
|
|
if (!strbuf_avail(sb))
|
|
|
|
strbuf_grow(sb, 64);
|
2015-10-24 15:49:27 +00:00
|
|
|
|
|
|
|
va_copy(ap_saved, ap);
|
2012-10-23 13:44:50 +00:00
|
|
|
len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
|
2009-04-20 13:00:56 +00:00
|
|
|
if (len < 0)
|
2012-10-23 13:44:50 +00:00
|
|
|
die("your vsnprintf is broken");
|
2009-04-20 13:00:56 +00:00
|
|
|
if (len > strbuf_avail(sb)) {
|
|
|
|
strbuf_grow(sb, len);
|
2015-10-24 15:49:27 +00:00
|
|
|
len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap_saved);
|
|
|
|
va_end(ap_saved);
|
2009-04-20 13:00:56 +00:00
|
|
|
if (len > strbuf_avail(sb)) {
|
2012-10-23 13:44:50 +00:00
|
|
|
die("this should not happen, your vsnprintf is broken");
|
2009-04-20 13:00:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
strbuf_setlen(sb, sb->len + len);
|
|
|
|
}
|
|
|
|
|
2015-10-24 15:49:27 +00:00
|
|
|
void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
strbuf_addv(sb, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2009-07-01 10:37:06 +00:00
|
|
|
ssize_t strbuf_read(struct strbuf *sb, int fd, ssize_t hint)
|
2009-04-20 13:00:56 +00:00
|
|
|
{
|
|
|
|
size_t oldlen = sb->len;
|
|
|
|
size_t oldalloc = sb->alloc;
|
|
|
|
|
|
|
|
strbuf_grow(sb, hint ? hint : 8192);
|
|
|
|
for (;;) {
|
|
|
|
ssize_t cnt;
|
|
|
|
|
|
|
|
cnt = read(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
|
|
|
|
if (cnt < 0) {
|
|
|
|
if (oldalloc == 0)
|
|
|
|
strbuf_release(sb);
|
|
|
|
else
|
|
|
|
strbuf_setlen(sb, oldlen);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!cnt)
|
|
|
|
break;
|
|
|
|
sb->len += cnt;
|
|
|
|
strbuf_grow(sb, 8192);
|
|
|
|
}
|
|
|
|
|
|
|
|
sb->buf[sb->len] = '\0';
|
|
|
|
return sb->len - oldlen;
|
|
|
|
}
|