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*46947Sbostic static char sccsid[] = "@(#)kvm_mkdb.c	5.10 (Berkeley) 03/03/91";
1640358Smarc #endif /* not lint */
1742800Sbostic 
1840358Smarc #include <sys/param.h>
1946395Sbostic #include <sys/stat.h>
2046908Sbostic #include <sys/user.h>
2146395Sbostic #include <fcntl.h>
22*46947Sbostic #include <db.h>
2340358Smarc #include <errno.h>
2446395Sbostic #include <stdio.h>
2542064Sbostic #include <string.h>
2646395Sbostic #include <paths.h>
2740358Smarc 
2840358Smarc char *tmp;
2940358Smarc #define basename(cp)	((tmp=rindex((cp), '/')) ? tmp+1 : (cp))
3040358Smarc 
3140358Smarc main(argc, argv)
3246395Sbostic 	int argc;
3346395Sbostic 	char **argv;
3440358Smarc {
3546395Sbostic 	extern int optind;
36*46947Sbostic 	DB *db;
3740358Smarc 	int ch;
3846395Sbostic 	char *nlistpath, *nlistname, dbtemp[MAXPATHLEN], dbname[MAXPATHLEN];
3940358Smarc 
4040358Smarc 	while ((ch = getopt(argc, argv, "")) != EOF)
4140358Smarc 		switch((char)ch) {
4240358Smarc 		case '?':
4340358Smarc 		default:
4446395Sbostic 			usage();
4540358Smarc 		}
4640358Smarc 	argc -= optind;
4740358Smarc 	argv += optind;
4840358Smarc 
4940358Smarc 	nlistpath = argc > 1 ? argv[0] : _PATH_UNIX;
5040358Smarc 	nlistname = basename(nlistpath);
51*46947Sbostic 
5246395Sbostic 	(void)sprintf(dbtemp, "%s/kvm_%s.tmp", _PATH_VARRUN, nlistname);
5346395Sbostic 	(void)sprintf(dbname, "%s/kvm_%s.db", _PATH_VARRUN, nlistname);
5446395Sbostic 	(void)umask(0);
55*46947Sbostic 	db = hash_open(dbtemp, O_CREAT|O_WRONLY|O_EXCL,
56*46947Sbostic 	    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, NULL);
57*46947Sbostic 	if (!db) {
5846395Sbostic 		(void)fprintf(stderr,
5946395Sbostic 		    "kvm_mkdb: %s: %s\n", dbtemp, strerror(errno));
6046395Sbostic 		exit(1);
6146395Sbostic 	}
6246395Sbostic 	create_knlist(nlistpath, db);
63*46947Sbostic 	(void)(db->close)(db);
6446395Sbostic 	if (rename(dbtemp, dbname)) {
6546395Sbostic 		(void)fprintf(stderr, "kvm_mkdb: %s to %s: %s.\n",
6646395Sbostic 		    dbtemp, dbname, strerror(errno));
6746395Sbostic 		exit(1);
6846395Sbostic 	}
6940358Smarc 	exit(0);
7040358Smarc }
7140358Smarc 
7246395Sbostic error(n)
7346395Sbostic 	char *n;
7440358Smarc {
7546395Sbostic 	int sverr;
7640358Smarc 
7746395Sbostic 	sverr = errno;
7846395Sbostic 	(void)fprintf(stderr, "kvm_mkdb: ");
7946395Sbostic 	if (n)
8046395Sbostic 		(void)fprintf(stderr, "%s: ", n);
8146395Sbostic 	(void)fprintf(stderr, "%s\n", strerror(sverr));
8240358Smarc 	exit(1);
8340358Smarc }
8440358Smarc 
8546395Sbostic usage()
8640358Smarc {
8746395Sbostic 	(void)fprintf(stderr, "usage: kvm_mkdb [file]\n");
8840358Smarc 	exit(1);
8940358Smarc }
90