This commit was manufactured by cvs2svn to create tag 'FreeBSD-release/1.0'.
[unix-history] / usr.sbin / dev_mkdb / dev_mkdb.c
CommitLineData
15637ed4
RG
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
35char 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
41static char sccsid[] = "@(#)dev_mkdb.c 5.9 (Berkeley) 5/17/91";
42#endif /* not lint */
43
44#include <sys/param.h>
45#include <sys/stat.h>
46#include <fcntl.h>
47#undef DIRBLKSIZ
48#include <dirent.h>
49#include <nlist.h>
50#include <kvm.h>
51#include <db.h>
52#include <errno.h>
53#include <unistd.h>
54#include <stdio.h>
55#include <paths.h>
56#include <stdlib.h>
57#include <string.h>
58
59void error(), usage();
60
61main(argc, argv)
62 int argc;
63 char **argv;
64{
65 extern int optind;
66 register DIR *dirp;
67 register struct dirent *dp;
68 struct stat sb;
69 struct {
70 mode_t type;
71 dev_t dev;
72 } bkey;
73 DB *db;
74 DBT data, key;
75 int ch;
76 u_char buf[MAXNAMLEN + 1];
77 char dbtmp[MAXPATHLEN + 1], dbname[MAXPATHLEN + 1];
78
79 while ((ch = getopt(argc, argv, "")) != EOF)
80 switch((char)ch) {
81 case '?':
82 default:
83 usage();
84 }
85 argc -= optind;
86 argv += optind;
87
88 if (chdir(_PATH_DEV))
89 error(_PATH_DEV);
90
91 dirp = opendir(".");
92
93 (void)snprintf(dbtmp, sizeof(dbtmp), "%s/dev.tmp", _PATH_VARRUN);
94 (void)snprintf(dbname, sizeof(dbtmp), "%s/dev.db", _PATH_VARRUN);
78ed81a3 95 db = dbopen(dbtmp, O_CREAT|O_RDWR|O_EXCL, DEFFILEMODE, DB_HASH,
15637ed4
RG
96 (HASHINFO *)NULL);
97 if (!db)
98 error(dbtmp);
99
100 /*
101 * Keys are a mode_t followed by a dev_t. The former is the type of
102 * the file (mode & S_IFMT), the latter is the st_rdev field.
103 */
104 key.data = &bkey;
105 key.size = sizeof(bkey);
106 data.data = buf;
107 while (dp = readdir(dirp)) {
108 if (stat(dp->d_name, &sb)) {
109 (void)fprintf(stderr, "dev_mkdb: can't stat %s\n",
110 dp->d_name);
111 continue;
112 }
113
114 /* Create the key. */
115 if (S_ISCHR(sb.st_mode))
116 bkey.type = S_IFCHR;
117 else if (S_ISBLK(sb.st_mode))
118 bkey.type = S_IFBLK;
119 else
120 continue;
121 bkey.dev = sb.st_rdev;
122
123 /*
124 * Create the data; nul terminate the name so caller doesn't
125 * have to.
126 */
127 bcopy(dp->d_name, buf, dp->d_namlen);
128 buf[dp->d_namlen] = '\0';
129 data.size = dp->d_namlen + 1;
130 if ((db->put)(db, &key, &data, 0))
131 error(dbtmp);
132 }
133 (void)(db->close)(db);
134 if (rename(dbtmp, dbname)) {
135 (void)fprintf(stderr, "dev_mkdb: %s to %s: %s.\n",
136 dbtmp, dbname, strerror(errno));
137 exit(1);
138 }
139 exit(0);
140}
141
142void
143error(n)
144 char *n;
145{
146 (void)fprintf(stderr, "dev_mkdb: %s: %s\n", n, strerror(errno));
147 exit(1);
148}
149
150void
151usage()
152{
153 (void)fprintf(stderr, "usage: dev_mkdb\n");
154 exit(1);
155}