142800Sbostic /*-
261836Sbostic * Copyright (c) 1990, 1993
361836Sbostic * The Regents of the University of California. All rights reserved.
440358Smarc *
542800Sbostic * %sccs.include.redist.c%
640358Smarc */
740358Smarc
840358Smarc #ifndef lint
961836Sbostic static char copyright[] =
1061836Sbostic "@(#) Copyright (c) 1990, 1993\n\
1161836Sbostic The Regents of the University of California. All rights reserved.\n";
1240358Smarc #endif /* not lint */
1340358Smarc
1440358Smarc #ifndef lint
15*69254Sbostic static char sccsid[] = "@(#)kvm_mkdb.c 8.3 (Berkeley) 05/04/95";
1640358Smarc #endif /* not lint */
1742800Sbostic
1840358Smarc #include <sys/param.h>
1946395Sbostic #include <sys/stat.h>
2059022Sbostic
2146947Sbostic #include <db.h>
2259022Sbostic #include <err.h>
2340358Smarc #include <errno.h>
2459022Sbostic #include <fcntl.h>
2559022Sbostic #include <paths.h>
2646395Sbostic #include <stdio.h>
2753326Sbostic #include <stdlib.h>
2842064Sbostic #include <string.h>
29*69254Sbostic #include <unistd.h>
3040358Smarc
3153326Sbostic #include "extern.h"
3240358Smarc
3359022Sbostic static void usage __P((void));
3453326Sbostic
3568949Sbostic HASHINFO openinfo = {
3668949Sbostic 4096, /* bsize */
3768949Sbostic 128, /* ffactor */
3868949Sbostic 1024, /* nelem */
3968949Sbostic 2048 * 1024, /* cachesize */
4068949Sbostic NULL, /* hash() */
4168949Sbostic 0 /* lorder */
4268949Sbostic };
4368949Sbostic
4453326Sbostic int
main(argc,argv)4540358Smarc main(argc, argv)
4646395Sbostic int argc;
4759022Sbostic char *argv[];
4840358Smarc {
4946947Sbostic DB *db;
5040358Smarc int ch;
5153326Sbostic char *p, *nlistpath, *nlistname, dbtemp[MAXPATHLEN], dbname[MAXPATHLEN];
5240358Smarc
5340358Smarc while ((ch = getopt(argc, argv, "")) != EOF)
5459022Sbostic switch (ch) {
5540358Smarc case '?':
5640358Smarc default:
5746395Sbostic usage();
5840358Smarc }
5940358Smarc argc -= optind;
6040358Smarc argv += optind;
6140358Smarc
6253261Sbostic if (argc > 1)
6353261Sbostic usage();
6453261Sbostic
6553326Sbostic /* If the existing db file matches the currently running kernel, exit */
6653326Sbostic if (testdb())
6753326Sbostic exit(0);
6853326Sbostic
6953326Sbostic #define basename(cp) ((p = rindex((cp), '/')) != NULL ? p + 1 : (cp))
7053802Sbostic nlistpath = argc > 0 ? argv[0] : _PATH_UNIX;
7140358Smarc nlistname = basename(nlistpath);
7246947Sbostic
7353326Sbostic (void)snprintf(dbtemp, sizeof(dbtemp), "%skvm_%s.tmp",
7453326Sbostic _PATH_VARDB, nlistname);
7553326Sbostic (void)snprintf(dbname, sizeof(dbname), "%skvm_%s.db",
7653326Sbostic _PATH_VARDB, nlistname);
7746395Sbostic (void)umask(0);
7859022Sbostic db = dbopen(dbtemp, O_CREAT | O_EXLOCK | O_TRUNC | O_RDWR,
7968949Sbostic S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, DB_HASH, &openinfo);
8059022Sbostic if (db == NULL)
8159022Sbostic err(1, "%s", dbtemp);
8246395Sbostic create_knlist(nlistpath, db);
8359022Sbostic if (db->close(db))
8459022Sbostic err(1, "%s", dbtemp);
8559022Sbostic if (rename(dbtemp, dbname))
8659022Sbostic err(1, "rename %s to %s", dbtemp, dbname);
8740358Smarc exit(0);
8840358Smarc }
8940358Smarc
9053326Sbostic void
usage()9146395Sbostic usage()
9240358Smarc {
9346395Sbostic (void)fprintf(stderr, "usage: kvm_mkdb [file]\n");
9440358Smarc exit(1);
9540358Smarc }
96