home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frostbyte's 1980s DOS Shareware Collection
/
floppyshareware.zip
/
floppyshareware
/
DOOG
/
CBASE09.ZIP
/
LSEQ.ZIP
/
RCOPS.C
< prev
Wrap
Text File
|
1989-08-30
|
10KB
|
449 lines
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "rcops.c 1.1 - 89/07/03" */
#include <blkio.h>
#include <errno.h>
/* #include <stdlib.h> */
#include "lseq_.h"
/*man---------------------------------------------------------------------------
NAME
ls_rcalloc - allocate memory for lseq record
SYNOPSIS
#include "lseq_.h"
lsrec_t *ls_rcalloc(lsp)
lseq_t *lsp;
DESCRIPTION
The ls_rcalloc function creates a record of the appropriate configuration
for lseq lsp and initializes it. The address of the record created is
returned.
ls_rcalloc will fail if one or more of the following is true:
[EINVAL] lsp is not a valid lseq pointer.
[ENOMEM] Not enough memory is available for the
calling process to allocate.
[LSENOPEN] lsp is not open.
SEE ALSO
ls_rcfree, ls_rcinit.
DIAGNOSTICS
On failure, a value of NULL is returned, and errno set to indicate
the error.
------------------------------------------------------------------------------*/
lsrec_t *ls_rcalloc(lsp)
lseq_t *lsp;
{
lsrec_t * lsrp = NULL;
errno = 0;
#ifdef DEBUG
/* validate arguments */
if (!ls_valid(lsp)) {
LSEPRINT;
errno = EINVAL;
return NULL;
}
/* check if not open */
if (!(lsp->flags & LSOPEN)) {
LSEPRINT;
errno = LSENOPEN;
return NULL;
}
#endif
/* allocate storage for main record structure */
/* (calloc is used throughout to automatically set all bits 0) */
lsrp = (lsrec_t *)calloc((size_t)1, sizeof(lsrec_t));
if (lsrp == NULL) {
LSEPRINT;
errno = ENOMEM;
return NULL;
}
lsrp->next = 0;
lsrp->prev = 0;
lsrp->recbuf = calloc((size_t)1, lsp->lshdr.recsize);
if (lsrp->recbuf == NULL) {
LSEPRINT;
free((void *)lsrp);
errno = ENOMEM;
return NULL;
}
errno = 0;
return lsrp;
}
/*man---------------------------------------------------------------------------
NAME
ls_rccopy - copy lseq record
SYNOPSIS
#include "lseq_.h"
int ls_rccopy(lsp, tlsrp, slsrp)
lseq_t *lsp;
lsrec_t *tlsrp;
lsrec_t *slsrp;
DESCRIPTION
The ls_rccopy function makes an exact copy of source record slsrp in
target record tlsrp.
ls_rccopy will fail if one or more of the following is true:
[EINVAL] lsp is not a valid lseq pointer.
[EINVAL] tlsrp or slsrp is the NULL pointer.
DIAGNOSTICS
Upon successful completion, a value of 0 is returned. Otherwise, a
value of -1 is returned, and errno set to indicate the error.
------------------------------------------------------------------------------*/
int ls_rccopy(lsp, tlsrp, slsrp)
lseq_t * lsp;
lsrec_t * tlsrp;
lsrec_t * slsrp;
{
errno = 0;
#ifdef DEBUG
/* validate arguments */
if ((!ls_valid(lsp)) || (tlsrp == NULL) || (slsrp == NULL)) {
LSEPRINT;
errno = EINVAL;
return -1;
}
#endif
/* copy record slsrp into tlsrp */
tlsrp->next = slsrp->next;
tlsrp->prev = slsrp->prev;
memcpy(tlsrp->recbuf, slsrp->recbuf, lsp->lshdr.recsize);
errno = 0;
return 0;
}
/*man---------------------------------------------------------------------------
NAME
ls_rcfree - free memory allocated for lseq record
SYNOPSIS
#include "lseq_.h"
void ls_rcfree(lsrp)
lsrec_t *lsrp;
DESCRIPTION
The ls_rcfree function frees all memory allocated for lseq record
lsrp.
SEE ALSO
ls_rcalloc.
------------------------------------------------------------------------------*/
void ls_rcfree(lsrp)
lsrec_t *lsrp;
{
if (lsrp != NULL) {
if (lsrp->recbuf != NULL) {
free(lsrp->recbuf);
lsrp->recbuf = NULL;
}
free((void *)lsrp);
}
return;
}
/*man---------------------------------------------------------------------------
NAME
ls_rcget - lseq record get
SYNOPSIS
#include "lseq_.h"
int ls_rcget(lsp, lspos, lsrp)
lseq_t *lsp;
lspos_t lspos;
lsrec_t *lsrp;
DESCRIPTION
The ls_rcget function reads the record at position lspos into the record
pointed to be lsrp. The entire record is read, including the links.
SEE ALSO
ls_rcput.
DIAGNOSTICS
Upon successful completion, a value of 0 is returned. Otherwise, a
value of -1 is returned, and errno set to indicate the error.
------------------------------------------------------------------------------*/
int ls_rcget(lsp, lspos, lsrp)
lseq_t * lsp;
lspos_t lspos;
lsrec_t * lsrp;
{
int rs = 0;
void * buf = NULL;
errno = 0;
#ifdef DEBUG
/* validate arguments */
if ((!ls_valid(lsp)) || (lsrp == NULL) || (lspos == 0)) {
LSEPRINT;
errno = EINVAL;
return -1;
}
/* check if not open */
if (!(lsp->flags & LSOPEN)) {
LSEPRINT;
errno = LSENOPEN;
return -1;
}
#endif
/* read record from file */
buf = calloc(1, ls_blksize(lsp));
if (buf == NULL) {
LSEPRINT;
errno = ENOMEM;
return -1;
}
rs = bgetb(lsp->bp, (bpos_t)lspos, buf);
if (rs == -1) {
LSEPRINT;
free(buf);
return -1;
}
/* convert record from file format */
memcpy((void *)lsrp, buf, offsetof(lsrec_t, recbuf));
memcpy(lsrp->recbuf,
(void *)((char *)buf + offsetof(lsrec_t, recbuf)),
lsp->lshdr.recsize);
/* free buffer */
free(buf);
buf = NULL;
errno = 0;
return 0;
}
/*man---------------------------------------------------------------------------
NAME
ls_rcinit - lseq record initialize
SYNOPSIS
#include "lseq_.h"
void ls_rcinit(lsp, lsrp)
lseq_t *lsp;
lsrec_t *lsrp;
DESCRIPTION
The ls_rcinit function initializes record lsrp.
------------------------------------------------------------------------------*/
void ls_rcinit(lsp, lsrp)
lseq_t * lsp;
lsrec_t * lsrp;
{
#ifdef DEBUG
/* validate arguments */
if ((!ls_valid(lsp)) || (lsrp == NULL)) {
LSEPRINT;
return;
}
#endif
/* initialize lsrp */
lsrp->next = 0;
lsrp->prev = 0;
if (lsrp->recbuf == NULL) {
LSEPRINT;
return;
}
memset(lsrp->recbuf, 0, lsp->lshdr.recsize);
return;
}
/*man---------------------------------------------------------------------------
NAME
ls_rcput - lseq record put
SYNOPSIS
#include "lseq_.h"
int ls_rcput(lsp, lspos, lsrp)
lseq_t *lsp;
lspos_t lspos;
lsrec_t *lsrp;
DESCRIPTION
The ls_rcput function writes the record pointed to by lsrp into record
position lspos. The entire record is written, including the links.
ls_rcput will fail if one or more of the following is true:
[EINVAL] lsp is not a valid lseq pointer.
[EINVAL] lspos is less than 0.
[LSENOPEN] lsp is not open.
SEE ALSO
ls_rcget, ls_rcputf.
DIAGNOSTICS
Upon successful completion, a value of 0 is returned. Otherwise, a
value of -1 is returned, and errno set to indicate the error.
------------------------------------------------------------------------------*/
int ls_rcput(lsp, lspos, lsrp)
lseq_t * lsp;
lspos_t lspos;
lsrec_t * lsrp;
{
int rs = 0;
void * buf = NULL;
errno = 0;
#ifdef BOOP
/* validate arguments */
if ((!ls_valid(lsp)) || (lspos < 1) || (lsrp == NULL)) {
LSEPRINT;
errno = EINVAL;
return -1;
}
/* check if not open */
if (!(lsp->flags & LSOPEN)) {
LSEPRINT;
errno = LSENOPEN;
return -1;
}
#endif
/* convert record to file format */
buf = calloc(1, ls_blksize(lsp));
if (buf == NULL) {
LSEPRINT;
errno = ENOMEM;
return -1;
}
memcpy(buf, (void *)lsrp, offsetof(lsrec_t, recbuf));
memcpy((void *)((char *)buf + offsetof(lsrec_t, recbuf)), lsrp->recbuf,
lsp->lshdr.recsize);
/* write record to file */
rs = bputb(lsp->bp, (bpos_t)lspos, buf);
if (rs == -1) {
LSEPRINT;
free(buf);
return -1;
}
/* free buffer */
free(buf);
buf = NULL;
errno = 0;
return 0;
}
/*man---------------------------------------------------------------------------
NAME
ls_rcputf - lseq record field put
SYNOPSIS
#include "lseq_.h"
int ls_rcputf(lsp, lspos, offset, buf, bufsize)
lseq_t *lsp;
lspos_t lspos;
size_t offset;
void *buf;
size_t bufsize;
DESCRIPTION
The ls_rcputf function writes the field pointed to by buf into record
position lspos. Only the field is written.
ls_rcputf will fail if one or more of the following is true:
[EINVAL] lsp is not a valid lseq pointer.
[EINVAL] lspos is less than 0.
[LSEBOUND]
[LSENOPEN]
SEE ALSO
ls_rcget, ls_rcput.
DIAGNOSTICS
Upon successful completion, a value of 0 is returned. Otherwise, a
value of -1 is returned, and errno set to indicate the error.
------------------------------------------------------------------------------*/
int ls_rcputf(lsp, lspos, offset, buf, bufsize)
lseq_t * lsp;
lspos_t lspos;
size_t offset;
void * buf;
size_t bufsize;
{
int rs = 0;
errno = 0;
#ifdef DEBUG
/* validate arguments */
if ((!ls_valid(lsp)) || (lspos < 1) || (buf == NULL) || (bufsize < 1)) {
LSEPRINT;
errno = EINVAL;
return -1;
}
/* check if not open */
if (!(lsp->flags & LSOPEN)) {
LSEPRINT;
errno = LSENOPEN;
return -1;
}
/* check if record boundary crossed */
if ((offset + bufsize) > lsp->lshdr.recsize) {
LSEPRINT;
errno = LSEBOUND;
return -1;
}
#endif
/* write record to file */
rs = bputbf(lsp->bp, (bpos_t)lspos, offsetof(lsrec_t, recbuf) + offset, buf, bufsize);
if (rs == -1) {
LSEPRINT;
free(buf);
return -1;
}
errno = 0;
return 0;
}