xref: /csrg-svn/old/dbconv/dbconv.c (revision 51746)
146411Sbostic /*-
246411Sbostic  * Copyright (c) 1990 The Regents of the University of California.
346411Sbostic  * All rights reserved.
446411Sbostic  *
546411Sbostic  * %sccs.include.redist.c%
646411Sbostic  */
746411Sbostic 
846411Sbostic #ifndef lint
946411Sbostic char copyright[] =
1046411Sbostic "@(#) Copyright (c) 1990 The Regents of the University of California.\n\
1146411Sbostic  All rights reserved.\n";
1246411Sbostic #endif /* not lint */
1346411Sbostic 
1446411Sbostic #ifndef lint
15*51746Sbostic static char sccsid[] = "@(#)dbconv.c	5.2 (Berkeley) 11/19/91";
1646411Sbostic #endif /* not lint */
1746411Sbostic 
1846411Sbostic #include <sys/types.h>
1946411Sbostic #include <sys/stat.h>
2046411Sbostic #include <fcntl.h>
2146411Sbostic #include <ndbm.h>
2246411Sbostic #include <errno.h>
2346411Sbostic #include <stdio.h>
2446411Sbostic 
main(argc,argv)2546411Sbostic main(argc, argv)
2646411Sbostic 	int argc;
2746411Sbostic 	char **argv;
2846411Sbostic {
2946411Sbostic 	extern int optind;
3046411Sbostic 	DB *db;
3146411Sbostic 	DBM *dbm;
3246411Sbostic 	DBT t_key, t_data;
3346411Sbostic 	datum f_key, f_data;
3446411Sbostic 	int ch, dup, rec;
3546411Sbostic 
3646411Sbostic 	while ((ch = getopt(argc, argv, "")) != EOF)
3746411Sbostic 		switch((char)ch) {
3846411Sbostic 		case '?':
3946411Sbostic 		default:
4046411Sbostic 			usage();
4146411Sbostic 		}
4246411Sbostic 	argc -= optind;
4346411Sbostic 	argv += optind;
4446411Sbostic 
4546411Sbostic 	dbm = dbm_open(*argv, O_RDONLY, 0);
4646411Sbostic 	if (!dbm)
4746411Sbostic 		error(*argv);
48*51746Sbostic 	db = dbopen(*++argv, O_CREAT|O_WRONLY, DEFFILEMODE, DB_HASH, NULL);
4946411Sbostic 	if (!db)
5046411Sbostic 		error(*argv);
5146411Sbostic 
5246411Sbostic 	dup = rec = 0;
5346411Sbostic 	for (f_key = dbm_firstkey(dbm); f_key.dptr; f_key = dbm_nextkey(dbm)) {
5446411Sbostic 		f_data = dbm_fetch(dbm, f_key);
5546411Sbostic 		t_key.data = f_key.dptr;
5646411Sbostic 		t_key.size = f_key.dsize;
5746411Sbostic 		t_data.data = f_data.dptr;
5846411Sbostic 		t_data.size = f_data.dsize;
5946411Sbostic 		switch((db->put)(db, &t_key, &t_data, R_NOOVERWRITE)) {
6046411Sbostic 		case -1:
6146411Sbostic 			error(*argv);
6246411Sbostic 		case 0:
6346411Sbostic 			++rec;
6446411Sbostic 			break;
6546411Sbostic 		case 1:
6646411Sbostic 			if (!dup++)
6746411Sbostic 				(void)fprintf(stderr,
6846411Sbostic 				    "dbconv: duplicate records discarded\n");
6946411Sbostic 			break;
7046411Sbostic 		}
7146411Sbostic 	}
7246411Sbostic 	(void)(db->close)(db);
7346411Sbostic 	(void)printf("%d records, %d duplicates discarded.\n", rec + dup, dup);
7446411Sbostic 	exit(dup ? 1 : 0);
7546411Sbostic }
7646411Sbostic 
error(p)7746411Sbostic error(p)
7846411Sbostic 	char *p;
7946411Sbostic {
8046411Sbostic 	(void)fprintf(stderr, "dbconv: %s: %s\n", p, strerror(errno));
8146411Sbostic 	exit(1);
8246411Sbostic }
8346411Sbostic 
usage()8446411Sbostic usage()
8546411Sbostic {
8646411Sbostic 	(void)fprintf(stderr, "usage: dbconv from to\n");
8746411Sbostic 	exit(1);
8846411Sbostic }
89