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
|
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <libsec.h>
#include <ctype.h>
#include "iso9660.h"
typedef struct Stringtab Stringtab;
struct Stringtab {
Stringtab *link;
char *str;
};
static Stringtab *stab[1024];
static uint
hash(char *s)
{
uint h;
uchar *p;
h = 0;
for(p=(uchar*)s; *p; p++)
h = h*37 + *p;
return h;
}
static char*
estrdup(char *s)
{
if((s = strdup(s)) == nil)
sysfatal("strdup(%.10s): out of memory", s);
return s;
}
char*
atom(char *str)
{
uint h;
Stringtab *tab;
h = hash(str) % nelem(stab);
for(tab=stab[h]; tab; tab=tab->link)
if(strcmp(str, tab->str) == 0)
return tab->str;
tab = emalloc(sizeof *tab);
tab->str = estrdup(str);
tab->link = stab[h];
stab[h] = tab;
return tab->str;
}
void*
emalloc(ulong n)
{
void *p;
if((p = malloc(n)) == nil)
sysfatal("malloc(%lud): out of memory", n);
memset(p, 0, n);
return p;
}
void*
erealloc(void *v, ulong n)
{
if((v = realloc(v, n)) == nil)
sysfatal("realloc(%p, %lud): out of memory", v, n);
return v;
}
char*
struprcpy(char *p, char *s)
{
char *op;
op = p;
for(; *s; s++)
*p++ = toupper(*s);
*p = '\0';
return op;
}
int
chat(char *fmt, ...)
{
va_list arg;
if(!chatty)
return 0;
va_start(arg, fmt);
vfprint(2, fmt, arg);
va_end(arg);
return 1;
}
|