home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / c / cbase.zoo / lseq101.zoo / lsdelcur.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-20  |  3.0 KB  |  137 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)lsdelcur.c    1.4 - 90/06/20" */
  5.  
  6. /* ansi headers */
  7. #include <errno.h>
  8.  
  9. /* library headers */
  10. #include <blkio.h>
  11.  
  12. /* local headers */
  13. #include "lseq_.h"
  14.  
  15. /*man---------------------------------------------------------------------------
  16. NAME
  17.      lsdelcur - delete current lseq record
  18.  
  19. SYNOPSIS
  20.      #include <lseq.h>
  21.  
  22.      int lsdelcur(lsp)
  23.      lseq_t *lsp;
  24.  
  25. DESCRIPTION
  26.      The lsdelcur function deletes the current record from lseq lsp.
  27.      The The cursor is positioned to the record following the deleted
  28.      record.
  29.  
  30.      lsdelcur will fail if one or more of the following is true:
  31.  
  32.      [EINVAL]       lsp is not a valid lseq pointer.
  33.      [LSELOCK]      lsp is not write locked.
  34.      [LSENOPEN]     lsp is not open.
  35.      [LSENREC]      The cursor is null.
  36.  
  37. SEE ALSO
  38.      lsinsert, lssearch.
  39.  
  40. DIAGNOSTICS
  41.      Upon successful completion, a value of 0 is returned.  Otherwise,
  42.      a value of -1 is returned, and errno set to indicate the error.
  43.  
  44. ------------------------------------------------------------------------------*/
  45. int lsdelcur(lsp)
  46. lseq_t * lsp;
  47. {
  48.     bpos_t bpos = 0;
  49.  
  50.     /* validate arguments */
  51.     if (!ls_valid(lsp)) {
  52.         errno = EINVAL;
  53.         return -1;
  54.     }
  55.  
  56.     /* check if not open */
  57.     if (!(lsp->flags & LSOPEN)) {
  58.         errno = LSENOPEN;
  59.         return -1;
  60.     }
  61.  
  62.     /* check if not write locked */
  63.     if (!(lsp->flags & LSWRLCK)) {
  64.         errno = LSELOCK;
  65.         return -1;
  66.     }
  67.  
  68.     /* check if cursor is null */
  69.     if (lsp->clspos == NIL) {
  70.         errno = LSENREC;
  71.         return -1;
  72.     }
  73.  
  74.     /* set modify bit in header */
  75.     lsp->lshdr.flags |= LSHMOD;
  76.     if (bputhf(lsp->bp, sizeof(bpos_t),
  77.         (void *)((char *)&lsp->lshdr + sizeof(bpos_t)),
  78.             sizeof(lshdr_t) - sizeof(bpos_t)) == -1) {
  79.         LSEPRINT;
  80.         return -1;
  81.     }
  82.     if (bsync(lsp->bp) == -1) {
  83.         LSEPRINT;
  84.         return -1;
  85.     }
  86.  
  87.     /* fix links */
  88.     if (lsp->clsrp->next == NIL) {
  89.         lsp->lshdr.last = lsp->clsrp->prev;
  90.     } else {
  91.         if (bputbf(lsp->bp, (bpos_t)lsp->clsrp->next, offsetof(lsrec_t, prev), &lsp->clsrp->prev, sizeof(lsp->clsrp->prev)) == -1) {
  92.             LSEPRINT;
  93.             return -1;
  94.         }
  95.     }
  96.     if (lsp->clsrp->prev == NIL) {
  97.         lsp->lshdr.first = lsp->clsrp->next;
  98.     } else {
  99.         if (bputbf(lsp->bp, (bpos_t)lsp->clsrp->prev, offsetof(lsrec_t, next), &lsp->clsrp->next, sizeof(lsp->clsrp->next)) == -1) {
  100.             LSEPRINT;
  101.             return -1;
  102.         }
  103.     }
  104.  
  105.     /* decrement record count */
  106.     lsp->lshdr.reccnt--;
  107.  
  108.     /* return block to free list */
  109.     bpos = lsp->clspos;
  110.     if (bflpush(lsp->bp, &bpos) == -1) {
  111.         LSEPRINT;
  112.         return -1;
  113.     }
  114.  
  115.     /* clear modify bit in header */
  116.     lsp->lshdr.flags &= ~LSHMOD;
  117.     if (bputhf(lsp->bp, sizeof(bpos_t),
  118.         (void *)((char *)&lsp->lshdr + sizeof(bpos_t)),
  119.             sizeof(lshdr_t) - sizeof(bpos_t)) == -1) {
  120.         LSEPRINT;
  121.         return -1;
  122.     }
  123.     if (bsync(lsp->bp) == -1) {
  124.         LSEPRINT;
  125.         return -1;
  126.     }
  127.  
  128.     /* position cursor to next record */
  129.     if (lsnext(lsp) == -1) {
  130.         LSEPRINT;
  131.         return -1;
  132.     }
  133.  
  134.     errno = 0;
  135.     return 0;
  136. }
  137.