masqmail

view src/tables.c @ 17:6c59dedd06be

fixed delivery with empty or non-existent alias file Thanks to Marcos Dione for the hint where to look. (Closes Debian bug #417842)
author meillo@marmaro.de
date Thu, 06 Nov 2008 09:41:35 +0100
parents f671821d8222
children 7354c2e0eb31
line source
1 /* MasqMail
2 Copyright (C) 1999-2001 Oliver Kurth
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
19 #include <fnmatch.h>
21 #include "masqmail.h"
23 table_pair*
24 create_pair(gchar * key, gpointer value)
25 {
26 table_pair *pair;
28 pair = g_malloc(sizeof(table_pair));
29 pair->key = g_strdup(key);
30 pair->value = value;
32 return pair;
33 }
35 table_pair*
36 create_pair_string(gchar * key, gpointer value)
37 {
38 table_pair *pair;
40 pair = g_malloc(sizeof(table_pair));
41 pair->key = g_strdup(key);
42 pair->value = (gpointer) (g_strdup(value));
44 return pair;
45 }
47 table_pair*
48 parse_table_pair(gchar * line, char delim)
49 {
50 gchar buf[256];
51 gchar *p, *q;
52 table_pair *pair;
54 p = line;
55 q = buf;
56 while ((*p != '\0') && (*p != delim) && q < buf + 255)
57 *(q++) = *(p++);
58 *q = '\0';
60 pair = g_malloc(sizeof(table_pair));
61 pair->key = g_strdup(g_strstrip(buf));
63 if (*p) {
64 p++;
65 /* while(isspace(*p)) p++; */
66 pair->value = (gpointer *) (g_strdup(g_strstrip(p)));
67 } else
68 pair->value = (gpointer *) g_strdup("");
70 return pair;
71 }
73 gpointer*
74 table_find_func(GList * table_list, gchar * key, int (*cmp_func) (const char *, const char *))
75 {
76 GList *node;
78 foreach(table_list, node) {
79 table_pair *pair = (table_pair *) (node->data);
80 if (cmp_func(pair->key, key) == 0)
81 return pair->value;
82 }
83 return NULL;
84 }
86 gpointer*
87 table_find(GList * table_list, gchar * key)
88 {
89 return table_find_func(table_list, key, strcmp);
90 }
92 gpointer*
93 table_find_case(GList * table_list, gchar * key)
94 {
95 return table_find_func(table_list, key, strcasecmp);
96 }
98 static int
99 fnmatch0(const char *pattern, const char *string)
100 {
101 return fnmatch(pattern, string, 0);
102 }
104 gpointer*
105 table_find_fnmatch(GList * table_list, gchar * key)
106 {
107 return table_find_func(table_list, key, fnmatch0);
108 }
110 GList*
111 table_read(gchar * fname, gchar delim)
112 {
113 GList *list = NULL;
114 FILE *fptr;
116 if ((fptr = fopen(fname, "rt"))) {
117 gchar buf[256];
119 while (fgets(buf, 255, fptr)) {
120 if (buf[0] && (buf[0] != '#') && (buf[0] != '\n')) {
121 table_pair *pair;
122 g_strchomp(buf);
123 pair = parse_table_pair(buf, delim);
124 list = g_list_append(list, pair);
125 }
126 }
127 fclose(fptr);
128 if (list == NULL)
129 logwrite(LOG_NOTICE, "empty table file %s\n", fname);
130 return list;
131 }
132 logwrite(LOG_ALERT, "could not open table file %s: %s. Thus no aliasing is done\n", fname, strerror(errno));
134 return NULL;
135 }
137 void
138 destroy_table(GList * table)
139 {
140 GList *node;
142 foreach(table, node) {
143 table_pair *p = (table_pair *) (node->data);
144 g_free(p->key);
145 g_free(p->value);
146 g_free(p);
147 }
148 g_list_free(table);
149 }