1 /* $NetBSD: output_db.c,v 1.1 2010/04/25 00:54:46 joerg Exp $ */
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Luke Mewburn and Christos Zoulas.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: output_db.c,v 1.1 2010/04/25 00:54:46 joerg Exp $");
37 #include <sys/param.h>
51 #include <stringlist.h>
57 static const HASHINFO hinfo
= {
66 static void store(DBT
*, DBT
*, int);
67 static void killproto(DBT
*);
68 static const char *mkaliases(StringList
*, char *, size_t);
71 db_open(const char *tname
)
73 db
= dbopen(tname
, O_RDWR
| O_CREAT
| O_EXCL
,
74 (S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
), DB_HASH
, &hinfo
);
76 return db
!= NULL
? 0 : -1;
91 db_add(StringList
*sl
, size_t port
, const char *proto
, size_t *cnt
,
95 char keyb
[BUFSIZ
], datab
[BUFSIZ
], abuf
[BUFSIZ
];
100 /* key `indirect key', data `full line' */
101 data
.size
= snprintf(datab
, sizeof(datab
), "%zu", (*cnt
)++) + 1;
102 key
.size
= snprintf(keyb
, sizeof(keyb
), "%s %zu/%s %s",
103 sl
->sl_str
[0], port
, proto
, mkaliases(sl
, abuf
, sizeof(abuf
))) + 1;
104 store(&data
, &key
, warndup
);
106 /* key `\377port/proto', data = `indirect key' */
107 key
.size
= snprintf(keyb
, sizeof(keyb
), "\377%zu/%s",
109 store(&key
, &data
, warndup
);
111 /* key `\377port', data = `indirect key' */
113 store(&key
, &data
, warndup
);
115 /* add references for service and all aliases */
116 for (i
= 0; i
< sl
->sl_cur
; i
++) {
117 /* key `\376service/proto', data = `indirect key' */
118 key
.size
= snprintf(keyb
, sizeof(keyb
), "\376%s/%s",
119 sl
->sl_str
[i
], proto
) + 1;
120 store(&key
, &data
, warndup
);
122 /* key `\376service', data = `indirect key' */
124 store(&key
, &data
, warndup
);
132 char *p
, *d
= key
->data
;
134 if ((p
= strchr(d
, '/')) == NULL
)
141 store(DBT
*key
, DBT
*data
, int warndup
)
144 int k
= key
->size
- 1;
145 int d
= data
->size
- 1;
146 (void)printf("store [%*.*s] [%*.*s]\n",
147 k
, k
, (char *)key
->data
+ 1,
148 d
, d
, (char *)data
->data
+ 1);
150 switch ((db
->put
)(db
, key
, data
, R_NOOVERWRITE
)) {
155 warnx("duplicate service `%s'",
156 &((char *)key
->data
)[1]);
168 mkaliases(StringList
*sl
, char *buf
, size_t len
)
173 for (i
= 1, pos
= 0; i
< sl
->sl_cur
; i
++) {
174 nc
= strlcpy(buf
+ pos
, sl
->sl_str
[i
], len
);
179 nc
= strlcpy(buf
+ pos
, " ", len
);
187 warn("aliases for `%s' truncated", sl
->sl_str
[0]);