mk-amd-map.c revision 82811
1/* 2 * Copyright (c) 1997-2001 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. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Jan-Simon Pendry at Imperial College, London. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 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. 26 * 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 37 * SUCH DAMAGE. 38 * 39 * %W% (Berkeley) %G% 40 * 41 * $Id: mk-amd-map.c,v 1.5.2.1 2001/01/10 03:23:42 ezk Exp $ 42 * $FreeBSD: head/contrib/amd/mk-amd-map/mk-amd-map.c 82811 2001-09-02 19:28:19Z obrien $ 43 */ 44 45/* 46 * Convert a file map into an ndbm map 47 */ 48 49#ifdef HAVE_CONFIG_H 50# include <config.h> 51#endif /* HAVE_CONFIG_H */ 52#include <am_defs.h> 53 54/* (libdb version 2) uses .db extensions but an old dbm API */ 55/* check for libgdbm to distinguish it from linux systems */ 56#if defined(DBM_SUFFIX) && !defined(HAVE_LIBGDBM) 57# define HAVE_DB_SUFFIX 58#endif /* not defined(DBM_SUFFIX) && !defined(HAVE_LIBGDBM) */ 59 60#ifdef HAVE_MAP_NDBM 61 62static int 63store_data(voidp db, char *k, char *v) 64{ 65 datum key, val; 66 67 key.dptr = k; 68 val.dptr = v; 69 key.dsize = strlen(k) + 1; 70 val.dsize = strlen(v) + 1; 71 return dbm_store((DBM *) db, key, val, DBM_INSERT); 72} 73 74 75/* 76 * Read one line from file. 77 */ 78static int 79read_line(char *buf, int size, FILE *fp) 80{ 81 int done = 0; 82 83 do { 84 while (fgets(buf, size, fp)) { 85 int len = strlen(buf); 86 87 done += len; 88 if (len > 1 && buf[len - 2] == '\\' && buf[len - 1] == '\n') { 89 int ch; 90 buf += len - 2; 91 size -= len - 2; 92 *buf = '\n'; 93 buf[1] = '\0'; 94 95 /* 96 * Skip leading white space on next line 97 */ 98 while ((ch = getc(fp)) != EOF && isascii(ch) && isspace(ch)) ; 99 (void) ungetc(ch, fp); 100 } else { 101 return done; 102 } 103 } 104 } while (size > 0 && !feof(fp)); 105 106 return done; 107} 108 109 110/* 111 * Read through a map. 112 */ 113static int 114read_file(FILE *fp, char *map, voidp db) 115{ 116 char key_val[2048]; 117 int chuck = 0; 118 int line_no = 0; 119 int errs = 0; 120 121 while (read_line(key_val, 2048, fp)) { 122 char *kp; 123 char *cp; 124 char *hash; 125 int len = strlen(key_val); 126 127 line_no++; 128 129 /* 130 * Make sure we got the whole line 131 */ 132 if (key_val[len - 1] != '\n') { 133 fprintf(stderr, "line %d in \"%s\" is too long", line_no, map); 134 chuck = 1; 135 } else { 136 key_val[len - 1] = '\0'; 137 } 138 139 /* 140 * Strip comments 141 */ 142 hash = strchr(key_val, '#'); 143 if (hash) 144 *hash = '\0'; 145 146 /* 147 * Find start of key 148 */ 149 for (kp = key_val; *kp && isascii(*kp) && isspace((int)*kp); kp++) ; 150 151 /* 152 * Ignore blank lines 153 */ 154 if (!*kp) 155 goto again; 156 157 /* 158 * Find end of key 159 */ 160 for (cp = kp; *cp && (!isascii(*cp) || !isspace((int)*cp)); cp++) ; 161 162 /* 163 * Check whether key matches, or whether 164 * the entry is a wildcard entry. 165 */ 166 if (*cp) 167 *cp++ = '\0'; 168 while (*cp && isascii(*cp) && isspace((int)*cp)) 169 cp++; 170 if (*kp == '+') { 171 fprintf(stderr, "Can't interpolate %s\n", kp); 172 errs++; 173 } else if (*cp) { 174 if (db) { 175 if (store_data(db, kp, cp) < 0) { 176 fprintf(stderr, "Could store %s -> %s\n", kp, cp); 177 errs++; 178 } 179 } else { 180 printf("%s\t%s\n", kp, cp); 181 } 182 } else { 183 fprintf(stderr, "%s: line %d has no value field", map, line_no); 184 errs++; 185 } 186 187 again: 188 /* 189 * If the last read didn't get a whole line then 190 * throw away the remainder before continuing... 191 */ 192 if (chuck) { 193 while (fgets(key_val, sizeof(key_val), fp) && 194 !strchr(key_val, '\n')) ; 195 chuck = 0; 196 } 197 } 198 return errs; 199} 200 201 202static int 203remove_file(char *f) 204{ 205 if (unlink(f) < 0 && errno != ENOENT) 206 return -1; 207 208 return 0; 209} 210 211 212int 213main(int argc, char *argv[]) 214{ 215 FILE *mapf; /* the input file to read from */ 216 int error; 217 char *mapsrc; 218 DBM *db = NULL; 219 static char maptmp[] = "dbmXXXXXX"; 220#ifdef HAVE_DB_SUFFIX 221 char maptdb[16]; 222 char *map_name_db = (char *) NULL; 223#else /* not HAVE_DB_SUFFIX */ 224 char maptpag[16], maptdir[16]; 225 char *map_name_pag = (char *) NULL, *map_name_dir = (char *) NULL; 226#endif /* not HAVE_DB_SUFFIX */ 227 int len; 228 char *sl; 229 int printit = 0; 230 int usage = 0; 231 int ch; 232 extern int optind; 233 234 /* test options */ 235 while ((ch = getopt(argc, argv, "p")) != -1) 236 switch (ch) { 237 case 'p': 238 printit = 1; 239 break; 240 default: 241 usage++; 242 break; 243 } 244 245 if (usage || optind != (argc - 1)) { 246 fputs("Usage: mk-amd-map [-p] file-map\n", stderr); 247 exit(1); 248 } 249 mapsrc = argv[optind]; 250 251 /* test if can get to the map directory */ 252 sl = strrchr(mapsrc, '/'); 253 if (sl) { 254 *sl = '\0'; 255 if (chdir(mapsrc) < 0) { 256 fputs("Can't chdir to ", stderr); 257 perror(mapsrc); 258 exit(1); 259 } 260 mapsrc = sl + 1; 261 } 262 263 /* open source file */ 264 mapf = fopen(mapsrc, "r"); 265 if (!mapf) { 266 fprintf(stderr, "cannot open source file "); 267 perror(mapsrc); 268 exit(1); 269 } 270 271#ifndef DEBUG 272 signal(SIGINT, SIG_IGN); 273#endif /* DEBUG */ 274 275 if (!printit) { 276 len = strlen(mapsrc); 277#ifdef HAVE_DB_SUFFIX 278 map_name_db = (char *) malloc(len + 4); 279 error = (map_name_db == NULL); 280#else /* not HAVE_DB_SUFFIX */ 281 map_name_pag = (char *) malloc(len + 5); 282 map_name_dir = (char *) malloc(len + 5); 283 error = (map_name_pag == NULL || map_name_dir == NULL); 284#endif /* not HAVE_DB_SUFFIX */ 285 if (error) { 286 perror("mk-amd-map: malloc"); 287 exit(1); 288 } 289 290 mktemp(maptmp); 291 292 /* remove existing temps (if any) */ 293#ifdef HAVE_DB_SUFFIX 294 sprintf(maptdb, "%s.db", maptmp); 295 if (remove_file(maptdb) < 0) { 296 fprintf(stderr, "Can't remove existing temporary file; "); 297 perror(maptdb); 298 exit(1); 299 } 300#else /* not HAVE_DB_SUFFIX */ 301 sprintf(maptpag, "%s.pag", maptmp); 302 sprintf(maptdir, "%s.dir", maptmp); 303 if (remove_file(maptpag) < 0 || remove_file(maptdir) < 0) { 304 fprintf(stderr, "Can't remove existing temporary files; %s and ", maptpag); 305 perror(maptdir); 306 exit(1); 307 } 308#endif /* not HAVE_DB_SUFFIX */ 309 310 db = dbm_open(maptmp, O_RDWR|O_CREAT, 0444); 311 if (!db) { 312 fprintf(stderr, "cannot initialize temporary database: %s", maptmp); 313 exit(1); 314 } 315 } 316 317 /* print db to stdout or to temp database */ 318 error = read_file(mapf, mapsrc, db); 319 fclose(mapf); 320 if (error) { 321 if (printit) 322 fprintf(stderr, "Error reading source file %s\n", mapsrc); 323 else 324 fprintf(stderr, "Error creating database map for %s\n", mapsrc); 325 exit(1); 326 } 327 328 if (printit) 329 exit(0); /* nothing more to do */ 330 331 /* if gets here, we wrote to a database */ 332 333 dbm_close(db); 334 /* all went well */ 335 336#ifdef HAVE_DB_SUFFIX 337 sprintf(map_name_db, "%s.db", mapsrc); 338 if (rename(maptdb, map_name_db) < 0) { 339 fprintf(stderr, "Couldn't rename %s to ", maptdb); 340 perror(map_name_db); 341 /* Throw away the temporary map */ 342 unlink(maptdb); 343 exit(1); 344 } 345#else /* not HAVE_DB_SUFFIX */ 346 sprintf(map_name_pag, "%s.pag", mapsrc); 347 sprintf(map_name_dir, "%s.dir", mapsrc); 348 if (rename(maptpag, map_name_pag) < 0) { 349 fprintf(stderr, "Couldn't rename %s to ", maptpag); 350 perror(map_name_pag); 351 /* Throw away the temporary map */ 352 unlink(maptpag); 353 unlink(maptdir); 354 exit(1); 355 } 356 if (rename(maptdir, map_name_dir) < 0) { 357 fprintf(stderr, "Couldn't rename %s to ", maptdir); 358 perror(map_name_dir); 359 /* remove the (presumably bad) .pag file */ 360 unlink(map_name_pag); 361 /* throw away remaining part of original map */ 362 unlink(map_name_dir); 363 /* throw away the temporary map */ 364 unlink(maptdir); 365 fprintf(stderr, "WARNING: existing map \"%s.{dir,pag}\" destroyed\n", 366 mapsrc); 367 exit(1); 368 } 369#endif /* not HAVE_DB_SUFFIX */ 370 371 exit(0); 372} 373 374#else /* not HAVE_MAP_NDBM */ 375 376int 377main() 378{ 379 fputs("mk-amd-map: This system does not support hashed database files\n", stderr); 380 exit(1); 381} 382 383#endif /* not HAVE_MAP_NDBM */ 384