home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frostbyte's 1980s DOS Shareware Collection
/
floppyshareware.zip
/
floppyshareware
/
DOOG
/
CBASE09.ZIP
/
CBASE.ZIP
/
CBLOCK.C
< prev
next >
Wrap
Text File
|
1989-08-31
|
4KB
|
180 lines
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "cblock.c 1.1 - 89/08/31" */
#include <blkio.h>
#include <errno.h>
#include <lseq.h>
#include "cbase_.h"
/*man---------------------------------------------------------------------------
NAME
cblock - lock cbase
SYNOPSIS
#include <cbase.h>
int cblock(cbp, l_type)
cbase_t *cbp;
int l_type;
DESCRIPTION
The cblock function controls the lock status of a cbase. The cbp
argument is an open cbase. l_type indicates the target status of
the lock on the cbase.
The lock types available are:
CB_RDLCK - lock cbase for reading
CB_WRLCK - lock cbase for reading and writing
CB_RDLKW - lock cbase for reading (wait)
CB_WRLKW - lock cbase for reading and writing (wait)
CB_UNLCK - unlock cbase
For the lock types which wait, cblock will not return until the
lock is available. For the lock types which do not wait, if the
lock is unavailable because of a lock held by another process a
value of -1 is returned and errno set to EAGAIN.
cblock will fail if one or more of the following is true:
[EAGAIN] l_type is CB_RDLCK and the cbase is already
write locked by another process, or l_type
is CB_WRLCK and the cbase is already read
or write locked by another process.
[EINVAL] cbp is is not a valid cbase.
[EINVAL] l_type is not one of the valid lock types.
[CBECORRUPT] Either the record file or one of the key
files of cbp is corrupt.
[CBENOPEN] cbp is not open.
[CBENOPEN] l_type is CB_RDLCK or CB_RDLKW and cbp
is not opened for reading or l_type is
CB_WRLCK or CB_WRLKW and cbp not open for
writing.
SEE ALSO
cbgetlck.
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 cblock(cbp, l_type)
cbase_t * cbp;
int l_type;
{
int rs = 0;
int lsl_type = 0;
int btl_type = 0;
int i = 0;
errno = 0;
/* validate arguments */
if (!cb_valid(cbp)) {
errno = EINVAL;
return -1;
}
/* check if cbase not open */
if (!(cbp->flags & CBOPEN)) {
errno = CBENOPEN;
return -1;
}
/* check open flags and set lsl_type and btl_type */
switch (l_type) {
case CB_RDLCK:
if (!(cbp->flags & CBREAD)) {
errno = CBENOPEN;
return -1;
}
lsl_type = LS_RDLCK;
btl_type = BT_RDLCK;
break;
case CB_RDLKW:
if (!(cbp->flags & CBREAD)) {
errno = CBENOPEN;
return -1;
}
lsl_type = LS_RDLKW;
btl_type = BT_RDLCK;
break;
case CB_WRLCK:
if (!(cbp->flags & CBWRITE)) {
errno = CBENOPEN;
return -1;
}
lsl_type = LS_WRLCK;
btl_type = BT_WRLCK;
break;
case CB_WRLKW:
if (!(cbp->flags & CBWRITE)) {
errno = CBENOPEN;
return -1;
}
lsl_type = LS_WRLKW;
btl_type = BT_WRLCK;
break;
case CB_UNLCK:
lsl_type = LS_UNLCK;
btl_type = BT_UNLCK;
break;
default:
errno = EINVAL;
return -1;
break;
}
/* lock record file */
rs = lslock(cbp->lsp, lsl_type);
if (rs == -1) {
if (errno == EAGAIN) {
return -1;
}
if (errno == LSECORRUPT) {
errno = CBECORRUPT;
return -1;
}
CBEPRINT;
return -1;
}
/* lock key files */
for (i = 0; i < cbp->fldcnt; i++) {
if (cbp->fields[i].flags & CBFKEY) {
rs = btlock(cbp->btp[i], btl_type);
if (rs == -1) {
CBEPRINT;
return -1;
}
}
}
/* set lock bits */
switch (l_type) {
case CB_RDLCK:
case CB_RDLKW:
cbp->flags |= CBRDLCK;
cbp->flags &= ~CBWRLCK;
break;
case CB_WRLCK:
case CB_WRLKW:
cbp->flags |= (CBRDLCK | CBWRLCK);
break;
case CB_UNLCK:
cbp->flags &= ~CBLOCKS;
break;
default:
CBEPRINT;
errno = CBEPANIC;
return -1;
break;
}
errno = 0;
return 0;
}