alias.c
6.14 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2001, 2002, 2005 Free Software Foundation, Inc.
GNU Mailutils 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 2, or (at your option)
any later version.
GNU Mailutils 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 GNU Mailutils; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301 USA */
#include "mail.h"
static void alias_print __P ((char *name));
static void alias_print_group __P ((char *name, list_t list));
static int alias_create __P ((char *name, list_t *plist));
static int alias_lookup __P ((char *name, list_t *plist));
/*
* a[lias] [alias [address...]]
* g[roup] [alias [address...]]
*/
int
mail_alias (int argc, char **argv)
{
if (argc == 1)
alias_print (NULL);
else if (argc == 2)
alias_print (argv[1]);
else
{
list_t list;
if (alias_create (argv[1], &list))
return 1;
argc --;
argv ++;
while (--argc)
util_slist_add (&list, *++argv);
}
return 0;
}
typedef struct _alias alias_t;
struct _alias
{
char *name;
list_t list;
};
/* Hash sizes. These are prime numbers, the distance between each
pair of them grows exponentially, starting from 64.
Hopefully no one will need more than 32797 aliases, and even if
someone will, it is easy enough to add more numbers to the sequence. */
static unsigned int hash_size[] =
{
37, 101, 229, 487, 1009, 2039, 4091, 8191, 16411, 32797,
};
/* Maximum number of re-hashes: */
static unsigned int max_rehash = sizeof (hash_size) / sizeof (hash_size[0]);
static alias_t *aliases; /* Table of aliases */
static unsigned int hash_num; /* Index to hash_size table */
static unsigned int hash __P((char *name));
static int alias_rehash __P((void));
static alias_t *alias_lookup_or_install __P((char *name, int install));
static void alias_print_group __P((char *name, list_t list));
unsigned
hash (char *name)
{
unsigned i;
for (i = 0; *name; name++) {
i <<= 1;
i ^= *(unsigned char*) name;
}
return i % hash_size[hash_num];
}
int
alias_rehash ()
{
alias_t *old_aliases = aliases;
alias_t *ap;
unsigned int i;
if (++hash_num >= max_rehash)
{
util_error (_("alias hash table full"));
return 1;
}
aliases = xcalloc (hash_size[hash_num], sizeof (aliases[0]));
if (old_aliases)
{
for (i = 0; i < hash_size[hash_num-1]; i++)
{
if (old_aliases[i].name)
{
ap = alias_lookup_or_install (old_aliases[i].name, 1);
ap->name = old_aliases[i].name;
ap->list = old_aliases[i].list;
}
}
free (old_aliases);
}
return 0;
}
alias_t *
alias_lookup_or_install (char *name, int install)
{
unsigned i, pos;
if (!aliases)
{
if (install)
{
if (alias_rehash ())
return NULL;
}
else
return NULL;
}
pos = hash (name);
for (i = pos; aliases[i].name;)
{
if (strcmp(aliases[i].name, name) == 0)
return &aliases[i];
if (++i >= hash_size[hash_num])
i = 0;
if (i == pos)
break;
}
if (!install)
return NULL;
if (aliases[i].name == NULL)
return &aliases[i];
if (alias_rehash ())
return NULL;
return alias_lookup_or_install (name, install);
}
static int
alias_lookup (char *name, list_t *plist)
{
alias_t *ap = alias_lookup_or_install (name, 0);
if (ap)
{
*plist = ap->list;
return 1;
}
return 0;
}
void
alias_print (char *name)
{
if (!name)
{
unsigned int i;
if (!aliases)
return;
for (i = 0; i < hash_size[hash_num]; i++)
{
if (aliases[i].name)
alias_print_group (aliases[i].name, aliases[i].list);
}
}
else
{
list_t list;
if (!alias_lookup (name, &list))
{
util_error (_("\"%s\": not a group"), name);
return;
}
alias_print_group (name, list);
}
}
int
alias_create (char *name, list_t *plist)
{
alias_t *ap = alias_lookup_or_install (name, 1);
if (!ap)
return 1;
if (!ap->name)
{
/* new entry */
if (list_create (&ap->list))
return 1;
ap->name = strdup (name);
if (!ap->name)
return 1;
}
*plist = ap->list;
return 0;
}
void
alias_print_group (char *name, list_t list)
{
fprintf (ofile, "%s ", name);
util_slist_print (list, 0);
fprintf (ofile, "\n");
}
void
alias_destroy (char *name)
{
unsigned int i, j, r;
alias_t *alias = alias_lookup_or_install (name, 0);
if (!alias)
return;
free (alias->name);
util_slist_destroy (&alias->list);
for (i = alias - aliases;;)
{
aliases[i].name = NULL;
j = i;
do
{
if (++i >= hash_size[hash_num])
i = 0;
if (!aliases[i].name)
return;
r = hash(aliases[i].name);
}
while ((j < r && r <= i) || (i < j && j < r) || (r <= i && i < j));
aliases[j] = aliases[i];
}
}
char *
alias_expand (char *name)
{
list_t list;
if (!alias_lookup (name, &list))
return NULL;
return util_slist_to_string (list, ",");
}
struct alias_iterator
{
const char *prefix;
int prefixlen;
int pos;
};
const char *
alias_iterate_next (alias_iterator_t itr)
{
int i;
for (i = itr->pos; i < hash_size[hash_num]; i++)
if (aliases[i].name
&& strlen (aliases[i].name) >= itr->prefixlen
&& strncmp (aliases[i].name, itr->prefix, itr->prefixlen) == 0)
{
itr->pos = i + 1;
return aliases[i].name;
}
return NULL;
}
const char *
alias_iterate_first (const char *prefix, alias_iterator_t *pc)
{
struct alias_iterator *itr;
if (!aliases)
{
*pc = NULL;
return NULL;
}
itr = xmalloc (sizeof *itr);
itr->prefix = prefix;
itr->prefixlen = strlen (prefix);
itr->pos = 0;
*pc = itr;
return alias_iterate_next (itr);
}
void
alias_iterate_end (alias_iterator_t *pc)
{
free (*pc);
*pc = NULL;
}