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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)bputhf.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.      bputhf - put a header field into a block file
  17.  
  18. SYNOPSIS
  19.      #include <blkio.h>
  20.  
  21.      int bputhf(bp, offset, buf, bufsize)
  22.      BLKFILE *bp;
  23.      size_t offset;
  24.      const void *buf;
  25.      size_t bufsize;
  26.  
  27. DESCRIPTION
  28.      The bputhf function writes the contents of buf into a field of
  29.      the header of the block file associated with BLKFILE pointer bp.
  30.      The field begins offset characters from the beginning of the
  31.      header and is bufsize characters long.  buf must point to a
  32.      storage area at least bufsize characters long.
  33.  
  34.      bputhf will fail if one or more of the following is true:
  35.  
  36.      [EINVAL]       bp is not a valid BLKFILE pointer.
  37.      [EINVAL]       bufsize is less than 1.
  38.      [EINVAL]       buf is the NULL pointer.
  39.      [BEBOUND]      offset + bufsize extends beyond the
  40.                     boundary of the header.
  41.      [BEEOF]        Attempt to write a field before the
  42.                     complete header has been written.
  43.      [BENOPEN]      bp is not open for writing.
  44.  
  45. SEE ALSO
  46.      bgethf, bputh, bputbf.
  47.  
  48. DIAGNOSTICS
  49.      Upon successful completion, a value of 0 is returned.  Otherwise,
  50.      a value of -1 is returned, and errno set to indicate the error.
  51.  
  52. NOTES
  53.      When the header is created,  the entire header must be written,
  54.      so offset must have a value of 0 and bufsize must be equal to the
  55.      block size.
  56.  
  57. ------------------------------------------------------------------------------*/
  58. int bputhf(bp, offset, buf, bufsize)
  59. BLKFILE *bp;
  60. size_t offset;
  61. const void *buf;
  62. size_t bufsize;
  63. {
  64.     /* validate arguments */
  65.     if (!b_valid(bp) || buf == NULL || bufsize < 1) {
  66.         errno = EINVAL;
  67.         return -1;
  68.     }
  69.  
  70.     /* check if not open for reading */
  71.     if (!(bp->flags & BIOWRITE)) {
  72.         errno = BENOPEN;
  73.         return -1;
  74.     }
  75.  
  76.     /* check if block boundary is crossed */
  77.     if ((offset + bufsize) > bp->hdrsize) {
  78.         errno = BEBOUND;
  79.         return -1;
  80.     }
  81.  
  82.     /* check if incomplete header */
  83.     if (offset != 0 || bufsize != bp->hdrsize) {
  84.         if (bp->endblk < 1) {
  85.             errno = BEEOF;
  86.             return -1;
  87.         }
  88.     }
  89.  
  90.     /* check if not buffered */
  91.     if (bp->bufcnt == 0) {
  92.         if (b_uputf(bp, (bpos_t)0, offset, buf, bufsize) == -1) {
  93.             BEPRINT;
  94.             return -1;
  95.         }
  96.         if (bp->endblk < 1) {
  97.             bp->endblk = 1;
  98.         }
  99.         errno = 0;
  100.         return 0;
  101.     }
  102.  
  103.     /* check if buffer is not loaded */
  104.     if (!(b_blockp(bp, (size_t)0)->flags & BLKREAD)) {
  105.         if (offset != 0 || bufsize != bp->hdrsize) {
  106.             if (b_get(bp, (size_t)0) == -1) {    /* read block from file */
  107.                 if (errno != BEEOF) BEPRINT;
  108.                 return -1;
  109.             }
  110.         }
  111.     }
  112.  
  113.     /* copy from buf into block buffer and set flags */
  114.     memcpy(((char *)b_blkbuf(bp, (size_t)0) + offset), buf, bufsize);
  115.     b_blockp(bp, (size_t)0)->flags = BLKREAD | BLKWRITE;
  116.  
  117.     /* adjust endblk */
  118.     if (bp->endblk < 1) {
  119.         bp->endblk = 1;
  120.     }
  121.  
  122.     errno = 0;
  123.     return 0;
  124. }
  125.