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.9 (Berkeley) 03/02/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 <ndbm.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 	DBM *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 	(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 	if ((db = dbm_open(dbtemp, O_CREAT|O_WRONLY|O_EXCL,
55 	    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) == NULL) {
56 		(void)fprintf(stderr,
57 		    "kvm_mkdb: %s: %s\n", dbtemp, strerror(errno));
58 		exit(1);
59 	}
60 	create_knlist(nlistpath, db);
61 	(void)dbm_close(db);
62 	(void)strcat(dbtemp, DBM_SUFFIX);
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