xref: /csrg-svn/old/dbconv/dbconv.c (revision 46411)
1*46411Sbostic /*-
2*46411Sbostic  * Copyright (c) 1990 The Regents of the University of California.
3*46411Sbostic  * All rights reserved.
4*46411Sbostic  *
5*46411Sbostic  * %sccs.include.redist.c%
6*46411Sbostic  */
7*46411Sbostic 
8*46411Sbostic #ifndef lint
9*46411Sbostic char copyright[] =
10*46411Sbostic "@(#) Copyright (c) 1990 The Regents of the University of California.\n\
11*46411Sbostic  All rights reserved.\n";
12*46411Sbostic #endif /* not lint */
13*46411Sbostic 
14*46411Sbostic #ifndef lint
15*46411Sbostic static char sccsid[] = "@(#)dbconv.c	5.1 (Berkeley) 02/14/91";
16*46411Sbostic #endif /* not lint */
17*46411Sbostic 
18*46411Sbostic #include <sys/types.h>
19*46411Sbostic #include <sys/stat.h>
20*46411Sbostic #include <fcntl.h>
21*46411Sbostic #include <ndbm.h>
22*46411Sbostic #include <errno.h>
23*46411Sbostic #include <stdio.h>
24*46411Sbostic 
25*46411Sbostic main(argc, argv)
26*46411Sbostic 	int argc;
27*46411Sbostic 	char **argv;
28*46411Sbostic {
29*46411Sbostic 	extern int optind;
30*46411Sbostic 	DB *db;
31*46411Sbostic 	DBM *dbm;
32*46411Sbostic 	DBT t_key, t_data;
33*46411Sbostic 	datum f_key, f_data;
34*46411Sbostic 	int ch, dup, rec;
35*46411Sbostic 
36*46411Sbostic 	while ((ch = getopt(argc, argv, "")) != EOF)
37*46411Sbostic 		switch((char)ch) {
38*46411Sbostic 		case '?':
39*46411Sbostic 		default:
40*46411Sbostic 			usage();
41*46411Sbostic 		}
42*46411Sbostic 	argc -= optind;
43*46411Sbostic 	argv += optind;
44*46411Sbostic 
45*46411Sbostic 	dbm = dbm_open(*argv, O_RDONLY, 0);
46*46411Sbostic 	if (!dbm)
47*46411Sbostic 		error(*argv);
48*46411Sbostic 	db = hash_open(*++argv, O_CREAT|O_WRONLY, DEFFILEMODE,
49*46411Sbostic 	    (HASHINFO *)NULL);
50*46411Sbostic 	if (!db)
51*46411Sbostic 		error(*argv);
52*46411Sbostic 
53*46411Sbostic 	dup = rec = 0;
54*46411Sbostic 	for (f_key = dbm_firstkey(dbm); f_key.dptr; f_key = dbm_nextkey(dbm)) {
55*46411Sbostic 		f_data = dbm_fetch(dbm, f_key);
56*46411Sbostic 		t_key.data = f_key.dptr;
57*46411Sbostic 		t_key.size = f_key.dsize;
58*46411Sbostic 		t_data.data = f_data.dptr;
59*46411Sbostic 		t_data.size = f_data.dsize;
60*46411Sbostic 		switch((db->put)(db, &t_key, &t_data, R_NOOVERWRITE)) {
61*46411Sbostic 		case -1:
62*46411Sbostic 			error(*argv);
63*46411Sbostic 		case 0:
64*46411Sbostic 			++rec;
65*46411Sbostic 			break;
66*46411Sbostic 		case 1:
67*46411Sbostic 			if (!dup++)
68*46411Sbostic 				(void)fprintf(stderr,
69*46411Sbostic 				    "dbconv: duplicate records discarded\n");
70*46411Sbostic 			break;
71*46411Sbostic 		}
72*46411Sbostic 	}
73*46411Sbostic 	(void)(db->close)(db);
74*46411Sbostic 	(void)printf("%d records, %d duplicates discarded.\n", rec + dup, dup);
75*46411Sbostic 	exit(dup ? 1 : 0);
76*46411Sbostic }
77*46411Sbostic 
78*46411Sbostic error(p)
79*46411Sbostic 	char *p;
80*46411Sbostic {
81*46411Sbostic 	(void)fprintf(stderr, "dbconv: %s: %s\n", p, strerror(errno));
82*46411Sbostic 	exit(1);
83*46411Sbostic }
84*46411Sbostic 
85*46411Sbostic usage()
86*46411Sbostic {
87*46411Sbostic 	(void)fprintf(stderr, "usage: dbconv from to\n");
88*46411Sbostic 	exit(1);
89*46411Sbostic }
90