/*
    PROGRAM:  fixnbyte.c
    USAGE:  fixnbyte <database-name>

    This program fixes the block_dir_entry.block_nbytes and
    block_hdr.block_nbytes fields in a CODAS3 database to
    correctly reflect the size of the referenced block file.
    This fixes a bug in the old CODAS3 source that caused the
    block_hdr.block_nbytes field to be underreported by the
    size of the profile directory at load time, and to
    be overreported by some multiple of the profile directory
    size depending on what kind of updating has been done to
    the block file.

*/
#include "dbinc.h"
#include "use_db.h"

#if PROTOTYPE_ALLOWED
void main(int argc, char *argv[])
#else
void main(argc, argv)
int argc;
char *argv[];
#endif
{
   FILE_NAME_TYPE dbname;
   int iblkprf[2];

   if (argc < 2)
   {
      printf("\n Enter database name ==> ");
      gets(dbname);
   }
   else
      strcpy(dbname, argv[1]);
   if (dbopen_cnf(1, dbname, READ_WRITE, DIR_IN_MEMORY)) return;

   iblkprf[0] = iblkprf[1] = 0;
   do
   {
      fseek(db->fpblkdata, 0L, 2);
      db->block_hdr.block_nbytes = ftell(db->fpblkdata);
      db->block_dir_entry.block_nbytes = db->block_hdr.block_nbytes;
      write_block_hdr();
      write_block_dir_entry();
      ++iblkprf[0];
   } while (dbsrch_cnf(BLOCK_PROFILE_SEARCH, (char *) iblkprf) == 0);

   dbclose_cnf();
}
