BSD 4_3_Net_2 release
[unix-history] / usr / src / usr.sbin / amd / mk-amd-map / mk-amd-map.c
index 3ca4d3b..76fa5d2 100644 (file)
@@ -1,6 +1,4 @@
 /*
 /*
- * $Id: mk-amd-map.c,v 5.2 90/06/23 22:20:10 jsp Rel $
- *
  * Copyright (c) 1990 Jan-Simon Pendry
  * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
  * Copyright (c) 1990 The Regents of the University of California.
  * Copyright (c) 1990 Jan-Simon Pendry
  * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
  * Copyright (c) 1990 The Regents of the University of California.
@@ -9,21 +7,37 @@
  * This code is derived from software contributed to Berkeley by
  * Jan-Simon Pendry at Imperial College, London.
  *
  * This code is derived from software contributed to Berkeley by
  * Jan-Simon Pendry at Imperial College, London.
  *
- * Redistribution and use in source and binary forms are permitted provided
- * that: (1) source distributions retain this entire copyright notice and
- * comment, and (2) distributions including binaries display the following
- * acknowledgement:  ``This product includes software developed by the
- * University of California, Berkeley and its contributors'' in the
- * documentation or other materials provided with the distribution and in
- * all advertising materials mentioning features or use of this software.
- * Neither the name of the University nor the names of its contributors may
- * be used to endorse or promote products derived from this software without
- * specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    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 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
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *     @(#)mk-amd-map.c        5.4 (Berkeley) 5/12/91
  *
  *
- *     @(#)mk-amd-map.c        5.1 (Berkeley) 7/19/90
+ * $Id: mk-amd-map.c,v 5.2.1.4 91/05/07 22:18:47 jsp Alpha $
  */
 
 /*
  */
 
 /*
@@ -39,21 +53,18 @@ char copyright[] = "\
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char rcsid[] = "$Id: mk-amd-map.c,v 5.2 90/06/23 22:20:10 jsp Rel $";
-static char sccsid[] = "@(#)mk-amd-map.c       5.1 (Berkeley) 7/19/90";
+static char rcsid[] = "$Id: mk-amd-map.c,v 5.2.1.4 91/05/07 22:18:47 jsp Alpha $";
+static char sccsid[] = "@(#)mk-amd-map.c       5.4 (Berkeley) 5/12/91";
 #endif /* not lint */
 
 #include "am.h"
 
 #endif /* not lint */
 
 #include "am.h"
 
-#ifdef OS_HAS_GDBM
-#define HAS_DATABASE
-#include "gdbm.h"
-#endif /* OS_HAS_GDBM */
+#ifndef SIGINT
+#include <signal.h>
+#endif
 
 
-#ifndef HAS_DATABASE
 #ifdef OS_HAS_NDBM
 #define HAS_DATABASE
 #ifdef OS_HAS_NDBM
 #define HAS_DATABASE
-#define        USE_NDBM
 #include <ndbm.h>
 
 #define create_database(name) dbm_open(name, O_RDWR|O_CREAT, 0444)
 #include <ndbm.h>
 
 #define create_database(name) dbm_open(name, O_RDWR|O_CREAT, 0444)
@@ -71,7 +82,6 @@ char *k, *v;
 }
 
 #endif /* OS_HAS_NDBM */
 }
 
 #endif /* OS_HAS_NDBM */
-#endif /* !OS_HAS_DATABASE */
 
 #ifdef HAS_DATABASE
 #include <fcntl.h>
 
 #ifdef HAS_DATABASE
 #include <fcntl.h>
@@ -93,6 +103,7 @@ FILE *fp;
                                int ch;
                                buf += len - 2;
                                size -= len - 2;
                                int ch;
                                buf += len - 2;
                                size -= len - 2;
+                               *buf = '\n'; buf[1] = '\0';
                                /*
                                 * Skip leading white space on next line
                                 */
                                /*
                                 * Skip leading white space on next line
                                 */
@@ -176,12 +187,13 @@ voidp db;
                        fprintf(stderr, "Can't interpolate %s\n", kp);
                        errs++;
                } else if (*cp) {
                        fprintf(stderr, "Can't interpolate %s\n", kp);
                        errs++;
                } else if (*cp) {
-#ifdef DEBUG
-                       printf("%s\t%s\n", kp, cp);
-#endif /* DEBUG */
-                       if (store_data(db, kp, cp) < 0) {
-                               fprintf(stderr, "Could store %s -> %s\n", kp, cp);
-                               errs++;
+                       if (db) {
+                               if (store_data(db, kp, cp) < 0) {
+                                       fprintf(stderr, "Could store %s -> %s\n", kp, cp);
+                                       errs++;
+                               }
+                       } else {
+                               printf("%s\t%s\n", kp, cp);
                        }
                } else {
                        fprintf(stderr, "%s: line %d has no value field", map, line_no);
                        }
                } else {
                        fprintf(stderr, "%s: line %d has no value field", map, line_no);
@@ -203,7 +215,7 @@ again:
        return errs;
 }
 
        return errs;
 }
 
