blob: 00abeb55afdd68089676d2007fbfd248b5a45e79 [file] [log] [blame]
Christopher Li812666c2005-04-26 12:00:58 -07001#include "cache.h"
Junio C Hamanoa2fab532009-03-21 14:35:51 -07002#include "refs.h"
Elijah Newrenf6f77552018-04-19 10:58:08 -07003#include "string-list.h"
Jeff Kingd4241f52014-05-22 05:30:14 -04004#include "utf8.h"
Ævar Arnfjörð Bjarmason88c7b4c2022-02-16 09:14:02 +01005#include "date.h"
Junio C Hamanod1df5742005-04-25 18:26:45 -07006
Christian Couder95662312013-12-01 08:49:16 +01007int starts_with(const char *str, const char *prefix)
8{
9 for (; ; str++, prefix++)
10 if (!*prefix)
11 return 1;
12 else if (*str != *prefix)
13 return 0;
14}
15
Lars Schneider66b8af32018-03-09 18:35:29 +010016int istarts_with(const char *str, const char *prefix)
17{
18 for (; ; str++, prefix++)
19 if (!*prefix)
20 return 1;
21 else if (tolower(*str) != tolower(*prefix))
22 return 0;
23}
24
Christian Couderafaef552017-12-09 21:40:07 +010025int skip_to_optional_arg_default(const char *str, const char *prefix,
26 const char **arg, const char *def)
27{
28 const char *p;
29
30 if (!skip_prefix(str, prefix, &p))
31 return 0;
32
33 if (!*p) {
34 if (arg)
35 *arg = def;
36 return 1;
37 }
38
39 if (*p != '=')
40 return 0;
41
42 if (arg)
43 *arg = p + 1;
44 return 1;
45}
46
Pierre Habouzitb315c5c2007-09-27 12:58:23 +020047/*
48 * Used as the default ->buf value, so that people can always assume
49 * buf is non NULL and ->buf is NUL terminated even for a freshly
50 * initialized strbuf.
51 */
52char strbuf_slopbuf[1];
53
Pierre Habouzit917c9a72007-09-15 15:56:50 +020054void strbuf_init(struct strbuf *sb, size_t hint)
55{
Ævar Arnfjörð Bjarmason5726a6b2021-07-01 12:51:26 +020056 struct strbuf blank = STRBUF_INIT;
57 memcpy(sb, &blank, sizeof(*sb));
Thomas Rast8c74ef12011-08-29 23:16:12 +020058 if (hint)
Pierre Habouzitf1696ee2007-09-10 12:35:04 +020059 strbuf_grow(sb, hint);
Junio C Hamanod1df5742005-04-25 18:26:45 -070060}
61
Pierre Habouzit917c9a72007-09-15 15:56:50 +020062void strbuf_release(struct strbuf *sb)
63{
Pierre Habouzitb315c5c2007-09-27 12:58:23 +020064 if (sb->alloc) {
65 free(sb->buf);
66 strbuf_init(sb, 0);
67 }
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020068}
69
Pierre Habouzitb315c5c2007-09-27 12:58:23 +020070char *strbuf_detach(struct strbuf *sb, size_t *sz)
Pierre Habouzit917c9a72007-09-15 15:56:50 +020071{
Jeff King08ad56f2012-10-18 06:00:12 -040072 char *res;
73 strbuf_grow(sb, 0);
74 res = sb->buf;
Pierre Habouzitb315c5c2007-09-27 12:58:23 +020075 if (sz)
76 *sz = sb->len;
Pierre Habouzitf1696ee2007-09-10 12:35:04 +020077 strbuf_init(sb, 0);
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020078 return res;
Junio C Hamanod1df5742005-04-25 18:26:45 -070079}
80
Pierre Habouzit917c9a72007-09-15 15:56:50 +020081void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
82{
83 strbuf_release(sb);
84 sb->buf = buf;
85 sb->len = len;
86 sb->alloc = alloc;
87 strbuf_grow(sb, 0);
88 sb->buf[sb->len] = '\0';
89}
90
91void strbuf_grow(struct strbuf *sb, size_t extra)
92{
Thomas Rast8c74ef12011-08-29 23:16:12 +020093 int new_buf = !sb->alloc;
Jonathan Nieder1368f652010-10-10 21:59:26 -050094 if (unsigned_add_overflows(extra, 1) ||
95 unsigned_add_overflows(sb->len, extra + 1))
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020096 die("you want to use way too much memory");
Thomas Rast8c74ef12011-08-29 23:16:12 +020097 if (new_buf)
Pierre Habouzitb315c5c2007-09-27 12:58:23 +020098 sb->buf = NULL;
Pierre Habouzitb449f4c2007-09-06 13:20:05 +020099 ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
Thomas Rast8c74ef12011-08-29 23:16:12 +0200100 if (new_buf)
101 sb->buf[0] = '\0';
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200102}
103
Lukas Sandströmeacd6dc2008-07-13 20:29:18 +0200104void strbuf_trim(struct strbuf *sb)
105{
Brian Gesiak3bb55e82014-04-30 17:58:06 +0900106 strbuf_rtrim(sb);
107 strbuf_ltrim(sb);
Lukas Sandströmeacd6dc2008-07-13 20:29:18 +0200108}
Nguyễn Thái Ngọc Duyc64a8d22018-02-12 16:49:37 +0700109
Pierre Habouzitf1696ee2007-09-10 12:35:04 +0200110void strbuf_rtrim(struct strbuf *sb)
111{
112 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
113 sb->len--;
114 sb->buf[sb->len] = '\0';
115}
116
Nguyễn Thái Ngọc Duyc64a8d22018-02-12 16:49:37 +0700117void strbuf_trim_trailing_dir_sep(struct strbuf *sb)
118{
119 while (sb->len > 0 && is_dir_sep((unsigned char)sb->buf[sb->len - 1]))
120 sb->len--;
121 sb->buf[sb->len] = '\0';
122}
123
Pratik Karkif9573622018-08-08 20:51:16 +0545124void strbuf_trim_trailing_newline(struct strbuf *sb)
125{
126 if (sb->len > 0 && sb->buf[sb->len - 1] == '\n') {
127 if (--sb->len > 0 && sb->buf[sb->len - 1] == '\r')
128 --sb->len;
129 sb->buf[sb->len] = '\0';
130 }
131}
132
Lukas Sandströmeacd6dc2008-07-13 20:29:18 +0200133void strbuf_ltrim(struct strbuf *sb)
134{
135 char *b = sb->buf;
136 while (sb->len > 0 && isspace(*b)) {
137 b++;
138 sb->len--;
139 }
140 memmove(sb->buf, b, sb->len);
141 sb->buf[sb->len] = '\0';
142}
143
Jeff Kingd4241f52014-05-22 05:30:14 -0400144int strbuf_reencode(struct strbuf *sb, const char *from, const char *to)
145{
146 char *out;
Jeff Kingc7d017d2018-07-24 06:50:33 -0400147 size_t len;
Jeff Kingd4241f52014-05-22 05:30:14 -0400148
149 if (same_encoding(from, to))
150 return 0;
151
152 out = reencode_string_len(sb->buf, sb->len, to, from, &len);
153 if (!out)
154 return -1;
155
156 strbuf_attach(sb, out, len, len);
157 return 0;
158}
159
Jeff Kingffb20ce2014-05-23 16:03:47 -0400160void strbuf_tolower(struct strbuf *sb)
161{
162 char *p = sb->buf, *end = sb->buf + sb->len;
163 for (; p < end; p++)
164 *p = tolower(*p);
165}
166
Michael Haggerty17b73dc2012-11-04 07:46:53 +0100167struct strbuf **strbuf_split_buf(const char *str, size_t slen,
168 int terminator, int max)
Lukas Sandströmeacd6dc2008-07-13 20:29:18 +0200169{
Michael Haggertyb8c2c1f2012-11-04 07:46:51 +0100170 struct strbuf **ret = NULL;
171 size_t nr = 0, alloc = 0;
Lukas Sandströmeacd6dc2008-07-13 20:29:18 +0200172 struct strbuf *t;
173
Michael Haggerty1173bb32012-11-04 07:46:52 +0100174 while (slen) {
175 int len = slen;
176 if (max <= 0 || nr + 1 < max) {
Michael Haggerty17b73dc2012-11-04 07:46:53 +0100177 const char *end = memchr(str, terminator, slen);
Michael Haggerty1173bb32012-11-04 07:46:52 +0100178 if (end)
179 len = end - str + 1;
180 }
Lukas Sandströmeacd6dc2008-07-13 20:29:18 +0200181 t = xmalloc(sizeof(struct strbuf));
182 strbuf_init(t, len);
Michael Haggerty1173bb32012-11-04 07:46:52 +0100183 strbuf_add(t, str, len);
Michael Haggertyb8c2c1f2012-11-04 07:46:51 +0100184 ALLOC_GROW(ret, nr + 2, alloc);
185 ret[nr++] = t;
Michael Haggerty1173bb32012-11-04 07:46:52 +0100186 str += len;
187 slen -= len;
Lukas Sandströmeacd6dc2008-07-13 20:29:18 +0200188 }
Michael Haggertyb8c2c1f2012-11-04 07:46:51 +0100189 ALLOC_GROW(ret, nr + 1, alloc); /* In case string was empty */
190 ret[nr] = NULL;
Lukas Sandströmeacd6dc2008-07-13 20:29:18 +0200191 return ret;
192}
193
Elijah Newrenf6f77552018-04-19 10:58:08 -0700194void strbuf_add_separated_string_list(struct strbuf *str,
195 const char *sep,
196 struct string_list *slist)
197{
198 struct string_list_item *item;
199 int sep_needed = 0;
200
201 for_each_string_list_item(item, slist) {
202 if (sep_needed)
203 strbuf_addstr(str, sep);
204 strbuf_addstr(str, item->string);
205 sep_needed = 1;
206 }
207}
208
Lukas Sandströmeacd6dc2008-07-13 20:29:18 +0200209void strbuf_list_free(struct strbuf **sbs)
210{
211 struct strbuf **s = sbs;
212
Andrzej Huntf3a96802021-04-25 14:16:16 +0000213 if (!s)
214 return;
Lukas Sandströmeacd6dc2008-07-13 20:29:18 +0200215 while (*s) {
216 strbuf_release(*s);
217 free(*s++);
218 }
219 free(sbs);
220}
221
Lukas Sandström9b200fd2008-07-13 20:28:24 +0200222int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
Pierre Habouzit45f66f62007-09-24 11:25:03 +0200223{
Jeff King26114c02018-07-24 06:51:08 -0400224 size_t len = a->len < b->len ? a->len: b->len;
Alex Riesen8f024652009-03-19 23:27:32 +0100225 int cmp = memcmp(a->buf, b->buf, len);
226 if (cmp)
227 return cmp;
228 return a->len < b->len ? -1: a->len != b->len;
Pierre Habouzit45f66f62007-09-24 11:25:03 +0200229}
230
Pierre Habouzit917c9a72007-09-15 15:56:50 +0200231void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
232 const void *data, size_t dlen)
233{
Jonathan Nieder1368f652010-10-10 21:59:26 -0500234 if (unsigned_add_overflows(pos, len))
Pierre Habouzit917c9a72007-09-15 15:56:50 +0200235 die("you want to use way too much memory");
236 if (pos > sb->len)
237 die("`pos' is too far after the end of the buffer");
238 if (pos + len > sb->len)
239 die("`pos + len' is too far after the end of the buffer");
240
241 if (dlen >= len)
242 strbuf_grow(sb, dlen - len);
243 memmove(sb->buf + pos + dlen,
244 sb->buf + pos + len,
245 sb->len - pos - len);
246 memcpy(sb->buf + pos, data, dlen);
247 strbuf_setlen(sb, sb->len + dlen - len);
248}
249
Pierre Habouzitc76689d2007-09-20 00:42:12 +0200250void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
251{
252 strbuf_splice(sb, pos, 0, data, len);
253}
254
Paul-Sebastian Ungureanu5ef264d2019-02-25 23:16:07 +0000255void strbuf_vinsertf(struct strbuf *sb, size_t pos, const char *fmt, va_list ap)
256{
257 int len, len2;
258 char save;
259 va_list cp;
260
261 if (pos > sb->len)
262 die("`pos' is too far after the end of the buffer");
263 va_copy(cp, ap);
264 len = vsnprintf(sb->buf + sb->len, 0, fmt, cp);
265 va_end(cp);
266 if (len < 0)
267 BUG("your vsnprintf is broken (returned %d)", len);
268 if (!len)
269 return; /* nothing to do */
270 if (unsigned_add_overflows(sb->len, len))
271 die("you want to use way too much memory");
272 strbuf_grow(sb, len);
273 memmove(sb->buf + pos + len, sb->buf + pos, sb->len - pos);
274 /* vsnprintf() will append a NUL, overwriting one of our characters */
275 save = sb->buf[pos + len];
276 len2 = vsnprintf(sb->buf + pos, len + 1, fmt, ap);
277 sb->buf[pos + len] = save;
278 if (len2 != len)
279 BUG("your vsnprintf is broken (returns inconsistent lengths)");
280 strbuf_setlen(sb, sb->len + len);
281}
282
283void strbuf_insertf(struct strbuf *sb, size_t pos, const char *fmt, ...)
284{
285 va_list ap;
286 va_start(ap, fmt);
287 strbuf_vinsertf(sb, pos, fmt, ap);
288 va_end(ap);
289}
290
Pierre Habouzitc76689d2007-09-20 00:42:12 +0200291void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
292{
René Scharfea8342a42016-09-13 18:40:22 +0200293 strbuf_splice(sb, pos, len, "", 0);
Pierre Habouzitc76689d2007-09-20 00:42:12 +0200294}
295
Pierre Habouzit917c9a72007-09-15 15:56:50 +0200296void strbuf_add(struct strbuf *sb, const void *data, size_t len)
297{
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200298 strbuf_grow(sb, len);
299 memcpy(sb->buf + sb->len, data, len);
300 strbuf_setlen(sb, sb->len + len);
301}
302
René Scharfe31471ba2016-07-21 18:46:44 +0200303void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2)
304{
305 strbuf_grow(sb, sb2->len);
306 memcpy(sb->buf + sb->len, sb2->buf, sb2->len);
307 strbuf_setlen(sb, sb->len + sb2->len);
308}
309
Paul-Sebastian Ungureanue71c4a82019-02-25 23:16:06 +0000310const char *strbuf_join_argv(struct strbuf *buf,
311 int argc, const char **argv, char delim)
312{
313 if (!argc)
314 return buf->buf;
315
316 strbuf_addstr(buf, *argv);
317 while (--argc) {
318 strbuf_addch(buf, delim);
319 strbuf_addstr(buf, *(++argv));
320 }
321
322 return buf->buf;
323}
324
René Scharfed07235a2014-09-07 09:03:32 +0200325void strbuf_addchars(struct strbuf *sb, int c, size_t n)
326{
327 strbuf_grow(sb, n);
328 memset(sb->buf + sb->len, c, n);
329 strbuf_setlen(sb, sb->len + n);
330}
331
Pierre Habouzit917c9a72007-09-15 15:56:50 +0200332void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
333{
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200334 va_list ap;
Jeff Kingebeb6092011-02-25 23:08:53 -0600335 va_start(ap, fmt);
336 strbuf_vaddf(sb, fmt, ap);
337 va_end(ap);
338}
339
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100340static void add_lines(struct strbuf *out,
341 const char *prefix1,
342 const char *prefix2,
343 const char *buf, size_t size)
344{
345 while (size) {
346 const char *prefix;
347 const char *next = memchr(buf, '\n', size);
348 next = next ? (next + 1) : (buf + size);
349
Junio C Hamanod55aeb72014-10-27 14:13:15 -0700350 prefix = ((prefix2 && (buf[0] == '\n' || buf[0] == '\t'))
351 ? prefix2 : prefix1);
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100352 strbuf_addstr(out, prefix);
353 strbuf_add(out, buf, next - buf);
354 size -= next - buf;
355 buf = next;
356 }
357 strbuf_complete_line(out);
358}
359
360void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size)
361{
362 static char prefix1[3];
363 static char prefix2[2];
364
365 if (prefix1[0] != comment_line_char) {
Jeff King5096d492015-09-24 17:06:08 -0400366 xsnprintf(prefix1, sizeof(prefix1), "%c ", comment_line_char);
367 xsnprintf(prefix2, sizeof(prefix2), "%c", comment_line_char);
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100368 }
369 add_lines(out, prefix1, prefix2, buf, size);
370}
371
372void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
373{
374 va_list params;
375 struct strbuf buf = STRBUF_INIT;
376 int incomplete_line = sb->len && sb->buf[sb->len - 1] != '\n';
377
378 va_start(params, fmt);
379 strbuf_vaddf(&buf, fmt, params);
380 va_end(params);
381
382 strbuf_add_commented_lines(sb, buf.buf, buf.len);
383 if (incomplete_line)
384 sb->buf[--sb->len] = '\0';
385
386 strbuf_release(&buf);
387}
388
Jeff Kingebeb6092011-02-25 23:08:53 -0600389void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
390{
391 int len;
392 va_list cp;
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200393
Shawn O. Pearcef141bd82007-11-13 03:22:44 -0500394 if (!strbuf_avail(sb))
395 strbuf_grow(sb, 64);
Jeff Kingebeb6092011-02-25 23:08:53 -0600396 va_copy(cp, ap);
397 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, cp);
398 va_end(cp);
Shawn O. Pearcef141bd82007-11-13 03:22:44 -0500399 if (len < 0)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200400 BUG("your vsnprintf is broken (returned %d)", len);
Pierre Habouzitf1696ee2007-09-10 12:35:04 +0200401 if (len > strbuf_avail(sb)) {
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200402 strbuf_grow(sb, len);
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200403 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
Jeff Kingebeb6092011-02-25 23:08:53 -0600404 if (len > strbuf_avail(sb))
Johannes Schindelin033abf92018-05-02 11:38:39 +0200405 BUG("your vsnprintf is broken (insatiable)");
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200406 }
407 strbuf_setlen(sb, sb->len + len);
Junio C Hamanod1df5742005-04-25 18:26:45 -0700408}
409
Marco Costalbac3a670d2008-02-09 15:40:19 +0100410void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
411 void *context)
René Scharfecde75e52007-11-09 01:49:42 +0100412{
413 for (;;) {
Marco Costalbac3a670d2008-02-09 15:40:19 +0100414 const char *percent;
415 size_t consumed;
René Scharfecde75e52007-11-09 01:49:42 +0100416
417 percent = strchrnul(format, '%');
418 strbuf_add(sb, format, percent - format);
419 if (!*percent)
420 break;
421 format = percent + 1;
422
Jeff King0a0416a2010-01-13 12:35:31 -0500423 if (*format == '%') {
424 strbuf_addch(sb, '%');
425 format++;
426 continue;
427 }
428
Marco Costalbac3a670d2008-02-09 15:40:19 +0100429 consumed = fn(sb, format, context);
430 if (consumed)
431 format += consumed;
432 else
René Scharfecde75e52007-11-09 01:49:42 +0100433 strbuf_addch(sb, '%');
434 }
435}
436
Anders Waldenborgfd2015b2019-01-28 22:33:36 +0100437size_t strbuf_expand_literal_cb(struct strbuf *sb,
438 const char *placeholder,
439 void *context)
440{
441 int ch;
442
443 switch (placeholder[0]) {
444 case 'n': /* newline */
445 strbuf_addch(sb, '\n');
446 return 1;
447 case 'x':
448 /* %x00 == NUL, %x0a == LF, etc. */
449 ch = hex2chr(placeholder + 1);
450 if (ch < 0)
451 return 0;
452 strbuf_addch(sb, ch);
453 return 3;
454 }
455 return 0;
456}
457
René Scharfe9b864e72008-11-23 00:09:30 +0100458size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
459 void *context)
460{
461 struct strbuf_expand_dict_entry *e = context;
462 size_t len;
463
464 for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
465 if (!strncmp(placeholder, e->placeholder, len)) {
466 if (e->value)
467 strbuf_addstr(sb, e->value);
468 return len;
469 }
470 }
471 return 0;
472}
473
Jeff King361df5d2010-01-13 12:36:42 -0500474void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
475{
Jeff King26114c02018-07-24 06:51:08 -0400476 size_t i, len = src->len;
Jeff King361df5d2010-01-13 12:36:42 -0500477
478 for (i = 0; i < len; i++) {
479 if (src->buf[i] == '%')
480 strbuf_addch(dst, '%');
481 strbuf_addch(dst, src->buf[i]);
482 }
483}
484
brian m. carlsonb44d0112020-04-27 01:18:08 +0000485#define URL_UNSAFE_CHARS " <>\"%{}|\\^`:?#[]@!$&'()*+,;="
brian m. carlson46fd7b32020-02-20 02:24:13 +0000486
brian m. carlsonb44d0112020-04-27 01:18:08 +0000487void strbuf_add_percentencode(struct strbuf *dst, const char *src, int flags)
brian m. carlson46fd7b32020-02-20 02:24:13 +0000488{
489 size_t i, len = strlen(src);
490
491 for (i = 0; i < len; i++) {
492 unsigned char ch = src[i];
brian m. carlsonb44d0112020-04-27 01:18:08 +0000493 if (ch <= 0x1F || ch >= 0x7F ||
494 (ch == '/' && (flags & STRBUF_ENCODE_SLASH)) ||
495 strchr(URL_UNSAFE_CHARS, ch))
brian m. carlson46fd7b32020-02-20 02:24:13 +0000496 strbuf_addf(dst, "%%%02X", (unsigned char)ch);
497 else
498 strbuf_addch(dst, ch);
499 }
500}
501
Pierre Habouzit917c9a72007-09-15 15:56:50 +0200502size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
503{
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200504 size_t res;
René Scharfe2fc64702009-01-06 21:41:14 +0100505 size_t oldalloc = sb->alloc;
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200506
507 strbuf_grow(sb, size);
508 res = fread(sb->buf + sb->len, 1, size, f);
René Scharfe2fc64702009-01-06 21:41:14 +0100509 if (res > 0)
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200510 strbuf_setlen(sb, sb->len + res);
Roel Kluin6651c3f2009-06-22 18:42:33 +0200511 else if (oldalloc == 0)
René Scharfe2fc64702009-01-06 21:41:14 +0100512 strbuf_release(sb);
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200513 return res;
514}
515
Pierre Habouzitf1696ee2007-09-10 12:35:04 +0200516ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200517{
518 size_t oldlen = sb->len;
René Scharfe2fc64702009-01-06 21:41:14 +0100519 size_t oldalloc = sb->alloc;
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200520
Pierre Habouzitf1696ee2007-09-10 12:35:04 +0200521 strbuf_grow(sb, hint ? hint : 8192);
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200522 for (;;) {
Jim Hill3ebbd002015-05-31 11:16:45 -0700523 ssize_t want = sb->alloc - sb->len - 1;
524 ssize_t got = read_in_full(fd, sb->buf + sb->len, want);
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200525
Jim Hill3ebbd002015-05-31 11:16:45 -0700526 if (got < 0) {
René Scharfe2fc64702009-01-06 21:41:14 +0100527 if (oldalloc == 0)
528 strbuf_release(sb);
529 else
530 strbuf_setlen(sb, oldlen);
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200531 return -1;
532 }
Jim Hill3ebbd002015-05-31 11:16:45 -0700533 sb->len += got;
534 if (got < want)
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200535 break;
Pierre Habouzitf1696ee2007-09-10 12:35:04 +0200536 strbuf_grow(sb, 8192);
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200537 }
538
539 sb->buf[sb->len] = '\0';
540 return sb->len - oldlen;
Junio C Hamanod1df5742005-04-25 18:26:45 -0700541}
542
Stefan Bellerb4e04fb2015-12-15 16:04:08 -0800543ssize_t strbuf_read_once(struct strbuf *sb, int fd, size_t hint)
544{
René Scharfec3ff8f62017-12-07 21:51:26 +0100545 size_t oldalloc = sb->alloc;
Stefan Bellerb4e04fb2015-12-15 16:04:08 -0800546 ssize_t cnt;
547
548 strbuf_grow(sb, hint ? hint : 8192);
549 cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
550 if (cnt > 0)
551 strbuf_setlen(sb, sb->len + cnt);
René Scharfec3ff8f62017-12-07 21:51:26 +0100552 else if (oldalloc == 0)
553 strbuf_release(sb);
Stefan Bellerb4e04fb2015-12-15 16:04:08 -0800554 return cnt;
555}
556
Stefan Beller2dac9b52016-02-29 18:07:15 -0800557ssize_t strbuf_write(struct strbuf *sb, FILE *f)
558{
559 return sb->len ? fwrite(sb->buf, 1, sb->len, f) : 0;
560}
561
Linus Torvaldsb11b7e12008-12-17 09:36:40 -0800562#define STRBUF_MAXLINK (2*PATH_MAX)
563
564int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
565{
René Scharfe2fc64702009-01-06 21:41:14 +0100566 size_t oldalloc = sb->alloc;
567
Linus Torvaldsb11b7e12008-12-17 09:36:40 -0800568 if (hint < 32)
569 hint = 32;
570
571 while (hint < STRBUF_MAXLINK) {
Jeff Kingf3e76ed2018-07-24 06:51:25 -0400572 ssize_t len;
Linus Torvaldsb11b7e12008-12-17 09:36:40 -0800573
574 strbuf_grow(sb, hint);
575 len = readlink(path, sb->buf, hint);
576 if (len < 0) {
577 if (errno != ERANGE)
578 break;
579 } else if (len < hint) {
580 strbuf_setlen(sb, len);
581 return 0;
582 }
583
584 /* .. the buffer was too small - try again */
585 hint *= 2;
586 }
René Scharfe2fc64702009-01-06 21:41:14 +0100587 if (oldalloc == 0)
588 strbuf_release(sb);
Linus Torvaldsb11b7e12008-12-17 09:36:40 -0800589 return -1;
590}
591
René Scharfef22a76e2014-07-28 20:24:29 +0200592int strbuf_getcwd(struct strbuf *sb)
593{
594 size_t oldalloc = sb->alloc;
595 size_t guessed_len = 128;
596
597 for (;; guessed_len *= 2) {
598 strbuf_grow(sb, guessed_len);
599 if (getcwd(sb->buf, sb->alloc)) {
600 strbuf_setlen(sb, strlen(sb->buf));
601 return 0;
602 }
René Scharfea54e9382017-03-26 15:43:50 +0200603
604 /*
605 * If getcwd(3) is implemented as a syscall that falls
606 * back to a regular lookup using readdir(3) etc. then
607 * we may be able to avoid EACCES by providing enough
608 * space to the syscall as it's not necessarily bound
609 * to the same restrictions as the fallback.
610 */
611 if (errno == EACCES && guessed_len < PATH_MAX)
612 continue;
613
René Scharfef22a76e2014-07-28 20:24:29 +0200614 if (errno != ERANGE)
615 break;
616 }
617 if (oldalloc == 0)
618 strbuf_release(sb);
619 else
620 strbuf_reset(sb);
621 return -1;
622}
623
Jeff King0cc30e02015-04-16 05:01:38 -0400624#ifdef HAVE_GETDELIM
625int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
626{
627 ssize_t r;
628
629 if (feof(fp))
630 return EOF;
631
632 strbuf_reset(sb);
633
634 /* Translate slopbuf to NULL, as we cannot call realloc on it */
635 if (!sb->alloc)
636 sb->buf = NULL;
René Scharfe642956c2017-08-10 22:56:40 +0200637 errno = 0;
Jeff King0cc30e02015-04-16 05:01:38 -0400638 r = getdelim(&sb->buf, &sb->alloc, term, fp);
639
640 if (r > 0) {
641 sb->len = r;
642 return 0;
643 }
644 assert(r == -1);
645
646 /*
647 * Normally we would have called xrealloc, which will try to free
648 * memory and recover. But we have no way to tell getdelim() to do so.
649 * Worse, we cannot try to recover ENOMEM ourselves, because we have
650 * no idea how many bytes were read by getdelim.
651 *
652 * Dying here is reasonable. It mirrors what xrealloc would do on
653 * catastrophic memory failure. We skip the opportunity to free pack
654 * memory and retry, but that's unlikely to help for a malloc small
655 * enough to hold a single line of input, anyway.
656 */
657 if (errno == ENOMEM)
658 die("Out of memory, getdelim failed");
659
Jeff Kingb7090432016-03-05 13:43:30 -0500660 /*
661 * Restore strbuf invariants; if getdelim left us with a NULL pointer,
662 * we can just re-init, but otherwise we should make sure that our
663 * length is empty, and that the result is NUL-terminated.
664 */
Jeff King0cc30e02015-04-16 05:01:38 -0400665 if (!sb->buf)
666 strbuf_init(sb, 0);
Jeff Kingb7090432016-03-05 13:43:30 -0500667 else
668 strbuf_reset(sb);
Jeff King0cc30e02015-04-16 05:01:38 -0400669 return EOF;
670}
671#else
Brandon Caseyc7e4f0d2009-08-04 22:31:56 -0500672int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
Pierre Habouzit917c9a72007-09-15 15:56:50 +0200673{
Junio C Hamanod1df5742005-04-25 18:26:45 -0700674 int ch;
Pierre Habouzite6c019d2007-09-17 11:19:04 +0200675
Pierre Habouzite6c019d2007-09-17 11:19:04 +0200676 if (feof(fp))
677 return EOF;
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200678
679 strbuf_reset(sb);
Jeff King82912d12015-04-16 04:49:06 -0400680 flockfile(fp);
681 while ((ch = getc_unlocked(fp)) != EOF) {
Jeff Kingf80c1532015-04-16 04:58:54 -0400682 if (!strbuf_avail(sb))
683 strbuf_grow(sb, 1);
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200684 sb->buf[sb->len++] = ch;
Brandon Caseyc7e4f0d2009-08-04 22:31:56 -0500685 if (ch == term)
686 break;
Junio C Hamanod1df5742005-04-25 18:26:45 -0700687 }
Jeff King82912d12015-04-16 04:49:06 -0400688 funlockfile(fp);
Pierre Habouzite6c019d2007-09-17 11:19:04 +0200689 if (ch == EOF && sb->len == 0)
690 return EOF;
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200691
Pierre Habouzitb449f4c2007-09-06 13:20:05 +0200692 sb->buf[sb->len] = '\0';
Pierre Habouzite6c019d2007-09-17 11:19:04 +0200693 return 0;
Junio C Hamanod1df5742005-04-25 18:26:45 -0700694}
Jeff King0cc30e02015-04-16 05:01:38 -0400695#endif
Kristian Høgsberga9390b92007-09-17 20:06:46 -0400696
Patrick Steinhardtbd021f32020-03-30 15:46:27 +0200697int strbuf_appendwholeline(struct strbuf *sb, FILE *fp, int term)
698{
699 struct strbuf line = STRBUF_INIT;
700 if (strbuf_getwholeline(&line, fp, term))
701 return EOF;
702 strbuf_addbuf(sb, &line);
703 strbuf_release(&line);
704 return 0;
705}
706
Junio C Hamano1a0c8df2016-01-13 18:32:23 -0800707static int strbuf_getdelim(struct strbuf *sb, FILE *fp, int term)
Brandon Caseyc7e4f0d2009-08-04 22:31:56 -0500708{
709 if (strbuf_getwholeline(sb, fp, term))
710 return EOF;
Junio C Hamanodce80bd2016-01-13 15:10:45 -0800711 if (sb->buf[sb->len - 1] == term)
712 strbuf_setlen(sb, sb->len - 1);
Brandon Caseyc7e4f0d2009-08-04 22:31:56 -0500713 return 0;
714}
715
Junio C Hamano1a0c8df2016-01-13 18:32:23 -0800716int strbuf_getline(struct strbuf *sb, FILE *fp)
Junio C Hamanoc8aa9fd2015-10-28 13:17:29 -0700717{
718 if (strbuf_getwholeline(sb, fp, '\n'))
719 return EOF;
720 if (sb->buf[sb->len - 1] == '\n') {
721 strbuf_setlen(sb, sb->len - 1);
722 if (sb->len && sb->buf[sb->len - 1] == '\r')
723 strbuf_setlen(sb, sb->len - 1);
724 }
725 return 0;
726}
727
Junio C Hamano8f309ae2016-01-13 15:31:17 -0800728int strbuf_getline_lf(struct strbuf *sb, FILE *fp)
729{
Junio C Hamano1a0c8df2016-01-13 18:32:23 -0800730 return strbuf_getdelim(sb, fp, '\n');
Junio C Hamano8f309ae2016-01-13 15:31:17 -0800731}
732
733int strbuf_getline_nul(struct strbuf *sb, FILE *fp)
734{
Junio C Hamano1a0c8df2016-01-13 18:32:23 -0800735 return strbuf_getdelim(sb, fp, '\0');
Junio C Hamano8f309ae2016-01-13 15:31:17 -0800736}
737
Thomas Rast5e8617f2012-02-22 20:34:22 +0100738int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term)
739{
740 strbuf_reset(sb);
741
742 while (1) {
743 char ch;
744 ssize_t len = xread(fd, &ch, 1);
745 if (len <= 0)
746 return EOF;
747 strbuf_addch(sb, ch);
748 if (ch == term)
749 break;
750 }
751 return 0;
752}
753
Michael Haggerty6c8afe42015-07-03 15:59:32 +0200754ssize_t strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
Kristian Høgsberga9390b92007-09-17 20:06:46 -0400755{
Michael Haggerty6c8afe42015-07-03 15:59:32 +0200756 int fd;
757 ssize_t len;
Jeff King79f0ba12018-02-23 02:00:54 -0500758 int saved_errno;
Kristian Høgsberga9390b92007-09-17 20:06:46 -0400759
760 fd = open(path, O_RDONLY);
761 if (fd < 0)
762 return -1;
Pierre Habouzit387e7e12007-09-27 15:25:55 +0200763 len = strbuf_read(sb, fd, hint);
Jeff King79f0ba12018-02-23 02:00:54 -0500764 saved_errno = errno;
Kristian Høgsberga9390b92007-09-17 20:06:46 -0400765 close(fd);
Jeff King79f0ba12018-02-23 02:00:54 -0500766 if (len < 0) {
767 errno = saved_errno;
Kristian Høgsberga9390b92007-09-17 20:06:46 -0400768 return -1;
Jeff King79f0ba12018-02-23 02:00:54 -0500769 }
Kristian Høgsberga9390b92007-09-17 20:06:46 -0400770
771 return len;
772}
Junio C Hamano895680f2011-11-04 21:06:30 -0700773
774void strbuf_add_lines(struct strbuf *out, const char *prefix,
775 const char *buf, size_t size)
776{
Junio C Hamanoeff80a92013-01-16 20:18:48 +0100777 add_lines(out, prefix, NULL, buf, size);
Junio C Hamano895680f2011-11-04 21:06:30 -0700778}
Junio C Hamano367d20e2011-12-19 16:05:16 -0800779
Michael Haggerty5963c032012-11-25 12:08:34 +0100780void strbuf_addstr_xml_quoted(struct strbuf *buf, const char *s)
781{
782 while (*s) {
783 size_t len = strcspn(s, "\"<>&");
784 strbuf_add(buf, s, len);
785 s += len;
786 switch (*s) {
787 case '"':
788 strbuf_addstr(buf, "&quot;");
789 break;
790 case '<':
791 strbuf_addstr(buf, "&lt;");
792 break;
793 case '>':
794 strbuf_addstr(buf, "&gt;");
795 break;
796 case '&':
797 strbuf_addstr(buf, "&amp;");
798 break;
799 case 0:
800 return;
801 }
802 s++;
803 }
804}
805
Matthew DeVorec2694952019-06-27 15:54:11 -0700806int is_rfc3986_reserved_or_unreserved(char ch)
Jeff Kingc5051162011-12-10 05:34:20 -0500807{
Matthew DeVorec2694952019-06-27 15:54:11 -0700808 if (is_rfc3986_unreserved(ch))
809 return 1;
Jeff Kingc5051162011-12-10 05:34:20 -0500810 switch (ch) {
811 case '!': case '*': case '\'': case '(': case ')': case ';':
812 case ':': case '@': case '&': case '=': case '+': case '$':
813 case ',': case '/': case '?': case '#': case '[': case ']':
814 return 1;
815 }
816 return 0;
817}
818
Matthew DeVorec2694952019-06-27 15:54:11 -0700819int is_rfc3986_unreserved(char ch)
Jeff Kingc5051162011-12-10 05:34:20 -0500820{
821 return isalnum(ch) ||
822 ch == '-' || ch == '_' || ch == '.' || ch == '~';
823}
824
Junio C Hamanoea03a8e2012-09-15 22:43:15 -0700825static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
Matthew DeVorec2694952019-06-27 15:54:11 -0700826 char_predicate allow_unencoded_fn)
Jeff Kingc5051162011-12-10 05:34:20 -0500827{
828 strbuf_grow(sb, len);
829 while (len--) {
830 char ch = *s++;
Matthew DeVorec2694952019-06-27 15:54:11 -0700831 if (allow_unencoded_fn(ch))
Jeff Kingc5051162011-12-10 05:34:20 -0500832 strbuf_addch(sb, ch);
833 else
Julien Dusser4c267f22017-12-22 18:24:37 +0100834 strbuf_addf(sb, "%%%02x", (unsigned char)ch);
Jeff Kingc5051162011-12-10 05:34:20 -0500835 }
836}
837
838void strbuf_addstr_urlencode(struct strbuf *sb, const char *s,
Matthew DeVorec2694952019-06-27 15:54:11 -0700839 char_predicate allow_unencoded_fn)
Jeff Kingc5051162011-12-10 05:34:20 -0500840{
Matthew DeVorec2694952019-06-27 15:54:11 -0700841 strbuf_add_urlencode(sb, s, strlen(s), allow_unencoded_fn);
Jeff Kingc5051162011-12-10 05:34:20 -0500842}
Nguyễn Thái Ngọc Duy9a0a30a2012-04-23 19:30:22 +0700843
Dimitriy Ryazantcev8f354a12019-07-02 21:22:48 +0300844static void strbuf_humanise(struct strbuf *buf, off_t bytes,
845 int humanise_rate)
Antoine Pelisse079b5462013-04-10 21:03:23 +0200846{
847 if (bytes > 1 << 30) {
Dimitriy Ryazantcev8f354a12019-07-02 21:22:48 +0300848 strbuf_addf(buf,
849 humanise_rate == 0 ?
850 /* TRANSLATORS: IEC 80000-13:2008 gibibyte */
851 _("%u.%2.2u GiB") :
852 /* TRANSLATORS: IEC 80000-13:2008 gibibyte/second */
853 _("%u.%2.2u GiB/s"),
Jeff King7726d362018-07-24 06:52:29 -0400854 (unsigned)(bytes >> 30),
855 (unsigned)(bytes & ((1 << 30) - 1)) / 10737419);
Antoine Pelisse079b5462013-04-10 21:03:23 +0200856 } else if (bytes > 1 << 20) {
Jeff King7726d362018-07-24 06:52:29 -0400857 unsigned x = bytes + 5243; /* for rounding */
Dimitriy Ryazantcev8f354a12019-07-02 21:22:48 +0300858 strbuf_addf(buf,
859 humanise_rate == 0 ?
860 /* TRANSLATORS: IEC 80000-13:2008 mebibyte */
861 _("%u.%2.2u MiB") :
862 /* TRANSLATORS: IEC 80000-13:2008 mebibyte/second */
863 _("%u.%2.2u MiB/s"),
Antoine Pelisse079b5462013-04-10 21:03:23 +0200864 x >> 20, ((x & ((1 << 20) - 1)) * 100) >> 20);
865 } else if (bytes > 1 << 10) {
Jeff King7726d362018-07-24 06:52:29 -0400866 unsigned x = bytes + 5; /* for rounding */
Dimitriy Ryazantcev8f354a12019-07-02 21:22:48 +0300867 strbuf_addf(buf,
868 humanise_rate == 0 ?
869 /* TRANSLATORS: IEC 80000-13:2008 kibibyte */
870 _("%u.%2.2u KiB") :
871 /* TRANSLATORS: IEC 80000-13:2008 kibibyte/second */
872 _("%u.%2.2u KiB/s"),
Antoine Pelisse079b5462013-04-10 21:03:23 +0200873 x >> 10, ((x & ((1 << 10) - 1)) * 100) >> 10);
874 } else {
Dimitriy Ryazantcev8f354a12019-07-02 21:22:48 +0300875 strbuf_addf(buf,
876 humanise_rate == 0 ?
877 /* TRANSLATORS: IEC 80000-13:2008 byte */
878 Q_("%u byte", "%u bytes", (unsigned)bytes) :
879 /* TRANSLATORS: IEC 80000-13:2008 byte/second */
880 Q_("%u byte/s", "%u bytes/s", (unsigned)bytes),
881 (unsigned)bytes);
Antoine Pelisse079b5462013-04-10 21:03:23 +0200882 }
883}
884
Dimitriy Ryazantcev8f354a12019-07-02 21:22:48 +0300885void strbuf_humanise_bytes(struct strbuf *buf, off_t bytes)
886{
887 strbuf_humanise(buf, bytes, 0);
888}
889
890void strbuf_humanise_rate(struct strbuf *buf, off_t bytes)
891{
892 strbuf_humanise(buf, bytes, 1);
893}
894
René Scharfe679eebe2014-07-28 20:33:55 +0200895void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
896{
897 if (!*path)
898 die("The empty string is not a valid path");
899 if (!is_absolute_path(path)) {
900 struct stat cwd_stat, pwd_stat;
901 size_t orig_len = sb->len;
902 char *cwd = xgetcwd();
903 char *pwd = getenv("PWD");
904 if (pwd && strcmp(pwd, cwd) &&
905 !stat(cwd, &cwd_stat) &&
906 (cwd_stat.st_dev || cwd_stat.st_ino) &&
907 !stat(pwd, &pwd_stat) &&
908 pwd_stat.st_dev == cwd_stat.st_dev &&
909 pwd_stat.st_ino == cwd_stat.st_ino)
910 strbuf_addstr(sb, pwd);
911 else
912 strbuf_addstr(sb, cwd);
913 if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1]))
914 strbuf_addch(sb, '/');
915 free(cwd);
916 }
917 strbuf_addstr(sb, path);
918}
919
René Scharfe33ad9dd2017-02-25 17:00:33 +0100920void strbuf_add_real_path(struct strbuf *sb, const char *path)
921{
922 if (sb->len) {
923 struct strbuf resolved = STRBUF_INIT;
924 strbuf_realpath(&resolved, path, 1);
925 strbuf_addbuf(sb, &resolved);
926 strbuf_release(&resolved);
927 } else
928 strbuf_realpath(sb, path, 1);
929}
930
Nguyễn Thái Ngọc Duy9a0a30a2012-04-23 19:30:22 +0700931int printf_ln(const char *fmt, ...)
932{
933 int ret;
934 va_list ap;
935 va_start(ap, fmt);
936 ret = vprintf(fmt, ap);
937 va_end(ap);
938 if (ret < 0 || putchar('\n') == EOF)
939 return -1;
940 return ret + 1;
941}
942
943int fprintf_ln(FILE *fp, const char *fmt, ...)
944{
945 int ret;
946 va_list ap;
947 va_start(ap, fmt);
948 ret = vfprintf(fp, fmt, ap);
949 va_end(ap);
950 if (ret < 0 || putc('\n', fp) == EOF)
951 return -1;
952 return ret + 1;
953}
Jeff King88d5a6f2014-05-22 05:44:09 -0400954
955char *xstrdup_tolower(const char *string)
956{
957 char *result;
958 size_t len, i;
959
960 len = strlen(string);
Jeff King3733e692016-02-22 17:44:28 -0500961 result = xmallocz(len);
Jeff King88d5a6f2014-05-22 05:44:09 -0400962 for (i = 0; i < len; i++)
963 result[i] = tolower(string[i]);
Jeff King88d5a6f2014-05-22 05:44:09 -0400964 return result;
965}
Jeff King30a0ddb2014-06-18 16:01:34 -0400966
Lars Schneider13ecb4632018-02-15 16:27:06 +0100967char *xstrdup_toupper(const char *string)
968{
969 char *result;
970 size_t len, i;
971
972 len = strlen(string);
973 result = xmallocz(len);
974 for (i = 0; i < len; i++)
975 result[i] = toupper(string[i]);
Junio C Hamanod1df5742005-04-25 18:26:45 -0700976 return result;
977}
Jeff King30a0ddb2014-06-18 16:01:34 -0400978
979char *xstrvfmt(const char *fmt, va_list ap)
980{
981 struct strbuf buf = STRBUF_INIT;
982 strbuf_vaddf(&buf, fmt, ap);
983 return strbuf_detach(&buf, NULL);
984}
985
986char *xstrfmt(const char *fmt, ...)
987{
988 va_list ap;
989 char *ret;
990
991 va_start(ap, fmt);
992 ret = xstrvfmt(fmt, ap);
993 va_end(ap);
994
995 return ret;
996}
Jeff Kingaa1462c2015-06-25 12:55:45 -0400997
René Scharfec3fbf812017-06-15 14:29:53 +0200998void strbuf_addftime(struct strbuf *sb, const char *fmt, const struct tm *tm,
Ævar Arnfjörð Bjarmason3b702232017-07-01 13:15:47 +0000999 int tz_offset, int suppress_tz_name)
Jeff Kingaa1462c2015-06-25 12:55:45 -04001000{
René Scharfec3fbf812017-06-15 14:29:53 +02001001 struct strbuf munged_fmt = STRBUF_INIT;
Jeff Kinge4f031e2015-06-30 09:26:53 -04001002 size_t hint = 128;
Jeff Kingaa1462c2015-06-25 12:55:45 -04001003 size_t len;
1004
Jeff Kinge4f031e2015-06-30 09:26:53 -04001005 if (!*fmt)
1006 return;
1007
René Scharfec3fbf812017-06-15 14:29:53 +02001008 /*
1009 * There is no portable way to pass timezone information to
Jeff King9b591b92021-11-02 07:35:34 -04001010 * strftime, so we handle %z and %Z here. Likewise '%s', because
1011 * going back to an epoch time requires knowing the zone.
1012 *
1013 * Note that tz_offset is in the "[-+]HHMM" decimal form; this is what
1014 * we want for %z, but the computation for %s has to convert to number
1015 * of seconds.
René Scharfec3fbf812017-06-15 14:29:53 +02001016 */
1017 for (;;) {
1018 const char *percent = strchrnul(fmt, '%');
1019 strbuf_add(&munged_fmt, fmt, percent - fmt);
1020 if (!*percent)
1021 break;
1022 fmt = percent + 1;
1023 switch (*fmt) {
1024 case '%':
1025 strbuf_addstr(&munged_fmt, "%%");
1026 fmt++;
1027 break;
Jeff King9b591b92021-11-02 07:35:34 -04001028 case 's':
1029 strbuf_addf(&munged_fmt, "%"PRItime,
1030 (timestamp_t)tm_to_time_t(tm) -
1031 3600 * (tz_offset / 100) -
1032 60 * (tz_offset % 100));
1033 fmt++;
1034 break;
René Scharfec3fbf812017-06-15 14:29:53 +02001035 case 'z':
1036 strbuf_addf(&munged_fmt, "%+05d", tz_offset);
1037 fmt++;
1038 break;
1039 case 'Z':
Ævar Arnfjörð Bjarmason3b702232017-07-01 13:15:47 +00001040 if (suppress_tz_name) {
René Scharfec3fbf812017-06-15 14:29:53 +02001041 fmt++;
1042 break;
1043 }
1044 /* FALLTHROUGH */
1045 default:
1046 strbuf_addch(&munged_fmt, '%');
1047 }
1048 }
1049 fmt = munged_fmt.buf;
1050
Jeff Kinge4f031e2015-06-30 09:26:53 -04001051 strbuf_grow(sb, hint);
Jeff Kingaa1462c2015-06-25 12:55:45 -04001052 len = strftime(sb->buf + sb->len, sb->alloc - sb->len, fmt, tm);
1053
1054 if (!len) {
1055 /*
Jeff Kinge4f031e2015-06-30 09:26:53 -04001056 * strftime reports "0" if it could not fit the result in the buffer.
1057 * Unfortunately, it also reports "0" if the requested time string
1058 * takes 0 bytes. So our strategy is to munge the format so that the
1059 * output contains at least one character, and then drop the extra
1060 * character before returning.
Jeff Kingaa1462c2015-06-25 12:55:45 -04001061 */
René Scharfec3fbf812017-06-15 14:29:53 +02001062 strbuf_addch(&munged_fmt, ' ');
Jeff Kinge4f031e2015-06-30 09:26:53 -04001063 while (!len) {
1064 hint *= 2;
1065 strbuf_grow(sb, hint);
1066 len = strftime(sb->buf + sb->len, sb->alloc - sb->len,
1067 munged_fmt.buf, tm);
1068 }
Jeff Kinge4f031e2015-06-30 09:26:53 -04001069 len--; /* drop munged space */
Jeff Kingaa1462c2015-06-25 12:55:45 -04001070 }
René Scharfec3fbf812017-06-15 14:29:53 +02001071 strbuf_release(&munged_fmt);
Jeff Kinge4f031e2015-06-30 09:26:53 -04001072 strbuf_setlen(sb, sb->len + len);
Jeff Kingaa1462c2015-06-25 12:55:45 -04001073}
Jeff Kingaf49c6d2015-09-24 17:05:45 -04001074
Jonathan Tan155b5172021-10-08 14:08:17 -07001075void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo,
1076 const struct object_id *oid, int abbrev_len)
Jeff Kingaf49c6d2015-09-24 17:05:45 -04001077{
1078 int r;
brian m. carlson4b048c92018-07-16 01:27:57 +00001079 strbuf_grow(sb, GIT_MAX_HEXSZ + 1);
Jonathan Tan155b5172021-10-08 14:08:17 -07001080 r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len);
Jeff Kingaf49c6d2015-09-24 17:05:45 -04001081 strbuf_setlen(sb, sb->len + r);
1082}
Junio C Hamano1ad7c0f2015-10-26 15:55:20 -07001083
Jonathan Tan155b5172021-10-08 14:08:17 -07001084void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid,
1085 int abbrev_len)
1086{
1087 strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len);
1088}
1089
Tobias Klauser63af4a82015-10-16 17:16:42 +02001090/*
1091 * Returns the length of a line, without trailing spaces.
1092 *
1093 * If the line ends with newline, it will be removed too.
1094 */
1095static size_t cleanup(char *line, size_t len)
1096{
1097 while (len) {
1098 unsigned char c = line[len - 1];
1099 if (!isspace(c))
1100 break;
1101 len--;
1102 }
1103
1104 return len;
1105}
1106
1107/*
1108 * Remove empty lines from the beginning and end
1109 * and also trailing spaces from every line.
1110 *
1111 * Turn multiple consecutive empty lines between paragraphs
1112 * into just one empty line.
1113 *
1114 * If the input has only empty lines and spaces,
1115 * no output will be produced.
1116 *
1117 * If last line does not have a newline at the end, one is added.
1118 *
1119 * Enable skip_comments to skip every line starting with comment
1120 * character.
1121 */
1122void strbuf_stripspace(struct strbuf *sb, int skip_comments)
1123{
Jeff King26114c02018-07-24 06:51:08 -04001124 size_t empties = 0;
Tobias Klauser63af4a82015-10-16 17:16:42 +02001125 size_t i, j, len, newlen;
1126 char *eol;
1127
1128 /* We may have to add a newline. */
1129 strbuf_grow(sb, 1);
1130
1131 for (i = j = 0; i < sb->len; i += len, j += newlen) {
1132 eol = memchr(sb->buf + i, '\n', sb->len - i);
1133 len = eol ? eol - (sb->buf + i) + 1 : sb->len - i;
1134
1135 if (skip_comments && len && sb->buf[i] == comment_line_char) {
1136 newlen = 0;
1137 continue;
1138 }
1139 newlen = cleanup(sb->buf + i, len);
1140
1141 /* Not just an empty line? */
1142 if (newlen) {
1143 if (empties > 0 && j > 0)
1144 sb->buf[j++] = '\n';
1145 empties = 0;
1146 memmove(sb->buf + j, sb->buf + i, newlen);
1147 sb->buf[newlen + j++] = '\n';
1148 } else {
1149 empties++;
1150 }
1151 }
1152
1153 strbuf_setlen(sb, j);
1154}
Jeff King670c3592016-10-03 16:34:17 -04001155
1156int strbuf_normalize_path(struct strbuf *src)
1157{
1158 struct strbuf dst = STRBUF_INIT;
1159
1160 strbuf_grow(&dst, src->len);
1161 if (normalize_path_copy(dst.buf, src->buf) < 0) {
1162 strbuf_release(&dst);
1163 return -1;
1164 }
1165
1166 /*
1167 * normalize_path does not tell us the new length, so we have to
1168 * compute it by looking for the new NUL it placed
1169 */
1170 strbuf_setlen(&dst, strlen(dst.buf));
1171 strbuf_swap(src, &dst);
1172 strbuf_release(&dst);
1173 return 0;
1174}
Johannes Schindelinb38dd9e2019-12-13 08:08:00 +00001175
1176int strbuf_edit_interactively(struct strbuf *buffer, const char *path,
1177 const char *const *env)
1178{
1179 char *path2 = NULL;
1180 int fd, res = 0;
1181
1182 if (!is_absolute_path(path))
1183 path = path2 = xstrdup(git_path("%s", path));
1184
1185 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
1186 if (fd < 0)
1187 res = error_errno(_("could not open '%s' for writing"), path);
1188 else if (write_in_full(fd, buffer->buf, buffer->len) < 0) {
1189 res = error_errno(_("could not write to '%s'"), path);
1190 close(fd);
1191 } else if (close(fd) < 0)
1192 res = error_errno(_("could not close '%s'"), path);
1193 else {
1194 strbuf_reset(buffer);
1195 if (launch_editor(path, buffer, env) < 0)
1196 res = error_errno(_("could not edit '%s'"), path);
1197 unlink(path);
1198 }
1199
1200 free(path2);
1201 return res;
1202}