]> git.meshlink.io Git - catta/blob - avahi-utils/stdb.c
Allow storing the service type database as Solaris DBM file, alternatively to gdbm...
[catta] / avahi-utils / stdb.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10  
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #include <config.h>
23 #ifdef HAVE_GDBM
24 #include <gdbm.h>
25 #endif
26 #ifdef HAVE_DBM
27 #include <ndbm.h>
28 #include <fcntl.h>
29 #endif
30 #include <stdlib.h>
31 #include <string.h>
32 #include <locale.h>
33 #include <stdio.h>
34
35 #include <avahi-common/malloc.h>
36
37 #include "stdb.h"
38
39 #ifdef HAVE_GDBM
40 static GDBM_FILE gdbm_file = NULL;
41 #endif
42 #ifdef HAVE_DBM
43 static DBM *dbm_file = NULL;
44 #endif
45 static char *buffer = NULL;
46
47 static int init(void) {
48
49 #ifdef HAVE_GDBM
50     if (gdbm_file)
51         return 0;
52
53     if (!(gdbm_file = gdbm_open((char*) DATABASE_FILE, 0, GDBM_READER, 0, NULL)))
54         return -1;
55 #endif
56 #ifdef HAVE_DBM
57     if (dbm_file)
58         return 0;
59
60     if (!(dbm_file = dbm_open((char*) DATABASE_FILE, O_RDONLY, 0)))
61         return -1;
62 #endif
63
64     return 0;
65 }
66
67 const char* stdb_lookup(const char *name) {
68     datum key, data;
69     const char *loc;
70
71     if (init() < 0)
72         goto fail;
73
74     data.dptr = NULL;
75     data.dsize = 0;
76     
77     if ((loc = setlocale(LC_MESSAGES, NULL))) {
78         char k[256];
79         
80         snprintf(k, sizeof(k), "%s[%s]", name, loc);
81         key.dptr = k;
82         key.dsize = strlen(k);
83 #ifdef HAVE_GDBM
84         data = gdbm_fetch(gdbm_file, key);
85 #endif
86 #ifdef HAVE_DBM
87         data = dbm_fetch(dbm_file, key);
88 #endif
89
90         if (!data.dptr) {
91             char l[32], *e;
92             snprintf(l, sizeof(l), "%s", loc);
93             
94             if ((e = strchr(l, '@'))) {
95                 *e = 0;
96                 snprintf(k, sizeof(k), "%s[%s]", name, l);
97                 key.dptr = k;
98                 key.dsize = strlen(k);
99 #ifdef HAVE_GDBM
100                 data = gdbm_fetch(gdbm_file, key);
101 #endif
102 #ifdef HAVE_DBM
103                 data = dbm_fetch(dbm_file, key);
104 #endif
105             }
106
107             if (!data.dptr) {
108                 if ((e = strchr(l, '_'))) {
109                     *e = 0;
110                     snprintf(k, sizeof(k), "%s[%s]", name, l);
111                     key.dptr = k;
112                     key.dsize = strlen(k);
113 #ifdef HAVE_GDBM
114                     data = gdbm_fetch(gdbm_file, key);
115 #endif
116 #ifdef HAVE_DBM
117                     data = dbm_fetch(dbm_file, key);
118 #endif
119                 }
120             }
121         }
122     }
123
124     if (!data.dptr) {
125         key.dptr = (char*) name;
126         key.dsize = strlen(name);
127 #ifdef HAVE_GDBM
128         data = gdbm_fetch(gdbm_file, key);
129 #endif
130 #ifdef HAVE_DBM
131         data = dbm_fetch(dbm_file, key);
132 #endif
133     }
134
135     if (!data.dptr)
136         goto fail;
137
138     avahi_free(buffer);
139     buffer = avahi_strndup(data.dptr, data.dsize);
140     free(data.dptr);
141     
142     return buffer;
143     
144 fail:
145
146     return name;
147 }
148
149 void stdb_shutdown(void) {
150 #ifdef HAVE_GDBM
151     if (gdbm_file)
152         gdbm_close(gdbm_file);
153 #endif
154 #ifdef HAVE_DBM
155     if (dbm_file)
156         dbm_close(dbm_file);
157 #endif
158
159     avahi_free(buffer);
160 }