/*************************************************************************************************
 * sdbmtest
 * Writing test of SDBM
 *************************************************************************************************/


#include <sdbm.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define LOOPNUM        1000000
#define DBNAME         "casket.sdbm"


/* function prototypes */
int main(int argc, char **argv);


/* main routine */
int main(int argc, char **argv){
  DBM *db;
  int i, len;
  char buf[32];
  datum key, val;
  if(!(db = dbm_open(DBNAME, O_RDWR | O_CREAT | O_TRUNC, 00644))){
    fprintf(stderr, "%s: %s: dbm_open failed\n", argv[0], DBNAME);
    exit(1);
  }
  for(i = 1; i <= LOOPNUM; i++){
    len = sprintf(buf, "%08d", i);
    key.dptr = buf;
    key.dsize = len;
    val.dptr = buf;
    val.dsize = len;
    if(dbm_store(db, key, val, DBM_REPLACE) < 0){
      fprintf(stderr, "%s: %s: dbm_store failed\n", argv[0], DBNAME);
      break;
    }
    if(LOOPNUM > 250 && i % (LOOPNUM / 250) == 0){
      putchar('.');
      fflush(stdout);
      if(i == LOOPNUM || i % (LOOPNUM / 10) == 0){
        printf(" (%08d)\n", i);
        fflush(stdout);
      }
    }
  }
  dbm_close(db);
  return 0;
}



/* END OF FILE */
