util.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 <String.h>
  13. #include <ctype.h>
  14. #include <thread.h>
  15. #include "wiki.h"
  16. void*
  17. erealloc(void *v, uint32_t n)
  18. {
  19. v = realloc(v, n);
  20. if(v == nil)
  21. sysfatal("out of memory reallocating %lud", n);
  22. setmalloctag(v, getcallerpc(&v));
  23. return v;
  24. }
  25. void*
  26. emalloc(uint32_t n)
  27. {
  28. void *v;
  29. v = malloc(n);
  30. if(v == nil)
  31. sysfatal("out of memory allocating %lud", n);
  32. memset(v, 0, n);
  33. setmalloctag(v, getcallerpc(&n));
  34. return v;
  35. }
  36. char*
  37. estrdup(char *s)
  38. {
  39. int l;
  40. char *t;
  41. if (s == nil)
  42. return nil;
  43. l = strlen(s)+1;
  44. t = emalloc(l);
  45. memmove(t, s, l);
  46. setmalloctag(t, getcallerpc(&s));
  47. return t;
  48. }
  49. char*
  50. estrdupn(char *s, int n)
  51. {
  52. int l;
  53. char *t;
  54. l = strlen(s);
  55. if(l > n)
  56. l = n;
  57. t = emalloc(l+1);
  58. memmove(t, s, l);
  59. t[l] = '\0';
  60. setmalloctag(t, getcallerpc(&s));
  61. return t;
  62. }
  63. char*
  64. strlower(char *s)
  65. {
  66. char *p;
  67. for(p=s; *p; p++)
  68. if('A' <= *p && *p <= 'Z')
  69. *p += 'a'-'A';
  70. return s;
  71. }
  72. String*
  73. s_appendsub(String *s, char *p, int n, Sub *sub, int nsub)
  74. {
  75. int i, m;
  76. char *q, *r, *ep;
  77. ep = p+n;
  78. while(p<ep){
  79. q = ep;
  80. m = -1;
  81. for(i=0; i<nsub; i++){
  82. if(sub[i].sub && (r = strstr(p, sub[i].match)) && r < q){
  83. q = r;
  84. m = i;
  85. }
  86. }
  87. s = s_nappend(s, p, q-p);
  88. p = q;
  89. if(m >= 0){
  90. s = s_append(s, sub[m].sub);
  91. p += strlen(sub[m].match);
  92. }
  93. }
  94. return s;
  95. }
  96. String*
  97. s_appendlist(String *s, ...)
  98. {
  99. char *x;
  100. va_list arg;
  101. va_start(arg, s);
  102. while(x = va_arg(arg, char*))
  103. s = s_append(s, x);
  104. va_end(arg);
  105. return s;
  106. }
  107. int
  108. opentemp(char *template)
  109. {
  110. int fd, i;
  111. char *p;
  112. p = estrdup(template);
  113. fd = -1;
  114. for(i=0; i<10; i++){
  115. mktemp(p);
  116. if(access(p, 0) < 0 && (fd=create(p, ORDWR|ORCLOSE, 0444)) >= 0)
  117. break;
  118. strcpy(p, template);
  119. }
  120. if(fd >= 0)
  121. strcpy(template, p);
  122. free(p);
  123. return fd;
  124. }