File:  [LON-CAPA] / loncom / debugging_tools / dump_db.c
Revision 1.1: download - view: text, annotated - select for diffs
Mon Jun 19 21:21:47 2006 UTC (17 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: version_2_2_0, version_2_1_X, version_2_1_99_3, version_2_1_99_2, version_2_1_99_1, version_2_1_99_0, HEAD
- first pass at a c level database dumper

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <gdbm.h>
#include <errno.h>
#include <string.h>

void usage()
{
  printf("\nUsage:\ngdbm_convertor -f <gdbm file to convert>\n");
}

void read_db(char *filename)
{
  GDBM_FILE db;
  datum key, nextkey, content;
  db = gdbm_open(filename, 0, GDBM_READER, 0, 0);

  if (db == NULL) {
    printf("Unable to open db %s beacsue of %s (%d) -- %s (%d)\n",filename,
	   gdbm_strerror(gdbm_errno),gdbm_errno,strerror(errno),errno);
    return;
  }
  key = gdbm_firstkey(db);

  while ( key.dptr ) {
    content = gdbm_fetch(db, key);
    fwrite(key.dptr, key.dsize, sizeof(char), stdout);
    printf(" -> ");
    fwrite(content.dptr, content.dsize, sizeof(char), stdout);
    printf("\n");
    free(content.dptr);
    nextkey = gdbm_nextkey(db, key);
    free(key.dptr);
    key = nextkey;
  }
}

int main(int argc, char  **argv) 
{

  int c;
  char *filename=NULL;
  while (1) {
    c = getopt(argc,argv,"f:");  
    if (c == -1)
      break;
    switch (c) {
    case 'f':
      filename = optarg;
    }
  }

  if (filename == NULL) {
    usage();
    return 1;
  }

  read_db(filename);

  return 0;
}


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>