home *** CD-ROM | disk | FTP | other *** search
/ Frostbyte's 1980s DOS Shareware Collection / floppyshareware.zip / floppyshareware / DOOG / CBASE09.ZIP / LSEQ.ZIP / LSDELCUR.C < prev    next >
Text File  |  1989-08-30  |  3KB  |  138 lines

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