aboutsummaryrefslogtreecommitdiff
path: root/pathdefn.c
blob: 6c4d8b1dcdfcb0d4ac12d7cf7e26b257904c8ec9 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* This file is part of Dircond.
   Copyright (C) 2012, 2013 Sergey Poznyakoff.
 
   Dircond is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.
 
   Dircond is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with Dircond.  If not, see <http://www.gnu.org/licenses/>. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include "dircond.h"

unsigned
hash_string(const char *name, unsigned long hashsize)
{
	unsigned i;
	
	for (i = 0; *name; name++) {
		i <<= 1;
		i ^= *(unsigned char*) name;
	}
	return i % hashsize;
}		

static unsigned
pathdefn_hash(void *data, unsigned long hashsize)
{
	struct pathdefn *sym = data;
	return hash_string(sym->name, hashsize);
}

static int
pathdefn_cmp(const void *a, const void *b)
{
	struct pathdefn const *syma = a;
	struct pathdefn const *symb = b;

	return strcmp(syma->name, symb->name);
}

static int
pathdefn_copy(void *a, void *b)
{
	struct pathdefn *syma = a;
	struct pathdefn *symb = b;

	syma->used = 1;
	syma->name = strdup(symb->name);
	return syma->name == NULL;
}

static void
pathdefn_free(void *p)
{
	free(p);
}

static struct hashtab *pathtab;

/* FIXME: Should exit gracefully on out of memory errors */
int
pathdefn_add(const char *name, const char *dir, long depth)
{
	struct pathdefn key;
	struct pathdefn *defn;
	struct pathent *ent, *p, *prev;
	int install = 1;
	size_t len;
	
	len = strlen(dir);
	while (len > 0 && dir[len-1] == '/')
		--len;

	if (len == 0)
		return 0;
		
	if (!pathtab) {
		pathtab = hashtab_create(sizeof(struct pathdefn),
					 pathdefn_hash, pathdefn_cmp,
					 pathdefn_copy,
					 NULL, pathdefn_free);
		if (!pathtab) {
			diag(LOG_CRIT, "not enough memory");
			exit(1);
		}
	}

	memset(&key, 0, sizeof(key));
	key.name = (char*) name;
	defn = hashtab_lookup_or_install(pathtab, &key, &install);
	if (!defn) {
		diag(LOG_CRIT, "not enough memory");
		exit(1);
	}

	prev = NULL;
	for (p = defn->pathlist; p; prev = p, p = p->next) {
		if (p->len == len && strncmp(p->path, dir, len) == 0)
			return 0;
	}

	ent = emalloc(sizeof(*ent) + len);
	strcpy(ent->path, dir);
	ent->len = len;
	ent->depth = depth;
	if (prev)
		prev->next = ent;
	else
		defn->pathlist = ent;
	
	return 0;
}

struct pathent *
pathdefn_get(const char *name)
{
	struct pathdefn key;
	struct pathdefn *defn;

	if (!pathtab)
		return NULL;
	memset(&key, 0, sizeof(key));
	key.name = (char *) name;
	defn = hashtab_lookup_or_install(pathtab, &key, NULL);
	return defn ? defn->pathlist : NULL;
}

Return to:

Send suggestions and report system problems to the System administrator.