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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "cbopen.c    1.1 - 89/08/31" */
  5.  
  6. #include <blkio.h>
  7. #include <btree.h>
  8. #include <errno.h>
  9. #include <lseq.h>
  10. /* #include <string.h> */
  11. #include "cbase_.h"
  12.  
  13. /* cbase control structure table definition */
  14. cbase_t cbb[CBOPEN_MAX];
  15.  
  16. /*man---------------------------------------------------------------------------
  17. NAME
  18.      cbopen - open a cbase
  19.  
  20. SYNOPSIS
  21.      #include <cbase.h>
  22.  
  23.      cbase_t *cbopen(cbname, type, fields, fldcnt)
  24.      char *cbname;
  25.      char *type;
  26.      cbfield_t fields[];
  27.      size_t fldcnt;
  28.  
  29. DESCRIPTION
  30.      The cbopen function opens the cbase named by cbname.  A pointer to
  31.      the cbase_t structure associated with the cbase is returned.
  32.  
  33.      cbname points to a character string that contains the name of the
  34.      cbase to be opened.
  35.  
  36.      type is a character string having one of the following values:
  37.           "r"            open for reading
  38.           "r+"           open for update (reading and writing)
  39.  
  40.      See cbcreate for an explanation of the field definition array
  41.      fields and the number of fields fldcnt.
  42.  
  43.      cbopen will fail if one or more of the following is true:
  44.  
  45.      [EINVAL]       cbname is the NULL pointer.
  46.      [EINVAL]       type is not "r" or "r+".
  47.      [CBECORRUPT]   A file in the named cbase is corrupt.
  48.      [CBEMFILE]     Too many open cbases.  The maximum
  49.                     is defined as CBOPEN_MAX in cbase.h.
  50.  
  51. SEE ALSO
  52.      cbclose, cbcreate.
  53.  
  54. DIAGNOSTICS
  55.      cbopen returns a NULL pointer on failure, and errno is set to
  56.      indicate the error.
  57.  
  58. ------------------------------------------------------------------------------*/
  59. cbase_t *cbopen(cbname, type, fields, fldcnt)
  60. char *    cbname;
  61. char *    type;
  62. cbfield_t fields[];
  63. size_t    fldcnt;
  64. {
  65.     int        rs    = 0;
  66.     int        terrno    = 0;
  67.     cbase_t *    cbp    = NULL;
  68.     int        i    = 0;
  69.  
  70.     errno = 0;
  71.  
  72.     /* validate arguments */
  73.     if ((cbname == NULL) || (type == NULL)) {
  74.         errno = EINVAL;
  75.         return NULL;
  76.     }
  77.  
  78.     /* find free slot in cbb table */
  79.     for (cbp = cbb; cbp < (cbb + CBOPEN_MAX); cbp++) {
  80.         if (!(cbp->flags & CBOPEN)) {
  81.             break;        /* found */
  82.         }
  83.     }
  84.     if (cbp >= cbb + CBOPEN_MAX) {
  85.         errno = CBEMFILE;
  86.         return NULL;        /* no free slots */
  87.     }
  88.  
  89.     /* open record file */
  90.     if (strcmp(type, CB_READ) == 0) {
  91.         cbp->flags = CBREAD;
  92.     } else if (strcmp(type, CB_RDWR) == 0) {
  93.         cbp->flags = CBREAD | CBWRITE;
  94.     } else {
  95.         errno = EINVAL;
  96.         return NULL;
  97.     }
  98.     cbp->lsp = lsopen(cbname, type);
  99.     if (cbp->lsp == NULL) {
  100.         if ((errno != ENOENT) && (errno != LSECORRUPT)) CBEPRINT;
  101.         if (errno == LSECORRUPT) errno = CBECORRUPT;
  102.         memset((void *)cbp, 0, sizeof(cbb[0]));
  103.         cbp->flags = 0;
  104.         return NULL;
  105.     }
  106.     if (lsrecsize(cbp->lsp) < sizeof(lspos_t)) {
  107.         if ((errno != ENOENT) && (errno != LSECORRUPT)) CBEPRINT;
  108.         if (errno == LSECORRUPT) errno = CBECORRUPT;
  109.         memset((void *)cbp, 0, sizeof(cbb[0]));
  110.         cbp->flags = 0;
  111.         return NULL;
  112.     }
  113.  
  114.     /* validate arguments */
  115.     if (!cb_fdlvalid(fields, fldcnt, lsrecsize(cbp->lsp))) {
  116.         lsclose(cbp->lsp);
  117.         memset((void *)cbp, 0, sizeof(cbb[0]));
  118.         cbp->flags = 0;
  119.         errno = EINVAL;
  120.         return NULL;
  121.     }
  122.  
  123.     /* copy field definitions into cbase structure */
  124.     cbp->fldcnt = fldcnt;
  125.     memset((void *)cbp->fields, 0, sizeof(cbp->fields));
  126.     memcpy((void *)cbp->fields, (void *)fields, cbp->fldcnt * sizeof(cbp->fields[0]));
  127.  
  128.     /* open key files */
  129.     for (i = 0; i < cbp->fldcnt; i++) {
  130.         if (cbp->fields[i].flags & CBFKEY) {
  131.             cbp->btp[i] = btopen(cbp->fields[i].filename, type,
  132.                     cbcmp_p[cbp->fields[i].type - 1]);
  133.             if (cbp->btp[i] == NULL) {
  134.                 if ((errno != ENOENT) && (errno != LSECORRUPT))
  135.                     CBEPRINT;
  136.                 if (errno == BTECORRUPT) errno = CBECORRUPT;
  137.                 terrno = errno;
  138.                 cbclose(cbp);
  139.                 memset((void *)cbp, 0, sizeof(cbb[0]));
  140.                 cbp->flags = 0;
  141.                 errno = terrno;
  142.                 return NULL;
  143.             }
  144.         }
  145.     }
  146.  
  147.     errno = 0;
  148.     return cbp;
  149. }
  150.