strbuf.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "debug.h"
  2. #include "util.h"
  3. #include <linux/kernel.h>
  4. int prefixcmp(const char *str, const char *prefix)
  5. {
  6. for (; ; str++, prefix++)
  7. if (!*prefix)
  8. return 0;
  9. else if (*str != *prefix)
  10. return (unsigned char)*prefix - (unsigned char)*str;
  11. }
  12. /*
  13. * Used as the default ->buf value, so that people can always assume
  14. * buf is non NULL and ->buf is NUL terminated even for a freshly
  15. * initialized strbuf.
  16. */
  17. char strbuf_slopbuf[1];
  18. int strbuf_init(struct strbuf *sb, ssize_t hint)
  19. {
  20. sb->alloc = sb->len = 0;
  21. sb->buf = strbuf_slopbuf;
  22. if (hint)
  23. return strbuf_grow(sb, hint);
  24. return 0;
  25. }
  26. void strbuf_release(struct strbuf *sb)
  27. {
  28. if (sb->alloc) {
  29. zfree(&sb->buf);
  30. strbuf_init(sb, 0);
  31. }
  32. }
  33. char *strbuf_detach(struct strbuf *sb, size_t *sz)
  34. {
  35. char *res = sb->alloc ? sb->buf : NULL;
  36. if (sz)
  37. *sz = sb->len;
  38. strbuf_init(sb, 0);
  39. return res;
  40. }
  41. int strbuf_grow(struct strbuf *sb, size_t extra)
  42. {
  43. char *buf;
  44. size_t nr = sb->len + extra + 1;
  45. if (nr < sb->alloc)
  46. return 0;
  47. if (nr <= sb->len)
  48. return -E2BIG;
  49. if (alloc_nr(sb->alloc) > nr)
  50. nr = alloc_nr(sb->alloc);
  51. /*
  52. * Note that sb->buf == strbuf_slopbuf if sb->alloc == 0, and it is
  53. * a static variable. Thus we have to avoid passing it to realloc.
  54. */
  55. buf = realloc(sb->alloc ? sb->buf : NULL, nr * sizeof(*buf));
  56. if (!buf)
  57. return -ENOMEM;
  58. sb->buf = buf;
  59. sb->alloc = nr;
  60. return 0;
  61. }
  62. int strbuf_addch(struct strbuf *sb, int c)
  63. {
  64. int ret = strbuf_grow(sb, 1);
  65. if (ret)
  66. return ret;
  67. sb->buf[sb->len++] = c;
  68. sb->buf[sb->len] = '\0';
  69. return 0;
  70. }
  71. int strbuf_add(struct strbuf *sb, const void *data, size_t len)
  72. {
  73. int ret = strbuf_grow(sb, len);
  74. if (ret)
  75. return ret;
  76. memcpy(sb->buf + sb->len, data, len);
  77. return strbuf_setlen(sb, sb->len + len);
  78. }
  79. static int strbuf_addv(struct strbuf *sb, const char *fmt, va_list ap)
  80. {
  81. int len, ret;
  82. va_list ap_saved;
  83. if (!strbuf_avail(sb)) {
  84. ret = strbuf_grow(sb, 64);
  85. if (ret)
  86. return ret;
  87. }
  88. va_copy(ap_saved, ap);
  89. len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
  90. if (len < 0)
  91. return len;
  92. if (len > strbuf_avail(sb)) {
  93. ret = strbuf_grow(sb, len);
  94. if (ret)
  95. return ret;
  96. len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap_saved);
  97. va_end(ap_saved);
  98. if (len > strbuf_avail(sb)) {
  99. pr_debug("this should not happen, your vsnprintf is broken");
  100. return -EINVAL;
  101. }
  102. }
  103. return strbuf_setlen(sb, sb->len + len);
  104. }
  105. int strbuf_addf(struct strbuf *sb, const char *fmt, ...)
  106. {
  107. va_list ap;
  108. int ret;
  109. va_start(ap, fmt);
  110. ret = strbuf_addv(sb, fmt, ap);
  111. va_end(ap);
  112. return ret;
  113. }
  114. ssize_t strbuf_read(struct strbuf *sb, int fd, ssize_t hint)
  115. {
  116. size_t oldlen = sb->len;
  117. size_t oldalloc = sb->alloc;
  118. int ret;
  119. ret = strbuf_grow(sb, hint ? hint : 8192);
  120. if (ret)
  121. return ret;
  122. for (;;) {
  123. ssize_t cnt;
  124. cnt = read(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
  125. if (cnt < 0) {
  126. if (oldalloc == 0)
  127. strbuf_release(sb);
  128. else
  129. strbuf_setlen(sb, oldlen);
  130. return cnt;
  131. }
  132. if (!cnt)
  133. break;
  134. sb->len += cnt;
  135. ret = strbuf_grow(sb, 8192);
  136. if (ret)
  137. return ret;
  138. }
  139. sb->buf[sb->len] = '\0';
  140. return sb->len - oldlen;
  141. }