summaryrefslogtreecommitdiffstats
path: root/contrib/amd/mk-amd-map
diff options
context:
space:
mode:
authorobrien <obrien@FreeBSD.org>1998-11-14 03:13:33 +0000
committerobrien <obrien@FreeBSD.org>1998-11-14 03:13:33 +0000
commitf3f64ef024d63360298245ec26ebf66eb395bd71 (patch)
treed7c98193482b80a21d31de21be70e8f70eb820f1 /contrib/amd/mk-amd-map
parentd9a3e6b7f346909cb3bf355119d588c1c35beb53 (diff)
downloadFreeBSD-src-f3f64ef024d63360298245ec26ebf66eb395bd71.zip
FreeBSD-src-f3f64ef024d63360298245ec26ebf66eb395bd71.tar.gz
Clean up conflicts.
Diffstat (limited to 'contrib/amd/mk-amd-map')
-rw-r--r--contrib/amd/mk-amd-map/mk-amd-map.c188
1 files changed, 117 insertions, 71 deletions
diff --git a/contrib/amd/mk-amd-map/mk-amd-map.c b/contrib/amd/mk-amd-map/mk-amd-map.c
index 1339485..1c2640f 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.3 1998/08/27 02:56:47 obrien Exp $
+ * $Id: mk-amd-map.c,v 1.4 1998/08/27 07:25:25 obrien Exp $
*/
/*
@@ -56,6 +56,11 @@ int orig_umask, foreground, debug_flags;
pid_t mypid;
serv_state amd_state;
+/* (libdb version 2) uses .db extensions but an old dbm API */
+/* check for libgdbm to distinguish it from linux systems */
+#if defined(DBM_SUFFIX) && !defined(HAVE_LIBGDBM)
+# define HAVE_DB_SUFFIX
+#endif /* not defined(DBM_SUFFIX) && !defined(HAVE_LIBGDBM) */
#ifdef HAVE_MAP_NDBM
@@ -118,7 +123,7 @@ read_file(FILE *fp, char *map, voidp db)
int line_no = 0;
int errs = 0;
- while (read_line(key_val, sizeof(key_val), fp)) {
+ while (read_line(key_val, 2048, fp)) {
char *kp;
char *cp;
char *hash;
@@ -212,14 +217,18 @@ remove_file(char *f)
int
main(int argc, char *argv[])
{
- FILE *mapf;
- int mapfd = -1;
- char *map;
- int rc = 0;
- DBM *mapd = NULL;
+ FILE *mapf; /* the input file to read from */
+ int error;
+ char *mapsrc;
+ DBM *db = NULL;
static char maptmp[] = "dbmXXXXXX";
+#ifdef HAVE_DB_SUFFIX
char maptdb[16];
- char *mapdb = (char *) NULL;
+ char *map_name_db = (char *) NULL;
+#else /* not HAVE_DB_SUFFIX */
+ char maptpag[16], maptdir[16];
+ char *map_name_pag = (char *) NULL, *map_name_dir = (char *) NULL;
+#endif /* not HAVE_DB_SUFFIX */
int len;
char *sl;
int printit = 0;
@@ -242,97 +251,134 @@ main(int argc, char *argv[])
fputs("Usage: mk-amd-map [-p] file-map\n", stderr);
exit(1);
}
- map = argv[optind];
+ mapsrc = argv[optind];
/* test if can get to the map directory */
- sl = strrchr(map, '/');
+ sl = strrchr(mapsrc, '/');
if (sl) {
*sl = '\0';
- if (chdir(map) < 0) {
+ if (chdir(mapsrc) < 0) {
fputs("Can't chdir to ", stderr);
- perror(map);
+ perror(mapsrc);
exit(1);
}
- map = sl + 1;
+ mapsrc = sl + 1;
}
+ /* open source file */
+ mapf = fopen(mapsrc, "r");
+ if (!mapf) {
+ fprintf(stderr, "cannot open source file ");
+ perror(mapsrc);
+ exit(1);
+ }
+
+#ifndef DEBUG
+ signal(SIGINT, SIG_IGN);
+#endif /* DEBUG */
+
if (!printit) {
- len = strlen(map);
- mapdb = (char *) malloc(len + 4);
- if (!mapdb) {
+ len = strlen(mapsrc);
+#ifdef HAVE_DB_SUFFIX
+ map_name_db = (char *) malloc(len + 4);
+ error = (map_name_db == NULL);
+#else /* not HAVE_DB_SUFFIX */
+ map_name_pag = (char *) malloc(len + 5);
+ map_name_dir = (char *) malloc(len + 5);
+ error = (map_name_pag == NULL || map_name_dir == NULL);
+#endif /* not HAVE_DB_SUFFIX */
+ if (error) {
perror("mk-amd-map: malloc");
exit(1);
}
-#ifdef HAVE_MKSTEMP
- mapfd = mkstemp(maptmp);
-#else /* not HAVE_MKSTEMP */
- map = mktemp(maptmp);
- if (!maptmp) {
- fprintf(stderr, "cannot create temporary file\n");
- exit(1);
- }
- mapfd = open(map, O_RDONLY);
-#endif /* not HAVE_MKSTEMP */
- /* open DBM files */
+ mktemp(maptmp);
+
+ /* remove existing temps (if any) */
+#ifdef HAVE_DB_SUFFIX
sprintf(maptdb, "%s.db", maptmp);
if (remove_file(maptdb) < 0) {
- fprintf(stderr, "Can't remove existing temporary files;");
+ fprintf(stderr, "Can't remove existing temporary file; ");
perror(maptdb);
exit(1);
}
+#else /* not HAVE_DB_SUFFIX */
+ 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);
+ exit(1);
+ }
+#endif /* not HAVE_DB_SUFFIX */
+
+ db = dbm_open(maptmp, O_RDWR|O_CREAT, 0444);
+ if (!db) {
+ fprintf(stderr, "cannot initialize temporary database: %s", maptmp);
+ exit(1);
+ }
}
- /* open and check if map file was opened OK */
- mapf = fdopen(mapfd, "r");
- if (mapf && !printit)
- mapd = dbm_open(maptmp, O_RDWR|O_CREAT, 0444);
- else
- mapd = 0;
-#ifndef DEBUG
- /* ignore ^C if debuggung is on (but why?) */
- signal(SIGINT, SIG_IGN);
-#endif /* not DEBUG */
-
- if (mapd || printit) {
- int error = read_file(mapf, map, mapd);
- (void) close(mapfd);
- (void) fclose(mapf);
- dbm_close(mapd);
- if (printit) {
- if (error) {
- fprintf(stderr, "Error creating ndbm map for %s\n", map);
- rc = 1;
- }
- } else {
+ /* print db to stdout or to temp database */
+ error = read_file(mapf, mapsrc, db);
+ fclose(mapf);
+ if (error) {
+ if (printit)
+ fprintf(stderr, "Error reading source file %s\n", mapsrc);
+ else
+ fprintf(stderr, "Error creating database map for %s\n", mapsrc);
+ exit(1);
+ }
- if (error) {
- fprintf(stderr, "Error reading source file %s\n", map);
- rc = 1;
- } else {
- 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(maptdb);
- rc = 1;
- }
- }
- }
+ if (printit)
+ exit(0); /* nothing more to do */
+
+ /* if gets here, we wrote to a database */
- } else {
- fprintf(stderr, "Can't open \"%s.db\" for ", map);
- perror("writing");
- rc = 1;
+ dbm_close(db);
+ /* all went well */
+
+#ifdef HAVE_DB_SUFFIX
+ sprintf(map_name_db, "%s.db", mapsrc);
+ if (rename(maptdb, map_name_db) < 0) {
+ fprintf(stderr, "Couldn't rename %s to ", maptdb);
+ perror(map_name_db);
+ /* Throw away the temporary map */
+ unlink(maptdb);
+ exit(1);
+ }
+#else /* not HAVE_DB_SUFFIX */
+ sprintf(map_name_pag, "%s.pag", mapsrc);
+ sprintf(map_name_dir, "%s.dir", mapsrc);
+ if (rename(maptpag, map_name_pag) < 0) {
+ fprintf(stderr, "Couldn't rename %s to ", maptpag);
+ perror(map_name_pag);
+ /* Throw away the temporary map */
+ unlink(maptpag);
+ unlink(maptdir);
+ exit(1);
}
- exit(rc);
+ if (rename(maptdir, map_name_dir) < 0) {
+ fprintf(stderr, "Couldn't rename %s to ", maptdir);
+ perror(map_name_dir);
+ /* remove the (presumably bad) .pag file */
+ unlink(map_name_pag);
+ /* throw away remaining part of original map */
+ unlink(map_name_dir);
+ /* throw away the temporary map */
+ unlink(maptdir);
+ fprintf(stderr, "WARNING: existing map \"%s.{dir,pag}\" destroyed\n",
+ mapsrc);
+ exit(1);
+ }
+#endif /* not HAVE_DB_SUFFIX */
+
+ exit(0);
}
#else /* not HAVE_MAP_NDBM */
+int
main()
{
fputs("mk-amd-map: This system does not support hashed database files\n", stderr);
OpenPOWER on IntegriCloud