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 >
Wrap
Text File
|
1989-08-30
|
3KB
|
156 lines
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "lsinscur.c 1.1 - 89/07/03" */
#include <blkio.h>
#include <errno.h>
/* #include <string.h> */
#include "lseq_.h"
/*man---------------------------------------------------------------------------
NAME
lsinscur - insert record after current lseq record
SYNOPSIS
#include <lseq.h>
int lsinscur(lsp, buf)
lseq_t *lsp;
void *buf;
DESCRIPTION
The lsinscur function creates a new record following the current record
and writes the data pointed to by buf into that record. The cursor is
set to the inserted record.
lsinscur will fail if one or more of the following is true:
[EINVAL] lsp is not a valid lseq pointer.
[EINVAL] buf is the NULL pointer.
[LSELOCK] lsp is not write locked.
[LSENOPEN] lsp is not open.
SEE ALSO
lsdelcur, lsinsert, lssearch.
DIAGNOSTICS
Upon successful completion, a value of 0 is returned. Otherwise, a
value of -1 is returned and errno is set to indicate the error.
------------------------------------------------------------------------------*/
int lsinscur(lsp, buf)
lseq_t * lsp;
void * buf;
{
int rs = 0;
int terrno = 0;
bpos_t bpos = 0;
errno = 0;
/* validate arguments */
if ((!ls_valid(lsp)) || (buf == NULL)) {
errno = EINVAL;
return -1;
}
/* check if not open */
if (!(lsp->flags & LSOPEN)) {
errno = LSENOPEN;
return -1;
}
/* check if not write locked */
if (!(lsp->flags & LSWRLCK)) {
errno = LSELOCK;
return -1;
}
/* build record to insert */
lsp->clsrp->prev = lsp->clspos;
if (lsp->clspos == 0) {
lsp->clsrp->next = lsp->lshdr.first;
}
memcpy(lsp->clsrp->recbuf, buf, lsp->lshdr.recsize);
/* set modify bit in header */
lsp->lshdr.flags |= LSHMOD;
rs = bputh(lsp->bp, (void *)&lsp->lshdr);
if (rs == -1) {
LSEPRINT;
return -1;
}
rs = bsync(lsp->bp);
if (rs == -1) {
LSEPRINT;
return -1;
}
/* get empty block from free list */
rs = bflpop(lsp->bp, &bpos);
if (rs == -1) {
LSEPRINT;
return -1;
}
lsp->clspos = bpos;
/* write new record */
rs = ls_rcput(lsp, lsp->clspos, lsp->clsrp);
if (rs == -1) {
LSEPRINT;
terrno = errno;
bpos = lsp->clspos;
bflpush(lsp, &bpos);
errno = terrno;
return -1;
}
/* increment record count */
lsp->lshdr.reccnt++;
/* fix links */
if (lsp->clsrp->next == 0) {
lsp->lshdr.last = lsp->clspos;
} else {
rs = bputbf(lsp->bp, (bpos_t)lsp->clsrp->next, offsetof(lsrec_t, prev), (void *)&lsp->clspos, sizeof(lsp->clsrp->prev));
if (rs == -1) {
LSEPRINT;
terrno = errno;
bpos = lsp->clspos;
bflpush(lsp, &bpos);
errno = terrno;
return -1;
}
}
if (lsp->clsrp->prev == 0) {
lsp->lshdr.first = lsp->clspos;
} else {
rs = bputbf(lsp->bp, (bpos_t)lsp->clsrp->prev, offsetof(lsrec_t, next), (void *)&lsp->clspos, sizeof(lsp->clsrp->next));
if (rs == -1) {
LSEPRINT;
terrno = errno;
bpos = lsp->clspos;
bflpush(lsp, &bpos);
errno = terrno;
return -1;
}
}
/* clear modify bit in header */
lsp->lshdr.flags &= ~LSHMOD;
rs = bputh(lsp->bp, (void *)&lsp->lshdr);
if (rs == -1) {
LSEPRINT;
return -1;
}
rs = bsync(lsp->bp);
if (rs == -1) {
LSEPRINT;
return -1;
}
errno = 0;
return 0;
}