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

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