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*46407Sbostic static char sccsid[] = "@(#)kvm_mkdb.c	5.8 (Berkeley) 02/14/91";
1640358Smarc #endif /* not lint */
1742800Sbostic 
1840358Smarc #include <sys/param.h>
1946395Sbostic #include <sys/stat.h>
2046395Sbostic #include <fcntl.h>
2140358Smarc #include <ndbm.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;
3540358Smarc 	DBM *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);
5046395Sbostic 	(void)sprintf(dbtemp, "%s/kvm_%s.tmp", _PATH_VARRUN, nlistname);
5146395Sbostic 	(void)sprintf(dbname, "%s/kvm_%s.db", _PATH_VARRUN, nlistname);
5246395Sbostic 	(void)umask(0);
5346395Sbostic 	if ((db = dbm_open(dbtemp, O_CREAT|O_WRONLY|O_EXCL,
5446395Sbostic 	    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) == NULL) {
5546395Sbostic 		(void)fprintf(stderr,
5646395Sbostic 		    "kvm_mkdb: %s: %s\n", dbtemp, strerror(errno));
5746395Sbostic 		exit(1);
5846395Sbostic 	}
5946395Sbostic 	create_knlist(nlistpath, db);
6046395Sbostic 	(void)dbm_close(db);
61*46407Sbostic 	(void)strcat(dbtemp, DBM_SUFFIX);
6246395Sbostic 	if (rename(dbtemp, dbname)) {
6346395Sbostic 		(void)fprintf(stderr, "kvm_mkdb: %s to %s: %s.\n",
6446395Sbostic 		    dbtemp, dbname, strerror(errno));
6546395Sbostic 		exit(1);
6646395Sbostic 	}
6740358Smarc 	exit(0);
6840358Smarc }
6940358Smarc 
7046395Sbostic error(n)
7146395Sbostic 	char *n;
7240358Smarc {
7346395Sbostic 	int sverr;
7440358Smarc 
7546395Sbostic 	sverr = errno;
7646395Sbostic 	(void)fprintf(stderr, "kvm_mkdb: ");
7746395Sbostic 	if (n)
7846395Sbostic 		(void)fprintf(stderr, "%s: ", n);
7946395Sbostic 	(void)fprintf(stderr, "%s\n", strerror(sverr));
8040358Smarc 	exit(1);
8140358Smarc }
8240358Smarc 
8346395Sbostic usage()
8440358Smarc {
8546395Sbostic 	(void)fprintf(stderr, "usage: kvm_mkdb [file]\n");
8640358Smarc 	exit(1);
8740358Smarc }
88