1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 char copyright[] = 10 "@(#) Copyright (c) 1990 The Regents of the University of California.\n\ 11 All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)dev_mkdb.c 5.8 (Berkeley) 05/06/91"; 16 #endif /* not lint */ 17 18 #include <sys/param.h> 19 #include <sys/stat.h> 20 #include <fcntl.h> 21 #undef DIRBLKSIZ 22 #include <dirent.h> 23 #include <nlist.h> 24 #include <kvm.h> 25 #include <db.h> 26 #include <errno.h> 27 #include <unistd.h> 28 #include <stdio.h> 29 #include <paths.h> 30 #include <stdlib.h> 31 #include <string.h> 32 33 static void error(), usage(); 34 35 main(argc, argv) 36 int argc; 37 char **argv; 38 { 39 extern int optind; 40 register DIR *dirp; 41 register struct dirent *dp; 42 struct stat sb; 43 struct { 44 mode_t type; 45 dev_t dev; 46 } bkey; 47 DB *db; 48 DBT data, key; 49 int ch; 50 u_char buf[MAXNAMLEN + 1]; 51 char dbtmp[MAXPATHLEN + 1], dbname[MAXPATHLEN + 1]; 52 53 while ((ch = getopt(argc, argv, "")) != EOF) 54 switch((char)ch) { 55 case '?': 56 default: 57 usage(); 58 } 59 argc -= optind; 60 argv += optind; 61 62 if (chdir(_PATH_DEV)) 63 error(_PATH_DEV); 64 65 dirp = opendir("."); 66 67 (void)snprintf(dbtmp, sizeof(dbtmp), "%s/dev.tmp", _PATH_VARRUN); 68 (void)snprintf(dbname, sizeof(dbtmp), "%s/dev.db", _PATH_VARRUN); 69 db = hash_open(dbtmp, O_CREAT|O_WRONLY|O_EXCL, DEFFILEMODE, 70 (HASHINFO *)NULL); 71 if (!db) 72 error(dbtmp); 73 74 /* 75 * Keys are a mode_t followed by a dev_t. The former is the type of 76 * the file (mode & S_IFMT), the latter is the st_rdev field. 77 */ 78 key.data = &bkey; 79 key.size = sizeof(bkey); 80 data.data = buf; 81 while (dp = readdir(dirp)) { 82 if (stat(dp->d_name, &sb)) { 83 (void)fprintf(stderr, "dev_mkdb: can't stat %s\n", 84 dp->d_name); 85 continue; 86 } 87 88 /* Create the key. */ 89 if (S_ISCHR(sb.st_mode)) 90 bkey.type = S_IFCHR; 91 else if (S_ISBLK(sb.st_mode)) 92 bkey.type = S_IFBLK; 93 else 94 continue; 95 bkey.dev = sb.st_rdev; 96 97 /* 98 * Create the data; nul terminate the name so caller doesn't 99 * have to. 100 */ 101 bcopy(dp->d_name, buf, dp->d_namlen); 102 buf[dp->d_namlen] = '\0'; 103 data.size = dp->d_namlen + 1; 104 if ((db->put)(db, &key, &data, 0)) 105 error(dbtmp); 106 } 107 (void)(db->close)(db); 108 if (rename(dbtmp, dbname)) { 109 (void)fprintf(stderr, "dev_mkdb: %s to %s: %s.\n", 110 dbtmp, dbname, strerror(errno)); 111 exit(1); 112 } 113 exit(0); 114 } 115 116 void 117 error(n) 118 char *n; 119 { 120 (void)fprintf(stderr, "dev_mkdb: %s: %s\n", n, strerror(errno)); 121 exit(1); 122 } 123 124 void 125 usage() 126 { 127 (void)fprintf(stderr, "usage: dev_mkdb\n"); 128 exit(1); 129 } 130