exim and gdbm

Pierre A. Humblet Pierre.Humblet@ieee.org
Sun Jul 20 00:25:00 GMT 2003


At 10:14 PM 7/18/2003 -0400, Charles Wilson wrote:

>Plus, with some luck or help, I can write a program that dumps a database
>to an interchange format.  executable #1 is linked to the old gdbm dll,
>and is used to write the database out to interchange format.  executable
>#2 is linked to the new gdbm dll, and is used to read the interchange
>format in, and save out to a new gdbm file.

Here is an attempt.
There should probably be a shell script, e.g.

#! /bin/sh
dumpgdbm $1 | loadgdbm $1.cygnew && mv $1 $1.bak && mv $1.cygnew $1

Pierre

-------------- next part --------------
#include <gdbm.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

/* 
   Dump a gdbm data base on stdout in binary format:
   key_size (4 bytes), key, content_size (4 bytes), content

   P. Humblet July 2003. Public domain, use at own risk.
*/
 
main (int argc, char * argv[])
{
  GDBM_FILE dbf;
  datum key, nextkey, content;
  int count = 0;

  if (argc != 2) 
    {
      fprintf (stderr,"Usage: dumpgdbm filename\n");
      exit (1);
    }

  dbf = gdbm_open (argv[1], 0, GDBM_READER, 0, NULL);
  if (dbf == NULL) {
    fprintf(stderr, "Error opening %s: %s\n",
	    argv[1], errno?strerror(errno):gdbm_strerror (gdbm_errno));
    exit(1);
  }
  
  key = gdbm_firstkey (dbf);
  
  while (key.dptr) 
    {
      content = gdbm_fetch (dbf, key);
      if (!content.dptr) 
	{
	  fprintf(stderr, "gdbm_fetch: %s\n", 
		  errno?strerror (errno):gdbm_strerror (gdbm_errno));
	  goto err;
	}
#ifdef DEBUG
      fprintf(stderr, "%d key size %d content size %d\n",
	      count, key.dsize, content.dsize);
#endif
      if (fwrite (&key.dsize, sizeof(key.dsize), 1, stdout) != 1)
	{  
	  perror ("Key size\n");
	  goto err;
	}
      if (fwrite (key.dptr, key.dsize, 1, stdout) != 1)
	{  
	  perror ("Key\n");
	  goto err;
	}
      if (fwrite (&content.dsize, sizeof(content.dsize), 1, stdout) != 1)
	{  
	  perror ("Content size\n");
	  goto err;
	}
      if (fwrite (content.dptr, content.dsize, 1, stdout) != 1)
	{  
	  perror ("Content\n");
	  goto err;
	}
      nextkey = gdbm_nextkey (dbf, key);
      free (key.dptr);
      free (content.dptr);
      key = nextkey;
      count++;
    }
  gdbm_close (dbf);
  fprintf (stderr,"%d records dumped\n", count);
  exit (0);

 err:
  gdbm_close (dbf);
  fprintf (stderr,"Error at record %d\n", count);
  exit (1);
}

-------------- next part --------------
#include <gdbm.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/unistd.h>

/* 
   Create a gdbm data base from stdin with binary format:
   key_size (4 bytes), key, content_size (4 bytes), content

   P. Humblet July 2003. Public domain, use at own risk.
*/

main (int argc, char * argv[])
{
  GDBM_FILE dbf;
  datum key, content;
  int block_size, count = 0;
  int res;

  if (argc < 2 || argc > 3) 
    {
      fprintf (stderr,"Usage loadgdbm filename [block_size]\n");
      exit (1);
    }
  if (argc == 2)
    block_size = 512;
  else
    block_size = atoi (argv[2]);

  if (!access (argv[1], F_OK))
    {
      fprintf (stderr,"%s already exists\n", argv[1]);
      exit (1);
    } 
  dbf = gdbm_open (argv[1], block_size, GDBM_NEWDB, 0777, NULL);
  if (dbf == 0) 
    {
    fprintf(stderr, "Error opening %s: %s\n",
	    argv[1], errno?strerror (errno):gdbm_strerror (gdbm_errno));
    exit(1);
  }
  
  while (1) 
    {
      if (fread (&key.dsize, sizeof(key.dsize), 1, stdin) != 1)
	{  
	  break;
	}
      if (!(key.dptr = (char *) malloc (key.dsize)))
	{  
	  perror ("Malloc key");
	  goto err;
	}
      if (fread (key.dptr, key.dsize, 1, stdin) != 1)
	{  
	  perror ("Key");
	  goto err;
	}
      if (fread (&content.dsize, sizeof(content.dsize), 1, stdin) != 1)
	{  
	  perror("Content size");
	  goto err;
	}
      if (!(content.dptr = (char *) malloc (content.dsize)))
	{  
	  perror ("Malloc content");
	  goto err;
	}
      if ((res=fread (content.dptr, content.dsize, 1, stdin)) != 1)
	{  
	  perror("Content");
	  fprintf(stderr, "%d %d\n", res, content.dsize); 
	  goto err;
	}
      if (gdbm_store ( dbf, key, content, GDBM_INSERT ))
	{
	  fprintf(stderr, "gdbm_store: %s\n", 
		  errno?strerror (errno):gdbm_strerror (gdbm_errno));
	  goto err;
	}
      free (key.dptr);
      free (content.dptr);
      count++;
    }
  gdbm_close (dbf);
  fprintf (stderr,"%d records loaded\n", count);
  exit (0);

 err:
  gdbm_close (dbf);
  fprintf (stderr,"Error at record %d\n", count);
  exit (1);
}



More information about the Cygwin-apps mailing list