2 * Copyright (c) 1997-1998 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 acknowledgement:
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
41 * $Id: mk-amd-map.c,v 1.4 1998/08/27 07:25:25 obrien Exp $
45 * Convert a file map into an ndbm map
50 #endif /* HAVE_CONFIG_H */
54 char hostname[MAXHOSTNAMELEN];
55 int orig_umask, foreground, debug_flags;
59 /* (libdb version 2) uses .db extensions but an old dbm API */
60 /* check for libgdbm to distinguish it from linux systems */
61 #if defined(DBM_SUFFIX) && !defined(HAVE_LIBGDBM)
62 # define HAVE_DB_SUFFIX
63 #endif /* not defined(DBM_SUFFIX) && !defined(HAVE_LIBGDBM) */
68 store_data(voidp db, char *k, char *v)
74 key.dsize = strlen(k) + 1;
75 val.dsize = strlen(v) + 1;
76 return dbm_store((DBM *) db, key, val, DBM_INSERT);
81 * Read one line from file.
84 read_line(char *buf, int size, FILE *fp)
89 while (fgets(buf, size, fp)) {
90 int len = strlen(buf);
93 if (len > 1 && buf[len - 2] == '\\' && buf[len - 1] == '\n') {
101 * Skip leading white space on next line
103 while ((ch = getc(fp)) != EOF && isascii(ch) && isspace(ch)) ;
104 (void) ungetc(ch, fp);
109 } while (size > 0 && !feof(fp));
116 * Read through a map.
119 read_file(FILE *fp, char *map, voidp db)
126 while (read_line(key_val, 2048, fp)) {
130 int len = strlen(key_val);
135 * Make sure we got the whole line
137 if (key_val[len - 1] != '\n') {
138 fprintf(stderr, "line %d in \"%s\" is too long", line_no, map);
141 key_val[len - 1] = '\0';
147 hash = strchr(key_val, '#');
154 for (kp = key_val; *kp && isascii(*kp) && isspace((int)*kp); kp++) ;
165 for (cp = kp; *cp && (!isascii(*cp) || !isspace((int)*cp)); cp++) ;
168 * Check whether key matches, or whether
169 * the entry is a wildcard entry.
173 while (*cp && isascii(*cp) && isspace((int)*cp))
176 fprintf(stderr, "Can't interpolate %s\n", kp);
180 if (store_data(db, kp, cp) < 0) {
181 fprintf(stderr, "Could store %s -> %s\n", kp, cp);
185 printf("%s\t%s\n", kp, cp);
188 fprintf(stderr, "%s: line %d has no value field", map, line_no);
194 * If the last read didn't get a whole line then
195 * throw away the remainder before continuing...
198 while (fgets(key_val, sizeof(key_val), fp) &&
199 !strchr(key_val, '\n')) ;
210 if (unlink(f) < 0 && errno != ENOENT)
218 main(int argc, char *argv[])
220 FILE *mapf; /* the input file to read from */
224 static char maptmp[] = "dbmXXXXXX";
225 #ifdef HAVE_DB_SUFFIX
227 char *map_name_db = (char *) NULL;
228 #else /* not HAVE_DB_SUFFIX */
229 char maptpag[16], maptdir[16];
230 char *map_name_pag = (char *) NULL, *map_name_dir = (char *) NULL;
231 #endif /* not HAVE_DB_SUFFIX */
240 while ((ch = getopt(argc, argv, "p")) != -1)
250 if (usage || optind != (argc - 1)) {
251 fputs("Usage: mk-amd-map [-p] file-map\n", stderr);
254 mapsrc = argv[optind];
256 /* test if can get to the map directory */
257 sl = strrchr(mapsrc, '/');
260 if (chdir(mapsrc) < 0) {
261 fputs("Can't chdir to ", stderr);
268 /* open source file */
269 mapf = fopen(mapsrc, "r");
271 fprintf(stderr, "cannot open source file ");
277 signal(SIGINT, SIG_IGN);
281 len = strlen(mapsrc);
282 #ifdef HAVE_DB_SUFFIX
283 map_name_db = (char *) malloc(len + 4);
284 error = (map_name_db == NULL);
285 #else /* not HAVE_DB_SUFFIX */
286 map_name_pag = (char *) malloc(len + 5);
287 map_name_dir = (char *) malloc(len + 5);
288 error = (map_name_pag == NULL || map_name_dir == NULL);
289 #endif /* not HAVE_DB_SUFFIX */
291 perror("mk-amd-map: malloc");
297 /* remove existing temps (if any) */
298 #ifdef HAVE_DB_SUFFIX
299 sprintf(maptdb, "%s.db", maptmp);
300 if (remove_file(maptdb) < 0) {
301 fprintf(stderr, "Can't remove existing temporary file; ");
305 #else /* not HAVE_DB_SUFFIX */
306 sprintf(maptpag, "%s.pag", maptmp);
307 sprintf(maptdir, "%s.dir", maptmp);
308 if (remove_file(maptpag) < 0 || remove_file(maptdir) < 0) {
309 fprintf(stderr, "Can't remove existing temporary files; %s and ", maptpag);
313 #endif /* not HAVE_DB_SUFFIX */
315 db = dbm_open(maptmp, O_RDWR|O_CREAT, 0444);
317 fprintf(stderr, "cannot initialize temporary database: %s", maptmp);
322 /* print db to stdout or to temp database */
323 error = read_file(mapf, mapsrc, db);
327 fprintf(stderr, "Error reading source file %s\n", mapsrc);
329 fprintf(stderr, "Error creating database map for %s\n", mapsrc);
334 exit(0); /* nothing more to do */
336 /* if gets here, we wrote to a database */
341 #ifdef HAVE_DB_SUFFIX
342 sprintf(map_name_db, "%s.db", mapsrc);
343 if (rename(maptdb, map_name_db) < 0) {
344 fprintf(stderr, "Couldn't rename %s to ", maptdb);
346 /* Throw away the temporary map */
350 #else /* not HAVE_DB_SUFFIX */
351 sprintf(map_name_pag, "%s.pag", mapsrc);
352 sprintf(map_name_dir, "%s.dir", mapsrc);
353 if (rename(maptpag, map_name_pag) < 0) {
354 fprintf(stderr, "Couldn't rename %s to ", maptpag);
355 perror(map_name_pag);
356 /* Throw away the temporary map */
361 if (rename(maptdir, map_name_dir) < 0) {
362 fprintf(stderr, "Couldn't rename %s to ", maptdir);
363 perror(map_name_dir);
364 /* remove the (presumably bad) .pag file */
365 unlink(map_name_pag);
366 /* throw away remaining part of original map */
367 unlink(map_name_dir);
368 /* throw away the temporary map */
370 fprintf(stderr, "WARNING: existing map \"%s.{dir,pag}\" destroyed\n",
374 #endif /* not HAVE_DB_SUFFIX */
379 #else /* not HAVE_MAP_NDBM */
384 fputs("mk-amd-map: This system does not support hashed database files\n", stderr);
388 #endif /* not HAVE_MAP_NDBM */