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