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
|
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <thread.h>
#include <regexp.h>
#include "mail.h"
void *
emalloc(ulong n)
{
void *v;
v = mallocz(n, 1);
if(v == nil)
sysfatal("malloc: %r");
setmalloctag(v, getcallerpc(&n));
return v;
}
void *
erealloc(void *p, ulong n)
{
void *v;
v = realloc(p, n);
if(v == nil)
sysfatal("realloc: %r");
setmalloctag(v, getcallerpc(&p));
return v;
}
char*
estrdup(char *s)
{
s = strdup(s);
if(s == nil)
sysfatal("strdup: %r");
setmalloctag(s, getcallerpc(&s));
return s;
}
char*
estrjoin(char *s, ...)
{
va_list ap;
char *r, *t, *p, *e;
int n;
va_start(ap, s);
n = strlen(s) + 1;
while((p = va_arg(ap, char*)) != nil)
n += strlen(p);
va_end(ap);
r = emalloc(n);
e = r + n;
va_start(ap, s);
t = strecpy(r, e, s);
while((p = va_arg(ap, char*)) != nil)
t = strecpy(t, e, p);
va_end(ap);
return r;
}
char*
esmprint(char *fmt, ...)
{
char *s;
va_list ap;
va_start(ap, fmt);
s = vsmprint(fmt, ap);
va_end(ap);
if(s == nil)
sysfatal("smprint: %r");
setmalloctag(s, getcallerpc(&fmt));
return s;
}
char*
fslurp(int fd, int *nbuf)
{
int n, sz, r;
char *buf;
n = 0;
sz = 128;
buf = emalloc(sz);
while(1){
r = read(fd, buf + n, sz - n);
if(r == 0)
break;
if(r == -1)
goto error;
n += r;
if(n == sz){
sz += sz/2;
buf = erealloc(buf, sz);
}
}
buf[n] = 0;
if(nbuf)
*nbuf = n;
return buf;
error:
free(buf);
return nil;
}
char *
rslurp(Mesg *m, char *f, int *nbuf)
{
char *path;
int fd;
char *r;
if(m == nil)
path = estrjoin(mbox.path, "/", f, nil);
else
path = estrjoin(mbox.path, "/", m->name, "/", f, nil);
fd = open(path, OREAD);
free(path);
if(fd == -1)
return nil;
r = fslurp(fd, nbuf);
close(fd);
return r;
}
u32int
strhash(char *s)
{
u32int h, c;
h = 5381;
while(c = *s++ & 0xff)
h = ((h << 5) + h) + c;
return h;
}
|