home *** CD-ROM | disk | FTP | other *** search
/ Frostbyte's 1980s DOS Shareware Collection / floppyshareware.zip / floppyshareware / DOOG / CBASE09.ZIP / BTREE.ZIP / BTCLOSE.C < prev    next >
Text File  |  1989-08-31  |  2KB  |  81 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "btclose.c    1.1 - 89/07/03" */
  5.  
  6. #include <blkio.h>
  7. #include <errno.h>
  8. /* #include <string.h> */
  9. #include "btree_.h"
  10.  
  11. /*man---------------------------------------------------------------------------
  12. NAME
  13.      btclose - close btree
  14.  
  15. SYNOPSIS
  16.      #include <btree.h>
  17.  
  18.      int btclose(btp)
  19.      btree_t *btp;
  20.  
  21. DESCRIPTION
  22.      The btclose function causes any buffered data for the named btree to
  23.      be written out, the file unlocked, and the btree to be closed.
  24.  
  25.      btclose will fail if one or more of the following is true:
  26.  
  27.      [EINVAL]       btp is not a valid btree pointer.
  28.      [BTENOPEN]     btp is not open.
  29.  
  30. SEE ALSO
  31.      btcreate, btlock, btopen, btsync.
  32.  
  33. DIAGNOSTICS
  34.      Upon successful completion, a value of 0 is returned.  Otherwise, a
  35.      value of -1 is returned, and errno set to indicate the error.
  36.  
  37. ------------------------------------------------------------------------------*/
  38. int btclose(btp)
  39. btree_t *btp;
  40. {
  41.     int rs = 0;
  42.  
  43.     errno = 0;
  44.  
  45.     /* validate arguments */
  46.     if (!bt_valid(btp)) {
  47.         errno = EINVAL;
  48.         return -1;
  49.     }
  50.  
  51.     /* check if not open */
  52.     if (!(btp->flags & BTOPEN)) {
  53.         errno = BTENOPEN;
  54.         return -1;
  55.     }
  56.  
  57.     /* synchronize file with buffers and unlock file */
  58.     rs = btlock(btp, BT_UNLCK);
  59.     if (rs == -1) {
  60.         BTEPRINT;
  61.         return -1;
  62.     }
  63.  
  64.     /* free memory allocated for btree */
  65.     bt_free(btp);
  66.  
  67.     /* close btree file */
  68.     rs = bclose(btp->bp);
  69.     if (rs == -1) {
  70.         BTEPRINT;
  71.         return -1;
  72.     }
  73.  
  74.     /* scrub slot in btb table then free it */
  75.     memset((void *)btp, 0, sizeof(btb[0]));
  76.     btp->flags = 0;
  77.  
  78.     errno = 0;
  79.     return 0;
  80. }
  81.