utils.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <auth.h>
  5. #include "imap4d.h"
  6. /*
  7. * reverse string [s:e) in place
  8. */
  9. void
  10. strrev(char *s, char *e)
  11. {
  12. int c;
  13. while(--e > s){
  14. c = *s;
  15. *s++ = *e;
  16. *e = c;
  17. }
  18. }
  19. int
  20. isdotdot(char *s)
  21. {
  22. return s[0] == '.' && s[1] == '.' && (s[2] == '/' || s[2] == '\0');
  23. }
  24. int
  25. issuffix(char *suf, char *s)
  26. {
  27. int n;
  28. n = strlen(s) - strlen(suf);
  29. if(n < 0)
  30. return 0;
  31. return strcmp(s + n, suf) == 0;
  32. }
  33. int
  34. isprefix(char *pre, char *s)
  35. {
  36. return strncmp(pre, s, strlen(pre)) == 0;
  37. }
  38. int
  39. ciisprefix(char *pre, char *s)
  40. {
  41. return cistrncmp(pre, s, strlen(pre)) == 0;
  42. }
  43. char*
  44. readFile(int fd)
  45. {
  46. Dir *d;
  47. long length;
  48. char *s;
  49. d = dirfstat(fd);
  50. if(d == nil)
  51. return nil;
  52. length = d->length;
  53. free(d);
  54. s = binalloc(&parseBin, length + 1, 0);
  55. if(s == nil || read(fd, s, length) != length)
  56. return nil;
  57. s[length] = '\0';
  58. return s;
  59. }
  60. /*
  61. * create the imap tmp file.
  62. * it just happens that we don't need multiple temporary files.
  63. */
  64. int
  65. imapTmp(void)
  66. {
  67. char buf[ERRMAX], name[MboxNameLen];
  68. int tries, fd;
  69. snprint(name, sizeof(name), "/mail/box/%s/mbox.tmp.imp", username);
  70. for(tries = 0; tries < LockSecs*2; tries++){
  71. fd = create(name, ORDWR|ORCLOSE|OCEXEC, DMEXCL|0600);
  72. if(fd >= 0)
  73. return fd;
  74. errstr(buf, sizeof buf);
  75. if(cistrstr(buf, "locked") == nil)
  76. break;
  77. sleep(500);
  78. }
  79. return -1;
  80. }
  81. /*
  82. * open a file which might be locked.
  83. * if it is, spin until available
  84. */
  85. int
  86. openLocked(char *dir, char *file, int mode)
  87. {
  88. char buf[ERRMAX];
  89. int tries, fd;
  90. for(tries = 0; tries < LockSecs*2; tries++){
  91. fd = cdOpen(dir, file, mode);
  92. if(fd >= 0)
  93. return fd;
  94. errstr(buf, sizeof buf);
  95. if(cistrstr(buf, "locked") == nil)
  96. break;
  97. sleep(500);
  98. }
  99. return -1;
  100. }
  101. int
  102. fqid(int fd, Qid *qid)
  103. {
  104. Dir *d;
  105. d = dirfstat(fd);
  106. if(d == nil)
  107. return -1;
  108. *qid = d->qid;
  109. free(d);
  110. return 0;
  111. }
  112. ulong
  113. mapInt(NamedInt *map, char *name)
  114. {
  115. int i;
  116. for(i = 0; map[i].name != nil; i++)
  117. if(cistrcmp(map[i].name, name) == 0)
  118. break;
  119. return map[i].v;
  120. }
  121. char*
  122. estrdup(char *s)
  123. {
  124. char *t;
  125. t = emalloc(strlen(s) + 1);
  126. strcpy(t, s);
  127. return t;
  128. }
  129. void*
  130. emalloc(ulong n)
  131. {
  132. void *p;
  133. p = malloc(n);
  134. if(p == nil)
  135. bye("server out of memory");
  136. setmalloctag(p, getcallerpc(&n));
  137. return p;
  138. }
  139. void*
  140. ezmalloc(ulong n)
  141. {
  142. void *p;
  143. p = malloc(n);
  144. if(p == nil)
  145. bye("server out of memory");
  146. setmalloctag(p, getcallerpc(&n));
  147. memset(p, 0, n);
  148. return p;
  149. }
  150. void*
  151. erealloc(void *p, ulong n)
  152. {
  153. p = realloc(p, n);
  154. if(p == nil)
  155. bye("server out of memory");
  156. setrealloctag(p, getcallerpc(&p));
  157. return p;
  158. }