2 * Copyright (c) 1997-2006 Erez Zadok
3 * Copyright (c) 1990 Jan-Simon Pendry
4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1990 The Regents of the University of California.
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgment:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * File: am-utils/mk-amd-map/mk-amd-map.c
44 * Convert a file map into an ndbm map
49 #endif /* HAVE_CONFIG_H */
52 /* (libdb version 2) uses .db extensions but an old dbm API */
53 /* check for libgdbm to distinguish it from linux systems */
54 #if defined(DBM_SUFFIX) && !defined(HAVE_LIBGDBM)
55 # define HAVE_DB_SUFFIX
56 #endif /* not defined(DBM_SUFFIX) && !defined(HAVE_LIBGDBM) */
61 store_data(voidp db, char *k, char *v)
67 key.dsize = strlen(k) + 1;
68 val.dsize = strlen(v) + 1;
69 return dbm_store((DBM *) db, key, val, DBM_INSERT);
74 * Read one line from file.
77 read_line(char *buf, int size, FILE *fp)
82 while (fgets(buf, size, fp)) {
83 int len = strlen(buf);
86 if (len > 1 && buf[len - 2] == '\\' && buf[len - 1] == '\n') {
94 * Skip leading white space on next line
96 while ((ch = getc(fp)) != EOF && isascii(ch) && isspace(ch)) ;
97 (void) ungetc(ch, fp);
102 } while (size > 0 && !feof(fp));
109 * Read through a map.
112 read_file(FILE *fp, char *map, voidp db)
119 while (read_line(key_val, 2048, fp)) {
123 int len = strlen(key_val);
128 * Make sure we got the whole line
130 if (key_val[len - 1] != '\n') {
131 fprintf(stderr, "line %d in \"%s\" is too long", line_no, map);
134 key_val[len - 1] = '\0';
140 hash = strchr(key_val, '#');
147 for (kp = key_val; *kp && isascii(*kp) && isspace((int)*kp); kp++) ;
158 for (cp = kp; *cp && (!isascii(*cp) || !isspace((int)*cp)); cp++) ;
161 * Check whether key matches, or whether
162 * the entry is a wildcard entry.
166 while (*cp && isascii(*cp) && isspace((int)*cp))
169 fprintf(stderr, "Can't interpolate %s\n", kp);
173 if (store_data(db, kp, cp) < 0) {
174 fprintf(stderr, "Could store %s -> %s\n", kp, cp);
178 printf("%s\t%s\n", kp, cp);
181 fprintf(stderr, "%s: line %d has no value field", map, line_no);
187 * If the last read didn't get a whole line then
188 * throw away the remainder before continuing...
191 while (fgets(key_val, sizeof(key_val), fp) &&
192 !strchr(key_val, '\n')) ;
203 if (unlink(f) < 0 && errno != ENOENT)
211 main(int argc, char *argv[])
213 FILE *mapf; /* the input file to read from */
217 static char maptmp[] = "dbmXXXXXX";
218 #ifdef HAVE_DB_SUFFIX
220 char *map_name_db = (char *) NULL;
221 #else /* not HAVE_DB_SUFFIX */
222 char maptpag[16], maptdir[16];
223 char *map_name_pag = (char *) NULL, *map_name_dir = (char *) NULL;
224 #endif /* not HAVE_DB_SUFFIX */
233 while ((ch = getopt(argc, argv, "p")) != -1)
243 if (usage || optind != (argc - 1)) {
244 fputs("Usage: mk-amd-map [-p] file-map\n", stderr);
247 mapsrc = argv[optind];
249 /* test if can get to the map directory */
250 sl = strrchr(mapsrc, '/');
253 if (chdir(mapsrc) < 0) {
254 fputs("Can't chdir to ", stderr);
261 /* open source file */
262 mapf = fopen(mapsrc, "r");
264 fprintf(stderr, "cannot open source file ");
270 signal(SIGINT, SIG_IGN);
274 /* enough space for ".db" or ".pag" or ".dir" appended */
275 l = strlen(mapsrc) + 5;
276 #ifdef HAVE_DB_SUFFIX
277 map_name_db = (char *) malloc(l);
278 error = (map_name_db == NULL);
279 #else /* not HAVE_DB_SUFFIX */
280 map_name_pag = (char *) malloc(l);
281 map_name_dir = (char *) malloc(l);
282 error = (map_name_pag == NULL || map_name_dir == NULL);
283 #endif /* not HAVE_DB_SUFFIX */
285 perror("mk-amd-map: malloc");
292 * XXX: hack to avoid compiler complaints about mktemp not being
293 * secure, since we have to do a dbm_open on this anyway. So use
294 * mkstemp if you can, and then close the fd, but we get a safe
295 * and unique file name.
298 dummyfd = mkstemp(maptmp);
302 #else /* not HAVE_MKSTEMP */
304 #endif /* not HAVE_MKSTEMP */
306 /* remove existing temps (if any) */
307 #ifdef HAVE_DB_SUFFIX
308 xsnprintf(maptdb, sizeof(maptdb), "%s.db", maptmp);
309 if (remove_file(maptdb) < 0) {
310 fprintf(stderr, "Can't remove existing temporary file; ");
314 #else /* not HAVE_DB_SUFFIX */
315 xsnprintf(maptpag, sizeof(maptpag), "%s.pag", maptmp);
316 xsnprintf(maptdir, sizeof(maptdir), "%s.dir", maptmp);
317 if (remove_file(maptpag) < 0 || remove_file(maptdir) < 0) {
318 fprintf(stderr, "Can't remove existing temporary files; %s and ", maptpag);
322 #endif /* not HAVE_DB_SUFFIX */
324 db = dbm_open(maptmp, O_RDWR|O_CREAT|O_EXCL, 0444);
326 fprintf(stderr, "cannot initialize temporary database: %s", maptmp);
331 /* print db to stdout or to temp database */
332 error = read_file(mapf, mapsrc, db);
336 fprintf(stderr, "Error reading source file %s\n", mapsrc);
338 fprintf(stderr, "Error creating database map for %s\n", mapsrc);
343 exit(0); /* nothing more to do */
345 /* if gets here, we wrote to a database */
350 #ifdef HAVE_DB_SUFFIX
351 /* sizeof(map_name_db) is malloc'ed above */
352 xsnprintf(map_name_db, l, "%s.db", mapsrc);
353 if (rename(maptdb, map_name_db) < 0) {
354 fprintf(stderr, "Couldn't rename %s to ", maptdb);
356 /* Throw away the temporary map */
360 #else /* not HAVE_DB_SUFFIX */
361 /* sizeof(map_name_{pag,dir}) are malloc'ed above */
362 xsnprintf(map_name_pag, l, "%s.pag", mapsrc);
363 xsnprintf(map_name_dir, l, "%s.dir", mapsrc);
364 if (rename(maptpag, map_name_pag) < 0) {
365 fprintf(stderr, "Couldn't rename %s to ", maptpag);
366 perror(map_name_pag);
367 /* Throw away the temporary map */
372 if (rename(maptdir, map_name_dir) < 0) {
373 fprintf(stderr, "Couldn't rename %s to ", maptdir);
374 perror(map_name_dir);
375 /* remove the (presumably bad) .pag file */
376 unlink(map_name_pag);
377 /* throw away remaining part of original map */
378 unlink(map_name_dir);
379 /* throw away the temporary map */
381 fprintf(stderr, "WARNING: existing map \"%s.{dir,pag}\" destroyed\n",
385 #endif /* not HAVE_DB_SUFFIX */
390 #else /* not HAVE_MAP_NDBM */
395 fputs("mk-amd-map: This system does not support hashed database files\n", stderr);
399 #endif /* not HAVE_MAP_NDBM */