File: tlscachetest.c

package info (click to toggle)
courier 0.60.0-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 52,288 kB
  • ctags: 12,677
  • sloc: ansic: 165,348; cpp: 24,820; sh: 16,410; perl: 6,839; makefile: 3,621; yacc: 289; sed: 16
file content (73 lines) | stat: -rw-r--r-- 1,326 bytes parent folder | download | duplicates (5)
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
/*
** Copyright 2002-2006 Double Precision, Inc.
** See COPYING for distribution information.
*/

#define TLSCACHEMINSIZE (sizeof(struct hdr) + 5 * (sizeof(struct obj)+8))
#include "tlscache.c"

static const char tlscachetest_rcsid[]="$Id: tlscachetest.c,v 1.3 2006/05/28 15:29:52 mrsam Exp $";

static int printcache(void *rec, size_t recsize, int *doupdate,
		      void *arg)
{
	if (fwrite((const char *)rec, recsize, 1, stdout) == 1)
		printf("\n");
	return 0;
}

static int replacecache(void *rec, size_t recsize, int *doupdate,
		      void *arg)
{
	const char *p=(const char *)arg;
	const char *q;

	if ((q=strchr(p, '-')) == NULL || strlen(q+1) != q-p)
		return (0);

	if (recsize == q-p && memcmp(rec, p, q-p) == 0)
	{
		memcpy(rec, q+1, q-p);
		*doupdate=1;
	}
	return 0;
}

int main(int argc, char **argv)
{
	struct CACHE *p=tls_cache_open("test.dat", TLSCACHEMINSIZE);

	if (!p)
	{
		perror("test.dat");
		return (-1);
	}

	if (argc > 1)
	{
		char *s=argv[1];

		if (*s == '+')
		{
			++s;
			if (tls_cache_add(p, s, strlen(s)))
			{
				perror("tls_cache_add");
			}
		}

		if (*s == '-')
		{
			if (tls_cache_walk(p, replacecache, s+1) < 0)
			{
				perror("tls_cache_walk");
				exit(1);
			}
		}
	}

	if (tls_cache_walk(p, printcache, NULL) < 0)
		perror("tls_cache_walk");
	tls_cache_close(p);
	return (0);
}