Annotation of loncom/debugging_tools/dump_db.c, revision 1.2
1.1 albertel 1: #include <stdio.h>
2: #include <stdlib.h>
3: #include <unistd.h>
4: #include <gdbm.h>
5: #include <errno.h>
6: #include <string.h>
1.2 ! albertel 7: #include <ctype.h>
! 8:
! 9: static const char c2x_table[] = "0123456789abcdef";
! 10:
! 11: void c2x(unsigned what, unsigned char prefix, char *where,
! 12: unsigned int offset)
! 13: {
! 14: where[offset] = prefix;
! 15: where[offset+1] = c2x_table[what >> 4];
! 16: where[offset+2] = c2x_table[what & 0xf];
! 17: }
! 18:
! 19:
! 20: datum* http_escape(datum* data)
! 21: {
! 22: datum *escaped_data;
! 23: escaped_data = malloc(sizeof(datum));
! 24: escaped_data->dsize = (3 * data->dsize) + 3;
! 25: escaped_data->dptr = malloc(escaped_data->dsize);
! 26: unsigned int c,i,j;
! 27:
! 28: j=0;
! 29: for (i=0;i<data->dsize;i++) {
! 30: c = data->dptr[i];
! 31: if (!isalnum(c)) {
! 32: c2x(c, '%', escaped_data->dptr, j);
! 33: j+=3;
! 34: } else {
! 35: escaped_data->dptr[j] = c;
! 36: j++;
! 37: }
! 38: }
! 39: escaped_data->dsize=j;
! 40: return escaped_data;
! 41: }
! 42:
1.1 albertel 43:
44: void usage()
45: {
46: printf("\nUsage:\ngdbm_convertor -f <gdbm file to convert>\n");
47: }
48:
49: void read_db(char *filename)
50: {
51: GDBM_FILE db;
52: datum key, nextkey, content;
1.2 ! albertel 53: datum *escaped_key, *escaped_content;
1.1 albertel 54: db = gdbm_open(filename, 0, GDBM_READER, 0, 0);
55:
56: if (db == NULL) {
1.2 ! albertel 57: printf("ERROR:Unable to open db %s because of %s (%d) -- %s (%d)\n",
! 58: filename,gdbm_strerror(gdbm_errno),gdbm_errno,strerror(errno),
! 59: errno);
1.1 albertel 60: return;
61: }
62: key = gdbm_firstkey(db);
63:
64: while ( key.dptr ) {
65: content = gdbm_fetch(db, key);
1.2 ! albertel 66: escaped_key = http_escape(&key);
! 67: escaped_content = http_escape(&content);
! 68: fwrite(escaped_key->dptr,
! 69: escaped_key->dsize, sizeof(char), stdout);
1.1 albertel 70: printf(" -> ");
1.2 ! albertel 71: fwrite(escaped_content->dptr,
! 72: escaped_content->dsize, sizeof(char), stdout);
1.1 albertel 73: printf("\n");
74: free(content.dptr);
1.2 ! albertel 75: free(escaped_content->dptr);
! 76: free(escaped_content);
! 77: free(escaped_key->dptr);
! 78: free(escaped_key);
1.1 albertel 79: nextkey = gdbm_nextkey(db, key);
80: free(key.dptr);
81: key = nextkey;
82: }
83: }
84:
85: int main(int argc, char **argv)
86: {
87:
88: int c;
89: char *filename=NULL;
90: while (1) {
91: c = getopt(argc,argv,"f:");
92: if (c == -1)
93: break;
94: switch (c) {
95: case 'f':
96: filename = optarg;
97: }
98: }
99:
100: if (filename == NULL) {
101: usage();
102: return 1;
103: }
104:
105: read_db(filename);
106:
107: return 0;
108: }
109:
1.2 ! albertel 110:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>