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*46395Sbostic static char sccsid[] = "@(#)kvm_mkdb.c	5.7 (Berkeley) 02/12/91";
1640358Smarc #endif /* not lint */
1742800Sbostic 
1840358Smarc #include <sys/param.h>
19*46395Sbostic #include <sys/stat.h>
20*46395Sbostic #include <fcntl.h>
2140358Smarc #include <ndbm.h>
2240358Smarc #include <errno.h>
23*46395Sbostic #include <stdio.h>
2442064Sbostic #include <string.h>
25*46395Sbostic #include <paths.h>
2640358Smarc 
2740358Smarc char *tmp;
2840358Smarc #define basename(cp)	((tmp=rindex((cp), '/')) ? tmp+1 : (cp))
2940358Smarc 
3040358Smarc main(argc, argv)
31*46395Sbostic 	int argc;
32*46395Sbostic 	char **argv;
3340358Smarc {
34*46395Sbostic 	extern int optind;
3540358Smarc 	DBM *db;
3640358Smarc 	int ch;
37*46395Sbostic 	char *nlistpath, *nlistname, dbtemp[MAXPATHLEN], dbname[MAXPATHLEN];
3840358Smarc 
3940358Smarc 	while ((ch = getopt(argc, argv, "")) != EOF)
4040358Smarc 		switch((char)ch) {
4140358Smarc 		case '?':
4240358Smarc 		default:
43*46395Sbostic 			usage();
4440358Smarc 		}
4540358Smarc 	argc -= optind;
4640358Smarc 	argv += optind;
4740358Smarc 
4840358Smarc 	nlistpath = argc > 1 ? argv[0] : _PATH_UNIX;
4940358Smarc 	nlistname = basename(nlistpath);
50*46395Sbostic 	(void)sprintf(dbtemp, "%s/kvm_%s.tmp", _PATH_VARRUN, nlistname);
51*46395Sbostic 	(void)sprintf(dbname, "%s/kvm_%s.db", _PATH_VARRUN, nlistname);
52*46395Sbostic 	(void)umask(0);
53*46395Sbostic 	if ((db = dbm_open(dbtemp, O_CREAT|O_WRONLY|O_EXCL,
54*46395Sbostic 	    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) == NULL) {
55*46395Sbostic 		(void)fprintf(stderr,
56*46395Sbostic 		    "kvm_mkdb: %s: %s\n", dbtemp, strerror(errno));
57*46395Sbostic 		exit(1);
58*46395Sbostic 	}
59*46395Sbostic 	create_knlist(nlistpath, db);
60*46395Sbostic 	(void)dbm_close(db);
61*46395Sbostic 	if (rename(dbtemp, dbname)) {
62*46395Sbostic 		(void)fprintf(stderr, "kvm_mkdb: %s to %s: %s.\n",
63*46395Sbostic 		    dbtemp, dbname, strerror(errno));
64*46395Sbostic 		exit(1);
65*46395Sbostic 	}
6640358Smarc 	exit(0);
6740358Smarc }
6840358Smarc 
69*46395Sbostic error(n)
70*46395Sbostic 	char *n;
7140358Smarc {
72*46395Sbostic 	int sverr;
7340358Smarc 
74*46395Sbostic 	sverr = errno;
75*46395Sbostic 	(void)fprintf(stderr, "kvm_mkdb: ");
76*46395Sbostic 	if (n)
77*46395Sbostic 		(void)fprintf(stderr, "%s: ", n);
78*46395Sbostic 	(void)fprintf(stderr, "%s\n", strerror(sverr));
7940358Smarc 	exit(1);
8040358Smarc }
8140358Smarc 
82*46395Sbostic usage()
8340358Smarc {
84*46395Sbostic 	(void)fprintf(stderr, "usage: kvm_mkdb [file]\n");
8540358Smarc 	exit(1);
8640358Smarc }
87