xref: /netbsd-src/usr.sbin/dev_mkdb/dev_mkdb.c (revision ae1bfcddc410612bc8c58b807e1830becb69a24c)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1990 The Regents of the University of California.\n\
37  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)dev_mkdb.c	5.9 (Berkeley) 5/17/91";*/
42 static char rcsid[] = "$Id: dev_mkdb.c,v 1.4 1993/10/22 10:38:33 mycroft Exp $";
43 #endif /* not lint */
44 
45 #include <sys/param.h>
46 #include <sys/stat.h>
47 #include <fcntl.h>
48 #undef DIRBLKSIZ
49 #include <dirent.h>
50 #include <nlist.h>
51 #include <kvm.h>
52 #include <db.h>
53 #include <errno.h>
54 #include <unistd.h>
55 #include <stdio.h>
56 #include <paths.h>
57 #include <stdlib.h>
58 #include <string.h>
59 
60 void error(), usage();
61 
62 main(argc, argv)
63 	int argc;
64 	char **argv;
65 {
66 	extern int optind;
67 	register DIR *dirp;
68 	register struct dirent *dp;
69 	struct stat sb;
70 	struct {
71 		mode_t type;
72 		dev_t dev;
73 	} bkey;
74 	DB *db;
75 	DBT data, key;
76 	int ch;
77 	u_char buf[MAXNAMLEN + 1];
78 	char dbtmp[MAXPATHLEN + 1], dbname[MAXPATHLEN + 1];
79 
80 	while ((ch = getopt(argc, argv, "")) != EOF)
81 		switch((char)ch) {
82 		case '?':
83 		default:
84 			usage();
85 		}
86 	argc -= optind;
87 	argv += optind;
88 
89 	if (chdir(_PATH_DEV))
90 		error(_PATH_DEV);
91 
92 	dirp = opendir(".");
93 
94 	(void)snprintf(dbtmp, sizeof(dbtmp), "%s/dev.tmp", _PATH_VARRUN);
95 	(void)snprintf(dbname, sizeof(dbtmp), "%s/dev.db", _PATH_VARRUN);
96 	db = dbopen(dbtmp, O_CREAT|O_RDWR|O_EXCL, DEFFILEMODE, DB_HASH,
97 	    (HASHINFO *)NULL);
98 	if (!db)
99 		error(dbtmp);
100 
101 	/*
102 	 * Keys are a mode_t followed by a dev_t.  The former is the type of
103 	 * the file (mode & S_IFMT), the latter is the st_rdev field.
104 	 */
105 	key.data = &bkey;
106 	key.size = sizeof(bkey);
107 	data.data = buf;
108 	while (dp = readdir(dirp)) {
109 		if (lstat(dp->d_name, &sb)) {
110 			(void)fprintf(stderr, "dev_mkdb: can't stat %s\n",
111 				dp->d_name);
112 			continue;
113 		}
114 
115 		/* Create the key. */
116 		if (S_ISCHR(sb.st_mode))
117 			bkey.type = S_IFCHR;
118 		else if (S_ISBLK(sb.st_mode))
119 			bkey.type = S_IFBLK;
120 		else
121 			continue;
122 		bkey.dev = sb.st_rdev;
123 
124 		/*
125 		 * Create the data; nul terminate the name so caller doesn't
126 		 * have to.
127 		 */
128 		bcopy(dp->d_name, buf, dp->d_namlen);
129 		buf[dp->d_namlen] = '\0';
130 		data.size = dp->d_namlen + 1;
131 		if ((db->put)(db, &key, &data, 0))
132 			error(dbtmp);
133 	}
134 	(void)(db->close)(db);
135 	if (rename(dbtmp, dbname)) {
136 		(void)fprintf(stderr, "dev_mkdb: %s to %s: %s.\n",
137 		    dbtmp, dbname, strerror(errno));
138 		exit(1);
139 	}
140 	exit(0);
141 }
142 
143 void
144 error(n)
145 	char *n;
146 {
147 	(void)fprintf(stderr, "dev_mkdb: %s: %s\n", n, strerror(errno));
148 	exit(1);
149 }
150 
151 void
152 usage()
153 {
154 	(void)fprintf(stderr, "usage: dev_mkdb\n");
155 	exit(1);
156 }
157