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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)bgethf.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.      bgethf - get a header field from a block file
  17.  
  18. SYNOPSIS
  19.      #include <blkio.h>
  20.  
  21.      int bgethf(bp, offset, buf, bufsize)
  22.      BLKFILE *bp;
  23.      size_t offset;
  24.      void *buf;
  25.      size_t bufsize;
  26.  
  27. DESCRIPTION
  28.      The bgethf function reads a field from the header in the block
  29.      file associated with BLKFILE pointer bp into buf.  The field
  30.      begins offset characters from the beginning of the header and is
  31.      bufsize characters long.  buf must point to a storage area at
  32.      least bufsize characters long.
  33.  
  34.      bgethf will fail if one or more of the following is true:
  35.  
  36.      [EINVAL]       bp is not a valid BLKFILE pointer.
  37.      [EINVAL]       bn or bufsize is less than 1.
  38.      [EINVAL]       buf is the NULL pointer.
  39.      [EINVAL]       offset + bufsize extends beyond the
  40.                     boundary of the header.
  41.      [BEEOF]        bp is empty.
  42.      [BEEOF]        End of file encountered within header.
  43.      [BENOPEN]      bp is not open for reading.
  44.  
  45. SEE ALSO
  46.      bgeth, bgetbf, bputhf.
  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. ------------------------------------------------------------------------------*/
  53. int bgethf(bp, offset, buf, bufsize)
  54. BLKFILE *bp;
  55. size_t offset;
  56. void *buf;
  57. size_t bufsize;
  58. {
  59.     /* validate arguments */
  60.     if (!b_valid(bp) || buf == NULL || bufsize < 1) {
  61.         errno = EINVAL;
  62.         return -1;
  63.     }
  64.  
  65.     /* check if not open for reading */
  66.     if (!(bp->flags & BIOREAD)) {
  67.         errno = BENOPEN;
  68.         return -1;
  69.     }
  70.  
  71.     /* check if block boundary is crossed */
  72.     if ((offset + bufsize) > bp->hdrsize) {
  73.         errno = EINVAL;
  74.         return -1;
  75.     }
  76.  
  77.     /* check if header not yet written */
  78.     if (bp->endblk < 1) {
  79.         errno = BEEOF;
  80.         return -1;
  81.     }
  82.  
  83.     /* check if not buffered */
  84.     if (bp->bufcnt == 0) {
  85.         if (b_ugetf(bp, (bpos_t)0, offset, buf, bufsize) == -1) {
  86.             BEPRINT;
  87.             return -1;
  88.         }
  89.         errno = 0;
  90.         return 0;
  91.     }
  92.  
  93.     /* check if buffer is not loaded */
  94.     if (!(b_blockp(bp, (size_t)0)->flags & BLKREAD)) {
  95.         b_blockp(bp, (size_t)0)->bn = 0; /* read header from file */
  96.         if (b_get(bp, (size_t)0) == -1) {
  97.             BEPRINT;
  98.             return -1;
  99.         }
  100.     }
  101.  
  102.     /* copy from block buffer into buf */
  103.     memcpy(buf, ((char *)b_blkbuf(bp, (size_t)0) + offset), bufsize);
  104.  
  105.     errno = 0;
  106.     return 0;
  107. }
  108.