1 /*
2 ** Copyright 2002-2006 Double Precision, Inc.
3 ** See COPYING for distribution information.
4 */
5
6 #define TLSCACHEMINSIZE (sizeof(struct hdr) + 5 * (sizeof(struct obj)+8))
7 #include "tlscache.c"
8
9
printcache(void * rec,size_t recsize,int * doupdate,void * arg)10 static int printcache(void *rec, size_t recsize, int *doupdate,
11 void *arg)
12 {
13 if (fwrite((const char *)rec, recsize, 1, stdout) == 1)
14 printf("\n");
15 return 0;
16 }
17
replacecache(void * rec,size_t recsize,int * doupdate,void * arg)18 static int replacecache(void *rec, size_t recsize, int *doupdate,
19 void *arg)
20 {
21 const char *p=(const char *)arg;
22 const char *q;
23
24 if ((q=strchr(p, '-')) == NULL || strlen(q+1) != q-p)
25 return (0);
26
27 if (recsize == q-p && memcmp(rec, p, q-p) == 0)
28 {
29 memcpy(rec, q+1, q-p);
30 *doupdate=1;
31 }
32 return 0;
33 }
34
main(int argc,char ** argv)35 int main(int argc, char **argv)
36 {
37 struct CACHE *p=tls_cache_open("test.dat", TLSCACHEMINSIZE);
38
39 if (!p)
40 {
41 perror("test.dat");
42 return (-1);
43 }
44
45 if (argc > 1)
46 {
47 char *s=argv[1];
48
49 if (*s == '+')
50 {
51 ++s;
52 if (tls_cache_add(p, s, strlen(s)))
53 {
54 perror("tls_cache_add");
55 }
56 }
57
58 if (*s == '-')
59 {
60 if (tls_cache_walk(p, replacecache, s+1) < 0)
61 {
62 perror("tls_cache_walk");
63 exit(1);
64 }
65 }
66 }
67
68 if (tls_cache_walk(p, printcache, NULL) < 0)
69 perror("tls_cache_walk");
70 tls_cache_close(p);
71 return (0);
72 }
73