]> git.ipfire.org Git - thirdparty/git.git/blame - strbuf.c
run-command: use BUG() to report bugs, not die()
[thirdparty/git.git] / strbuf.c
CommitLineData
812666c8 1#include "cache.h"
a2fab531 2#include "refs.h"
d4241f52 3#include "utf8.h"
d1df5743 4
95662315
CC
5int starts_with(const char *str, const char *prefix)
6{
7 for (; ; str++, prefix++)
8 if (!*prefix)
9 return 1;
10 else if (*str != *prefix)
11 return 0;
12}
13
afaef55e
CC
14int skip_to_optional_arg_default(const char *str, const char *prefix,
15 const char **arg, const char *def)
16{
17 const char *p;
18
19 if (!skip_prefix(str, prefix, &p))
20 return 0;
21
22 if (!*p) {
23 if (arg)
24 *arg = def;
25 return 1;
26 }
27
28 if (*p != '=')
29 return 0;
30
31 if (arg)
32 *arg = p + 1;
33 return 1;
34}
35
b315c5c0
PH
36/*
37 * Used as the default ->buf value, so that people can always assume
38 * buf is non NULL and ->buf is NUL terminated even for a freshly
39 * initialized strbuf.
40 */
41char strbuf_slopbuf[1];
42
917c9a71
PH
43void strbuf_init(struct strbuf *sb, size_t hint)
44{
b315c5c0
PH
45 sb->alloc = sb->len = 0;
46 sb->buf = strbuf_slopbuf;
8c74ef1e 47 if (hint)
f1696ee3 48 strbuf_grow(sb, hint);
d1df5743
JH
49}
50
917c9a71
PH
51void strbuf_release(struct strbuf *sb)
52{
b315c5c0
PH
53 if (sb->alloc) {
54 free(sb->buf);
55 strbuf_init(sb, 0);
56 }
b449f4cf
PH
57}
58
b315c5c0 59char *strbuf_detach(struct strbuf *sb, size_t *sz)
917c9a71 60{
08ad56f3
JK
61 char *res;
62 strbuf_grow(sb, 0);
63 res = sb->buf;
b315c5c0
PH
64 if (sz)
65 *sz = sb->len;
f1696ee3 66 strbuf_init(sb, 0);
b449f4cf
PH
67 return res;
68}
69
917c9a71
PH
70void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
71{
72 strbuf_release(sb);
73 sb->buf = buf;
74 sb->len = len;
75 sb->alloc = alloc;
76 strbuf_grow(sb, 0);
77 sb->buf[sb->len] = '\0';
78}
79
80void strbuf_grow(struct strbuf *sb, size_t extra)
81{
8c74ef1e 82 int new_buf = !sb->alloc;
1368f650
JN
83 if (unsigned_add_overflows(extra, 1) ||
84 unsigned_add_overflows(sb->len, extra + 1))
b449f4cf 85 die("you want to use way too much memory");
8c74ef1e 86 if (new_buf)
b315c5c0 87 sb->buf = NULL;
b449f4cf 88 ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
8c74ef1e
TR
89 if (new_buf)
90 sb->buf[0] = '\0';
b449f4cf
PH
91}
92
eacd6dc5
LS
93void strbuf_trim(struct strbuf *sb)
94{
3bb55e8a
BG
95 strbuf_rtrim(sb);
96 strbuf_ltrim(sb);
eacd6dc5 97}
c64a8d20 98
f1696ee3
PH
99void strbuf_rtrim(struct strbuf *sb)
100{
101 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
102 sb->len--;
103 sb->buf[sb->len] = '\0';
104}
105
c64a8d20
NTND
106void strbuf_trim_trailing_dir_sep(struct strbuf *sb)
107{
108 while (sb->len > 0 && is_dir_sep((unsigned char)sb->buf[sb->len - 1]))
109 sb->len--;
110 sb->buf[sb->len] = '\0';
111}
112
eacd6dc5
LS
113void strbuf_ltrim(struct strbuf *sb)
114{
115 char *b = sb->buf;
116 while (sb->len > 0 && isspace(*b)) {
117 b++;
118 sb->len--;
119 }
120 memmove(sb->buf, b, sb->len);
121 sb->buf[sb->len] = '\0';
122}
123
d4241f52
JK
124int strbuf_reencode(struct strbuf *sb, const char *from, const char *to)
125{
126 char *out;
127 int len;
128
129 if (same_encoding(from, to))
130 return 0;
131
132 out = reencode_string_len(sb->buf, sb->len, to, from, &len);
133 if (!out)
134 return -1;
135
136 strbuf_attach(sb, out, len, len);
137 return 0;
138}
139
ffb20ce1
JK
140void strbuf_tolower(struct strbuf *sb)
141{
142 char *p = sb->buf, *end = sb->buf + sb->len;
143 for (; p < end; p++)
144 *p = tolower(*p);
145}
146
17b73dc6
MH
147struct strbuf **strbuf_split_buf(const char *str, size_t slen,
148 int terminator, int max)
eacd6dc5 149{
b8c2c1fa
MH
150 struct strbuf **ret = NULL;
151 size_t nr = 0, alloc = 0;
eacd6dc5
LS
152 struct strbuf *t;
153
1173bb33
MH
154 while (slen) {
155 int len = slen;
156 if (max <= 0 || nr + 1 < max) {
17b73dc6 157 const char *end = memchr(str, terminator, slen);
1173bb33
MH
158 if (end)
159 len = end - str + 1;
160 }
eacd6dc5
LS
161 t = xmalloc(sizeof(struct strbuf));
162 strbuf_init(t, len);
1173bb33 163 strbuf_add(t, str, len);
b8c2c1fa
MH
164 ALLOC_GROW(ret, nr + 2, alloc);
165 ret[nr++] = t;
1173bb33
MH
166 str += len;
167 slen -= len;
eacd6dc5 168 }
b8c2c1fa
MH
169 ALLOC_GROW(ret, nr + 1, alloc); /* In case string was empty */
170 ret[nr] = NULL;
eacd6dc5
LS
171 return ret;
172}
173
174void strbuf_list_free(struct strbuf **sbs)
175{
176 struct strbuf **s = sbs;
177
178 while (*s) {
179 strbuf_release(*s);
180 free(*s++);
181 }
182 free(sbs);
183}
184
9b200fd6 185int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
45f66f64 186{
8f024655
AR
187 int len = a->len < b->len ? a->len: b->len;
188 int cmp = memcmp(a->buf, b->buf, len);
189 if (cmp)
190 return cmp;
191 return a->len < b->len ? -1: a->len != b->len;
45f66f64
PH
192}
193
917c9a71
PH
194void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
195 const void *data, size_t dlen)
196{
1368f650 197 if (unsigned_add_overflows(pos, len))
917c9a71
PH
198 die("you want to use way too much memory");
199 if (pos > sb->len)
200 die("`pos' is too far after the end of the buffer");
201 if (pos + len > sb->len)
202 die("`pos + len' is too far after the end of the buffer");
203
204 if (dlen >= len)
205 strbuf_grow(sb, dlen - len);
206 memmove(sb->buf + pos + dlen,
207 sb->buf + pos + len,
208 sb->len - pos - len);
209 memcpy(sb->buf + pos, data, dlen);
210 strbuf_setlen(sb, sb->len + dlen - len);
211}
212
c76689df
PH
213void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
214{
215 strbuf_splice(sb, pos, 0, data, len);
216}
217
218void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
219{
a8342a41 220 strbuf_splice(sb, pos, len, "", 0);
c76689df
PH
221}
222
917c9a71
PH
223void strbuf_add(struct strbuf *sb, const void *data, size_t len)
224{
b449f4cf
PH
225 strbuf_grow(sb, len);
226 memcpy(sb->buf + sb->len, data, len);
227 strbuf_setlen(sb, sb->len + len);
228}
229
31471ba2
RS
230void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2)
231{
232 strbuf_grow(sb, sb2->len);
233 memcpy(sb->buf + sb->len, sb2->buf, sb2->len);
234 strbuf_setlen(sb, sb->len + sb2->len);
235}
236
d07235a0
RS
237void strbuf_addchars(struct strbuf *sb, int c, size_t n)
238{
239 strbuf_grow(sb, n);
240 memset(sb->buf + sb->len, c, n);
241 strbuf_setlen(sb, sb->len + n);
242}
243
917c9a71
PH
244void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
245{
b449f4cf 246 va_list ap;
ebeb6090
JK
247 va_start(ap, fmt);
248 strbuf_vaddf(sb, fmt, ap);
249 va_end(ap);
250}
251
eff80a9f
JH
252static void add_lines(struct strbuf *out,
253 const char *prefix1,
254 const char *prefix2,
255 const char *buf, size_t size)
256{
257 while (size) {
258 const char *prefix;
259 const char *next = memchr(buf, '\n', size);
260 next = next ? (next + 1) : (buf + size);
261
d55aeb76
JH
262 prefix = ((prefix2 && (buf[0] == '\n' || buf[0] == '\t'))
263 ? prefix2 : prefix1);
eff80a9f
JH
264 strbuf_addstr(out, prefix);
265 strbuf_add(out, buf, next - buf);
266 size -= next - buf;
267 buf = next;
268 }
269 strbuf_complete_line(out);
270}
271
272void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size)
273{
274 static char prefix1[3];
275 static char prefix2[2];
276
277 if (prefix1[0] != comment_line_char) {
5096d490
JK
278 xsnprintf(prefix1, sizeof(prefix1), "%c ", comment_line_char);
279 xsnprintf(prefix2, sizeof(prefix2), "%c", comment_line_char);
eff80a9f
JH
280 }
281 add_lines(out, prefix1, prefix2, buf, size);
282}
283
284void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
285{
286 va_list params;
287 struct strbuf buf = STRBUF_INIT;
288 int incomplete_line = sb->len && sb->buf[sb->len - 1] != '\n';
289
290 va_start(params, fmt);
291 strbuf_vaddf(&buf, fmt, params);
292 va_end(params);
293
294 strbuf_add_commented_lines(sb, buf.buf, buf.len);
295 if (incomplete_line)
296 sb->buf[--sb->len] = '\0';
297
298 strbuf_release(&buf);
299}
300
ebeb6090
JK
301void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
302{
303 int len;
304 va_list cp;
b449f4cf 305
f141bd80
SP
306 if (!strbuf_avail(sb))
307 strbuf_grow(sb, 64);
ebeb6090
JK
308 va_copy(cp, ap);
309 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, cp);
310 va_end(cp);
f141bd80 311 if (len < 0)
ebeb6090 312 die("BUG: your vsnprintf is broken (returned %d)", len);
f1696ee3 313 if (len > strbuf_avail(sb)) {
b449f4cf 314 strbuf_grow(sb, len);
b449f4cf 315 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
ebeb6090
JK
316 if (len > strbuf_avail(sb))
317 die("BUG: your vsnprintf is broken (insatiable)");
b449f4cf
PH
318 }
319 strbuf_setlen(sb, sb->len + len);
d1df5743
JH
320}
321
c3a670de
MC
322void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
323 void *context)
cde75e59
RS
324{
325 for (;;) {
c3a670de
MC
326 const char *percent;
327 size_t consumed;
cde75e59
RS
328
329 percent = strchrnul(format, '%');
330 strbuf_add(sb, format, percent - format);
331 if (!*percent)
332 break;
333 format = percent + 1;
334
0a0416a3
JK
335 if (*format == '%') {
336 strbuf_addch(sb, '%');
337 format++;
338 continue;
339 }
340
c3a670de
MC
341 consumed = fn(sb, format, context);
342 if (consumed)
343 format += consumed;
344 else
cde75e59
RS
345 strbuf_addch(sb, '%');
346 }
347}
348
9b864e73
RS
349size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
350 void *context)
351{
352 struct strbuf_expand_dict_entry *e = context;
353 size_t len;
354
355 for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
356 if (!strncmp(placeholder, e->placeholder, len)) {
357 if (e->value)
358 strbuf_addstr(sb, e->value);
359 return len;
360 }
361 }
362 return 0;
363}
364
361df5df
JK
365void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
366{
367 int i, len = src->len;
368
369 for (i = 0; i < len; i++) {
370 if (src->buf[i] == '%')
371 strbuf_addch(dst, '%');
372 strbuf_addch(dst, src->buf[i]);
373 }
374}
375
917c9a71
PH
376size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
377{
b449f4cf 378 size_t res;
2fc64700 379 size_t oldalloc = sb->alloc;
b449f4cf
PH
380
381 strbuf_grow(sb, size);
382 res = fread(sb->buf + sb->len, 1, size, f);
2fc64700 383 if (res > 0)
b449f4cf 384 strbuf_setlen(sb, sb->len + res);
6651c3f7 385 else if (oldalloc == 0)
2fc64700 386 strbuf_release(sb);
b449f4cf 387 return res;
d1df5743
JH
388}
389
f1696ee3 390ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
b449f4cf
PH
391{
392 size_t oldlen = sb->len;
2fc64700 393 size_t oldalloc = sb->alloc;
b449f4cf 394
f1696ee3 395 strbuf_grow(sb, hint ? hint : 8192);
b449f4cf 396 for (;;) {
3ebbd00c
JH
397 ssize_t want = sb->alloc - sb->len - 1;
398 ssize_t got = read_in_full(fd, sb->buf + sb->len, want);
b449f4cf 399
3ebbd00c 400 if (got < 0) {
2fc64700
RS
401 if (oldalloc == 0)
402 strbuf_release(sb);
403 else
404 strbuf_setlen(sb, oldlen);
b449f4cf
PH
405 return -1;
406 }
3ebbd00c
JH
407 sb->len += got;
408 if (got < want)
b449f4cf 409 break;
f1696ee3 410 strbuf_grow(sb, 8192);
b449f4cf
PH
411 }
412
413 sb->buf[sb->len] = '\0';
414 return sb->len - oldlen;
d1df5743
JH
415}
416
b4e04fb6
SB
417ssize_t strbuf_read_once(struct strbuf *sb, int fd, size_t hint)
418{
c3ff8f6c 419 size_t oldalloc = sb->alloc;
b4e04fb6
SB
420 ssize_t cnt;
421
422 strbuf_grow(sb, hint ? hint : 8192);
423 cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
424 if (cnt > 0)
425 strbuf_setlen(sb, sb->len + cnt);
c3ff8f6c
RS
426 else if (oldalloc == 0)
427 strbuf_release(sb);
b4e04fb6
SB
428 return cnt;
429}
430
2dac9b56
SB
431ssize_t strbuf_write(struct strbuf *sb, FILE *f)
432{
433 return sb->len ? fwrite(sb->buf, 1, sb->len, f) : 0;
434}
435
436
b11b7e13
LT
437#define STRBUF_MAXLINK (2*PATH_MAX)
438
439int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
440{
2fc64700
RS
441 size_t oldalloc = sb->alloc;
442
b11b7e13
LT
443 if (hint < 32)
444 hint = 32;
445
446 while (hint < STRBUF_MAXLINK) {
447 int len;
448
449 strbuf_grow(sb, hint);
450 len = readlink(path, sb->buf, hint);
451 if (len < 0) {
452 if (errno != ERANGE)
453 break;
454 } else if (len < hint) {
455 strbuf_setlen(sb, len);
456 return 0;
457 }
458
459 /* .. the buffer was too small - try again */
460 hint *= 2;
461 }
2fc64700
RS
462 if (oldalloc == 0)
463 strbuf_release(sb);
b11b7e13
LT
464 return -1;
465}
466
f22a76e9
RS
467int strbuf_getcwd(struct strbuf *sb)
468{
469 size_t oldalloc = sb->alloc;
470 size_t guessed_len = 128;
471
472 for (;; guessed_len *= 2) {
473 strbuf_grow(sb, guessed_len);
474 if (getcwd(sb->buf, sb->alloc)) {
475 strbuf_setlen(sb, strlen(sb->buf));
476 return 0;
477 }
a54e938e
RS
478
479 /*
480 * If getcwd(3) is implemented as a syscall that falls
481 * back to a regular lookup using readdir(3) etc. then
482 * we may be able to avoid EACCES by providing enough
483 * space to the syscall as it's not necessarily bound
484 * to the same restrictions as the fallback.
485 */
486 if (errno == EACCES && guessed_len < PATH_MAX)
487 continue;
488
f22a76e9
RS
489 if (errno != ERANGE)
490 break;
491 }
492 if (oldalloc == 0)
493 strbuf_release(sb);
494 else
495 strbuf_reset(sb);
496 return -1;
497}
498
0cc30e0e
JK
499#ifdef HAVE_GETDELIM
500int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
501{
502 ssize_t r;
503
504 if (feof(fp))
505 return EOF;
506
507 strbuf_reset(sb);
508
509 /* Translate slopbuf to NULL, as we cannot call realloc on it */
510 if (!sb->alloc)
511 sb->buf = NULL;
642956cf 512 errno = 0;
0cc30e0e
JK
513 r = getdelim(&sb->buf, &sb->alloc, term, fp);
514
515 if (r > 0) {
516 sb->len = r;
517 return 0;
518 }
519 assert(r == -1);
520
521 /*
522 * Normally we would have called xrealloc, which will try to free
523 * memory and recover. But we have no way to tell getdelim() to do so.
524 * Worse, we cannot try to recover ENOMEM ourselves, because we have
525 * no idea how many bytes were read by getdelim.
526 *
527 * Dying here is reasonable. It mirrors what xrealloc would do on
528 * catastrophic memory failure. We skip the opportunity to free pack
529 * memory and retry, but that's unlikely to help for a malloc small
530 * enough to hold a single line of input, anyway.
531 */
532 if (errno == ENOMEM)
533 die("Out of memory, getdelim failed");
534
b7090430
JK
535 /*
536 * Restore strbuf invariants; if getdelim left us with a NULL pointer,
537 * we can just re-init, but otherwise we should make sure that our
538 * length is empty, and that the result is NUL-terminated.
539 */
0cc30e0e
JK
540 if (!sb->buf)
541 strbuf_init(sb, 0);
b7090430
JK
542 else
543 strbuf_reset(sb);
0cc30e0e
JK
544 return EOF;
545}
546#else
c7e4f0d7 547int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
917c9a71 548{
d1df5743 549 int ch;
e6c019d0 550
e6c019d0
PH
551 if (feof(fp))
552 return EOF;
b449f4cf
PH
553
554 strbuf_reset(sb);
82912d1d
JK
555 flockfile(fp);
556 while ((ch = getc_unlocked(fp)) != EOF) {
f80c153b
JK
557 if (!strbuf_avail(sb))
558 strbuf_grow(sb, 1);
b449f4cf 559 sb->buf[sb->len++] = ch;
c7e4f0d7
BC
560 if (ch == term)
561 break;
d1df5743 562 }
82912d1d 563 funlockfile(fp);
e6c019d0
PH
564 if (ch == EOF && sb->len == 0)
565 return EOF;
b449f4cf 566
b449f4cf 567 sb->buf[sb->len] = '\0';
e6c019d0 568 return 0;
d1df5743 569}
0cc30e0e 570#endif
a9390b9f 571
1a0c8dfd 572static int strbuf_getdelim(struct strbuf *sb, FILE *fp, int term)
c7e4f0d7
BC
573{
574 if (strbuf_getwholeline(sb, fp, term))
575 return EOF;
dce80bd1
JH
576 if (sb->buf[sb->len - 1] == term)
577 strbuf_setlen(sb, sb->len - 1);
c7e4f0d7
BC
578 return 0;
579}
580
1a0c8dfd 581int strbuf_getline(struct strbuf *sb, FILE *fp)
c8aa9fdf
JH
582{
583 if (strbuf_getwholeline(sb, fp, '\n'))
584 return EOF;
585 if (sb->buf[sb->len - 1] == '\n') {
586 strbuf_setlen(sb, sb->len - 1);
587 if (sb->len && sb->buf[sb->len - 1] == '\r')
588 strbuf_setlen(sb, sb->len - 1);
589 }
590 return 0;
591}
592
8f309aeb
JH
593int strbuf_getline_lf(struct strbuf *sb, FILE *fp)
594{
1a0c8dfd 595 return strbuf_getdelim(sb, fp, '\n');
8f309aeb
JH
596}
597
598int strbuf_getline_nul(struct strbuf *sb, FILE *fp)
599{
1a0c8dfd 600 return strbuf_getdelim(sb, fp, '\0');
8f309aeb
JH
601}
602
5e8617f5
TR
603int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term)
604{
605 strbuf_reset(sb);
606
607 while (1) {
608 char ch;
609 ssize_t len = xread(fd, &ch, 1);
610 if (len <= 0)
611 return EOF;
612 strbuf_addch(sb, ch);
613 if (ch == term)
614 break;
615 }
616 return 0;
617}
618
6c8afe49 619ssize_t strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
a9390b9f 620{
6c8afe49
MH
621 int fd;
622 ssize_t len;
79f0ba15 623 int saved_errno;
a9390b9f
KH
624
625 fd = open(path, O_RDONLY);
626 if (fd < 0)
627 return -1;
387e7e19 628 len = strbuf_read(sb, fd, hint);
79f0ba15 629 saved_errno = errno;
a9390b9f 630 close(fd);
79f0ba15
JK
631 if (len < 0) {
632 errno = saved_errno;
a9390b9f 633 return -1;
79f0ba15 634 }
a9390b9f
KH
635
636 return len;
637}
895680f0
JH
638
639void strbuf_add_lines(struct strbuf *out, const char *prefix,
640 const char *buf, size_t size)
641{
eff80a9f 642 add_lines(out, prefix, NULL, buf, size);
895680f0 643}
367d20ec 644
5963c036
MH
645void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s)
646{
647 while (*s) {
648 size_t len = strcspn(s, "\"<>&");
649 strbuf_add(buf, s, len);
650 s += len;
651 switch (*s) {
652 case '"':
653 strbuf_addstr(buf, "&quot;");
654 break;
655 case '<':
656 strbuf_addstr(buf, "&lt;");
657 break;
658 case '>':
659 strbuf_addstr(buf, "&gt;");
660 break;
661 case '&':
662 strbuf_addstr(buf, "&amp;");
663 break;
664 case 0:
665 return;
666 }
667 s++;
668 }
669}
670
c505116b
JK
671static int is_rfc3986_reserved(char ch)
672{
673 switch (ch) {
674 case '!': case '*': case '\'': case '(': case ')': case ';':
675 case ':': case '@': case '&': case '=': case '+': case '$':
676 case ',': case '/': case '?': case '#': case '[': case ']':
677 return 1;
678 }
679 return 0;
680}
681
682static int is_rfc3986_unreserved(char ch)
683{
684 return isalnum(ch) ||
685 ch == '-' || ch == '_' || ch == '.' || ch == '~';
686}
687
ea03a8e1
JH
688static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
689 int reserved)
c505116b
JK
690{
691 strbuf_grow(sb, len);
692 while (len--) {
693 char ch = *s++;
694 if (is_rfc3986_unreserved(ch) ||
695 (!reserved && is_rfc3986_reserved(ch)))
696 strbuf_addch(sb, ch);
697 else
4c267f2a 698 strbuf_addf(sb, "%%%02x", (unsigned char)ch);
c505116b
JK
699 }
700}
701
702void strbuf_addstr_urlencode(struct strbuf *sb, const char *s,
703 int reserved)
704{
705 strbuf_add_urlencode(sb, s, strlen(s), reserved);
706}
9a0a30aa 707
079b546a
AP
708void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes)
709{
710 if (bytes > 1 << 30) {
711 strbuf_addf(buf, "%u.%2.2u GiB",
712 (int)(bytes >> 30),
713 (int)(bytes & ((1 << 30) - 1)) / 10737419);
714 } else if (bytes > 1 << 20) {
715 int x = bytes + 5243; /* for rounding */
716 strbuf_addf(buf, "%u.%2.2u MiB",
717 x >> 20, ((x & ((1 << 20) - 1)) * 100) >> 20);
718 } else if (bytes > 1 << 10) {
719 int x = bytes + 5; /* for rounding */
720 strbuf_addf(buf, "%u.%2.2u KiB",
721 x >> 10, ((x & ((1 << 10) - 1)) * 100) >> 10);
722 } else {
723 strbuf_addf(buf, "%u bytes", (int)bytes);
724 }
725}
726
679eebe2
RS
727void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
728{
729 if (!*path)
730 die("The empty string is not a valid path");
731 if (!is_absolute_path(path)) {
732 struct stat cwd_stat, pwd_stat;
733 size_t orig_len = sb->len;
734 char *cwd = xgetcwd();
735 char *pwd = getenv("PWD");
736 if (pwd && strcmp(pwd, cwd) &&
737 !stat(cwd, &cwd_stat) &&
738 (cwd_stat.st_dev || cwd_stat.st_ino) &&
739 !stat(pwd, &pwd_stat) &&
740 pwd_stat.st_dev == cwd_stat.st_dev &&
741 pwd_stat.st_ino == cwd_stat.st_ino)
742 strbuf_addstr(sb, pwd);
743 else
744 strbuf_addstr(sb, cwd);
745 if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1]))
746 strbuf_addch(sb, '/');
747 free(cwd);
748 }
749 strbuf_addstr(sb, path);
750}
751
33ad9ddd
RS
752void strbuf_add_real_path(struct strbuf *sb, const char *path)
753{
754 if (sb->len) {
755 struct strbuf resolved = STRBUF_INIT;
756 strbuf_realpath(&resolved, path, 1);
757 strbuf_addbuf(sb, &resolved);
758 strbuf_release(&resolved);
759 } else
760 strbuf_realpath(sb, path, 1);
761}
762
9a0a30aa
NTND
763int printf_ln(const char *fmt, ...)
764{
765 int ret;
766 va_list ap;
767 va_start(ap, fmt);
768 ret = vprintf(fmt, ap);
769 va_end(ap);
770 if (ret < 0 || putchar('\n') == EOF)
771 return -1;
772 return ret + 1;
773}
774
775int fprintf_ln(FILE *fp, const char *fmt, ...)
776{
777 int ret;
778 va_list ap;
779 va_start(ap, fmt);
780 ret = vfprintf(fp, fmt, ap);
781 va_end(ap);
782 if (ret < 0 || putc('\n', fp) == EOF)
783 return -1;
784 return ret + 1;
785}
88d5a6f6
JK
786
787char *xstrdup_tolower(const char *string)
788{
789 char *result;
790 size_t len, i;
791
792 len = strlen(string);
3733e694 793 result = xmallocz(len);
88d5a6f6
JK
794 for (i = 0; i < len; i++)
795 result[i] = tolower(string[i]);
796 result[i] = '\0';
797 return result;
798}
30a0ddb7
JK
799
800char *xstrvfmt(const char *fmt, va_list ap)
801{
802 struct strbuf buf = STRBUF_INIT;
803 strbuf_vaddf(&buf, fmt, ap);
804 return strbuf_detach(&buf, NULL);
805}
806
807char *xstrfmt(const char *fmt, ...)
808{
809 va_list ap;
810 char *ret;
811
812 va_start(ap, fmt);
813 ret = xstrvfmt(fmt, ap);
814 va_end(ap);
815
816 return ret;
817}
aa1462cc 818
c3fbf81a 819void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm,
3b702239 820 int tz_offset, int suppress_tz_name)
aa1462cc 821{
c3fbf81a 822 struct strbuf munged_fmt = STRBUF_INIT;
e4f031e3 823 size_t hint = 128;
aa1462cc
JK
824 size_t len;
825
e4f031e3
JK
826 if (!*fmt)
827 return;
828
c3fbf81a
RS
829 /*
830 * There is no portable way to pass timezone information to
831 * strftime, so we handle %z and %Z here.
832 */
833 for (;;) {
834 const char *percent = strchrnul(fmt, '%');
835 strbuf_add(&munged_fmt, fmt, percent - fmt);
836 if (!*percent)
837 break;
838 fmt = percent + 1;
839 switch (*fmt) {
840 case '%':
841 strbuf_addstr(&munged_fmt, "%%");
842 fmt++;
843 break;
844 case 'z':
845 strbuf_addf(&munged_fmt, "%+05d", tz_offset);
846 fmt++;
847 break;
848 case 'Z':
3b702239 849 if (suppress_tz_name) {
c3fbf81a
RS
850 fmt++;
851 break;
852 }
853 /* FALLTHROUGH */
854 default:
855 strbuf_addch(&munged_fmt, '%');
856 }
857 }
858 fmt = munged_fmt.buf;
859
e4f031e3 860 strbuf_grow(sb, hint);
aa1462cc
JK
861 len = strftime(sb->buf + sb->len, sb->alloc - sb->len, fmt, tm);
862
863 if (!len) {
864 /*
e4f031e3
JK
865 * strftime reports "0" if it could not fit the result in the buffer.
866 * Unfortunately, it also reports "0" if the requested time string
867 * takes 0 bytes. So our strategy is to munge the format so that the
868 * output contains at least one character, and then drop the extra
869 * character before returning.
aa1462cc 870 */
c3fbf81a 871 strbuf_addch(&munged_fmt, ' ');
e4f031e3
JK
872 while (!len) {
873 hint *= 2;
874 strbuf_grow(sb, hint);
875 len = strftime(sb->buf + sb->len, sb->alloc - sb->len,
876 munged_fmt.buf, tm);
877 }
e4f031e3 878 len--; /* drop munged space */
aa1462cc 879 }
c3fbf81a 880 strbuf_release(&munged_fmt);
e4f031e3 881 strbuf_setlen(sb, sb->len + len);
aa1462cc 882}
af49c6d0 883
30e677e0 884void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
af49c6d0
JK
885 int abbrev_len)
886{
887 int r;
888 strbuf_grow(sb, GIT_SHA1_HEXSZ + 1);
aab9583f 889 r = find_unique_abbrev_r(sb->buf + sb->len, oid, abbrev_len);
af49c6d0
JK
890 strbuf_setlen(sb, sb->len + r);
891}
1ad7c0f6 892
63af4a84
TK
893/*
894 * Returns the length of a line, without trailing spaces.
895 *
896 * If the line ends with newline, it will be removed too.
897 */
898static size_t cleanup(char *line, size_t len)
899{
900 while (len) {
901 unsigned char c = line[len - 1];
902 if (!isspace(c))
903 break;
904 len--;
905 }
906
907 return len;
908}
909
910/*
911 * Remove empty lines from the beginning and end
912 * and also trailing spaces from every line.
913 *
914 * Turn multiple consecutive empty lines between paragraphs
915 * into just one empty line.
916 *
917 * If the input has only empty lines and spaces,
918 * no output will be produced.
919 *
920 * If last line does not have a newline at the end, one is added.
921 *
922 * Enable skip_comments to skip every line starting with comment
923 * character.
924 */
925void strbuf_stripspace(struct strbuf *sb, int skip_comments)
926{
927 int empties = 0;
928 size_t i, j, len, newlen;
929 char *eol;
930
931 /* We may have to add a newline. */
932 strbuf_grow(sb, 1);
933
934 for (i = j = 0; i < sb->len; i += len, j += newlen) {
935 eol = memchr(sb->buf + i, '\n', sb->len - i);
936 len = eol ? eol - (sb->buf + i) + 1 : sb->len - i;
937
938 if (skip_comments && len && sb->buf[i] == comment_line_char) {
939 newlen = 0;
940 continue;
941 }
942 newlen = cleanup(sb->buf + i, len);
943
944 /* Not just an empty line? */
945 if (newlen) {
946 if (empties > 0 && j > 0)
947 sb->buf[j++] = '\n';
948 empties = 0;
949 memmove(sb->buf + j, sb->buf + i, newlen);
950 sb->buf[newlen + j++] = '\n';
951 } else {
952 empties++;
953 }
954 }
955
956 strbuf_setlen(sb, j);
957}
670c359d
JK
958
959int strbuf_normalize_path(struct strbuf *src)
960{
961 struct strbuf dst = STRBUF_INIT;
962
963 strbuf_grow(&dst, src->len);
964 if (normalize_path_copy(dst.buf, src->buf) < 0) {
965 strbuf_release(&dst);
966 return -1;
967 }
968
969 /*
970 * normalize_path does not tell us the new length, so we have to
971 * compute it by looking for the new NUL it placed
972 */
973 strbuf_setlen(&dst, strlen(dst.buf));
974 strbuf_swap(src, &dst);
975 strbuf_release(&dst);
976 return 0;
977}