/*
 * dbbeginscan.c
 * File revision 0
 * Prepare to scan through a database file.
 * (c) 2000 Jacob Lundberg, jacob@chaos2.org
 */


/*
 * 2000.10.25	Initial implementation
 */


#include <stdio.h>
#include <errno.h>
#include "db.h"


int db_begin_scan(DB_FILE *db_fp) {
/*
 * db_begin_scan()
 * Reset a DB_FILE struct's fpos location to the start of the hash table.
 */

   db_header header;

   /* Check the database pointer. */
   if(!db_fp) {
      errno = EINVAL;
      return(-1);
   }

   /* Lock the database header for reading. */
   if(db_lock_read(db_fp->file, 0)) return(-1);

   /* Read in the database header. */
   if(fseek(db_fp->file, 0, SEEK_SET) || !fread(&header, DB_HEADER_SIZE, 1, db_fp->file))
      return(-1);

   /* Release the read lock. */
   db_unlock_read(db_fp->file, 0);

   /* Reset the locational info. */
   db_fp->fpos = header.trans_table + DB_LOCK_SIZE;
   db_fp->ldepth = 1;

   return(0);

}

