summaryrefslogtreecommitdiffstats
path: root/gdbmdump.c
blob: 8aa8be576f3e58ad6c64209b9f48cef56206f464 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * Copyright 2008,2010,2011 Red Hat, Inc.
 *
 * This Program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This Program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this Program; if not, write to the
 *
 *   Free Software Foundation, Inc.
 *   59 Temple Place, Suite 330
 *   Boston, MA 02111-1307 USA
 *
 */

/* Dump content from a gdbm database in a format resembling that which is used
 * by db_dump, so that it can be read by db_load. */
#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gdbm.h>

static int pflag = 0;
static int yflag = 0;

static void
dump_start(int total, const char *name)
{
	printf("VERSION=3\n");
	if (total > 1) {
		printf("database=%s\n", name);
	}
	if (pflag) {
		printf("format=print\n");
	}
	printf("type=hash\n");
	printf("HEADER=END\n");
}
static void
puthex(unsigned char c)
{
	const char chars[] = "0123456789abcdef";
	putchar(chars[c >> 4]);
	putchar(chars[c & 0xf]);
}
static void
dump_entry(datum key, datum value)
{
	int i;
	if (yflag) {
		if ((key.dsize >= 3) && (memcmp(key.dptr, "YP_", 3) == 0)) {
			return;
		}
	}
	if (pflag) {
		putchar(' ');
		for (i = 0; i < key.dsize; i++) {
			if (isprint(key.dptr[i]) &&
			    !isspace(key.dptr[i]) &&
			    (key.dptr[i] != '\\')) {
				putchar(key.dptr[i]);
			} else {
				putchar('\\');
				puthex(key.dptr[i]);
			}
		}
		putchar('\n');
		putchar(' ');
		for (i = 0; i < value.dsize; i++) {
			if (isprint(value.dptr[i]) &&
			    (value.dptr[i] != ' ') &&
			    (value.dptr[i] != '\t') &&
			    (value.dptr[i] != '\\')) {
				putchar(value.dptr[i]);
			} else {
				putchar('\\');
				puthex(value.dptr[i]);
			}
		}
		putchar('\n');
	} else {
		putchar(' ');
		for (i = 0; i < key.dsize; i++) {
			puthex(key.dptr[i]);
		}
		putchar('\n');
		putchar(' ');
		for (i = 0; i < value.dsize; i++) {
			puthex(value.dptr[i]);
		}
		putchar('\n');
	}
}
static void
dump_end()
{
	printf("DATA=END\n");
}

int
main(int argc, char **argv)
{
	GDBM_FILE dbf;
	datum key, next, value;
	int i;

	while ((i = getopt(argc, argv, "py")) != -1) {
		switch (i) {
		case 'p':
			pflag = 1;
			break;
		case 'y':
			yflag = 1;
			break;
		default:
			fprintf(stderr, "Usage: %s [-p] [-y] file [...]\n",
				strchr(argv[0], '/') ?
				strrchr(argv[0], '/') + 1 : argv[0]);
			return 1;
			break;
		}
	}
	for (i = optind; i < argc; i++) {
		dbf = gdbm_open(argv[i], 0, GDBM_READER, 0600, NULL);
		if (dbf == NULL) {
			fprintf(stderr, "Error opening `%s': %s\n", argv[i],
				gdbm_errno ? gdbm_strerror(gdbm_errno) :
				strerror(errno));
			return 1;
		}
		dump_start(argc - optind, argv[i]);
		key = gdbm_firstkey(dbf);
		while (key.dptr != NULL) {
			value = gdbm_fetch(dbf, key);
			if (value.dptr != NULL) {
				dump_entry(key, value);
				free(value.dptr);
			}
			next = gdbm_nextkey(dbf, key);
			free(key.dptr);
			key = next;
		}
		dump_end();
		gdbm_close(dbf);
	}
	return 0;
}