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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "cbclose.c    1.1 - 89/08/31" */
  5.  
  6. #include <btree.h>
  7. #include <errno.h>
  8. #include <lseq.h>
  9. /* #include <string.h> */
  10. #include "cbase_.h"
  11.  
  12. /*man---------------------------------------------------------------------------
  13. NAME
  14.      cbclose - close cbase
  15.  
  16. SYNOPSIS
  17.      #include <cbase.h>
  18.  
  19.      int cbclose(cbp)
  20.      cbase_t *cbp;
  21.  
  22. DESCRIPTION
  23.      The cbclose function causes any buffered data for cbase cbp to be
  24.      written out and the cbase to be closed.
  25.  
  26.      cbclose will fail if one or more of the following is true:
  27.  
  28.      [EINVAL]       cbp is not a valid cbase pointer.
  29.      [CBENOPEN]     cbp is not open.
  30.  
  31. SEE ALSO
  32.      cbcreate, cbopen, cbsync.
  33.  
  34. DIAGNOSTICS
  35.      Upon successful completion, a value of 0 is returned.  Otherwise, a
  36.      value of -1 is returned, and errno set to indicate the error.
  37.  
  38. ------------------------------------------------------------------------------*/
  39. int cbclose(cbp)
  40. cbase_t *cbp;
  41. {
  42.     int rs    = 0;
  43.     int i    = 0;
  44.  
  45.     errno = 0;
  46.  
  47.     /* validate input parameters */
  48.     if (!cb_valid(cbp)) {
  49.         errno = EINVAL;
  50.         return -1;
  51.     }
  52.  
  53.     /* check if not open */
  54.     if (!(cbp->flags & CBOPEN)) {
  55.         errno = CBENOPEN;
  56.         return -1;
  57.     }
  58.  
  59.     /* flush buffers and unlock file */
  60.     rs = cblock(cbp, CB_UNLCK);
  61.     if (rs == -1) {
  62.         CBEPRINT;
  63.         return -1;
  64.     }
  65.  
  66.     /* close record file */
  67.     rs = lsclose(cbp->lsp);
  68.     if (rs == -1) {
  69.         CBEPRINT;
  70.         return -1;
  71.     }
  72.  
  73.     /* close key files */
  74.     for (i = 1; i < cbp->fldcnt; i++) {
  75.         if (cbp->fields[i].flags & CBFKEY) {
  76.             rs = btclose(cbp->btp[i]);
  77.             if (rs == -1) {
  78.                 CBEPRINT;
  79.                 return -1;
  80.             }
  81.         }
  82.     }
  83.  
  84.     /* scrub slot in cbb table then free it */
  85.     memset((void *)cbp, 0, sizeof(cbb[0]));
  86.     cbp->flags = 0;
  87.  
  88.     errno = 0;
  89.     return 0;
  90. }
  91.