diff options
Diffstat (limited to 'contrib/amd/mk-amd-map/mk-amd-map.c')
-rw-r--r-- | contrib/amd/mk-amd-map/mk-amd-map.c | 198 |
1 files changed, 74 insertions, 124 deletions
diff --git a/contrib/amd/mk-amd-map/mk-amd-map.c b/contrib/amd/mk-amd-map/mk-amd-map.c index 480629762fbea..1339485c552d9 100644 --- a/contrib/amd/mk-amd-map/mk-amd-map.c +++ b/contrib/amd/mk-amd-map/mk-amd-map.c @@ -17,7 +17,7 @@ * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgment: + * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors @@ -38,7 +38,7 @@ * * %W% (Berkeley) %G% * - * $Id: mk-amd-map.c,v 1.5 1998/11/14 03:13:32 obrien Exp $ + * $Id: mk-amd-map.c,v 1.3 1998/08/27 02:56:47 obrien Exp $ */ /* @@ -51,20 +51,11 @@ #include <am_defs.h> /* dummy variables */ -#if 0 -char *progname; +char hostname[MAXHOSTNAMELEN]; +int orig_umask, foreground, debug_flags; pid_t mypid; serv_state amd_state; -int foreground, orig_umask; -int debug_flags; -#endif -char hostname[MAXHOSTNAMELEN]; -/* (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 @@ -127,7 +118,7 @@ read_file(FILE *fp, char *map, voidp db) int line_no = 0; int errs = 0; - while (read_line(key_val, 2048, fp)) { + while (read_line(key_val, sizeof(key_val), fp)) { char *kp; char *cp; char *hash; @@ -221,18 +212,14 @@ remove_file(char *f) int main(int argc, char *argv[]) { - FILE *mapf; /* the input file to read from */ - int error; - char *mapsrc; - DBM *db = NULL; + FILE *mapf; + int mapfd = -1; + char *map; + int rc = 0; + DBM *mapd = NULL; static char maptmp[] = "dbmXXXXXX"; -#ifdef HAVE_DB_SUFFIX char maptdb[16]; - 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 */ + char *mapdb = (char *) NULL; int len; char *sl; int printit = 0; @@ -255,134 +242,97 @@ main(int argc, char *argv[]) fputs("Usage: mk-amd-map [-p] file-map\n", stderr); exit(1); } - mapsrc = argv[optind]; + map = argv[optind]; /* test if can get to the map directory */ - sl = strrchr(mapsrc, '/'); + sl = strrchr(map, '/'); if (sl) { *sl = '\0'; - if (chdir(mapsrc) < 0) { + if (chdir(map) < 0) { fputs("Can't chdir to ", stderr); - perror(mapsrc); + perror(map); exit(1); } - mapsrc = sl + 1; - } - - /* open source file */ - mapf = fopen(mapsrc, "r"); - if (!mapf) { - fprintf(stderr, "cannot open source file "); - perror(mapsrc); - exit(1); + map = sl + 1; } -#ifndef DEBUG - signal(SIGINT, SIG_IGN); -#endif /* DEBUG */ - if (!printit) { - 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) { + len = strlen(map); + mapdb = (char *) malloc(len + 4); + if (!mapdb) { 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 */ - mktemp(maptmp); - - /* remove existing temps (if any) */ -#ifdef HAVE_DB_SUFFIX + /* open DBM files */ sprintf(maptdb, "%s.db", maptmp); if (remove_file(maptdb) < 0) { - fprintf(stderr, "Can't remove existing temporary file; "); + fprintf(stderr, "Can't remove existing temporary files;"); 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; - /* 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 (printit) - exit(0); /* nothing more to do */ - - /* if gets here, we wrote to a database */ +#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 { - dbm_close(db); - /* all went well */ + 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; + } + } + } -#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); + } else { + fprintf(stderr, "Can't open \"%s.db\" for ", map); + perror("writing"); + rc = 1; } - 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); + exit(rc); } #else /* not HAVE_MAP_NDBM */ -int main() { fputs("mk-amd-map: This system does not support hashed database files\n", stderr); |