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[] = "@(#)kvm_mkdb.c	5.13 (Berkeley) 04/23/92";
16 #endif /* not lint */
17 
18 #include <sys/param.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <db.h>
22 #include <errno.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <paths.h>
26 
27 char *tmp;
28 #define basename(cp)	((tmp=rindex((cp), '/')) ? tmp+1 : (cp))
29 
30 main(argc, argv)
31 	int argc;
32 	char **argv;
33 {
34 	extern int optind;
35 	DB *db;
36 	int ch;
37 	char *nlistpath, *nlistname, dbtemp[MAXPATHLEN], dbname[MAXPATHLEN];
38 
39 	while ((ch = getopt(argc, argv, "")) != EOF)
40 		switch((char)ch) {
41 		case '?':
42 		default:
43 			usage();
44 		}
45 	argc -= optind;
46 	argv += optind;
47 
48 	if (argc > 1)
49 		usage();
50 
51 	nlistpath = argc > 1 ? argv[0] : _PATH_UNIX;
52 	nlistname = basename(nlistpath);
53 
54 	(void)sprintf(dbtemp, "%s/kvm_%s.tmp", _PATH_VARRUN, nlistname);
55 	(void)sprintf(dbname, "%s/kvm_%s.db", _PATH_VARRUN, nlistname);
56 	(void)umask(0);
57 	db = dbopen(dbtemp, O_CREAT|O_WRONLY|O_EXCL,
58 	    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, DB_HASH, NULL);
59 	if (!db) {
60 		(void)fprintf(stderr,
61 		    "kvm_mkdb: %s: %s\n", dbtemp, strerror(errno));
62 		exit(1);
63 	}
64 	create_knlist(nlistpath, db);
65 	(void)(db->close)(db);
66 	if (rename(dbtemp, dbname)) {
67 		(void)fprintf(stderr, "kvm_mkdb: %s to %s: %s.\n",
68 		    dbtemp, dbname, strerror(errno));
69 		exit(1);
70 	}
71 	exit(0);
72 }
73 
74 error(n)
75 	char *n;
76 {
77 	int sverr;
78 
79 	sverr = errno;
80 	(void)fprintf(stderr, "kvm_mkdb: ");
81 	if (n)
82 		(void)fprintf(stderr, "%s: ", n);
83 	(void)fprintf(stderr, "%s\n", strerror(sverr));
84 	exit(1);
85 }
86 
87 usage()
88 {
89 	(void)fprintf(stderr, "usage: kvm_mkdb [file]\n");
90 	exit(1);
91 }
92