xref: /original-bsd/usr.sbin/dev_mkdb/dev_mkdb.c (revision df23cbe6)
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.12 (Berkeley) 11/02/92";
16 #endif /* not lint */
17 
18 #include <sys/param.h>
19 #include <sys/stat.h>
20 
21 #include <fcntl.h>
22 #undef DIRBLKSIZ
23 #include <dirent.h>
24 #include <nlist.h>
25 #include <kvm.h>
26 #include <db.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <paths.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 void	err __P((const char *, ...));
35 void	usage __P((void));
36 
37 int
38 main(argc, argv)
39 	int argc;
40 	char *argv[];
41 {
42 	register DIR *dirp;
43 	register struct dirent *dp;
44 	struct stat sb;
45 	struct {
46 		mode_t type;
47 		dev_t dev;
48 	} bkey;
49 	DB *db;
50 	DBT data, key;
51 	int ch;
52 	u_char buf[MAXNAMLEN + 1];
53 	char dbtmp[MAXPATHLEN + 1], dbname[MAXPATHLEN + 1];
54 
55 	while ((ch = getopt(argc, argv, "")) != EOF)
56 		switch((char)ch) {
57 		case '?':
58 		default:
59 			usage();
60 		}
61 	argc -= optind;
62 	argv += optind;
63 
64 	if (argc > 0)
65 		usage();
66 
67 	if (chdir(_PATH_DEV))
68 		err("%s: %s", _PATH_DEV, strerror(errno));
69 
70 	dirp = opendir(".");
71 
72 	(void)snprintf(dbtmp, sizeof(dbtmp), "%sdev.tmp", _PATH_VARRUN);
73 	(void)snprintf(dbname, sizeof(dbtmp), "%sdev.db", _PATH_VARRUN);
74 	db = dbopen(dbtmp, O_CREAT|O_EXLOCK|O_RDWR|O_TRUNC,
75 	    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, DB_HASH, NULL);
76 	if (db == NULL)
77 		err("%s: %s", dbtmp, strerror(errno));
78 
79 	/*
80 	 * Keys are a mode_t followed by a dev_t.  The former is the type of
81 	 * the file (mode & S_IFMT), the latter is the st_rdev field.
82 	 */
83 	key.data = &bkey;
84 	key.size = sizeof(bkey);
85 	data.data = buf;
86 	while (dp = readdir(dirp)) {
87 		if (stat(dp->d_name, &sb)) {
88 			(void)fprintf(stderr,
89 			    "dev_mkdb: %s: %s\n", dp->d_name, strerror(errno));
90 			continue;
91 		}
92 
93 		/* Create the key. */
94 		if (S_ISCHR(sb.st_mode))
95 			bkey.type = S_IFCHR;
96 		else if (S_ISBLK(sb.st_mode))
97 			bkey.type = S_IFBLK;
98 		else
99 			continue;
100 		bkey.dev = sb.st_rdev;
101 
102 		/*
103 		 * Create the data; nul terminate the name so caller doesn't
104 		 * have to.
105 		 */
106 		bcopy(dp->d_name, buf, dp->d_namlen);
107 		buf[dp->d_namlen] = '\0';
108 		data.size = dp->d_namlen + 1;
109 		if ((db->put)(db, &key, &data, 0))
110 			err("dbput %s: %s\n", dbtmp, strerror(errno));
111 	}
112 	(void)(db->close)(db);
113 	if (rename(dbtmp, dbname))
114 		err("rename %s to %s: %s", dbtmp, dbname, strerror(errno));
115 	exit(0);
116 }
117 
118 void
119 usage()
120 {
121 	(void)fprintf(stderr, "usage: dev_mkdb\n");
122 	exit(1);
123 }
124 
125 #if __STDC__
126 #include <stdarg.h>
127 #else
128 #include <varargs.h>
129 #endif
130 
131 void
132 #if __STDC__
133 err(const char *fmt, ...)
134 #else
135 err(fmt, va_alist)
136 	char *fmt;
137         va_dcl
138 #endif
139 {
140 	va_list ap;
141 #if __STDC__
142 	va_start(ap, fmt);
143 #else
144 	va_start(ap);
145 #endif
146 	(void)fprintf(stderr, "dev_mkdb: ");
147 	(void)vfprintf(stderr, fmt, ap);
148 	va_end(ap);
149 	(void)fprintf(stderr, "\n");
150 	exit(1);
151 	/* NOTREACHED */
152 }
153