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*46908Sbostic static char sccsid[] = "@(#)kvm_mkdb.c	5.9 (Berkeley) 03/02/91";
1640358Smarc #endif /* not lint */
1742800Sbostic 
1840358Smarc #include <sys/param.h>
1946395Sbostic #include <sys/stat.h>
20*46908Sbostic #include <sys/user.h>
2146395Sbostic #include <fcntl.h>
2240358Smarc #include <ndbm.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;
3640358Smarc 	DBM *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);
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);
5446395Sbostic 	if ((db = dbm_open(dbtemp, O_CREAT|O_WRONLY|O_EXCL,
5546395Sbostic 	    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) == NULL) {
5646395Sbostic 		(void)fprintf(stderr,
5746395Sbostic 		    "kvm_mkdb: %s: %s\n", dbtemp, strerror(errno));
5846395Sbostic 		exit(1);
5946395Sbostic 	}
6046395Sbostic 	create_knlist(nlistpath, db);
6146395Sbostic 	(void)dbm_close(db);
6246407Sbostic 	(void)strcat(dbtemp, DBM_SUFFIX);
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