home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / CBASE101.ZIP / BLKIO112.ZIP / BOPEN.C < prev    next >
Text File  |  1990-06-20  |  5KB  |  175 lines

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