make sleep spin-wait during autoconfiguration
[unix-history] / usr / src / old / mkhosts / mkhosts.c
index d406f60..921586d 100644 (file)
@@ -1,6 +1,18 @@
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1980 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif not lint
+
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)mkhosts.c   4.3 (Berkeley) 84/05/17";
-#endif
+static char sccsid[] = "@(#)mkhosts.c  5.1 (Berkeley) %G%";
+#endif not lint
 
 #include <sys/file.h>
 #include <stdio.h>
 
 #include <sys/file.h>
 #include <stdio.h>
@@ -16,8 +28,10 @@ main(argc, argv)
        register struct hostent *hp;
        datum key, content;
        register char *cp, *tp, **sp;
        register struct hostent *hp;
        datum key, content;
        register char *cp, *tp, **sp;
-       register int naliases, *nap;
-       int verbose = 0, entries = 0, maxlen = 0;
+       register int *nap;
+       int naliases;
+       int verbose = 0, entries = 0, maxlen = 0, error = 0;
+       char tempname[BUFSIZ], newname[BUFSIZ];
 
        if (argc > 1 && strcmp(argv[1], "-v") == 0) {
                verbose++;
 
        if (argc > 1 && strcmp(argv[1], "-v") == 0) {
                verbose++;
@@ -32,13 +46,14 @@ main(argc, argv)
                exit(1);
        }
        umask(0);
                exit(1);
        }
        umask(0);
-       dp = ndbmopen(argv[1], O_WRONLY|O_CREAT|O_EXCL, 0644);
+
+       sprintf(tempname, "%s.new", argv[1]);
+       dp = dbm_open(tempname, O_WRONLY|O_CREAT|O_EXCL, 0644);
        if (dp == NULL) {
        if (dp == NULL) {
-               fprintf(stderr, "dbminit failed: ");
+               fprintf(stderr, "dbm_open failed: ");
                perror(argv[1]);
                exit(1);
        }
                perror(argv[1]);
                exit(1);
        }
-       dp->db_maxbno = 0;
        sethostfile(argv[1]);
        sethostent(1);
        while (hp = gethostent()) {
        sethostfile(argv[1]);
        sethostent(1);
        while (hp = gethostent()) {
@@ -55,7 +70,7 @@ main(argc, argv)
                                ;
                        naliases++;
                }
                                ;
                        naliases++;
                }
-               *nap = naliases;
+               bcopy((char *)&naliases, (char *)nap, sizeof(int));
                bcopy((char *)&hp->h_addrtype, cp, sizeof (int));
                cp += sizeof (int);
                bcopy((char *)&hp->h_length, cp, sizeof (int));
                bcopy((char *)&hp->h_addrtype, cp, sizeof (int));
                cp += sizeof (int);
                bcopy((char *)&hp->h_length, cp, sizeof (int));
@@ -68,20 +83,49 @@ main(argc, argv)
                        printf("store %s, %d aliases\n", hp->h_name, naliases);
                key.dptr = hp->h_name;
                key.dsize = strlen(hp->h_name);
                        printf("store %s, %d aliases\n", hp->h_name, naliases);
                key.dptr = hp->h_name;
                key.dsize = strlen(hp->h_name);
-               dbmstore(dp, key, content, DB_INSERT);
+               if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
+                       perror(hp->h_name);
+                       goto err;
+               }
                for (sp = hp->h_aliases; *sp; sp++) {
                        key.dptr = *sp;
                        key.dsize = strlen(*sp);
                for (sp = hp->h_aliases; *sp; sp++) {
                        key.dptr = *sp;
                        key.dsize = strlen(*sp);
-                       dbmstore(dp, key, content, DB_INSERT);
+                       if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
+                               perror(*sp);
+                               goto err;
+                       }
                }
                key.dptr = hp->h_addr;
                key.dsize = hp->h_length;
                }
                key.dptr = hp->h_addr;
                key.dsize = hp->h_length;
-               dbmstore(dp, key, content, DB_INSERT);
+               if (dbm_store(dp, key, content, DBM_INSERT) < 0) {
+                       perror("dbm_store host address");
+                       goto err;
+               }
                entries++;
                if (cp - buf > maxlen)
                        maxlen = cp - buf;
        }
        endhostent();
                entries++;
                if (cp - buf > maxlen)
                        maxlen = cp - buf;
        }
        endhostent();
+       dbm_close(dp);
+
+       sprintf(tempname, "%s.new.pag", argv[1]);
+       sprintf(newname, "%s.pag", argv[1]);
+       if (rename(tempname, newname) < 0) {
+               perror("rename .pag");
+               exit(1);
+       }
+       sprintf(tempname, "%s.new.dir", argv[1]);
+       sprintf(newname, "%s.dir", argv[1]);
+       if (rename(tempname, newname) < 0) {
+               perror("rename .dir");
+               exit(1);
+       }
        printf("%d host entries, maximum length %d\n", entries, maxlen);
        exit(0);
        printf("%d host entries, maximum length %d\n", entries, maxlen);
        exit(0);
+err:
+       sprintf(tempname, "%s.new.pag", argv[1]);
+       unlink(tempname);
+       sprintf(tempname, "%s.new.dir", argv[1]);
+       unlink(tempname);
+       exit(1);
 }
 }