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

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "rcops.c    1.1 - 89/07/03" */
  5.  
  6. #include <blkio.h>
  7. #include <errno.h>
  8. /* #include <stdlib.h> */
  9. #include "lseq_.h"
  10.  
  11. /*man---------------------------------------------------------------------------
  12. NAME
  13.      ls_rcalloc - allocate memory for lseq record
  14.  
  15. SYNOPSIS
  16.      #include "lseq_.h"
  17.  
  18.      lsrec_t *ls_rcalloc(lsp)
  19.      lseq_t *lsp;
  20.  
  21. DESCRIPTION
  22.      The ls_rcalloc function creates a record of the appropriate configuration
  23.      for lseq lsp and initializes it.  The address of the record created is
  24.      returned.
  25.  
  26.      ls_rcalloc 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_rcfree, ls_rcinit.
  35.  
  36. DIAGNOSTICS
  37.      On failure, a value of NULL is returned, and errno set to indicate
  38.      the error.
  39.  
  40. ------------------------------------------------------------------------------*/
  41. lsrec_t *ls_rcalloc(lsp)
  42. lseq_t *lsp;
  43. {
  44.     lsrec_t * lsrp = NULL;
  45.  
  46.     errno = 0;
  47.  
  48. #ifdef DEBUG
  49.     /* validate arguments */
  50.     if (!ls_valid(lsp)) {
  51.         LSEPRINT;
  52.         errno = EINVAL;
  53.         return NULL;
  54.     }
  55.  
  56.     /* check if not open */
  57.     if (!(lsp->flags & LSOPEN)) {
  58.         LSEPRINT;
  59.         errno = LSENOPEN;
  60.         return NULL;
  61.     }
  62. #endif
  63.  
  64.     /* allocate storage for main record structure */
  65.     /* (calloc is used throughout to automatically set all bits 0) */
  66.     lsrp = (lsrec_t *)calloc((size_t)1, sizeof(lsrec_t));
  67.     if (lsrp == NULL) {
  68.         LSEPRINT;
  69.         errno = ENOMEM;
  70.         return NULL;
  71.     }
  72.     lsrp->next = 0;
  73.     lsrp->prev = 0;
  74.     lsrp->recbuf = calloc((size_t)1, lsp->lshdr.recsize);
  75.     if (lsrp->recbuf == NULL) {
  76.         LSEPRINT;
  77.         free((void *)lsrp);
  78.         errno = ENOMEM;
  79.         return NULL;
  80.     }
  81.  
  82.     errno = 0;
  83.     return lsrp;
  84. }
  85.  
  86. /*man---------------------------------------------------------------------------
  87. NAME
  88.      ls_rccopy - copy lseq record
  89.  
  90. SYNOPSIS
  91.      #include "lseq_.h"
  92.  
  93.      int ls_rccopy(lsp, tlsrp, slsrp)
  94.      lseq_t *lsp;
  95.      lsrec_t *tlsrp;
  96.      lsrec_t *slsrp;
  97.  
  98. DESCRIPTION
  99.      The ls_rccopy function makes an exact copy of source record slsrp in
  100.      target record tlsrp.
  101.  
  102.      ls_rccopy will fail if one or more of the following is true:
  103.  
  104.      [EINVAL]       lsp is not a valid lseq pointer.
  105.      [EINVAL]       tlsrp or slsrp is the NULL pointer.
  106.  
  107. DIAGNOSTICS
  108.      Upon successful completion, a value of 0 is returned.  Otherwise, a
  109.      value of -1 is returned, and errno set to indicate the error.
  110.  
  111. ------------------------------------------------------------------------------*/
  112. int ls_rccopy(lsp, tlsrp, slsrp)
  113. lseq_t *  lsp;
  114. lsrec_t * tlsrp;
  115. lsrec_t * slsrp;
  116. {
  117.     errno = 0;
  118.  
  119. #ifdef DEBUG
  120.     /* validate arguments */
  121.     if ((!ls_valid(lsp)) || (tlsrp == NULL) || (slsrp == NULL)) {
  122.         LSEPRINT;
  123.         errno = EINVAL;
  124.         return -1;
  125.     }
  126. #endif
  127.  
  128.     /* copy record slsrp into tlsrp */
  129.     tlsrp->next = slsrp->next;
  130.     tlsrp->prev = slsrp->prev;
  131.     memcpy(tlsrp->recbuf, slsrp->recbuf, lsp->lshdr.recsize);
  132.  
  133.     errno = 0;
  134.     return 0;
  135. }
  136.  
  137. /*man---------------------------------------------------------------------------
  138. NAME
  139.      ls_rcfree - free memory allocated for lseq record
  140.  
  141. SYNOPSIS
  142.      #include "lseq_.h"
  143.  
  144.      void ls_rcfree(lsrp)
  145.      lsrec_t *lsrp;
  146.  
  147. DESCRIPTION
  148.      The ls_rcfree function frees all memory allocated for lseq record
  149.      lsrp.
  150.  
  151. SEE ALSO
  152.      ls_rcalloc.
  153.  
  154. ------------------------------------------------------------------------------*/
  155. void ls_rcfree(lsrp)
  156. lsrec_t *lsrp;
  157. {
  158.  
  159.     if (lsrp != NULL) {
  160.         if (lsrp->recbuf != NULL) {
  161.             free(lsrp->recbuf);
  162.             lsrp->recbuf = NULL;
  163.         }
  164.         free((void *)lsrp);
  165.     }
  166.  
  167.     return;
  168. }
  169.  
  170. /*man---------------------------------------------------------------------------
  171. NAME
  172.      ls_rcget - lseq record get
  173.  
  174. SYNOPSIS
  175.      #include "lseq_.h"
  176.  
  177.      int ls_rcget(lsp, lspos, lsrp)
  178.      lseq_t *lsp;
  179.      lspos_t lspos;
  180.      lsrec_t *lsrp;
  181.  
  182. DESCRIPTION
  183.      The ls_rcget function reads the record at position lspos into the record
  184.      pointed to be lsrp.  The entire record is read, including the links.
  185.  
  186. SEE ALSO
  187.      ls_rcput.
  188.  
  189. DIAGNOSTICS
  190.      Upon successful completion, a value of 0 is returned.  Otherwise, a
  191.      value of -1 is returned, and errno set to indicate the error.
  192.  
  193. ------------------------------------------------------------------------------*/
  194. int ls_rcget(lsp, lspos, lsrp)
  195. lseq_t *  lsp;
  196. lspos_t   lspos;
  197. lsrec_t * lsrp;
  198. {
  199.     int    rs    = 0;
  200.     void *    buf    = NULL;
  201.  
  202.     errno = 0;
  203.  
  204. #ifdef DEBUG
  205.     /* validate arguments */
  206.     if ((!ls_valid(lsp)) || (lsrp == NULL) || (lspos == 0)) {
  207.         LSEPRINT;
  208.         errno = EINVAL;
  209.         return -1;
  210.     }
  211.  
  212.     /* check if not open */
  213.     if (!(lsp->flags & LSOPEN)) {
  214.         LSEPRINT;
  215.         errno = LSENOPEN;
  216.         return -1;
  217.     }
  218. #endif
  219.  
  220.     /* read record from file */
  221.     buf = calloc(1, ls_blksize(lsp));
  222.     if (buf == NULL) {
  223.         LSEPRINT;
  224.         errno = ENOMEM;
  225.         return -1;
  226.     }
  227.     rs = bgetb(lsp->bp, (bpos_t)lspos, buf);
  228.     if (rs == -1) {
  229.         LSEPRINT;
  230.         free(buf);
  231.         return -1;
  232.     }
  233.  
  234.     /* convert record from file format */
  235.     memcpy((void *)lsrp, buf, offsetof(lsrec_t, recbuf));
  236.     memcpy(lsrp->recbuf,
  237.             (void *)((char *)buf + offsetof(lsrec_t, recbuf)),
  238.                             lsp->lshdr.recsize);
  239.  
  240.     /* free buffer */
  241.     free(buf);
  242.     buf = NULL;
  243.  
  244.     errno = 0;
  245.     return 0;
  246. }
  247.  
  248. /*man---------------------------------------------------------------------------
  249. NAME
  250.      ls_rcinit - lseq record initialize
  251.  
  252. SYNOPSIS
  253.      #include "lseq_.h"
  254.  
  255.      void ls_rcinit(lsp, lsrp)
  256.      lseq_t *lsp;
  257.      lsrec_t *lsrp;
  258.  
  259. DESCRIPTION
  260.      The ls_rcinit function initializes record lsrp.
  261.  
  262. ------------------------------------------------------------------------------*/
  263. void ls_rcinit(lsp, lsrp)
  264. lseq_t *  lsp;
  265. lsrec_t * lsrp;
  266. {
  267. #ifdef DEBUG
  268.     /* validate arguments */
  269.     if ((!ls_valid(lsp)) || (lsrp == NULL)) {
  270.         LSEPRINT;
  271.         return;
  272.     }
  273. #endif
  274.  
  275.     /* initialize lsrp */
  276.     lsrp->next = 0;
  277.     lsrp->prev = 0;
  278.     if (lsrp->recbuf == NULL) {
  279.         LSEPRINT;
  280.         return;
  281.     }
  282.  
  283.     memset(lsrp->recbuf, 0, lsp->lshdr.recsize);
  284.  
  285.     return;
  286. }
  287.  
  288. /*man---------------------------------------------------------------------------
  289. NAME
  290.      ls_rcput - lseq record put
  291.  
  292. SYNOPSIS
  293.      #include "lseq_.h"
  294.  
  295.      int ls_rcput(lsp, lspos, lsrp)
  296.      lseq_t *lsp;
  297.      lspos_t lspos;
  298.      lsrec_t *lsrp;
  299.  
  300. DESCRIPTION
  301.      The ls_rcput function writes the record pointed to by lsrp into record
  302.      position lspos.  The entire record is written, including the links.
  303.  
  304.      ls_rcput will fail if one or more of the following is true:
  305.  
  306.      [EINVAL]       lsp is not a valid lseq pointer.
  307.      [EINVAL]       lspos is less than 0.
  308.      [LSENOPEN]     lsp is not open.
  309.  
  310. SEE ALSO
  311.      ls_rcget, ls_rcputf.
  312.  
  313. DIAGNOSTICS
  314.      Upon successful completion, a value of 0 is returned.  Otherwise, a
  315.      value of -1 is returned, and errno set to indicate the error.
  316.  
  317. ------------------------------------------------------------------------------*/
  318. int ls_rcput(lsp, lspos, lsrp)
  319. lseq_t *  lsp;
  320. lspos_t   lspos;
  321. lsrec_t * lsrp;
  322. {
  323.     int    rs    = 0;
  324.     void *    buf    = NULL;
  325.  
  326.     errno = 0;
  327.  
  328. #ifdef BOOP
  329.     /* validate arguments */
  330.     if ((!ls_valid(lsp)) || (lspos < 1) || (lsrp == NULL)) {
  331.         LSEPRINT;
  332.         errno = EINVAL;
  333.         return -1;
  334.     }
  335.  
  336.     /* check if not open */
  337.     if (!(lsp->flags & LSOPEN)) {
  338.         LSEPRINT;
  339.         errno = LSENOPEN;
  340.         return -1;
  341.     }
  342. #endif
  343.  
  344.     /* convert record to file format */
  345.     buf = calloc(1, ls_blksize(lsp));
  346.     if (buf == NULL) {
  347.         LSEPRINT;
  348.         errno = ENOMEM;
  349.         return -1;
  350.     }
  351.     memcpy(buf, (void *)lsrp, offsetof(lsrec_t, recbuf));
  352.     memcpy((void *)((char *)buf + offsetof(lsrec_t, recbuf)), lsrp->recbuf,
  353.                  lsp->lshdr.recsize);
  354.  
  355.     /* write record to file */
  356.     rs = bputb(lsp->bp, (bpos_t)lspos, buf);
  357.     if (rs == -1) {
  358.         LSEPRINT;
  359.         free(buf);
  360.         return -1;
  361.     }
  362.  
  363.     /* free buffer */
  364.     free(buf);
  365.     buf = NULL;
  366.  
  367.     errno = 0;
  368.     return 0;
  369. }
  370.  
  371. /*man---------------------------------------------------------------------------
  372. NAME
  373.      ls_rcputf - lseq record field put
  374.  
  375. SYNOPSIS
  376.      #include "lseq_.h"
  377.  
  378.      int ls_rcputf(lsp, lspos, offset, buf, bufsize)
  379.      lseq_t *lsp;
  380.      lspos_t lspos;
  381.      size_t offset;
  382.      void *buf;
  383.      size_t bufsize;
  384.  
  385. DESCRIPTION
  386.      The ls_rcputf function writes the field pointed to by buf into record
  387.      position lspos.  Only the field is written.
  388.  
  389.      ls_rcputf will fail if one or more of the following is true:
  390.  
  391.      [EINVAL]       lsp is not a valid lseq pointer.
  392.      [EINVAL]       lspos is less than 0.
  393.      [LSEBOUND]
  394.      [LSENOPEN]
  395.  
  396. SEE ALSO
  397.      ls_rcget, ls_rcput.
  398.  
  399. DIAGNOSTICS
  400.      Upon successful completion, a value of 0 is returned.  Otherwise, a
  401.      value of -1 is returned, and errno set to indicate the error.
  402.  
  403. ------------------------------------------------------------------------------*/
  404. int ls_rcputf(lsp, lspos, offset, buf, bufsize)
  405. lseq_t * lsp;
  406. lspos_t  lspos;
  407. size_t   offset;
  408. void *   buf;
  409. size_t   bufsize;
  410. {
  411.     int rs = 0;
  412.  
  413.     errno = 0;
  414.  
  415. #ifdef DEBUG
  416.     /* validate arguments */
  417.     if ((!ls_valid(lsp)) || (lspos < 1) || (buf == NULL) || (bufsize < 1)) {
  418.         LSEPRINT;
  419.         errno = EINVAL;
  420.         return -1;
  421.     }
  422.  
  423.     /* check if not open */
  424.     if (!(lsp->flags & LSOPEN)) {
  425.         LSEPRINT;
  426.         errno = LSENOPEN;
  427.         return -1;
  428.     }
  429.  
  430.     /* check if record boundary crossed */
  431.     if ((offset + bufsize) > lsp->lshdr.recsize) {
  432.         LSEPRINT;
  433.         errno = LSEBOUND;
  434.         return -1;
  435.     }
  436. #endif
  437.  
  438.     /* write record to file */
  439.     rs = bputbf(lsp->bp, (bpos_t)lspos, offsetof(lsrec_t, recbuf) + offset, buf, bufsize);
  440.     if (rs == -1) {
  441.         LSEPRINT;
  442.         free(buf);
  443.         return -1;
  444.     }
  445.  
  446.     errno = 0;
  447.     return 0;
  448. }
  449.