xref: /original-bsd/usr.sbin/kvm_mkdb/kvm_mkdb.c (revision 6e33f6c8)
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.11 (Berkeley) 04/27/91";
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 	nlistpath = argc > 1 ? argv[0] : _PATH_UNIX;
49 	nlistname = basename(nlistpath);
50 
51 	(void)sprintf(dbtemp, "%s/kvm_%s.tmp", _PATH_VARRUN, nlistname);
52 	(void)sprintf(dbname, "%s/kvm_%s.db", _PATH_VARRUN, nlistname);
53 	(void)umask(0);
54 	db = hash_open(dbtemp, O_CREAT|O_WRONLY|O_EXCL,
55 	    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, NULL);
56 	if (!db) {
57 		(void)fprintf(stderr,
58 		    "kvm_mkdb: %s: %s\n", dbtemp, strerror(errno));
59 		exit(1);
60 	}
61 	create_knlist(nlistpath, db);
62 	(void)(db->close)(db);
63 	if (rename(dbtemp, dbname)) {
64 		(void)fprintf(stderr, "kvm_mkdb: %s to %s: %s.\n",
65 		    dbtemp, dbname, strerror(errno));
66 		exit(1);
67 	}
68 	exit(0);
69 }
70 
71 error(n)
72 	char *n;
73 {
74 	int sverr;
75 
76 	sverr = errno;
77 	(void)fprintf(stderr, "kvm_mkdb: ");
78 	if (n)
79 		(void)fprintf(stderr, "%s: ", n);
80 	(void)fprintf(stderr, "%s\n", strerror(sverr));
81 	exit(1);
82 }
83 
84 usage()
85 {
86 	(void)fprintf(stderr, "usage: kvm_mkdb [file]\n");
87 	exit(1);
88 }
89