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.7 (Berkeley) 02/12/91";
16 #endif /* not lint */
17 
18 #include <sys/param.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <ndbm.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 	DBM *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 	(void)sprintf(dbtemp, "%s/kvm_%s.tmp", _PATH_VARRUN, nlistname);
51 	(void)sprintf(dbname, "%s/kvm_%s.db", _PATH_VARRUN, nlistname);
52 	(void)umask(0);
53 	if ((db = dbm_open(dbtemp, O_CREAT|O_WRONLY|O_EXCL,
54 	    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) == NULL) {
55 		(void)fprintf(stderr,
56 		    "kvm_mkdb: %s: %s\n", dbtemp, strerror(errno));
57 		exit(1);
58 	}
59 	create_knlist(nlistpath, db);
60 	(void)dbm_close(db);
61 	if (rename(dbtemp, dbname)) {
62 		(void)fprintf(stderr, "kvm_mkdb: %s to %s: %s.\n",
63 		    dbtemp, dbname, strerror(errno));
64 		exit(1);
65 	}
66 	exit(0);
67 }
68 
69 error(n)
70 	char *n;
71 {
72 	int sverr;
73 
74 	sverr = errno;
75 	(void)fprintf(stderr, "kvm_mkdb: ");
76 	if (n)
77 		(void)fprintf(stderr, "%s: ", n);
78 	(void)fprintf(stderr, "%s\n", strerror(sverr));
79 	exit(1);
80 }
81 
82 usage()
83 {
84 	(void)fprintf(stderr, "usage: kvm_mkdb [file]\n");
85 	exit(1);
86 }
87