put block devices in database as we,lll
[unix-history] / usr / src / usr.sbin / dev_mkdb / dev_mkdb.c
index 7fa06e0..149f235 100644 (file)
@@ -12,17 +12,16 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)dev_mkdb.c 5.3 (Berkeley) %G%";
+static char sccsid[] = "@(#)dev_mkdb.c 5.6 (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 <sys/user.h>
 #include <fcntl.h>
 #include <fcntl.h>
-#include <ndbm.h>
 #undef DIRBLKSIZ
 #include <dirent.h>
 #include <kvm.h>
 #undef DIRBLKSIZ
 #include <dirent.h>
 #include <kvm.h>
+#include <db.h>
 #include <errno.h>
 #include <stdio.h>
 #include <paths.h>
 #include <errno.h>
 #include <stdio.h>
 #include <paths.h>
@@ -36,10 +35,12 @@ main(argc, argv)
        register DIR *dirp;
        register struct dirent *dp;
        struct stat sb;
        register DIR *dirp;
        register struct dirent *dp;
        struct stat sb;
-       DBM *db;
-       datum key, data;
+       char bkeybuf[sizeof(sb.st_rdev) + 1];
+       DB *db;
+       DBT data, key;
        int ch;
        int ch;
-       char buf[MAXNAMLEN + 1], dbtmp[MAXPATHLEN + 1], dbname[MAXPATHLEN + 1];
+       u_char buf[MAXNAMLEN + 1];
+       char dbtmp[MAXPATHLEN + 1], dbname[MAXPATHLEN + 1];
 
        while ((ch = getopt(argc, argv, "")) != EOF)
                switch((char)ch) {
 
        while ((ch = getopt(argc, argv, "")) != EOF)
                switch((char)ch) {
@@ -55,30 +56,43 @@ main(argc, argv)
 
        dirp = opendir(".");
 
 
        dirp = opendir(".");
 
-       (void)sprintf(dbtmp, "%s/dev.tmp", _PATH_VARRUN);
-       (void)sprintf(dbname, "%s/dev.db", _PATH_VARRUN);
-       if ((db = dbm_open(dbtmp, O_CREAT|O_WRONLY|O_EXCL,
-           S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) == NULL)
+       (void)snprintf(dbtmp, sizeof(dbtmp), "%s/dev.tmp", _PATH_VARRUN);
+       (void)snprintf(dbname, sizeof(dbtmp), "%s/dev.db", _PATH_VARRUN);
+       db = hash_open(dbtmp, O_CREAT|O_WRONLY|O_EXCL, DEFFILEMODE,
+           (HASHINFO *)NULL);
+       if (!db)
                error(dbtmp);
 
                error(dbtmp);
 
-       key.dptr = (char *)&sb.st_rdev;
-       key.dsize = sizeof(sb.st_rdev);
-       data.dptr = buf;
+       /*
+        * Character devices are stored using st_rdev as the key.
+        * Block devices are stores using st_rdev followed by exactly
+        * one NULL byte as the key.
+        */
+       key.data = bkeybuf;
+       bkeybuf[sizeof(sb.st_rdev)] = NULL;
+       data.data = buf;
        while (dp = readdir(dirp)) {
        while (dp = readdir(dirp)) {
-               if (stat(dp->d_name, &sb))
-                       error(dp->d_name);
-               if (!S_ISCHR(sb.st_mode))
+               if (stat(dp->d_name, &sb)) {
+                       (void)fprintf(stderr, "dev_mkdb: can't stat %s\n",
+                               dp->d_name);
                        continue;
                        continue;
-
-               /* Nul terminate the name so ps doesn't have to. */
+               }
+               if (S_ISCHR(sb.st_mode))
+                       key.size = sizeof(sb.st_rdev);
+               else if (S_ISBLK(sb.st_mode))
+                       key.size = sizeof(sb.st_rdev) + 1;
+               else
+                       continue;
+               /* 
+                * Nul terminate the name so caller doesn't have to. 
+                */
                bcopy(dp->d_name, buf, dp->d_namlen);
                buf[dp->d_namlen] = '\0';
                bcopy(dp->d_name, buf, dp->d_namlen);
                buf[dp->d_namlen] = '\0';
-               data.dsize = dp->d_namlen + 1;
-               if (dbm_store(db, key, data, DBM_INSERT) < 0)
-                       error("dbm_store");
+               data.size = dp->d_namlen + 1;
+               if ((db->put)(db, &key, &data, 0))
+                       error(dbtmp);
        }
        }
-       (void)dbm_close(db);
-       (void)strcat(dbtmp, DBM_SUFFIX);
+       (void)(db->close)(db);
        if (rename(dbtmp, dbname)) {
                (void)fprintf(stderr, "dev_mkdb: %s to %s: %s.\n",
                    dbtmp, dbname, strerror(errno));
        if (rename(dbtmp, dbname)) {
                (void)fprintf(stderr, "dev_mkdb: %s to %s: %s.\n",
                    dbtmp, dbname, strerror(errno));
@@ -90,7 +104,7 @@ main(argc, argv)
 error(n)
        char *n;
 {
 error(n)
        char *n;
 {
-       (void)fprintf(stderr, "kvm_mkdb: %s: %s\n", n, strerror(errno));
+       (void)fprintf(stderr, "dev_mkdb: %s: %s\n", n, strerror(errno));
        exit(1);
 }
 
        exit(1);
 }