123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- #include <u.h>
- #include <libc.h>
- #include <bio.h>
- #include <auth.h>
- #include "imap4d.h"
- /*
- * reverse string [s:e) in place
- */
- void
- strrev(char *s, char *e)
- {
- int c;
- while(--e > s){
- c = *s;
- *s++ = *e;
- *e = c;
- }
- }
- int
- isdotdot(char *s)
- {
- return s[0] == '.' && s[1] == '.' && (s[2] == '/' || s[2] == '\0');
- }
- int
- issuffix(char *suf, char *s)
- {
- int n;
- n = strlen(s) - strlen(suf);
- if(n < 0)
- return 0;
- return strcmp(s + n, suf) == 0;
- }
- int
- isprefix(char *pre, char *s)
- {
- return strncmp(pre, s, strlen(pre)) == 0;
- }
- int
- ciisprefix(char *pre, char *s)
- {
- return cistrncmp(pre, s, strlen(pre)) == 0;
- }
- char*
- readFile(int fd)
- {
- Dir *d;
- long length;
- char *s;
- d = dirfstat(fd);
- if(d == nil)
- return nil;
- length = d->length;
- free(d);
- s = binalloc(&parseBin, length + 1, 0);
- if(s == nil || read(fd, s, length) != length)
- return nil;
- s[length] = '\0';
- return s;
- }
- /*
- * create the imap tmp file.
- * it just happens that we don't need multiple temporary files.
- */
- int
- imapTmp(void)
- {
- char buf[ERRMAX], name[MboxNameLen];
- int tries, fd;
- snprint(name, sizeof(name), "/mail/box/%s/mbox.tmp.imp", username);
- for(tries = 0; tries < LockSecs*2; tries++){
- fd = create(name, ORDWR|ORCLOSE|OCEXEC, DMEXCL|0600);
- if(fd >= 0)
- return fd;
- errstr(buf, sizeof buf);
- if(cistrstr(buf, "locked") == nil)
- break;
- sleep(500);
- }
- return -1;
- }
- /*
- * open a file which might be locked.
- * if it is, spin until available
- */
- int
- openLocked(char *dir, char *file, int mode)
- {
- char buf[ERRMAX];
- int tries, fd;
- for(tries = 0; tries < LockSecs*2; tries++){
- fd = cdOpen(dir, file, mode);
- if(fd >= 0)
- return fd;
- errstr(buf, sizeof buf);
- if(cistrstr(buf, "locked") == nil)
- break;
- sleep(500);
- }
- return -1;
- }
- int
- fqid(int fd, Qid *qid)
- {
- Dir *d;
- d = dirfstat(fd);
- if(d == nil)
- return -1;
- *qid = d->qid;
- free(d);
- return 0;
- }
- ulong
- mapInt(NamedInt *map, char *name)
- {
- int i;
- for(i = 0; map[i].name != nil; i++)
- if(cistrcmp(map[i].name, name) == 0)
- break;
- return map[i].v;
- }
- char*
- estrdup(char *s)
- {
- char *t;
- t = emalloc(strlen(s) + 1);
- strcpy(t, s);
- return t;
- }
- void*
- emalloc(ulong n)
- {
- void *p;
- p = malloc(n);
- if(p == nil)
- bye("server out of memory");
- return p;
- }
- void*
- ezmalloc(ulong n)
- {
- void *p;
- p = malloc(n);
- if(p == nil)
- bye("server out of memory");
- memset(p, 0, n);
- return p;
- }
- void*
- erealloc(void *p, ulong n)
- {
- p = realloc(p, n);
- if(p == nil)
- bye("server out of memory");
- return p;
- }
|