blob: 8fb73295ec34cd74150cf77e042a3a7b39611cfe [file] [log] [blame]
Ingo Molnar07800602009-04-20 15:00:56 +02001#include "cache.h"
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -03002#include <linux/kernel.h>
Ingo Molnar07800602009-04-20 15:00:56 +02003
4int prefixcmp(const char *str, const char *prefix)
5{
6 for (; ; str++, prefix++)
7 if (!*prefix)
8 return 0;
9 else if (*str != *prefix)
10 return (unsigned char)*prefix - (unsigned char)*str;
11}
12
13/*
14 * Used as the default ->buf value, so that people can always assume
15 * buf is non NULL and ->buf is NUL terminated even for a freshly
16 * initialized strbuf.
17 */
18char strbuf_slopbuf[1];
19
Ingo Molnarf37a2912009-07-01 12:37:06 +020020void strbuf_init(struct strbuf *sb, ssize_t hint)
Ingo Molnar07800602009-04-20 15:00:56 +020021{
22 sb->alloc = sb->len = 0;
23 sb->buf = strbuf_slopbuf;
24 if (hint)
25 strbuf_grow(sb, hint);
26}
27
28void strbuf_release(struct strbuf *sb)
29{
30 if (sb->alloc) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -030031 zfree(&sb->buf);
Ingo Molnar07800602009-04-20 15:00:56 +020032 strbuf_init(sb, 0);
33 }
34}
35
36char *strbuf_detach(struct strbuf *sb, size_t *sz)
37{
38 char *res = sb->alloc ? sb->buf : NULL;
39 if (sz)
40 *sz = sb->len;
41 strbuf_init(sb, 0);
42 return res;
43}
44
Ingo Molnar07800602009-04-20 15:00:56 +020045void strbuf_grow(struct strbuf *sb, size_t extra)
46{
47 if (sb->len + extra + 1 <= sb->len)
48 die("you want to use way too much memory");
49 if (!sb->alloc)
50 sb->buf = NULL;
51 ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
52}
53
Arnaldo Carvalho de Melo07412082016-03-23 16:56:56 -030054void strbuf_addch(struct strbuf *sb, int c)
55{
56 strbuf_grow(sb, 1);
57 sb->buf[sb->len++] = c;
58 sb->buf[sb->len] = '\0';
59}
60
Ingo Molnar07800602009-04-20 15:00:56 +020061void strbuf_add(struct strbuf *sb, const void *data, size_t len)
62{
63 strbuf_grow(sb, len);
64 memcpy(sb->buf + sb->len, data, len);
65 strbuf_setlen(sb, sb->len + len);
66}
67
Arnaldo Carvalho de Melo07412082016-03-23 16:56:56 -030068static void strbuf_addv(struct strbuf *sb, const char *fmt, va_list ap)
Ingo Molnar07800602009-04-20 15:00:56 +020069{
70 int len;
Namhyung Kimc7118362015-10-25 00:49:27 +090071 va_list ap_saved;
Ingo Molnar07800602009-04-20 15:00:56 +020072
73 if (!strbuf_avail(sb))
74 strbuf_grow(sb, 64);
Namhyung Kimc7118362015-10-25 00:49:27 +090075
76 va_copy(ap_saved, ap);
Namhyung Kimf787d952012-10-23 22:44:50 +090077 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
Ingo Molnar07800602009-04-20 15:00:56 +020078 if (len < 0)
Namhyung Kimf787d952012-10-23 22:44:50 +090079 die("your vsnprintf is broken");
Ingo Molnar07800602009-04-20 15:00:56 +020080 if (len > strbuf_avail(sb)) {
81 strbuf_grow(sb, len);
Namhyung Kimc7118362015-10-25 00:49:27 +090082 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap_saved);
83 va_end(ap_saved);
Ingo Molnar07800602009-04-20 15:00:56 +020084 if (len > strbuf_avail(sb)) {
Namhyung Kimf787d952012-10-23 22:44:50 +090085 die("this should not happen, your vsnprintf is broken");
Ingo Molnar07800602009-04-20 15:00:56 +020086 }
87 }
88 strbuf_setlen(sb, sb->len + len);
89}
90
Namhyung Kimc7118362015-10-25 00:49:27 +090091void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
92{
93 va_list ap;
94
95 va_start(ap, fmt);
96 strbuf_addv(sb, fmt, ap);
97 va_end(ap);
98}
99
Ingo Molnarf37a2912009-07-01 12:37:06 +0200100ssize_t strbuf_read(struct strbuf *sb, int fd, ssize_t hint)
Ingo Molnar07800602009-04-20 15:00:56 +0200101{
102 size_t oldlen = sb->len;
103 size_t oldalloc = sb->alloc;
104
105 strbuf_grow(sb, hint ? hint : 8192);
106 for (;;) {
107 ssize_t cnt;
108
109 cnt = read(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
110 if (cnt < 0) {
111 if (oldalloc == 0)
112 strbuf_release(sb);
113 else
114 strbuf_setlen(sb, oldlen);
115 return -1;
116 }
117 if (!cnt)
118 break;
119 sb->len += cnt;
120 strbuf_grow(sb, 8192);
121 }
122
123 sb->buf[sb->len] = '\0';
124 return sb->len - oldlen;
125}