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*51168Sbostic static char sccsid[] = "@(#)kvm_mkdb.c	5.12 (Berkeley) 09/23/91";
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>
2442064Sbostic #include <string.h>
2546395Sbostic #include <paths.h>
2640358Smarc 
2740358Smarc char *tmp;
2840358Smarc #define basename(cp)	((tmp=rindex((cp), '/')) ? tmp+1 : (cp))
2940358Smarc 
3040358Smarc main(argc, argv)
3146395Sbostic 	int argc;
3246395Sbostic 	char **argv;
3340358Smarc {
3446395Sbostic 	extern int optind;
3546947Sbostic 	DB *db;
3640358Smarc 	int ch;
3746395Sbostic 	char *nlistpath, *nlistname, dbtemp[MAXPATHLEN], dbname[MAXPATHLEN];
3840358Smarc 
3940358Smarc 	while ((ch = getopt(argc, argv, "")) != EOF)
4040358Smarc 		switch((char)ch) {
4140358Smarc 		case '?':
4240358Smarc 		default:
4346395Sbostic 			usage();
4440358Smarc 		}
4540358Smarc 	argc -= optind;
4640358Smarc 	argv += optind;
4740358Smarc 
4840358Smarc 	nlistpath = argc > 1 ? argv[0] : _PATH_UNIX;
4940358Smarc 	nlistname = basename(nlistpath);
5046947Sbostic 
5146395Sbostic 	(void)sprintf(dbtemp, "%s/kvm_%s.tmp", _PATH_VARRUN, nlistname);
5246395Sbostic 	(void)sprintf(dbname, "%s/kvm_%s.db", _PATH_VARRUN, nlistname);
5346395Sbostic 	(void)umask(0);
54*51168Sbostic 	db = dbopen(dbtemp, O_CREAT|O_WRONLY|O_EXCL,
55*51168Sbostic 	    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, DB_HASH, NULL);
5646947Sbostic 	if (!db) {
5746395Sbostic 		(void)fprintf(stderr,
5846395Sbostic 		    "kvm_mkdb: %s: %s\n", dbtemp, strerror(errno));
5946395Sbostic 		exit(1);
6046395Sbostic 	}
6146395Sbostic 	create_knlist(nlistpath, db);
6246947Sbostic 	(void)(db->close)(db);
6346395Sbostic 	if (rename(dbtemp, dbname)) {
6446395Sbostic 		(void)fprintf(stderr, "kvm_mkdb: %s to %s: %s.\n",
6546395Sbostic 		    dbtemp, dbname, strerror(errno));
6646395Sbostic 		exit(1);
6746395Sbostic 	}
6840358Smarc 	exit(0);
6940358Smarc }
7040358Smarc 
7146395Sbostic error(n)
7246395Sbostic 	char *n;
7340358Smarc {
7446395Sbostic 	int sverr;
7540358Smarc 
7646395Sbostic 	sverr = errno;
7746395Sbostic 	(void)fprintf(stderr, "kvm_mkdb: ");
7846395Sbostic 	if (n)
7946395Sbostic 		(void)fprintf(stderr, "%s: ", n);
8046395Sbostic 	(void)fprintf(stderr, "%s\n", strerror(sverr));
8140358Smarc 	exit(1);
8240358Smarc }
8340358Smarc 
8446395Sbostic usage()
8540358Smarc {
8646395Sbostic 	(void)fprintf(stderr, "usage: kvm_mkdb [file]\n");
8740358Smarc 	exit(1);
8840358Smarc }
89