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

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