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
|
/* $Id$ */
#include "bbs.h"
#include <avltree.h>
void usage(void)
{
fprintf(stderr, "usage: cacheserver -p PASSWD\n");
}
AVL_IX_DESC avl;
typedef struct {
int uid;
unsigned int count;
char key[IDLEN + 1];
} rectype_t;
int loadpasswd(char *pfn)
{
int fd, i;
userec_t u;
rectype_t rec;
if( pfn == NULL ){
fprintf(stderr, "no password file!\n");
return -1;
}
if( (fd = open(pfn, O_RDONLY)) < 0 ){
perror("open password");
return -1;
}
avl_create_index(&avl, AVL_NO_DUP_KEYS, IDLEN + 1);
for( i = 0 ; read(fd, &u, sizeof(u)) == sizeof(u) && i < 1024 ; ++i ){
rec.uid = i;
rec.count = 0;
strlcpy(rec.key, u.userid, sizeof(rec.key));
printf("add %s -> %d\n", rec.key, rec.uid);
avl_add_key((AVL_IX_REC*)&rec, &avl);
}
#if 0
rec.uid = 0;
rec.count = 0;
strcpy(rec.key, "KTDOG");
avl_find_key((AVL_IX_REC*)&rec, &avl);
printf("%d\n", rec.uid);
#endif
return 0;
}
int main(int argc, char **argv)
{
int ch;
char *pfn = NULL;
while( (ch = getopt(argc, argv, "p:h")) != -1 )
switch( ch ){
case 'p':
pfn = strdup(optarg);
break;
case 'h':
default:
usage();
return 1;
}
if( loadpasswd(pfn) < 0 )
return 1;
return 0;
}
|