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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)bflpop.c    1.4 - 90/06/20" */
  5.  
  6. /* ansi headers */
  7. #include <errno.h>
  8. /*#include <stddef.h>*/
  9.  
  10. /* local headers */
  11. #include "blkio_.h"
  12.  
  13. /*man---------------------------------------------------------------------------
  14. NAME
  15.      bflpop - pop block off of free list
  16.  
  17. SYNOPSIS
  18.      #include <blkio.h>
  19.  
  20.      int bflpop(bp, bnp)
  21.      BLKFILE *bp;
  22.      bpos_t *bnp;
  23.  
  24. DESCRIPTION
  25.      The bflpop function gets a block out of the free list of the
  26.      block file associated with BLKFILE pointer bp.  If the free list
  27.      is empty, the block number of the first block past the end of
  28.      file is retrieved.  The block number of the new block is returned
  29.      in the location pointed to by bnp.
  30.  
  31.      Note that if the free list is empty, the same end block will be
  32.      retrieved by flpop until that block is written, extending the
  33.      file length.
  34.  
  35.      bflpop will fail if one or more of the following is true:
  36.  
  37.      [EINVAL]       bp is not a valid BLKFILE pointer.
  38.      [EINVAL]       bnp is the NULL pointer.
  39.      [BEEOF]        bp is empty.
  40.      [BEEOF]        The free list head past points past the end of the
  41.                     file.
  42.      [BENFL]        bp does not have a free list.
  43.      [BENOPEN]      bp is not open for writing.
  44.  
  45. SEE ALSO
  46.      bflpush.
  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.      To use the free list functions in the blkio library, the first
  54.      element of the file header must be the free list head with type
  55.      bpos_t.  This must be initialized to 0 immediately after the file
  56.      is created and not accessed afterward except using bflpop and
  57.      bflpush.  Also, the block size must be at least sizeof(bpos_t).
  58.  
  59. ------------------------------------------------------------------------------*/
  60. int bflpop(bp, bnp)
  61. BLKFILE *bp;
  62. bpos_t *bnp;
  63. {
  64.     bpos_t oldflhno = 0;
  65.     bpos_t newflhno = 0;
  66.  
  67.     /* validate arguments */
  68.     if (!b_valid(bp) || bnp == NULL) {
  69.         errno = EINVAL;
  70.         return -1;
  71.     }
  72.  
  73.     /* check if not open for writing */
  74.     if (!(bp->flags & BIOWRITE)) {
  75.         errno = BENOPEN;
  76.         return -1;
  77.     }
  78.  
  79.     /* check if no free list */
  80.     if (bp->hdrsize < sizeof(oldflhno) || bp->blksize < sizeof(oldflhno)) {
  81.         errno = BENFL;
  82.         return -1;
  83.     }
  84.  
  85.     /* check if header not yet written */
  86.     if (bp->endblk < 1) {
  87.         errno = BEEOF;
  88.         return -1;
  89.     }
  90.  
  91.     /* get block number of current free list head */
  92.     if (bgethf(bp, (size_t)0, &oldflhno, sizeof(oldflhno)) == -1) {
  93.         BEPRINT;
  94.         return -1;
  95.     }
  96.     if (oldflhno >= bp->endblk) {
  97.         errno = BEEOF;
  98.         return -1;
  99.     }
  100.  
  101.     /* if free list empty, get new block at end of file */
  102.     if (oldflhno == 0) {
  103.         oldflhno = bp->endblk;
  104.     } else {    /* else get new free list head */
  105.         if (bgetbf(bp, oldflhno, (size_t)0, &newflhno, sizeof(newflhno)) == -1) {
  106.             BEPRINT;
  107.             return -1;
  108.         }
  109.         if (bputhf(bp, (size_t)0, &newflhno, sizeof(newflhno)) == -1) {
  110.             BEPRINT;
  111.             return -1;
  112.         }
  113.     }
  114.  
  115.     /* load return argument */
  116.     *bnp = oldflhno;
  117.  
  118.     errno = 0;
  119.     return 0;
  120. }
  121.