1#include "cache.h"
2#include "refs.h"
3
4int starts_with(const char *str, const char *prefix)
5{
6 for (; ; str++, prefix++)
7 if (!*prefix)
8 return 1;
9 else if (*str != *prefix)
10 return 0;
11}
12
13int ends_with(const char *str, const char *suffix)
14{
15 int len = strlen(str), suflen = strlen(suffix);
16 if (len < suflen)
17 return 0;
18 else
19 return !strcmp(str + len - suflen, suffix);
20}
21
22/*
23 * Used as the default ->buf value, so that people can always assume
24 * buf is non NULL and ->buf is NUL terminated even for a freshly
25 * initialized strbuf.
26 */
27char strbuf_slopbuf[1];
28
29void strbuf_init(struct strbuf *sb, size_t hint)
30{
31 sb->alloc = sb->len = 0;
32 sb->buf = strbuf_slopbuf;
33 if (hint)
34 strbuf_grow(sb, hint);
35}
36
37void strbuf_release(struct strbuf *sb)
38{
39 if (sb->alloc) {
40 free(sb->buf);
41 strbuf_init(sb, 0);
42 }
43}
44
45char *strbuf_detach(struct strbuf *sb, size_t *sz)
46{
47 char *res;
48 strbuf_grow(sb, 0);
49 res = sb->buf;
50 if (sz)
51 *sz = sb->len;
52 strbuf_init(sb, 0);
53 return res;
54}
55
56void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
57{
58 strbuf_release(sb);
59 sb->buf = buf;
60 sb->len = len;
61 sb->alloc = alloc;
62 strbuf_grow(sb, 0);
63 sb->buf[sb->len] = '\0';
64}
65
66void strbuf_grow(struct strbuf *sb, size_t extra)
67{
68 int new_buf = !sb->alloc;
69 if (unsigned_add_overflows(extra, 1) ||
70 unsigned_add_overflows(sb->len, extra + 1))
71 die("you want to use way too much memory");
72 if (new_buf)
73 sb->buf = NULL;
74 ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
75 if (new_buf)
76 sb->buf[0] = '\0';
77}
78
79void strbuf_trim(struct strbuf *sb)
80{
81 strbuf_rtrim(sb);
82 strbuf_ltrim(sb);
83}
84void strbuf_rtrim(struct strbuf *sb)
85{
86 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
87 sb->len--;
88 sb->buf[sb->len] = '\0';
89}
90
91void strbuf_ltrim(struct strbuf *sb)
92{
93 char *b = sb->buf;
94 while (sb->len > 0 && isspace(*b)) {
95 b++;
96 sb->len--;
97 }
98 memmove(sb->buf, b, sb->len);
99 sb->buf[sb->len] = '\0';
100}
101
102struct strbuf **strbuf_split_buf(const char *str, size_t slen,
103 int terminator, int max)
104{
105 struct strbuf **ret = NULL;
106 size_t nr = 0, alloc = 0;
107 struct strbuf *t;
108
109 while (slen) {
110 int len = slen;
111 if (max <= 0 || nr + 1 < max) {
112 const char *end = memchr(str, terminator, slen);
113 if (end)
114 len = end - str + 1;
115 }
116 t = xmalloc(sizeof(struct strbuf));
117 strbuf_init(t, len);
118 strbuf_add(t, str, len);
119 ALLOC_GROW(ret, nr + 2, alloc);
120 ret[nr++] = t;
121 str += len;
122 slen -= len;
123 }
124 ALLOC_GROW(ret, nr + 1, alloc); /* In case string was empty */
125 ret[nr] = NULL;
126 return ret;
127}
128
129void strbuf_list_free(struct strbuf **sbs)
130{
131 struct strbuf **s = sbs;
132
133 while (*s) {
134 strbuf_release(*s);
135 free(*s++);
136 }
137 free(sbs);
138}
139
140int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
141{
142 int len = a->len < b->len ? a->len: b->len;
143 int cmp = memcmp(a->buf, b->buf, len);
144 if (cmp)
145 return cmp;
146 return a->len < b->len ? -1: a->len != b->len;
147}
148
149void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
150 const void *data, size_t dlen)
151{
152 if (unsigned_add_overflows(pos, len))
153 die("you want to use way too much memory");
154 if (pos > sb->len)
155 die("`pos' is too far after the end of the buffer");
156 if (pos + len > sb->len)
157 die("`pos + len' is too far after the end of the buffer");
158
159 if (dlen >= len)
160 strbuf_grow(sb, dlen - len);
161 memmove(sb->buf + pos + dlen,
162 sb->buf + pos + len,
163 sb->len - pos - len);
164 memcpy(sb->buf + pos, data, dlen);
165 strbuf_setlen(sb, sb->len + dlen - len);
166}
167
168void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
169{
170 strbuf_splice(sb, pos, 0, data, len);
171}
172
173void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
174{
175 strbuf_splice(sb, pos, len, NULL, 0);
176}
177
178void strbuf_add(struct strbuf *sb, const void *data, size_t len)
179{
180 strbuf_grow(sb, len);
181 memcpy(sb->buf + sb->len, data, len);
182 strbuf_setlen(sb, sb->len + len);
183}
184
185void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len)
186{
187 strbuf_grow(sb, len);
188 memcpy(sb->buf + sb->len, sb->buf + pos, len);
189 strbuf_setlen(sb, sb->len + len);
190}
191
192void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
193{
194 va_list ap;
195 va_start(ap, fmt);
196 strbuf_vaddf(sb, fmt, ap);
197 va_end(ap);
198}
199
200static void add_lines(struct strbuf *out,
201 const char *prefix1,
202 const char *prefix2,
203 const char *buf, size_t size)
204{
205 while (size) {
206 const char *prefix;
207 const char *next = memchr(buf, '\n', size);
208 next = next ? (next + 1) : (buf + size);
209
210 prefix = (prefix2 && buf[0] == '\n') ? prefix2 : prefix1;
211 strbuf_addstr(out, prefix);
212 strbuf_add(out, buf, next - buf);
213 size -= next - buf;
214 buf = next;
215 }
216 strbuf_complete_line(out);
217}
218
219void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size)
220{
221 static char prefix1[3];
222 static char prefix2[2];
223
224 if (prefix1[0] != comment_line_char) {
225 sprintf(prefix1, "%c ", comment_line_char);
226 sprintf(prefix2, "%c", comment_line_char);
227 }
228 add_lines(out, prefix1, prefix2, buf, size);
229}
230
231void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
232{
233 va_list params;
234 struct strbuf buf = STRBUF_INIT;
235 int incomplete_line = sb->len && sb->buf[sb->len - 1] != '\n';
236
237 va_start(params, fmt);
238 strbuf_vaddf(&buf, fmt, params);
239 va_end(params);
240
241 strbuf_add_commented_lines(sb, buf.buf, buf.len);
242 if (incomplete_line)
243 sb->buf[--sb->len] = '\0';
244
245 strbuf_release(&buf);
246}
247
248void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
249{
250 int len;
251 va_list cp;
252
253 if (!strbuf_avail(sb))
254 strbuf_grow(sb, 64);
255 va_copy(cp, ap);
256 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, cp);
257 va_end(cp);
258 if (len < 0)
259 die("BUG: your vsnprintf is broken (returned %d)", len);
260 if (len > strbuf_avail(sb)) {
261 strbuf_grow(sb, len);
262 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
263 if (len > strbuf_avail(sb))
264 die("BUG: your vsnprintf is broken (insatiable)");
265 }
266 strbuf_setlen(sb, sb->len + len);
267}
268
269void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
270 void *context)
271{
272 for (;;) {
273 const char *percent;
274 size_t consumed;
275
276 percent = strchrnul(format, '%');
277 strbuf_add(sb, format, percent - format);
278 if (!*percent)
279 break;
280 format = percent + 1;
281
282 if (*format == '%') {
283 strbuf_addch(sb, '%');
284 format++;
285 continue;
286 }
287
288 consumed = fn(sb, format, context);
289 if (consumed)
290 format += consumed;
291 else
292 strbuf_addch(sb, '%');
293 }
294}
295
296size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
297 void *context)
298{
299 struct strbuf_expand_dict_entry *e = context;
300 size_t len;
301
302 for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
303 if (!strncmp(placeholder, e->placeholder, len)) {
304 if (e->value)
305 strbuf_addstr(sb, e->value);
306 return len;
307 }
308 }
309 return 0;
310}
311
312void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
313{
314 int i, len = src->len;
315
316 for (i = 0; i < len; i++) {
317 if (src->buf[i] == '%')
318 strbuf_addch(dst, '%');
319 strbuf_addch(dst, src->buf[i]);
320 }
321}
322
323size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
324{
325 size_t res;
326 size_t oldalloc = sb->alloc;
327
328 strbuf_grow(sb, size);
329 res = fread(sb->buf + sb->len, 1, size, f);
330 if (res > 0)
331 strbuf_setlen(sb, sb->len + res);
332 else if (oldalloc == 0)
333 strbuf_release(sb);
334 return res;
335}
336
337ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
338{
339 size_t oldlen = sb->len;
340 size_t oldalloc = sb->alloc;
341
342 strbuf_grow(sb, hint ? hint : 8192);
343 for (;;) {
344 ssize_t cnt;
345
346 cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
347 if (cnt < 0) {
348 if (oldalloc == 0)
349 strbuf_release(sb);
350 else
351 strbuf_setlen(sb, oldlen);
352 return -1;
353 }
354 if (!cnt)
355 break;
356 sb->len += cnt;
357 strbuf_grow(sb, 8192);
358 }
359
360 sb->buf[sb->len] = '\0';
361 return sb->len - oldlen;
362}
363
364#define STRBUF_MAXLINK (2*PATH_MAX)
365
366int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
367{
368 size_t oldalloc = sb->alloc;
369
370 if (hint < 32)
371 hint = 32;
372
373 while (hint < STRBUF_MAXLINK) {
374 int len;
375
376 strbuf_grow(sb, hint);
377 len = readlink(path, sb->buf, hint);
378 if (len < 0) {
379 if (errno != ERANGE)
380 break;
381 } else if (len < hint) {
382 strbuf_setlen(sb, len);
383 return 0;
384 }
385
386 /* .. the buffer was too small - try again */
387 hint *= 2;
388 }
389 if (oldalloc == 0)
390 strbuf_release(sb);
391 return -1;
392}
393
394int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
395{
396 int ch;
397
398 if (feof(fp))
399 return EOF;
400
401 strbuf_reset(sb);
402 while ((ch = fgetc(fp)) != EOF) {
403 strbuf_grow(sb, 1);
404 sb->buf[sb->len++] = ch;
405 if (ch == term)
406 break;
407 }
408 if (ch == EOF && sb->len == 0)
409 return EOF;
410
411 sb->buf[sb->len] = '\0';
412 return 0;
413}
414
415int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
416{
417 if (strbuf_getwholeline(sb, fp, term))
418 return EOF;
419 if (sb->buf[sb->len-1] == term)
420 strbuf_setlen(sb, sb->len-1);
421 return 0;
422}
423
424int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term)
425{
426 strbuf_reset(sb);
427
428 while (1) {
429 char ch;
430 ssize_t len = xread(fd, &ch, 1);
431 if (len <= 0)
432 return EOF;
433 strbuf_addch(sb, ch);
434 if (ch == term)
435 break;
436 }
437 return 0;
438}
439
440int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
441{
442 int fd, len;
443
444 fd = open(path, O_RDONLY);
445 if (fd < 0)
446 return -1;
447 len = strbuf_read(sb, fd, hint);
448 close(fd);
449 if (len < 0)
450 return -1;
451
452 return len;
453}
454
455void strbuf_add_lines(struct strbuf *out, const char *prefix,
456 const char *buf, size_t size)
457{
458 add_lines(out, prefix, NULL, buf, size);
459}
460
461void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s)
462{
463 while (*s) {
464 size_t len = strcspn(s, "\"<>&");
465 strbuf_add(buf, s, len);
466 s += len;
467 switch (*s) {
468 case '"':
469 strbuf_addstr(buf, """);
470 break;
471 case '<':
472 strbuf_addstr(buf, "<");
473 break;
474 case '>':
475 strbuf_addstr(buf, ">");
476 break;
477 case '&':
478 strbuf_addstr(buf, "&");
479 break;
480 case 0:
481 return;
482 }
483 s++;
484 }
485}
486
487static int is_rfc3986_reserved(char ch)
488{
489 switch (ch) {
490 case '!': case '*': case '\'': case '(': case ')': case ';':
491 case ':': case '@': case '&': case '=': case '+': case '$':
492 case ',': case '/': case '?': case '#': case '[': case ']':
493 return 1;
494 }
495 return 0;
496}
497
498static int is_rfc3986_unreserved(char ch)
499{
500 return isalnum(ch) ||
501 ch == '-' || ch == '_' || ch == '.' || ch == '~';
502}
503
504static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
505 int reserved)
506{
507 strbuf_grow(sb, len);
508 while (len--) {
509 char ch = *s++;
510 if (is_rfc3986_unreserved(ch) ||
511 (!reserved && is_rfc3986_reserved(ch)))
512 strbuf_addch(sb, ch);
513 else
514 strbuf_addf(sb, "%%%02x", ch);
515 }
516}
517
518void strbuf_addstr_urlencode(struct strbuf *sb, const char *s,
519 int reserved)
520{
521 strbuf_add_urlencode(sb, s, strlen(s), reserved);
522}
523
524void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes)
525{
526 if (bytes > 1 << 30) {
527 strbuf_addf(buf, "%u.%2.2u GiB",
528 (int)(bytes >> 30),
529 (int)(bytes & ((1 << 30) - 1)) / 10737419);
530 } else if (bytes > 1 << 20) {
531 int x = bytes + 5243; /* for rounding */
532 strbuf_addf(buf, "%u.%2.2u MiB",
533 x >> 20, ((x & ((1 << 20) - 1)) * 100) >> 20);
534 } else if (bytes > 1 << 10) {
535 int x = bytes + 5; /* for rounding */
536 strbuf_addf(buf, "%u.%2.2u KiB",
537 x >> 10, ((x & ((1 << 10) - 1)) * 100) >> 10);
538 } else {
539 strbuf_addf(buf, "%u bytes", (int)bytes);
540 }
541}
542
543int printf_ln(const char *fmt, ...)
544{
545 int ret;
546 va_list ap;
547 va_start(ap, fmt);
548 ret = vprintf(fmt, ap);
549 va_end(ap);
550 if (ret < 0 || putchar('\n') == EOF)
551 return -1;
552 return ret + 1;
553}
554
555int fprintf_ln(FILE *fp, const char *fmt, ...)
556{
557 int ret;
558 va_list ap;
559 va_start(ap, fmt);
560 ret = vfprintf(fp, fmt, ap);
561 va_end(ap);
562 if (ret < 0 || putc('\n', fp) == EOF)
563 return -1;
564 return ret + 1;
565}