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

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