1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 char copyright[] = 10 "@(#) Copyright (c) 1990 The Regents of the University of California.\n\ 11 All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)kvm_mkdb.c 5.8 (Berkeley) 02/14/91"; 16 #endif /* not lint */ 17 18 #include <sys/param.h> 19 #include <sys/stat.h> 20 #include <fcntl.h> 21 #include <ndbm.h> 22 #include <errno.h> 23 #include <stdio.h> 24 #include <string.h> 25 #include <paths.h> 26 27 char *tmp; 28 #define basename(cp) ((tmp=rindex((cp), '/')) ? tmp+1 : (cp)) 29 30 main(argc, argv) 31 int argc; 32 char **argv; 33 { 34 extern int optind; 35 DBM *db; 36 int ch; 37 char *nlistpath, *nlistname, dbtemp[MAXPATHLEN], dbname[MAXPATHLEN]; 38 39 while ((ch = getopt(argc, argv, "")) != EOF) 40 switch((char)ch) { 41 case '?': 42 default: 43 usage(); 44 } 45 argc -= optind; 46 argv += optind; 47 48 nlistpath = argc > 1 ? argv[0] : _PATH_UNIX; 49 nlistname = basename(nlistpath); 50 (void)sprintf(dbtemp, "%s/kvm_%s.tmp", _PATH_VARRUN, nlistname); 51 (void)sprintf(dbname, "%s/kvm_%s.db", _PATH_VARRUN, nlistname); 52 (void)umask(0); 53 if ((db = dbm_open(dbtemp, O_CREAT|O_WRONLY|O_EXCL, 54 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) == NULL) { 55 (void)fprintf(stderr, 56 "kvm_mkdb: %s: %s\n", dbtemp, strerror(errno)); 57 exit(1); 58 } 59 create_knlist(nlistpath, db); 60 (void)dbm_close(db); 61 (void)strcat(dbtemp, DBM_SUFFIX); 62 if (rename(dbtemp, dbname)) { 63 (void)fprintf(stderr, "kvm_mkdb: %s to %s: %s.\n", 64 dbtemp, dbname, strerror(errno)); 65 exit(1); 66 } 67 exit(0); 68 } 69 70 error(n) 71 char *n; 72 { 73 int sverr; 74 75 sverr = errno; 76 (void)fprintf(stderr, "kvm_mkdb: "); 77 if (n) 78 (void)fprintf(stderr, "%s: ", n); 79 (void)fprintf(stderr, "%s\n", strerror(sverr)); 80 exit(1); 81 } 82 83 usage() 84 { 85 (void)fprintf(stderr, "usage: kvm_mkdb [file]\n"); 86 exit(1); 87 } 88