summaryrefslogtreecommitdiff
path: root/usr.sbin/popa3d/database.c
blob: c342cca54d22c5981c7d96508d540fd1100d5923 (plain)
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
/* $OpenBSD: database.c,v 1.1 2001/08/19 13:05:57 deraadt Exp $ */

/*
 * Message database management.
 */

#include <stdlib.h>
#include <string.h>

#include "params.h"
#include "database.h"

struct db_main db;

void db_init(void)
{
	db.head = db.tail = NULL;
	db.total_count = 0;
	db.total_size = 0;
	db.flags = 0;
#if POP_SUPPORT_LAST
	db.last = 0;
#endif
}

int db_add(struct db_message *msg)
{
	struct db_message *entry;

	if (db.total_count >= MAX_MAILBOX_MESSAGES) return 1;

	entry = malloc(sizeof(struct db_message));
	if (!entry) return 1;

	memcpy(entry, msg, sizeof(struct db_message));
	entry->next = NULL;
	entry->flags = 0;

	if (db.tail)
		db.tail = db.tail->next = entry;
	else
		db.tail = db.head = entry;

	if (++db.total_count <= 0) return 1;
	if ((db.total_size += entry->size) < 0 || entry->size < 0) return 1;

	return 0;
}

int db_delete(struct db_message *msg)
{
	if (msg->flags & MSG_DELETED) return 1;

	msg->flags |= MSG_DELETED;

	db.visible_count--;
	db.visible_size -= msg->size;
	db.flags |= DB_DIRTY;

	return 0;
}

int db_fix(void)
{
	int size;
	struct db_message *entry;
	int index;

	db.visible_count = db.total_count;
	db.visible_size = db.total_size;

	if (!db.total_count) return 0;

	size = sizeof(struct db_message *) * db.total_count;
	if (size <= 0) return 1;
	if (size / sizeof(struct db_message *) != db.total_count) return 1;

	db.array = malloc(size);
	if (!db.array) return 1;

	entry = db.head;
	index = 0;
	do {
		db.array[index++] = entry;
	} while ((entry = entry->next));

	return 0;
}