-static int remove(f)
+static int remove_file(f)
 char *f;
 {
        if (unlink(f) < 0 && errno != ENOENT)
 char *f;
 {
        if (unlink(f) < 0 && errno != ENOENT)
@@ -219,18 +231,32 @@ char *argv[];
        char *map;
        int rc = 0;
        DBM *mapd;
        char *map;
        int rc = 0;
        DBM *mapd;
-       char *maptmp = "dbmXXXXXX";
+       static char maptmp[] = "dbmXXXXXX";
        char maptpag[16], maptdir[16];
        char *mappag, *mapdir;
        int len;
        char *sl;
        char maptpag[16], maptdir[16];
        char *mappag, *mapdir;
        int len;
        char *sl;
+       int printit = 0;
+       int usage = 0;
+       int ch;
+       extern int optind;
+
+       while ((ch = getopt(argc, argv, "p")) != EOF)
+       switch (ch) {
+       case 'p':
+               printit = 1;
+               break;
+       default:
+               usage++;
+               break;
+       }
 
 
-       if (argc != 2) {
-               fputs("Usage: mk-amd-map file-map\n", stderr);
+       if (usage || optind != (argc - 1)) {
+               fputs("Usage: mk-amd-map [-p] file-map\n", stderr);
                exit(1);
        }
 
                exit(1);
        }
 
-       map = argv[1];
+       map = argv[optind];
        sl = strrchr(map, '/');
        if (sl) {
                *sl = '\0';
        sl = strrchr(map, '/');
        if (sl) {
                *sl = '\0';
@@ -241,63 +267,73 @@ char *argv[];
                }
                map = sl + 1;
        }
                }
                map = sl + 1;
        }
-#ifdef USE_NDBM
-       len = strlen(map);
-       mappag = (char *) malloc(len + 5);
-       mapdir = (char *) malloc(len + 5);
-       if (!mappag || !mapdir) {
-               perror("malloc");
-               exit(1);
-       }
-       mktemp(maptmp);
-       sprintf(maptpag, "%s.pag", maptmp);
-       sprintf(maptdir, "%s.dir", maptmp);
-       if (remove(maptpag) < 0 || remove(maptdir) < 0) {
-               fprintf(stderr, "Can't remove existing temporary files; %s and", maptpag);
-               perror(maptdir);
-               exit(1);
+
+       if (!printit) {
+               len = strlen(map);
+               mappag = (char *) malloc(len + 5);
+               mapdir = (char *) malloc(len + 5);
+               if (!mappag || !mapdir) {
+                       perror("mk-amd-map: malloc");
+                       exit(1);
+               }
+               mktemp(maptmp);
+               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 /* USE_NDBM */
+
        mapf =  fopen(map, "r");
        mapf =  fopen(map, "r");
-       if (mapf)
+       if (mapf && !printit)
                mapd = create_database(maptmp);
        else
                mapd = 0;
                mapd = create_database(maptmp);
        else
                mapd = 0;
+
 #ifndef DEBUG
        signal(SIGINT, SIG_IGN);
 #ifndef DEBUG
        signal(SIGINT, SIG_IGN);
-#endif /* DEBUG */
-       if (mapd) {
+#endif
+
+       if (mapd || printit) {
                int error = read_file(mapf, map, mapd);
                (void) fclose(mapf);
                int error = read_file(mapf, map, mapd);
                (void) fclose(mapf);
-               if (error) {
-                       fprintf(stderr, "Error creating ndbm map for %s\n", map);
-                       rc = 1;
-               }
-#ifdef USE_NDBM
-               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);
-                       /* 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);
-                       rc = 1;
+               if (printit) {
+                       if (error) {
+                               fprintf(stderr, "Error creating ndbm map for %s\n", map);
+                               rc = 1;
+                       }
+               } else {
+                       if (error) {
+                               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);
+                                       /* 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);
+                                       rc = 1;
+                               }
+                       }
                }
                }
-#endif /* USE_NDBM */
        } else {
        } else {
-#ifdef USE_NDBM
                fprintf(stderr, "Can't open \"%s.{dir,pag}\" for ", map);
                fprintf(stderr, "Can't open \"%s.{dir,pag}\" for ", map);
-#endif /* USE_NDBM */
                perror("writing");
                rc = 1;
        }
                perror("writing");
                rc = 1;
        }
@@ -306,7 +342,7 @@ char *argv[];
 #else
 main()
 {
 #else
 main()
 {
-       fputs("This system does not support hashed database files\n", stderr);
-       exit(0);
+       fputs("mk-amd-map: This system does not support hashed database files\n", stderr);
+       exit(1);
 }
 #endif /* HAS_DATABASE */
 }
 #endif /* HAS_DATABASE */