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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "bgetbf.c    1.1 - 89/07/03" */
  5.  
  6. #include <errno.h>
  7. /* #include <string.h> */
  8. #include "blkio_.h"
  9.  
  10. /*man---------------------------------------------------------------------------
  11. NAME
  12.      bgetbf - get a block field from a block file
  13.  
  14. SYNOPSIS
  15.      #include <blkio.h>
  16.  
  17.      int bgetbf(bp, bn, offset, buf, bufsize)
  18.      BLKFILE *bp;
  19.      bpos_t bn;
  20.      size_t offset;
  21.      void *buf;
  22.      size_t bufsize;
  23.  
  24. DESCRIPTION
  25.      The bgetbf function reads a field from block number bn in the block file
  26.      associated with BLKFILE pointer bp into buf.  The field begins offset
  27.      characters from the beginning of the block and is bufsize characters
  28.      long.  buf must point to a storage area at least bufsize characters long.
  29.      Block numbering starts at 1.
  30.  
  31.      bgetbf will fail if one or more of the following is true:
  32.  
  33.      [EINVAL]       bp is not a valid BLKFILE pointer.
  34.      [EINVAL]       bn or bufsize is less than 1.
  35.      [EINVAL]       buf is the NULL pointer.
  36.      [BEBOUND]      offset + bufsize extends beyond the boundary
  37.                     of block bn.
  38.      [BEBOUND]      End of file not on block boundary.
  39.      [BEEOF]        There are not bn blocks in the file.
  40.      [BENOPEN]      bp is not open for reading.
  41.  
  42. SEE ALSO
  43.      bgetb, bgethf, bputbf.
  44.  
  45. DIAGNOSTICS
  46.      Upon successful completion, a value of 0 is returned.  Otherwise, a
  47.      value of -1 is returned, and errno set to indicate the error.
  48.  
  49. ------------------------------------------------------------------------------*/
  50. int bgetbf(bp, bn, offset, buf, bufsize)
  51. BLKFILE * bp;
  52. bpos_t    bn;
  53. size_t    offset;
  54. void *    buf;
  55. size_t    bufsize;
  56. {
  57.     int    rs    = 0;
  58.     int    i    = 0;
  59.     size_t    bufno    = 0;
  60.  
  61.     errno = 0;
  62.  
  63.     /* validate arguments */
  64.     if (!b_valid(bp)) {
  65.         errno = EINVAL;
  66.         return -1;
  67.     }
  68.     if ((bn < 1) || (buf == NULL) || (bufsize < 1)) {
  69.         errno = EINVAL;
  70.         return -1;
  71.     }
  72.  
  73.     /* check if not open for reading */
  74.     if (!(bp->flags & BIOREAD)) {
  75.         errno = BENOPEN;
  76.         return -1;
  77.     }
  78.  
  79.     /* check if block boundary is crossed */
  80.     if ((offset + bufsize) > bp->blksize) {
  81.         errno = BEBOUND;
  82.         return -1;
  83.     }
  84.  
  85.     /* check if not bn blocks in file */
  86.     if (bn >= bp->endblk) {
  87.         errno = BEEOF;
  88.         return -1;
  89.     }
  90.  
  91.     /* check if not buffered */
  92.     if (bp->bufcnt == 0) {
  93.         rs = b_ugetf(bp, bn, offset, buf, bufsize);
  94.         if (rs == -1) {
  95.             BEPRINT;
  96.             return -1;
  97.         }
  98.         errno = 0;
  99.         return 0;
  100.     }
  101.  
  102.     /* search buffer list for block */
  103.     for (i = 1; i <= bp->bufcnt; i++) {
  104.         if ((b_block_p(bp, (size_t)i)->bn == bn)
  105.                    && (b_block_p(bp, (size_t)i)->flags & BLKREAD)) {
  106.             bufno = i;
  107.             break;
  108.         }
  109.     }
  110.  
  111.     /* if not found, use least recently used buffer */
  112.     if (bufno == 0) {
  113.         bufno = bp->least;
  114.         rs = b_put(bp, bufno);        /* flush previous contents */
  115.         if (rs == -1) {
  116.             BEPRINT;
  117.             return -1;
  118.         }
  119.         b_block_p(bp, bufno)->flags = 0;
  120.         b_block_p(bp, bufno)->bn = bn;
  121.         /* read block from file */
  122.         rs = b_get(bp, bufno);
  123.         if (rs == -1) {
  124.             BEPRINT;
  125.             return -1;
  126.         }
  127.     }
  128.  
  129.     /* copy from block buffer into buf */
  130.     memcpy(buf, (void *)((char *)b_blkbuf(bp, bufno) + offset), bufsize);
  131.  
  132.     /* move block buffer bufno to most recently used end of list */
  133.     rs = b_mkmru(bp, bufno);
  134.     if (rs == -1) {
  135.         BEPRINT;
  136.         return -1;
  137.     }
  138.  
  139.     errno = 0;
  140.     return 0;
  141. }
  142.