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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "bputbf.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.      bputbf - put a block field into a block file
  13.  
  14. SYNOPSIS
  15.      #include <blkio.h>
  16.  
  17.      int bputbf(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 bputbf function writes the contents of buf into a field of block
  26.      number bn of the block file associated with BLKFILE pointer bp.  The
  27.      field begins offset characters from the beginning of the block and is
  28.      bufsize characters long.  buf must point to a storage area at least
  29.      bufsize characters long.  Block numbering starts at 1.
  30.  
  31.      bputbf 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.      [BEEOF]        Partial block being written and block bn
  39.                     is past the end of file.
  40.      [BEEOF]        Complete block being written and block bn
  41.                     is more than 1 past the end of file.
  42.      [BENOPEN]      bp is not open for writing.
  43.  
  44. SEE ALSO
  45.      bgetbf, bputb, bputhf.
  46.  
  47. DIAGNOSTICS
  48.      Upon successful completion, a value of 0 is returned.  Otherwise, a
  49.      value of -1 is returned, and errno set to indicate the error.
  50.  
  51. NOTES
  52.      Whenever a new block is created at the end of the file, the entire
  53.      block must be written, so offset must have a value of 0 and bufsize
  54.      must be equal to the block size.
  55.  
  56. ------------------------------------------------------------------------------*/
  57. int bputbf(bp, bn, offset, buf, bufsize)
  58. BLKFILE * bp;
  59. bpos_t    bn;
  60. size_t    offset;
  61. void *    buf;
  62. size_t    bufsize;
  63. {
  64.     int    rs    = 0;
  65.     int    i    = 0;
  66.     size_t    bufno    = 0;
  67.  
  68.     errno = 0;
  69.  
  70.     /* validate arguments */
  71.     if (!b_valid(bp)) {
  72.         errno = EINVAL;
  73.         return -1;
  74.     }
  75.     if ((bn < 1) || (buf == NULL) || (bufsize < 1)) {
  76.         errno = EINVAL;
  77.         return -1;
  78.     }
  79.  
  80.     /* check if not open for writing */
  81.     if (!(bp->flags & BIOWRITE)) {
  82.         errno = BENOPEN;
  83.         return -1;
  84.     }
  85.  
  86.     /* check if block boundary is crossed */
  87.     if ((offset + bufsize) > bp->blksize) {
  88.         errno = BEBOUND;
  89.         return -1;
  90.     }
  91.  
  92.     /* check if past end of file */
  93.     if ((offset != 0) || (bufsize != bp->blksize)) {
  94.         if (bn >= bp->endblk) {
  95.             errno = BEEOF;
  96.             return -1;
  97.         }
  98.     } else {
  99.         if (bn > bp->endblk) {
  100.             errno = BEEOF;
  101.             return -1;
  102.         }
  103.     }
  104.  
  105.     /* check if not buffered */
  106.     if (bp->bufcnt == 0) {
  107.         rs = b_uputf(bp, bn, offset, buf, bufsize);
  108.         if (rs == -1) {
  109.             BEPRINT;
  110.             return -1;
  111.         }
  112.         if (bp->endblk <= bn) {
  113.             bp->endblk = bn + 1;
  114.         }
  115.         errno = 0;
  116.         return 0;
  117.     }
  118.  
  119.     /* search buffer list for block */
  120.     for (i = 1; i <= bp->bufcnt; i++) {
  121.         if ((b_block_p(bp, (size_t)i)->bn == bn)
  122.                    && (b_block_p(bp, (size_t)i)->flags & BLKREAD)) {
  123.             bufno = i;
  124.             break;
  125.         }
  126.     }
  127.  
  128.     /* if not found, use least recently used buffer */
  129.     if (bufno == 0) {
  130.         bufno = bp->least;
  131.         rs = b_put(bp, bufno);        /* flush previous contents */
  132.         if (rs == -1) {
  133.             BEPRINT;
  134.             return -1;
  135.         }
  136.         b_block_p(bp, bufno)->flags = 0;
  137.         b_block_p(bp, bufno)->bn = bn;
  138.         if ((offset != 0) || (bufsize != bp->blksize)) {
  139.             /* read block from file */
  140.             rs = b_get(bp, bufno);
  141.             if (rs == -1) {
  142.                 if (errno != BEEOF) BEPRINT;
  143.                 return -1;
  144.             }
  145.         }
  146.     }
  147.  
  148.     /* copy from buf into block buffer and set flags */
  149.     memcpy((void *)((char *)b_blkbuf(bp, bufno) + offset), buf, bufsize);
  150.     b_block_p(bp, bufno)->flags = BLKREAD | BLKWRITE;
  151.  
  152.     /* adjust endblk */
  153.     if (bp->endblk <= bn) {
  154.         bp->endblk = bn + 1;
  155.     }
  156.  
  157.     /* move block buffer bufno to most recently used end of list */
  158.     rs = b_mkmru(bp, bufno);
  159.     if (rs == -1) {
  160.         BEPRINT;
  161.         return -1;
  162.     }
  163.  
  164.     errno = 0;
  165.     return 0;
  166. }
  167.