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.2 (Berkeley) 02/14/91"; 16 #endif /* not lint */ 17 18 #include <sys/param.h> 19 #include <sys/stat.h> 20 #include <fcntl.h> 21 #include <dirent.h> 22 #include <ndbm.h> 23 #include <kvm.h> 24 #include <errno.h> 25 #include <stdio.h> 26 #include <paths.h> 27 #include <string.h> 28 29 main(argc, argv) 30 int argc; 31 char **argv; 32 { 33 extern int optind; 34 register DIR *dirp; 35 register struct dirent *dp; 36 struct stat sb; 37 DBM *db; 38 datum key, data; 39 int ch; 40 char buf[MAXNAMLEN + 1], dbtmp[MAXPATHLEN + 1], dbname[MAXPATHLEN + 1]; 41 42 while ((ch = getopt(argc, argv, "")) != EOF) 43 switch((char)ch) { 44 case '?': 45 default: 46 usage(); 47 } 48 argc -= optind; 49 argv += optind; 50 51 if (chdir(_PATH_DEV)) 52 error(_PATH_DEV); 53 54 dirp = opendir("."); 55 56 (void)sprintf(dbtmp, "%s/dev.tmp", _PATH_VARRUN); 57 (void)sprintf(dbname, "%s/dev.db", _PATH_VARRUN); 58 if ((db = dbm_open(dbtmp, O_CREAT|O_WRONLY|O_EXCL, 59 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) == NULL) 60 error(dbtmp); 61 62 key.dptr = (char *)&sb.st_rdev; 63 key.dsize = sizeof(sb.st_rdev); 64 data.dptr = buf; 65 while (dp = readdir(dirp)) { 66 if (stat(dp->d_name, &sb)) 67 error(dp->d_name); 68 if (!S_ISCHR(sb.st_mode)) 69 continue; 70 71 /* Nul terminate the name so ps doesn't have to. */ 72 bcopy(dp->d_name, buf, dp->d_namlen); 73 buf[dp->d_namlen] = '\0'; 74 data.dsize = dp->d_namlen + 1; 75 if (dbm_store(db, key, data, DBM_INSERT) < 0) 76 error("dbm_store"); 77 } 78 (void)dbm_close(db); 79 (void)strcat(dbtmp, DBM_SUFFIX); 80 if (rename(dbtmp, dbname)) { 81 (void)fprintf(stderr, "dev_mkdb: %s to %s: %s.\n", 82 dbtmp, dbname, strerror(errno)); 83 exit(1); 84 } 85 exit(0); 86 } 87 88 error(n) 89 char *n; 90 { 91 (void)fprintf(stderr, "kvm_mkdb: %s: %s\n", n, strerror(errno)); 92 exit(1); 93 } 94 95 usage() 96 { 97 (void)fprintf(stderr, "usage: dev_mkdb\n"); 98 exit(1); 99 } 100