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
|
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <ndb.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>
#include <ctype.h>
#include "dat.h"
#include "fns.h"
void*
erealloc(void *a, uint n)
{
a = realloc(a, n);
if(a == nil)
sysfatal("realloc %d: out of memory", n);
setrealloctag(a, getcallerpc(&a));
return a;
}
void*
emalloc(uint n)
{
void *a;
a = mallocz(n, 1);
if(a == nil)
sysfatal("malloc %d: out of memory", n);
setmalloctag(a, getcallerpc(&n));
return a;
}
char*
estrdup(char *s)
{
s = strdup(s);
if(s == nil)
sysfatal("strdup: out of memory");
setmalloctag(s, getcallerpc(&s));
return s;
}
char*
estredup(char *s, char *e)
{
char *t;
t = emalloc(e-s+1);
memmove(t, s, e-s);
t[e-s] = '\0';
setmalloctag(t, getcallerpc(&s));
return t;
}
char*
estrmanydup(char *s, ...)
{
char *p, *t;
int len;
va_list arg;
len = strlen(s);
va_start(arg, s);
while((p = va_arg(arg, char*)) != nil)
len += strlen(p);
len++;
t = emalloc(len);
strcpy(t, s);
va_start(arg, s);
while((p = va_arg(arg, char*)) != nil)
strcat(t, p);
return t;
}
char*
strlower(char *s)
{
char *t;
for(t=s; *t; t++)
if('A' <= *t && *t <= 'Z')
*t += 'a'-'A';
return s;
}
|