142800Sbostic /*-
242800Sbostic  * Copyright (c) 1990 The Regents of the University of California.
340358Smarc  * All rights reserved.
440358Smarc  *
542800Sbostic  * %sccs.include.redist.c%
640358Smarc  */
740358Smarc 
840358Smarc #ifndef lint
940358Smarc char copyright[] =
1042800Sbostic "@(#) Copyright (c) 1990 The Regents of the University of California.\n\
1140358Smarc  All rights reserved.\n";
1240358Smarc #endif /* not lint */
1340358Smarc 
1440358Smarc #ifndef lint
15*53326Sbostic static char sccsid[] = "@(#)kvm_mkdb.c	5.15 (Berkeley) 05/04/92";
1640358Smarc #endif /* not lint */
1742800Sbostic 
1840358Smarc #include <sys/param.h>
1946395Sbostic #include <sys/stat.h>
2046395Sbostic #include <fcntl.h>
2146947Sbostic #include <db.h>
2240358Smarc #include <errno.h>
2346395Sbostic #include <stdio.h>
24*53326Sbostic #include <stdlib.h>
2542064Sbostic #include <string.h>
2646395Sbostic #include <paths.h>
2740358Smarc 
28*53326Sbostic #include "extern.h"
2940358Smarc 
30*53326Sbostic static void usage __P(());
31*53326Sbostic 
32*53326Sbostic int
3340358Smarc main(argc, argv)
3446395Sbostic 	int argc;
3546395Sbostic 	char **argv;
3640358Smarc {
3746947Sbostic 	DB *db;
3840358Smarc 	int ch;
39*53326Sbostic 	char *p, *nlistpath, *nlistname, dbtemp[MAXPATHLEN], dbname[MAXPATHLEN];
4040358Smarc 
4140358Smarc 	while ((ch = getopt(argc, argv, "")) != EOF)
4240358Smarc 		switch((char)ch) {
4340358Smarc 		case '?':
4440358Smarc 		default:
4546395Sbostic 			usage();
4640358Smarc 		}
4740358Smarc 	argc -= optind;
4840358Smarc 	argv += optind;
4940358Smarc 
5053261Sbostic 	if (argc > 1)
5153261Sbostic 		usage();
5253261Sbostic 
53*53326Sbostic 	/* If the existing db file matches the currently running kernel, exit */
54*53326Sbostic 	if (testdb())
55*53326Sbostic 		exit(0);
56*53326Sbostic 
57*53326Sbostic #define	basename(cp)	((p = rindex((cp), '/')) != NULL ? p + 1 : (cp))
5840358Smarc 	nlistpath = argc > 1 ? argv[0] : _PATH_UNIX;
5940358Smarc 	nlistname = basename(nlistpath);
6046947Sbostic 
61*53326Sbostic 	(void)snprintf(dbtemp, sizeof(dbtemp), "%skvm_%s.tmp",
62*53326Sbostic 	    _PATH_VARDB, nlistname);
63*53326Sbostic 	(void)snprintf(dbname, sizeof(dbname), "%skvm_%s.db",
64*53326Sbostic 	    _PATH_VARDB, nlistname);
6546395Sbostic 	(void)umask(0);
66*53326Sbostic 	db = dbopen(dbtemp, O_CREAT|O_EXLOCK|O_TRUNC|O_WRONLY,
6751168Sbostic 	    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, DB_HASH, NULL);
6846947Sbostic 	if (!db) {
6946395Sbostic 		(void)fprintf(stderr,
7046395Sbostic 		    "kvm_mkdb: %s: %s\n", dbtemp, strerror(errno));
7146395Sbostic 		exit(1);
7246395Sbostic 	}
7346395Sbostic 	create_knlist(nlistpath, db);
7446947Sbostic 	(void)(db->close)(db);
7546395Sbostic 	if (rename(dbtemp, dbname)) {
7646395Sbostic 		(void)fprintf(stderr, "kvm_mkdb: %s to %s: %s.\n",
7746395Sbostic 		    dbtemp, dbname, strerror(errno));
7846395Sbostic 		exit(1);
7946395Sbostic 	}
8040358Smarc 	exit(0);
8140358Smarc }
8240358Smarc 
83*53326Sbostic void
8446395Sbostic error(n)
8546395Sbostic 	char *n;
8640358Smarc {
8746395Sbostic 	int sverr;
8840358Smarc 
8946395Sbostic 	sverr = errno;
9046395Sbostic 	(void)fprintf(stderr, "kvm_mkdb: ");
9146395Sbostic 	if (n)
9246395Sbostic 		(void)fprintf(stderr, "%s: ", n);
9346395Sbostic 	(void)fprintf(stderr, "%s\n", strerror(sverr));
9440358Smarc 	exit(1);
9540358Smarc }
9640358Smarc 
97*53326Sbostic void
9846395Sbostic usage()
9940358Smarc {
10046395Sbostic 	(void)fprintf(stderr, "usage: kvm_mkdb [file]\n");
10140358Smarc 	exit(1);
10240358Smarc }
103