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