update from LBL (Chris, Craig and Steve)
[unix-history] / usr / src / usr.sbin / kvm_mkdb / kvm_mkdb.c
CommitLineData
41485987
KB
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
c9619dbd
MT
3 * All rights reserved.
4 *
41485987 5 * %sccs.include.redist.c%
c9619dbd
MT
6 */
7
8#ifndef lint
9char copyright[] =
41485987 10"@(#) Copyright (c) 1990 The Regents of the University of California.\n\
c9619dbd
MT
11 All rights reserved.\n";
12#endif /* not lint */
13
c9619dbd 14#ifndef lint
13e7db0b 15static char sccsid[] = "@(#)kvm_mkdb.c 5.15 (Berkeley) %G%";
c9619dbd 16#endif /* not lint */
41485987 17
c9619dbd 18#include <sys/param.h>
01f88b32
KB
19#include <sys/stat.h>
20#include <fcntl.h>
573b8d71 21#include <db.h>
c9619dbd 22#include <errno.h>
c9619dbd 23#include <stdio.h>
13e7db0b 24#include <stdlib.h>
01f88b32
KB
25#include <string.h>
26#include <paths.h>
c9619dbd 27
13e7db0b 28#include "extern.h"
c9619dbd 29
13e7db0b
KB
30static void usage __P(());
31
32int
c9619dbd 33main(argc, argv)
01f88b32
KB
34 int argc;
35 char **argv;
c9619dbd 36{
573b8d71 37 DB *db;
c9619dbd 38 int ch;
13e7db0b 39 char *p, *nlistpath, *nlistname, dbtemp[MAXPATHLEN], dbname[MAXPATHLEN];
c9619dbd 40
c9619dbd
MT
41 while ((ch = getopt(argc, argv, "")) != EOF)
42 switch((char)ch) {
43 case '?':
44 default:
01f88b32 45 usage();
c9619dbd
MT
46 }
47 argc -= optind;
48 argv += optind;
49
9208bec5
KB
50 if (argc > 1)
51 usage();
52
13e7db0b
KB
53 /* If the existing db file matches the currently running kernel, exit */
54 if (testdb())
55 exit(0);
56
57#define basename(cp) ((p = rindex((cp), '/')) != NULL ? p + 1 : (cp))
c9619dbd
MT
58 nlistpath = argc > 1 ? argv[0] : _PATH_UNIX;
59 nlistname = basename(nlistpath);
573b8d71 60
13e7db0b
KB
61 (void)snprintf(dbtemp, sizeof(dbtemp), "%skvm_%s.tmp",
62 _PATH_VARDB, nlistname);
63 (void)snprintf(dbname, sizeof(dbname), "%skvm_%s.db",
64 _PATH_VARDB, nlistname);
01f88b32 65 (void)umask(0);
13e7db0b 66 db = dbopen(dbtemp, O_CREAT|O_EXLOCK|O_TRUNC|O_WRONLY,
ec521b9d 67 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, DB_HASH, NULL);
573b8d71 68 if (!db) {
01f88b32
KB
69 (void)fprintf(stderr,
70 "kvm_mkdb: %s: %s\n", dbtemp, strerror(errno));
71 exit(1);
c9619dbd 72 }
01f88b32 73 create_knlist(nlistpath, db);
573b8d71 74 (void)(db->close)(db);
01f88b32
KB
75 if (rename(dbtemp, dbname)) {
76 (void)fprintf(stderr, "kvm_mkdb: %s to %s: %s.\n",
77 dbtemp, dbname, strerror(errno));
78 exit(1);
79 }
80 exit(0);
c9619dbd
MT
81}
82
13e7db0b 83void
01f88b32
KB
84error(n)
85 char *n;
c9619dbd 86{
01f88b32 87 int sverr;
c9619dbd 88
01f88b32
KB
89 sverr = errno;
90 (void)fprintf(stderr, "kvm_mkdb: ");
91 if (n)
92 (void)fprintf(stderr, "%s: ", n);
93 (void)fprintf(stderr, "%s\n", strerror(sverr));
c9619dbd
MT
94 exit(1);
95}
96
13e7db0b 97void
01f88b32 98usage()
c9619dbd 99{
01f88b32 100 (void)fprintf(stderr, "usage: kvm_mkdb [file]\n");
c9619dbd
MT
101 exit(1);
102}