]>
Commit | Line | Data |
---|---|---|
812666c8 | 1 | #include "cache.h" |
a2fab531 | 2 | #include "refs.h" |
d1df5743 | 3 | |
95662315 CC |
4 | int 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 | ||
95662315 CC |
13 | int 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 | ||
b315c5c0 PH |
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 | */ | |
27 | char strbuf_slopbuf[1]; | |
28 | ||
917c9a71 PH |
29 | void strbuf_init(struct strbuf *sb, size_t hint) |
30 | { | |
b315c5c0 PH |
31 | sb->alloc = sb->len = 0; |
32 | sb->buf = strbuf_slopbuf; | |
8c74ef1e | 33 | if (hint) |
f1696ee3 | 34 | strbuf_grow(sb, hint); |
d1df5743 JH |
35 | } |
36 | ||
917c9a71 PH |
37 | void strbuf_release(struct strbuf *sb) |
38 | { | |
b315c5c0 PH |
39 | if (sb->alloc) { |
40 | free(sb->buf); | |
41 | strbuf_init(sb, 0); | |
42 | } | |
b449f4cf PH |
43 | } |
44 | ||
b315c5c0 | 45 | char *strbuf_detach(struct strbuf *sb, size_t *sz) |
917c9a71 | 46 | { |
08ad56f3 JK |
47 | char *res; |
48 | strbuf_grow(sb, 0); | |
49 | res = sb->buf; | |
b315c5c0 PH |
50 | if (sz) |
51 | *sz = sb->len; | |
f1696ee3 | 52 | strbuf_init(sb, 0); |
b449f4cf PH |
53 | return res; |
54 | } | |
55 | ||
917c9a71 PH |
56 | void 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 | ||
66 | void strbuf_grow(struct strbuf *sb, size_t extra) | |
67 | { | |
8c74ef1e | 68 | int new_buf = !sb->alloc; |
1368f650 JN |
69 | if (unsigned_add_overflows(extra, 1) || |
70 | unsigned_add_overflows(sb->len, extra + 1)) | |
b449f4cf | 71 | die("you want to use way too much memory"); |
8c74ef1e | 72 | if (new_buf) |
b315c5c0 | 73 | sb->buf = NULL; |
b449f4cf | 74 | ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc); |
8c74ef1e TR |
75 | if (new_buf) |
76 | sb->buf[0] = '\0'; | |
b449f4cf PH |
77 | } |
78 | ||
eacd6dc5 LS |
79 | void strbuf_trim(struct strbuf *sb) |
80 | { | |
3bb55e8a BG |
81 | strbuf_rtrim(sb); |
82 | strbuf_ltrim(sb); | |
eacd6dc5 | 83 | } |
f1696ee3 PH |
84 | void 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 | ||
eacd6dc5 LS |
91 | void 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 | ||
17b73dc6 MH |
102 | struct strbuf **strbuf_split_buf(const char *str, size_t slen, |
103 | int terminator, int max) | |
eacd6dc5 | 104 | { |
b8c2c1fa MH |
105 | struct strbuf **ret = NULL; |
106 | size_t nr = 0, alloc = 0; | |
eacd6dc5 LS |
107 | struct strbuf *t; |
108 | ||
1173bb33 MH |
109 | while (slen) { |
110 | int len = slen; | |
111 | if (max <= 0 || nr + 1 < max) { | |
17b73dc6 | 112 | const char *end = memchr(str, terminator, slen); |
1173bb33 MH |
113 | if (end) |
114 | len = end - str + 1; | |
115 | } | |
eacd6dc5 LS |
116 | t = xmalloc(sizeof(struct strbuf)); |
117 | strbuf_init(t, len); | |
1173bb33 | 118 | strbuf_add(t, str, len); |
b8c2c1fa MH |
119 | ALLOC_GROW(ret, nr + 2, alloc); |
120 | ret[nr++] = t; | |
1173bb33 MH |
121 | str += len; |
122 | slen -= len; | |
eacd6dc5 | 123 | } |
b8c2c1fa MH |
124 | ALLOC_GROW(ret, nr + 1, alloc); /* In case string was empty */ |
125 | ret[nr] = NULL; | |
eacd6dc5 LS |
126 | return ret; |
127 | } | |
128 | ||
129 | void 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 | ||
9b200fd6 | 140 | int strbuf_cmp(const struct strbuf *a, const struct strbuf *b) |
45f66f64 | 141 | { |
8f024655 AR |
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; | |
45f66f64 PH |
147 | } |
148 | ||
917c9a71 PH |
149 | void strbuf_splice(struct strbuf *sb, size_t pos, size_t len, |
150 | const void *data, size_t dlen) | |
151 | { | |
1368f650 | 152 | if (unsigned_add_overflows(pos, len)) |
917c9a71 PH |
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 | ||
c76689df PH |
168 | void 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 | ||
173 | void strbuf_remove(struct strbuf *sb, size_t pos, size_t len) | |
174 | { | |
175 | strbuf_splice(sb, pos, len, NULL, 0); | |
176 | } | |
177 | ||
917c9a71 PH |
178 | void strbuf_add(struct strbuf *sb, const void *data, size_t len) |
179 | { | |
b449f4cf PH |
180 | strbuf_grow(sb, len); |
181 | memcpy(sb->buf + sb->len, data, len); | |
182 | strbuf_setlen(sb, sb->len + len); | |
183 | } | |
184 | ||
91db267e RS |
185 | void 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 | ||
917c9a71 PH |
192 | void strbuf_addf(struct strbuf *sb, const char *fmt, ...) |
193 | { | |
b449f4cf | 194 | va_list ap; |
ebeb6090 JK |
195 | va_start(ap, fmt); |
196 | strbuf_vaddf(sb, fmt, ap); | |
197 | va_end(ap); | |
198 | } | |
199 | ||
eff80a9f JH |
200 | static 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 | ||
219 | void 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 | ||
231 | void 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 | ||
ebeb6090 JK |
248 | void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap) |
249 | { | |
250 | int len; | |
251 | va_list cp; | |
b449f4cf | 252 | |
f141bd80 SP |
253 | if (!strbuf_avail(sb)) |
254 | strbuf_grow(sb, 64); | |
ebeb6090 JK |
255 | va_copy(cp, ap); |
256 | len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, cp); | |
257 | va_end(cp); | |
f141bd80 | 258 | if (len < 0) |
ebeb6090 | 259 | die("BUG: your vsnprintf is broken (returned %d)", len); |
f1696ee3 | 260 | if (len > strbuf_avail(sb)) { |
b449f4cf | 261 | strbuf_grow(sb, len); |
b449f4cf | 262 | len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap); |
ebeb6090 JK |
263 | if (len > strbuf_avail(sb)) |
264 | die("BUG: your vsnprintf is broken (insatiable)"); | |
b449f4cf PH |
265 | } |
266 | strbuf_setlen(sb, sb->len + len); | |
d1df5743 JH |
267 | } |
268 | ||
c3a670de MC |
269 | void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn, |
270 | void *context) | |
cde75e59 RS |
271 | { |
272 | for (;;) { | |
c3a670de MC |
273 | const char *percent; |
274 | size_t consumed; | |
cde75e59 RS |
275 | |
276 | percent = strchrnul(format, '%'); | |
277 | strbuf_add(sb, format, percent - format); | |
278 | if (!*percent) | |
279 | break; | |
280 | format = percent + 1; | |
281 | ||
0a0416a3 JK |
282 | if (*format == '%') { |
283 | strbuf_addch(sb, '%'); | |
284 | format++; | |
285 | continue; | |
286 | } | |
287 | ||
c3a670de MC |
288 | consumed = fn(sb, format, context); |
289 | if (consumed) | |
290 | format += consumed; | |
291 | else | |
cde75e59 RS |
292 | strbuf_addch(sb, '%'); |
293 | } | |
294 | } | |
295 | ||
9b864e73 RS |
296 | size_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 | ||
361df5df JK |
312 | void 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 | ||
917c9a71 PH |
323 | size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f) |
324 | { | |
b449f4cf | 325 | size_t res; |
2fc64700 | 326 | size_t oldalloc = sb->alloc; |
b449f4cf PH |
327 | |
328 | strbuf_grow(sb, size); | |
329 | res = fread(sb->buf + sb->len, 1, size, f); | |
2fc64700 | 330 | if (res > 0) |
b449f4cf | 331 | strbuf_setlen(sb, sb->len + res); |
6651c3f7 | 332 | else if (oldalloc == 0) |
2fc64700 | 333 | strbuf_release(sb); |
b449f4cf | 334 | return res; |
d1df5743 JH |
335 | } |
336 | ||
f1696ee3 | 337 | ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint) |
b449f4cf PH |
338 | { |
339 | size_t oldlen = sb->len; | |
2fc64700 | 340 | size_t oldalloc = sb->alloc; |
b449f4cf | 341 | |
f1696ee3 | 342 | strbuf_grow(sb, hint ? hint : 8192); |
b449f4cf PH |
343 | for (;;) { |
344 | ssize_t cnt; | |
345 | ||
b449f4cf PH |
346 | cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1); |
347 | if (cnt < 0) { | |
2fc64700 RS |
348 | if (oldalloc == 0) |
349 | strbuf_release(sb); | |
350 | else | |
351 | strbuf_setlen(sb, oldlen); | |
b449f4cf PH |
352 | return -1; |
353 | } | |
354 | if (!cnt) | |
355 | break; | |
356 | sb->len += cnt; | |
f1696ee3 | 357 | strbuf_grow(sb, 8192); |
b449f4cf PH |
358 | } |
359 | ||
360 | sb->buf[sb->len] = '\0'; | |
361 | return sb->len - oldlen; | |
d1df5743 JH |
362 | } |
363 | ||
b11b7e13 LT |
364 | #define STRBUF_MAXLINK (2*PATH_MAX) |
365 | ||
366 | int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint) | |
367 | { | |
2fc64700 RS |
368 | size_t oldalloc = sb->alloc; |
369 | ||
b11b7e13 LT |
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 | } | |
2fc64700 RS |
389 | if (oldalloc == 0) |
390 | strbuf_release(sb); | |
b11b7e13 LT |
391 | return -1; |
392 | } | |
393 | ||
c7e4f0d7 | 394 | int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term) |
917c9a71 | 395 | { |
d1df5743 | 396 | int ch; |
e6c019d0 | 397 | |
e6c019d0 PH |
398 | if (feof(fp)) |
399 | return EOF; | |
b449f4cf PH |
400 | |
401 | strbuf_reset(sb); | |
d1df5743 | 402 | while ((ch = fgetc(fp)) != EOF) { |
b449f4cf PH |
403 | strbuf_grow(sb, 1); |
404 | sb->buf[sb->len++] = ch; | |
c7e4f0d7 BC |
405 | if (ch == term) |
406 | break; | |
d1df5743 | 407 | } |
e6c019d0 PH |
408 | if (ch == EOF && sb->len == 0) |
409 | return EOF; | |
b449f4cf | 410 | |
b449f4cf | 411 | sb->buf[sb->len] = '\0'; |
e6c019d0 | 412 | return 0; |
d1df5743 | 413 | } |
a9390b9f | 414 | |
c7e4f0d7 BC |
415 | int 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 | ||
5e8617f5 TR |
424 | int 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 | ||
387e7e19 | 440 | int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint) |
a9390b9f KH |
441 | { |
442 | int fd, len; | |
443 | ||
444 | fd = open(path, O_RDONLY); | |
445 | if (fd < 0) | |
446 | return -1; | |
387e7e19 | 447 | len = strbuf_read(sb, fd, hint); |
a9390b9f KH |
448 | close(fd); |
449 | if (len < 0) | |
450 | return -1; | |
451 | ||
452 | return len; | |
453 | } | |
895680f0 JH |
454 | |
455 | void strbuf_add_lines(struct strbuf *out, const char *prefix, | |
456 | const char *buf, size_t size) | |
457 | { | |
eff80a9f | 458 | add_lines(out, prefix, NULL, buf, size); |
895680f0 | 459 | } |
367d20ec | 460 | |
5963c036 MH |
461 | void 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 | ||
c505116b JK |
487 | static 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 | ||
498 | static int is_rfc3986_unreserved(char ch) | |
499 | { | |
500 | return isalnum(ch) || | |
501 | ch == '-' || ch == '_' || ch == '.' || ch == '~'; | |
502 | } | |
503 | ||
ea03a8e1 JH |
504 | static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len, |
505 | int reserved) | |
c505116b JK |
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 | ||
518 | void strbuf_addstr_urlencode(struct strbuf *sb, const char *s, | |
519 | int reserved) | |
520 | { | |
521 | strbuf_add_urlencode(sb, s, strlen(s), reserved); | |
522 | } | |
9a0a30aa | 523 | |
079b546a AP |
524 | void 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 | ||
9a0a30aa NTND |
543 | int 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 | ||
555 | int 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 | } |