home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frostbyte's 1980s DOS Shareware Collection
/
floppyshareware.zip
/
floppyshareware
/
DOOG
/
CBASE09.ZIP
/
CBASE.ZIP
/
CBOPEN.C
< prev
next >
Wrap
Text File
|
1989-08-31
|
4KB
|
150 lines
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "cbopen.c 1.1 - 89/08/31" */
#include <blkio.h>
#include <btree.h>
#include <errno.h>
#include <lseq.h>
/* #include <string.h> */
#include "cbase_.h"
/* cbase control structure table definition */
cbase_t cbb[CBOPEN_MAX];
/*man---------------------------------------------------------------------------
NAME
cbopen - open a cbase
SYNOPSIS
#include <cbase.h>
cbase_t *cbopen(cbname, type, fields, fldcnt)
char *cbname;
char *type;
cbfield_t fields[];
size_t fldcnt;
DESCRIPTION
The cbopen function opens the cbase named by cbname. A pointer to
the cbase_t structure associated with the cbase is returned.
cbname points to a character string that contains the name of the
cbase to be opened.
type is a character string having one of the following values:
"r" open for reading
"r+" open for update (reading and writing)
See cbcreate for an explanation of the field definition array
fields and the number of fields fldcnt.
cbopen will fail if one or more of the following is true:
[EINVAL] cbname is the NULL pointer.
[EINVAL] type is not "r" or "r+".
[CBECORRUPT] A file in the named cbase is corrupt.
[CBEMFILE] Too many open cbases. The maximum
is defined as CBOPEN_MAX in cbase.h.
SEE ALSO
cbclose, cbcreate.
DIAGNOSTICS
cbopen returns a NULL pointer on failure, and errno is set to
indicate the error.
------------------------------------------------------------------------------*/
cbase_t *cbopen(cbname, type, fields, fldcnt)
char * cbname;
char * type;
cbfield_t fields[];
size_t fldcnt;
{
int rs = 0;
int terrno = 0;
cbase_t * cbp = NULL;
int i = 0;
errno = 0;
/* validate arguments */
if ((cbname == NULL) || (type == NULL)) {
errno = EINVAL;
return NULL;
}
/* find free slot in cbb table */
for (cbp = cbb; cbp < (cbb + CBOPEN_MAX); cbp++) {
if (!(cbp->flags & CBOPEN)) {
break; /* found */
}
}
if (cbp >= cbb + CBOPEN_MAX) {
errno = CBEMFILE;
return NULL; /* no free slots */
}
/* open record file */
if (strcmp(type, CB_READ) == 0) {
cbp->flags = CBREAD;
} else if (strcmp(type, CB_RDWR) == 0) {
cbp->flags = CBREAD | CBWRITE;
} else {
errno = EINVAL;
return NULL;
}
cbp->lsp = lsopen(cbname, type);
if (cbp->lsp == NULL) {
if ((errno != ENOENT) && (errno != LSECORRUPT)) CBEPRINT;
if (errno == LSECORRUPT) errno = CBECORRUPT;
memset((void *)cbp, 0, sizeof(cbb[0]));
cbp->flags = 0;
return NULL;
}
if (lsrecsize(cbp->lsp) < sizeof(lspos_t)) {
if ((errno != ENOENT) && (errno != LSECORRUPT)) CBEPRINT;
if (errno == LSECORRUPT) errno = CBECORRUPT;
memset((void *)cbp, 0, sizeof(cbb[0]));
cbp->flags = 0;
return NULL;
}
/* validate arguments */
if (!cb_fdlvalid(fields, fldcnt, lsrecsize(cbp->lsp))) {
lsclose(cbp->lsp);
memset((void *)cbp, 0, sizeof(cbb[0]));
cbp->flags = 0;
errno = EINVAL;
return NULL;
}
/* copy field definitions into cbase structure */
cbp->fldcnt = fldcnt;
memset((void *)cbp->fields, 0, sizeof(cbp->fields));
memcpy((void *)cbp->fields, (void *)fields, cbp->fldcnt * sizeof(cbp->fields[0]));
/* open key files */
for (i = 0; i < cbp->fldcnt; i++) {
if (cbp->fields[i].flags & CBFKEY) {
cbp->btp[i] = btopen(cbp->fields[i].filename, type,
cbcmp_p[cbp->fields[i].type - 1]);
if (cbp->btp[i] == NULL) {
if ((errno != ENOENT) && (errno != LSECORRUPT))
CBEPRINT;
if (errno == BTECORRUPT) errno = CBECORRUPT;
terrno = errno;
cbclose(cbp);
memset((void *)cbp, 0, sizeof(cbb[0]));
cbp->flags = 0;
errno = terrno;
return NULL;
}
}
}
errno = 0;
return cbp;
}