util.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include <draw.h>
  13. #include <html.h>
  14. #include "dat.h"
  15. void*
  16. emalloc(uint32_t n)
  17. {
  18. void *p;
  19. p = malloc(n);
  20. if(p == nil)
  21. error("can't malloc: %r");
  22. memset(p, 0, n);
  23. return p;
  24. }
  25. void*
  26. erealloc(void *p, uint32_t n)
  27. {
  28. p = realloc(p, n);
  29. if(p == nil)
  30. error("can't malloc: %r");
  31. return p;
  32. }
  33. char*
  34. estrdup(char *s)
  35. {
  36. char *t;
  37. t = emalloc(strlen(s)+1);
  38. strcpy(t, s);
  39. return t;
  40. }
  41. char*
  42. estrstrdup(char *s, char *t)
  43. {
  44. int32_t ns, nt;
  45. char *u;
  46. ns = strlen(s);
  47. nt = strlen(t);
  48. /* use malloc to avoid memset */
  49. u = malloc(ns+nt+1);
  50. if(u == nil)
  51. error("can't malloc: %r");
  52. memmove(u, s, ns);
  53. memmove(u+ns, t, nt);
  54. u[ns+nt] = '\0';
  55. return u;
  56. }
  57. char*
  58. eappend(char *s, char *sep, char *t)
  59. {
  60. int32_t ns, nsep, nt;
  61. char *u;
  62. if(t == nil)
  63. u = estrstrdup(s, sep);
  64. else{
  65. ns = strlen(s);
  66. nsep = strlen(sep);
  67. nt = strlen(t);
  68. /* use malloc to avoid memset */
  69. u = malloc(ns+nsep+nt+1);
  70. if(u == nil)
  71. error("can't malloc: %r");
  72. memmove(u, s, ns);
  73. memmove(u+ns, sep, nsep);
  74. memmove(u+ns+nsep, t, nt);
  75. u[ns+nsep+nt] = '\0';
  76. }
  77. free(s);
  78. return u;
  79. }
  80. char*
  81. egrow(char *s, char *sep, char *t)
  82. {
  83. s = eappend(s, sep, t);
  84. free(t);
  85. return s;
  86. }
  87. void
  88. error(char *fmt, ...)
  89. {
  90. va_list arg;
  91. char buf[256];
  92. Fmt f;
  93. fmtfdinit(&f, 2, buf, sizeof buf);
  94. fmtprint(&f, "Mail: ");
  95. va_start(arg, fmt);
  96. fmtvprint(&f, fmt, arg);
  97. va_end(arg);
  98. fmtprint(&f, "\n");
  99. fmtfdflush(&f);
  100. exits(fmt);
  101. }
  102. void
  103. growbytes(Bytes *b, char *s, int32_t ns)
  104. {
  105. if(b->nalloc < b->n + ns + 1){
  106. b->nalloc = b->n + ns + 8000;
  107. /* use realloc to avoid memset */
  108. b->b = realloc(b->b, b->nalloc);
  109. if(b->b == nil)
  110. error("growbytes: can't realloc: %r");
  111. }
  112. memmove(b->b+b->n, s, ns);
  113. b->n += ns;
  114. b->b[b->n] = '\0';
  115. }