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.10 (Berkeley) 09/17/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 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 = dbopen(dbtmp, O_CREAT|O_WRONLY|O_EXCL, DEFFILEMODE, DB_HASH, NULL); 70 if (!db) 71 error(dbtmp); 72 73 /* 74 * Keys are a mode_t followed by a dev_t. The former is the type of 75 * the file (mode & S_IFMT), the latter is the st_rdev field. 76 */ 77 key.data = &bkey; 78 key.size = sizeof(bkey); 79 data.data = buf; 80 while (dp = readdir(dirp)) { 81 if (stat(dp->d_name, &sb)) { 82 (void)fprintf(stderr, "dev_mkdb: can't stat %s\n", 83 dp->d_name); 84 continue; 85 } 86 87 /* Create the key. */ 88 if (S_ISCHR(sb.st_mode)) 89 bkey.type = S_IFCHR; 90 else if (S_ISBLK(sb.st_mode)) 91 bkey.type = S_IFBLK; 92 else 93 continue; 94 bkey.dev = sb.st_rdev; 95 96 /* 97 * Create the data; nul terminate the name so caller doesn't 98 * have to. 99 */ 100 bcopy(dp->d_name, buf, dp->d_namlen); 101 buf[dp->d_namlen] = '\0'; 102 data.size = dp->d_namlen + 1; 103 if ((db->put)(db, &key, &data, 0)) 104 error(dbtmp); 105 } 106 (void)(db->close)(db); 107 if (rename(dbtmp, dbname)) { 108 (void)fprintf(stderr, "dev_mkdb: %s to %s: %s.\n", 109 dbtmp, dbname, strerror(errno)); 110 exit(1); 111 } 112 exit(0); 113 } 114 115 void 116 error(n) 117 char *n; 118 { 119 (void)fprintf(stderr, "dev_mkdb: %s: %s\n", n, strerror(errno)); 120 exit(1); 121 } 122 123 void 124 usage() 125 { 126 (void)fprintf(stderr, "usage: dev_mkdb\n"); 127 exit(1); 128 } 129