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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)bflpush.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.      bflpush - push block onto free list
  17.  
  18. SYNOPSIS
  19.      #include <blkio.h>
  20.  
  21.      int bflpush(bp, bnp)
  22.      BLKFILE *bp;
  23.      const bpos_t *bnp;
  24.  
  25. DESCRIPTION
  26.      The bflpush function puts the block number pointed to by bnp into
  27.      the free list of the block file associated with BLKFILE pointer
  28.      bp.  If the value pointed to by bnp is one block past the end of
  29.      the file, it is accepted but not actually added to the list.
  30.  
  31.      bflpush will fail if one or more of the following is true:
  32.  
  33.      [EINVAL]       bp is not a valid BLKFILE pointer.
  34.      [EINVAL]       bnp is the NULL pointer.
  35.      [EINVAL]       bnp points to a value less than 1.
  36.      [BEEOF]        bp is empty.
  37.      [BEEOF]        bnp points to a value farther than one block past
  38.                     the end of file.
  39.      [BENFL]        bp does not have a free list.
  40.      [BENOPEN]      bp is not open for writing.
  41.  
  42. SEE ALSO
  43.      bflpop.
  44.  
  45. DIAGNOSTICS
  46.      Upon successful completion, a value of 0 is returned.  Otherwise,
  47.      a value of -1 is returned, and errno set to indicate the error.
  48.  
  49. NOTES
  50.      To use the free list functions in the blkio library, the first
  51.      element of the file header must be the free list head with type
  52.      bpos_t.  This must be initialized to 0 immediately after the file
  53.      is created and not accessed afterward except using bflpush and
  54.      bflpush.  Also, the block size must be at least sizeof(bpos_t).
  55.  
  56. ------------------------------------------------------------------------------*/
  57. int bflpush(bp, bnp)
  58. BLKFILE *bp;
  59. const bpos_t *bnp;
  60. {
  61.     bpos_t oldflhno = 0;    /* old free list head number */
  62.     bpos_t newflhno = 0;    /* new free list head number */
  63.     void *buf = NULL;
  64.  
  65.     /* validate arguments */
  66.     if (!b_valid(bp) || bnp == NULL) {
  67.         errno = EINVAL;
  68.         return -1;
  69.     }
  70.     if (*bnp < 1) {
  71.         errno = EINVAL;
  72.         return -1;
  73.     }
  74.  
  75.     /* check if not open */
  76.     if (!(bp->flags & BIOWRITE)) {
  77.         errno = BENOPEN;
  78.         return -1;
  79.     }
  80.  
  81.     /* check if no free list */
  82.     if (bp->hdrsize < sizeof(oldflhno) || bp->blksize < sizeof(oldflhno)) {
  83.         errno = BENFL;
  84.         return -1;
  85.     }
  86.  
  87.     /* check if header not yet written */
  88.     if (bp->endblk < 1) {
  89.         errno = BEEOF;
  90.         return -1;
  91.     }
  92.  
  93.     /* check if block past endblk */
  94.     if (*bnp > bp->endblk) {
  95.         errno = BEEOF;
  96.         return -1;
  97.     }
  98.  
  99.     /* check if block is endblk */
  100.     if (*bnp == bp->endblk) {
  101.         errno = 0;
  102.         return 0;
  103.     }
  104.  
  105.     /* get block number of current free list head */
  106.     if (bgethf(bp, (size_t)0, &oldflhno, sizeof(oldflhno)) == -1) {
  107.         BEPRINT;
  108.         return -1;
  109.     }
  110.  
  111.     /* link to rest of free list */
  112.     newflhno = *bnp;
  113.     buf = calloc((size_t)1, bp->blksize);
  114.     if (buf == NULL) {
  115.         BEPRINT;
  116.         errno = ENOMEM;
  117.         return -1;
  118.     }
  119.     memcpy(buf, &oldflhno, sizeof(oldflhno));
  120.     if (bputb(bp, newflhno, buf) == -1) {
  121.         BEPRINT;
  122.         free(buf);
  123.         buf = NULL;
  124.         return -1;
  125.     }
  126.     free(buf);
  127.     buf = NULL;
  128.  
  129.     /* write new free list head number to header */
  130.     if (bputhf(bp, (size_t)0, &newflhno, sizeof(newflhno)) == -1) {
  131.         BEPRINT;
  132.         return -1;
  133.     }
  134.  
  135.     errno = 0;
  136.     return 0;
  137. }
  138.