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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "lsops.c    1.1 - 89/07/03" */
  5.  
  6. #include <blkio.h>
  7. #include <bool.h>
  8. #include <errno.h>
  9. /* #include <stdlib.h> */
  10. #include "lseq_.h"
  11.  
  12. /*man---------------------------------------------------------------------------
  13. NAME
  14.      ls_alloc - allocate memory for a lseq
  15.  
  16. SYNOPSIS
  17.      #include "lseq_.h"
  18.  
  19.      int ls_alloc(lsp);
  20.      lseq_t *lsp;
  21.  
  22. DESCRIPTION
  23.      The ls_alloc function allocates the memory needed by lseq lsp.  The
  24.      memory is initialized to 0.
  25.  
  26.      ls_alloc will fail if one or more of the following is true:
  27.  
  28.      [EINVAL]       lsp is not a valid lseq pointer.
  29.      [ENOMEM]       Not enough memory is available for the
  30.                     calling process to allocate.
  31.      [LSENOPEN]     lsp is not open.
  32.  
  33. SEE ALSO
  34.      ls_free.
  35.  
  36. DIAGNOSTICS
  37.      Upon successful completion, a value of 0 is returned.  Otherwise, a
  38.      value of -1 is returned, and errno set to indicate the error.
  39.  
  40. ------------------------------------------------------------------------------*/
  41. int ls_alloc(lsp)
  42. lseq_t *lsp;
  43. {
  44.     errno = 0;
  45.  
  46. #ifdef DEBUG
  47.     /* validate arguments */
  48.     if (!ls_valid(lsp)) {
  49.         LSEPRINT;
  50.         errno = EINVAL;
  51.         return -1;
  52.     }
  53.  
  54.     /* check if not open */
  55.     if (!(lsp->flags & LSOPEN)) {
  56.         LSEPRINT;
  57.         errno = LSENOPEN;
  58.         return -1;
  59.     }
  60. #endif
  61.  
  62.     /* free any previously allocated storage */
  63.     ls_free(lsp);
  64.  
  65.     /* current record */
  66.     lsp->clsrp = ls_rcalloc(lsp);
  67.     if (lsp->clsrp == NULL) {
  68.         LSEPRINT;
  69.         return -1;
  70.     }
  71.  
  72.     errno = 0;
  73.     return 0;
  74. }
  75.  
  76. /*man---------------------------------------------------------------------------
  77. NAME
  78.      ls_blksize - lseq block size
  79.  
  80. SYNOPSIS
  81.      #include <lseq.h>
  82.  
  83.      void *ls_blksize(lsp)
  84.      lseq_t *lsp;
  85.  
  86. DESCRIPTION
  87.      ls_blksize returns the size of the blocks in lseq lsp.  If lsp is not
  88.      a valid open lseq, the results are undefined.  ls_blksize is a macro.
  89.  
  90. ------------------------------------------------------------------------------*/
  91.  
  92. /* ls_blksize #defined in lseq_.h. */
  93.  
  94. /*man---------------------------------------------------------------------------
  95. NAME
  96.      ls_free - free memory allocated for an lseq
  97.  
  98. SYNOPSIS
  99.      #include "lseq_.h"
  100.  
  101.      void ls_free(lsp)
  102.      lseq_t *lsp;
  103.  
  104. DESCRIPTION
  105.      The ls_free function frees all memory allocated for lseq lsp.
  106.      If lsp is not a valid lseq, no action is taken.
  107.  
  108. SEE ALSO
  109.      ls_alloc.
  110.  
  111. ------------------------------------------------------------------------------*/
  112. void ls_free(lsp)
  113. lseq_t *lsp;
  114. {
  115.     /* validate arguments */
  116.     if (!ls_valid(lsp)) {
  117.         LSEPRINT;
  118.         return;
  119.     }
  120.  
  121.     /* free memory */
  122.     if (lsp->clsrp != NULL) {
  123.         ls_rcfree(lsp->clsrp);
  124.         lsp->clsrp = NULL;
  125.     }
  126.  
  127.     return;
  128. }
  129.  
  130. /*man---------------------------------------------------------------------------
  131. NAME
  132.      ls_valid - validate lseq
  133.  
  134. SYNOPSIS
  135.      #include "lseq_.h"
  136.  
  137.      bool ls_valid(lsp)
  138.      lseq_t *lsp;
  139.  
  140. DESCRIPTION
  141.      The ls_valid function determines if lsp points to a valid lseq
  142.      control structure.  If it is valid, TRUE is returned.  If not,
  143.      then FALSE is returned.
  144.  
  145. ------------------------------------------------------------------------------*/
  146. bool ls_valid(lsp)
  147. lseq_t *lsp;
  148. {
  149.     if ((lsp < lsb) || (lsp > (lsb + LSOPEN_MAX - 1))) {
  150.         return FALSE;
  151.     }
  152.     if (((size_t)((char *)lsp - (char *)lsb)) % sizeof(lsb[0]) != 0) {
  153.         return FALSE;
  154.     }
  155.  
  156.     return TRUE;
  157. }
  158.