Mercurial > masqmail
annotate src/tables.c @ 419:d209b4846f2b
The pid files goes to /var/run/masqmail.pid now. Added --with-piddir.
Now we can change the pid dir to /run on system that did this transition.
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Tue, 29 May 2012 21:35:17 +0200 |
parents | 01769f722a18 |
children |
rev | line source |
---|---|
367
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
1 /* |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
2 ** MasqMail |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
3 ** Copyright (C) 1999-2001 Oliver Kurth |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
4 ** Copyright (C) 2008 markus schnalke <meillo@marmaro.de> |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
5 ** |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
6 ** This program is free software; you can redistribute it and/or modify |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
7 ** it under the terms of the GNU General Public License as published by |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
8 ** the Free Software Foundation; either version 2 of the License, or |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
9 ** (at your option) any later version. |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
10 ** |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
11 ** This program is distributed in the hope that it will be useful, |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
14 ** GNU General Public License for more details. |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
15 ** |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
16 ** You should have received a copy of the GNU General Public License |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
17 ** along with this program; if not, write to the Free Software |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
18 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
0 | 19 */ |
20 | |
15 | 21 #include <fnmatch.h> |
385
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
22 #include "masqmail.h" |
15 | 23 |
0 | 24 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
25 table_pair* |
366
41958685480d
Switched to `type *name' style
markus schnalke <meillo@marmaro.de>
parents:
238
diff
changeset
|
26 create_pair(gchar *key, gpointer value) |
0 | 27 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
28 table_pair *pair; |
0 | 29 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
30 pair = g_malloc(sizeof(table_pair)); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
31 pair->key = g_strdup(key); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
32 pair->value = value; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
33 |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
34 return pair; |
0 | 35 } |
36 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
37 table_pair* |
366
41958685480d
Switched to `type *name' style
markus schnalke <meillo@marmaro.de>
parents:
238
diff
changeset
|
38 create_pair_string(gchar *key, gpointer value) |
0 | 39 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
40 table_pair *pair; |
0 | 41 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
42 pair = g_malloc(sizeof(table_pair)); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
43 pair->key = g_strdup(key); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
44 pair->value = (gpointer) (g_strdup(value)); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
45 |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
46 return pair; |
0 | 47 } |
48 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
49 table_pair* |
366
41958685480d
Switched to `type *name' style
markus schnalke <meillo@marmaro.de>
parents:
238
diff
changeset
|
50 parse_table_pair(gchar *line, char delim) |
0 | 51 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
52 gchar buf[256]; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
53 gchar *p, *q; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
54 table_pair *pair; |
0 | 55 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
56 p = line; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
57 q = buf; |
385
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
58 while (*p && (*p != delim) && q < buf + 255) { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
59 *(q++) = *(p++); |
385
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
60 } |
15 | 61 *q = '\0'; |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
62 |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
63 pair = g_malloc(sizeof(table_pair)); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
64 pair->key = g_strdup(g_strstrip(buf)); |
0 | 65 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
66 if (*p) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
67 p++; |
385
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
68 /* while(isspace(*p)) p++; */ |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
69 pair->value = (gpointer *) (g_strdup(g_strstrip(p))); |
385
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
70 } else { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
71 pair->value = (gpointer *) g_strdup(""); |
385
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
72 } |
0 | 73 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
74 return pair; |
0 | 75 } |
76 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
77 gpointer* |
385
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
78 table_find_func(GList *table_list, gchar *key, |
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
79 int (*cmp_func) (const char *, const char *)) |
0 | 80 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
81 GList *node; |
0 | 82 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
83 foreach(table_list, node) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
84 table_pair *pair = (table_pair *) (node->data); |
385
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
85 if (cmp_func(pair->key, key) == 0) { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
86 return pair->value; |
385
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
87 } |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
88 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
89 return NULL; |
0 | 90 } |
91 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
92 gpointer* |
366
41958685480d
Switched to `type *name' style
markus schnalke <meillo@marmaro.de>
parents:
238
diff
changeset
|
93 table_find(GList *table_list, gchar *key) |
0 | 94 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
95 return table_find_func(table_list, key, strcmp); |
0 | 96 } |
97 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
98 gpointer* |
366
41958685480d
Switched to `type *name' style
markus schnalke <meillo@marmaro.de>
parents:
238
diff
changeset
|
99 table_find_case(GList *table_list, gchar *key) |
0 | 100 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
101 return table_find_func(table_list, key, strcasecmp); |
0 | 102 } |
103 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
104 static int |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
105 fnmatch0(const char *pattern, const char *string) |
0 | 106 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
107 return fnmatch(pattern, string, 0); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
108 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
109 |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
110 gpointer* |
366
41958685480d
Switched to `type *name' style
markus schnalke <meillo@marmaro.de>
parents:
238
diff
changeset
|
111 table_find_fnmatch(GList *table_list, gchar *key) |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
112 { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
113 return table_find_func(table_list, key, fnmatch0); |
0 | 114 } |
115 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
116 GList* |
366
41958685480d
Switched to `type *name' style
markus schnalke <meillo@marmaro.de>
parents:
238
diff
changeset
|
117 table_read(gchar *fname, gchar delim) |
0 | 118 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
119 GList *list = NULL; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
120 FILE *fptr; |
385
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
121 gchar buf[256]; |
0 | 122 |
385
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
123 if (!(fptr = fopen(fname, "rt"))) { |
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
124 logwrite(LOG_ALERT, "could not open table file %s: %s. Thus " |
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
125 "no aliasing will be done\n", |
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
126 fname, strerror(errno)); |
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
127 return NULL; |
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
128 } |
0 | 129 |
385
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
130 while (fgets(buf, sizeof buf, fptr)) { |
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
131 if (!*buf || *buf == '#' || *buf == '\n') { |
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
132 continue; |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
133 } |
385
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
134 table_pair *pair; |
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
135 g_strchomp(buf); |
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
136 pair = parse_table_pair(buf, delim); |
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
137 list = g_list_append(list, pair); |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
138 } |
385
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
139 fclose(fptr); |
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
140 if (!list) { |
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
141 logwrite(LOG_NOTICE, "table file %s contained no entries\n", |
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
142 fname); |
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
143 } |
01769f722a18
Refactoring in tables.c.
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
144 return list; |
0 | 145 } |
146 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
147 void |
366
41958685480d
Switched to `type *name' style
markus schnalke <meillo@marmaro.de>
parents:
238
diff
changeset
|
148 destroy_table(GList *table) |
0 | 149 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
150 GList *node; |
0 | 151 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
152 foreach(table, node) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
153 table_pair *p = (table_pair *) (node->data); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
154 g_free(p->key); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
155 g_free(p->value); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
156 g_free(p); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
157 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
158 g_list_free(table); |
0 | 159 } |