cleanups from Criag Leres, and minor upgrades for cleanliness
authorKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Fri, 31 Jul 1992 07:30:31 +0000 (23:30 -0800)
committerKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Fri, 31 Jul 1992 07:30:31 +0000 (23:30 -0800)
SCCS-vsn: usr.sbin/dev_mkdb/dev_mkdb.c 5.11

usr/src/usr.sbin/dev_mkdb/dev_mkdb.c

index 250797b..7617155 100644 (file)
@@ -12,11 +12,12 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)dev_mkdb.c 5.10 (Berkeley) %G%";
+static char sccsid[] = "@(#)dev_mkdb.c 5.11 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/stat.h>
 #endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/stat.h>
+
 #include <fcntl.h>
 #undef DIRBLKSIZ
 #include <dirent.h>
 #include <fcntl.h>
 #undef DIRBLKSIZ
 #include <dirent.h>
@@ -30,13 +31,14 @@ static char sccsid[] = "@(#)dev_mkdb.c      5.10 (Berkeley) %G%";
 #include <stdlib.h>
 #include <string.h>
 
 #include <stdlib.h>
 #include <string.h>
 
-void error(), usage();
+void   err __P((const char *, ...));
+void   usage __P((void));
 
 
+int
 main(argc, argv)
        int argc;
 main(argc, argv)
        int argc;
-       char **argv;
+       char *argv[];
 {
 {
-       extern int optind;
        register DIR *dirp;
        register struct dirent *dp;
        struct stat sb;
        register DIR *dirp;
        register struct dirent *dp;
        struct stat sb;
@@ -59,16 +61,20 @@ main(argc, argv)
        argc -= optind;
        argv += optind;
 
        argc -= optind;
        argv += optind;
 
+       if (argc > 0)
+               usage();
+
        if (chdir(_PATH_DEV))
        if (chdir(_PATH_DEV))
-               error(_PATH_DEV);
+               err("%s: %s", _PATH_DEV, strerror(errno));
 
        dirp = opendir(".");
 
 
        dirp = opendir(".");
 
-       (void)snprintf(dbtmp, sizeof(dbtmp), "%s/dev.tmp", _PATH_VARRUN);
-       (void)snprintf(dbname, sizeof(dbtmp), "%s/dev.db", _PATH_VARRUN);
-       db = dbopen(dbtmp, O_CREAT|O_WRONLY|O_EXCL, DEFFILEMODE, DB_HASH, NULL);
-       if (!db)
-               error(dbtmp);
+       (void)snprintf(dbtmp, sizeof(dbtmp), "%sdev.tmp", _PATH_VARRUN);
+       (void)snprintf(dbname, sizeof(dbtmp), "%sdev.db", _PATH_VARRUN);
+       db = dbopen(dbtmp, O_CREAT|O_EXLOCK|O_TRUNC|O_WRONLY,
+           S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, DB_HASH, NULL);
+       if (db == NULL)
+               err("%s: %s", dbtmp, strerror(errno));
 
        /*
         * Keys are a mode_t followed by a dev_t.  The former is the type of
 
        /*
         * Keys are a mode_t followed by a dev_t.  The former is the type of
@@ -79,8 +85,8 @@ main(argc, argv)
        data.data = buf;
        while (dp = readdir(dirp)) {
                if (stat(dp->d_name, &sb)) {
        data.data = buf;
        while (dp = readdir(dirp)) {
                if (stat(dp->d_name, &sb)) {
-                       (void)fprintf(stderr, "dev_mkdb: can't stat %s\n",
-                               dp->d_name);
+                       (void)fprintf(stderr,
+                           "dev_mkdb: %s: %s\n", dp->d_name, strerror(errno));
                        continue;
                }
 
                        continue;
                }
 
@@ -101,28 +107,46 @@ main(argc, argv)
                buf[dp->d_namlen] = '\0';
                data.size = dp->d_namlen + 1;
                if ((db->put)(db, &key, &data, 0))
                buf[dp->d_namlen] = '\0';
                data.size = dp->d_namlen + 1;
                if ((db->put)(db, &key, &data, 0))
-                       error(dbtmp);
+                       err("dbput %s: %s\n", dbtmp, strerror(errno));
        }
        (void)(db->close)(db);
        }
        (void)(db->close)(db);
-       if (rename(dbtmp, dbname)) {
-               (void)fprintf(stderr, "dev_mkdb: %s to %s: %s.\n",
-                   dbtmp, dbname, strerror(errno));
-               exit(1);
-       }
+       if (rename(dbtmp, dbname))
+               err("rename %s to %s: %s", dbtmp, dbname, strerror(errno));
        exit(0);
 }
 
 void
        exit(0);
 }
 
 void
-error(n)
-       char *n;
+usage()
 {
 {
-       (void)fprintf(stderr, "dev_mkdb: %s: %s\n", n, strerror(errno));
+       (void)fprintf(stderr, "usage: dev_mkdb\n");
        exit(1);
 }
 
        exit(1);
 }
 
+#if __STDC__
+#include <stdarg.h>
+#else
+#include <varargs.h>
+#endif
+
 void
 void
-usage()
+#if __STDC__
+err(const char *fmt, ...)
+#else
+err(fmt, va_alist)
+       char *fmt;
+        va_dcl
+#endif
 {
 {
-       (void)fprintf(stderr, "usage: dev_mkdb\n");
+       va_list ap;
+#if __STDC__
+       va_start(ap, fmt);
+#else
+       va_start(ap);
+#endif
+       (void)fprintf(stderr, "dev_mkdb: ");
+       (void)vfprintf(stderr, fmt, ap);
+       va_end(ap);
+       (void)fprintf(stderr, "\n");
        exit(1);
        exit(1);
+       /* NOTREACHED */
 }
 }