strbuf.c 25 KB

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