1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
#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");
setmalloctag(p, getcallerpc(&n));
return p;
}
void*
ezmalloc(ulong n)
{
void *p;
p = malloc(n);
if(p == nil)
bye("server out of memory");
setmalloctag(p, getcallerpc(&n));
memset(p, 0, n);
return p;
}
void*
erealloc(void *p, ulong n)
{
p = realloc(p, n);
if(p == nil)
bye("server out of memory");
setrealloctag(p, getcallerpc(&p));
return p;
}
|