/* Copyright 1993 - Matti Aarnio, Turku University, Turku, Finland
   This will be free software, but only when it is finished.

   The way the Zmailer uses DBM entries is by using strings with
   their terminating NULL as keys, and as data..  Thus the length
   is strlen(string)+1, not strlen(string) !
*/

#include "hostenv.h"
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <fcntl.h>
#ifdef HAVE_NDBM_H
#define datum Ndatum
#include <ndbm.h>
#undef datum
#endif
#ifdef HAVE_GDBM_H
#define datum Gdatum
#include <gdbm.h>
#undef datum
#endif
#ifdef HAVE_DB_H
#include <db.h>
#endif

#include <errno.h>	/* This usually defines the following three.. */
extern int errno;

void
usage(prog,err,errno)
char *prog, *err;
int errno;
{
  fprintf(stderr,"Usage: %s dbtype database.name key\n",prog);
  fprintf(stderr,"  Dbtypes are:");
#ifdef HAVE_NDBM_H
  fprintf(stderr," ndbm");
#endif
#ifdef HAVE_GDBM_H
  fprintf(stderr," gdbm");
#endif
#ifdef HAVE_DB_H
  fprintf(stderr," btree");
#endif
  fprintf(stderr,"\n");
#ifdef HAVE_NDBM_H
  fprintf(stderr,"  (NDBM appends  .pag, and .dir  into actual db file names..)\n");
#endif
#ifdef HAVE_GDBM_H
  fprintf(stderr,"  (GDBM DOES NOT append .pag, nor .dir  into actual db file names..)\n");
#endif
#ifdef HAVE_DB_H
  fprintf(stderr,"  (DB appends  .db  into the actial db file names..)\n");
#endif
  fprintf(stderr," Error now: %s",err);
  fprintf(stderr,", errno=%d (%s)",errno,strerror(errno));
  fprintf(stderr,"\n");
  exit (1);
}

int
main(argc,argv)
int argc;
char *argv[];
{
  char *dbasename = NULL;
  
#ifdef HAVE_GDBM_H
  GDBM_FILE Gdbmfile;
#endif
#ifdef HAVE_DB_H
  DB *dbfile;
#endif

  if (argc != 4) usage(argv[0],"wrong number of arguments",0);
  dbasename = argv[2];
#ifdef HAVE_NDBM_H
  if (strcmp(argv[1],"ndbm")==0) {
    DBM *Ndbmfile;
    Ndatum key;
    Ndatum result;
    Ndbmfile = dbm_open(dbasename, O_RDONLY, 0644);

    if (!Ndbmfile) {
      fprintf(stderr,"Failed to open '%s' NDBM-dbase\n",dbasename);
      return 1;
    }

    key.dptr = argv[3];
    key.dsize = strlen(argv[3]) +1;

    result = dbm_fetch(Ndbmfile,key);

    if (result.dptr == NULL) {
      fprintf(stderr,"Key %s not found\n",argv[3]);
      return 2;
    }
    printf("siz:%d, dat: %s\n",result.dsize,result.dptr);

    dbm_close(Ndbmfile);

    return 0;
  }
#endif /* NDBM */
#ifdef HAVE_GDBM_H
  if (strcmp(argv[1],"gdbm")==0) {
    GDBM_FILE gdbmfile;
    Gdatum key;
    Gdatum result;
    gdbmfile = gdbm_open(dbasename, O_RDONLY, 0644, 0, NULL);

    if (!gdbmfile) {
      fprintf(stderr,"Failed to open '%s' GDBM-dbase\n",dbasename);
      return 1;
    }

    key.dptr = argv[3];
    key.dsize = strlen(argv[3]) +1;

    result = gdbm_fetch(gdbmfile,key);

    if (result.dptr == NULL) {
      fprintf(stderr,"Key %s not found\n",argv[3]);
      return 2;
    }
    printf("siz:%d, dat: %s\n",result.dsize,result.dptr);

    gdbm_close(gdbmfile);

    return 0;
  }
#endif /* GDBM */
#ifdef HAVE_DB_H
  if (strcmp(argv[1],"btree")==0) {
    DB *dbfile;
    DBT key;
    DBT result;
    int rc;

    dbfile = dbopen(dbasename, O_RDONLY, 0644, DB_BTREE, NULL);

    if (!dbfile) {
      fprintf(stderr,"Failed to open '%s' BTREE-dbase\n",dbasename);
      return 1;
    }

    key.data = argv[3];
    key.size = strlen(argv[3]) +1;

    rc = (dbfile->get)(dbfile,&key,&result,0);

    if (rc != 0) {
      fprintf(stderr,"Key %s not found\n",argv[3]);
      return 2;
    }
    printf("siz:%d, dat: %s\n",result.size,result.data);

    (dbfile->close)(dbfile);

    return 0;
  }
#endif
}
