From 13bcc37c0233d321e580cd3d82f6a958d07fea89 Mon Sep 17 00:00:00 2001 From: "David E. O'Brien" Date: Thu, 27 Aug 1998 02:56:47 +0000 Subject: [PATCH] Properly create db files vs. dbm files. Obtained from: NetBSD --- contrib/amd/mk-amd-map/mk-amd-map.c | 51 ++++++++++------------------- 1 file changed, 18 insertions(+), 33 deletions(-) diff --git a/contrib/amd/mk-amd-map/mk-amd-map.c b/contrib/amd/mk-amd-map/mk-amd-map.c index 3eba7b293b4b..45f47a6a1a2a 100644 --- a/contrib/amd/mk-amd-map/mk-amd-map.c +++ b/contrib/amd/mk-amd-map/mk-amd-map.c @@ -38,7 +38,7 @@ * * %W% (Berkeley) %G% * - * $Id: mk-amd-map.c,v 1.1.1.1 1998/08/23 22:07:21 obrien Exp $ + * $Id: mk-amd-map.c,v 1.2 1998/08/23 22:52:09 obrien Exp $ */ /* @@ -51,7 +51,6 @@ #include /* dummy variables */ -char *progname; char hostname[MAXHOSTNAMELEN]; int orig_umask, foreground, debug_flags; pid_t mypid; @@ -219,8 +218,8 @@ main(int argc, char *argv[]) int rc = 0; DBM *mapd = NULL; static char maptmp[] = "dbmXXXXXX"; - char maptpag[16], maptdir[16]; - char *mappag = (char *) NULL, *mapdir = (char *) NULL; + char maptdb[16]; + char *mapdb = (char *) NULL; int len; char *sl; int printit = 0; @@ -259,9 +258,8 @@ main(int argc, char *argv[]) if (!printit) { len = strlen(map); - mappag = (char *) malloc(len + 5); - mapdir = (char *) malloc(len + 5); - if (!mappag || !mapdir) { + mapdb = (char *) malloc(len + 4); + if (!mapdb) { perror("mk-amd-map: malloc"); exit(1); } @@ -277,11 +275,10 @@ main(int argc, char *argv[]) #endif /* not HAVE_MKSTEMP */ /* open DBM files */ - sprintf(maptpag, "%s.pag", maptmp); - sprintf(maptdir, "%s.dir", maptmp); - if (remove_file(maptpag) < 0 || remove_file(maptdir) < 0) { - fprintf(stderr, "Can't remove existing temporary files; %s and", maptpag); - perror(maptdir); + sprintf(maptdb, "%s.db", maptmp); + if (remove_file(maptdb) < 0) { + fprintf(stderr, "Can't remove existing temporary files;"); + perror(maptdb); exit(1); } } @@ -301,7 +298,7 @@ main(int argc, char *argv[]) int error = read_file(mapf, map, mapd); (void) close(mapfd); (void) fclose(mapf); - dbm_close(mapd); + dbm_close(mapd); if (printit) { if (error) { fprintf(stderr, "Error creating ndbm map for %s\n", map); @@ -313,33 +310,21 @@ main(int argc, char *argv[]) fprintf(stderr, "Error reading source file %s\n", map); rc = 1; } else { - sprintf(mappag, "%s.pag", map); - sprintf(mapdir, "%s.dir", map); - if (rename(maptpag, mappag) < 0) { - fprintf(stderr, "Couldn't rename %s to ", maptpag); - perror(mappag); + sprintf(mapdb, "%s.db", map); + if (unlink(mapdb) == 0) + fprintf(stderr, "WARNING: existing map \"%s.db\" destroyed\n", map); + if (rename(maptdb, mapdb) < 0) { + fprintf(stderr, "Couldn't rename %s to ", maptdb); + perror(mapdb); /* Throw away the temporary map */ - unlink(maptpag); - unlink(maptdir); - rc = 1; - - } else if (rename(maptdir, mapdir) < 0) { - fprintf(stderr, "Couldn't rename %s to ", maptdir); - perror(mapdir); - /* Put the .pag file back */ - rename(mappag, maptpag); - /* Throw away remaining part of original map */ - unlink(mapdir); - fprintf(stderr, - "WARNING: existing map \"%s.{dir,pag}\" destroyed\n", - map); + unlink(maptdb); rc = 1; } } } } else { - fprintf(stderr, "Can't open \"%s.{dir,pag}\" for ", map); + fprintf(stderr, "Can't open \"%s.db\" for ", map); perror("writing"); rc = 1; }