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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "bopen.c    1.1 - 89/07/03" */
  5.  
  6. #include <errno.h>
  7. /* #include <string.h> */
  8. #include "blkio_.h"
  9.  
  10. /* block file control structure table definition */
  11. BLKFILE biob[BOPEN_MAX];
  12.  
  13. /*man---------------------------------------------------------------------------
  14. NAME
  15.      bopen - open a block file
  16.  
  17. SYNOPSIS
  18.      #include <blkio.h>
  19.  
  20.      BLKFILE *bopen(filename, type, hdrsize, blksize, bufcnt)
  21.      char *filename;
  22.      char *type;
  23.      size_t hdrsize;
  24.      size_t blksize;
  25.      size_t bufcnt;
  26.  
  27. DESCRIPTION
  28.      The bopen function opens the file named by filename as a block file.
  29.      A pointer to the BLKFILE structure associated with file is returned.
  30.  
  31.      filename points to a character string that contains the name of the
  32.      file to be opened.
  33.  
  34.      type is a character string having one of the following values:
  35.  
  36.           "r"            open for reading
  37.           "r+"           open for update (reading and writing)
  38.           "w+"           truncate or create for update
  39.           "c"            create for update
  40.  
  41.      If type is "r" or "r+" and the file does not exist, bopen will
  42.      fail.  If type is "c" and the file already exists, bopen will fail.
  43.  
  44.      hdrsize is the size of the file header.  If there is no file header,
  45.      specify a value of 0 for hdrsize.
  46.  
  47.      blksize is the size of the blocks.
  48.  
  49.      bufcnt is the number of blocks to for which to create buffer storage.
  50.      For unbuffered operation, specify a value of 0 for bufcnt.
  51.  
  52.      bopen will fail if one or more of the following is true:
  53.  
  54.      [EEXIST]       type is "c" and the named file exists.
  55.      [EINVAL]       filename or type is the NULL pointer.
  56.      [EINVAL]       type is not "r", "r+", "w+", or "c".
  57.      [EINVAL]       blksize is 0.
  58.      [ENOENT]       type is "r" or "r+" and the named file
  59.                     does not exist.
  60.      [BEMFILE]      The maximum number of block files is
  61.                     already open.
  62.  
  63. SEE ALSO
  64.      bclose.
  65.  
  66. DIAGNOSTICS
  67.      bopen returns a NULL pointer on failure, and errno is set to
  68.      indicate the error.
  69.  
  70. ------------------------------------------------------------------------------*/
  71. BLKFILE *bopen(filename, type, hdrsize, blksize, bufcnt)
  72. char * filename;
  73. char * type;
  74. size_t hdrsize;
  75. size_t blksize;
  76. size_t bufcnt;
  77. {
  78.     int        rs    = 0;
  79.     BLKFILE *    bp    = NULL;
  80.     int        terrno    = 0;
  81.  
  82.     errno = 0;
  83.  
  84.     /* validate arguments */
  85.     if ((filename == NULL) || (type == NULL) || (blksize == 0)) {
  86.         errno = EINVAL;
  87.         return NULL;
  88.     }
  89.  
  90.     /* find free slot in biob table */
  91.     for (bp = biob; bp < (biob + BOPEN_MAX); bp++) {
  92.         if (!(bp->flags & BIOOPEN)) {
  93.             break;        /* found */
  94.         }
  95.     }
  96.     if (bp > (biob + BOPEN_MAX - 1)) {
  97.         errno = BEMFILE;
  98.         return NULL;        /* no free slots */
  99.     }
  100.  
  101.     /* set biob flags */
  102.     if (strcmp(type, BF_READ) == 0) {
  103.         bp->flags = BIOREAD;
  104.     } else if (strcmp(type, BF_RDWR) == 0) {
  105.         bp->flags = BIOREAD | BIOWRITE;
  106.     } else if (strcmp(type, BF_CREATE) == 0) {
  107.         bp->flags = BIOREAD | BIOWRITE;
  108.     } else if (strcmp(type, BF_CRTR) == 0) {
  109.         bp->flags = BIOREAD | BIOWRITE;
  110.     } else {
  111.         errno = EINVAL;
  112.         return NULL;
  113.     }
  114.  
  115.     /* open file */
  116.     rs = b_uopen(bp, filename, type);
  117.     if (rs == -1) {
  118.         if ((errno != EEXIST) && (errno != ENOENT)) BEPRINT;
  119.         memset((void *)bp, 0, sizeof(biob[0]));
  120.         bp->flags = 0;
  121.         return NULL;
  122.     }
  123.  
  124.     /* initialize */
  125.     bp->hdrsize = hdrsize;
  126.     bp->blksize = blksize;
  127.     bp->bufcnt = bufcnt;
  128.     bp->endblk = 0;
  129.     bp->most = 0;
  130.     bp->least = 0;
  131.     bp->block_p = NULL;
  132.     bp->blkbuf = NULL;
  133.     rs = b_uendblk(bp, &(bp->endblk));
  134.     if (rs == -1) {
  135.         if ((errno != BEBOUND) && (errno != BEEOF)) BEPRINT;
  136.         terrno = errno;
  137.         b_uclose(bp);
  138.         memset((void *)bp, 0, sizeof(biob[0]));
  139.         bp->flags = 0;
  140.         errno = terrno;
  141.         return NULL;
  142.     }
  143.     /* allocate memory for bp */
  144.     rs = b_alloc(bp);
  145.     if (rs == -1) {
  146.         BEPRINT;
  147.         terrno = errno;
  148.         b_uclose(bp);
  149.         memset((void *)bp, 0, sizeof(biob[0]));
  150.         bp->flags = 0;
  151.         errno = terrno;
  152.         return NULL;
  153.     }
  154.     /* initialize buffer storage */
  155.     rs = b_init(bp);
  156.     if (rs == -1) {
  157.         BEPRINT;
  158.         terrno = errno;
  159.         b_free(bp);
  160.         b_uclose(bp);
  161.         memset((void *)bp, 0, sizeof(biob[0]));
  162.         bp->flags = 0;
  163.         errno = terrno;
  164.         return NULL;
  165.     }
  166.  
  167.     errno = 0;
  168.     return bp;
  169. }
  170.