home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frostbyte's 1980s DOS Shareware Collection
/
floppyshareware.zip
/
floppyshareware
/
DOOG
/
CBASE09.ZIP
/
LSEQ.ZIP
/
LSLOCK.C
< prev
next >
Wrap
Text File
|
1989-08-30
|
4KB
|
187 lines
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "lslock.c 1.1 - 89/07/03" */
#include <blkio.h>
#include <errno.h>
#include "lseq_.h"
/*man---------------------------------------------------------------------------
NAME
lslock - lseq lock
SYNOPSIS
#include <lseq.h>
int lslock(lsp, l_type)
lseq_t *lsp;
int l_type;
DESCRIPTION
The lslock function controls the lock status of an lseq. The lsp
argument is an open lseq. l_type indicates the target status of
the lock on the lseq.
The lock types available are:
LS_RDLCK - lock lseq for reading
LS_WRLCK - lock lseq for reading and writing
LS_RDLKW - lock lseq for reading (wait)
LS_WRLKW - lock lseq for reading and writing (wait)
LS_UNLCK - unlock lseq
For the lock types which wait, lslock 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.
lslock will fail if one or more of the following is true:
[EAGAIN] l_type is LS_RDLCK and lsp is already
write locked by another process, or l_type
is LS_WRLCK and lsp is already read
or write locked by another process.
[EINVAL] lsp is is not a valid lseq pointer.
[EINVAL] l_type is not one of the valid lock types.
[LSECORRUPT] lsp is corrupt.
[LSENOPEN] lsp is not open.
[LSENOPEN] l_type is LS_RDLCK or LS_RDLKW and lsp
is not opened for reading or l_type is LS_WRLCK
or LS_WRLKW and lsp is not open for writing.
SEE ALSO
lsgetlck.
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 lslock(lsp, l_type)
lseq_t * lsp;
int l_type;
{
int rs = 0;
int bl_type = 0;
errno = 0;
/* validate arguments */
if (!ls_valid(lsp)) {
errno = EINVAL;
return -1;
}
/* check if lseq not open */
if (!(lsp->flags & LSOPEN)) {
errno = LSENOPEN;
return -1;
}
/* check if file is open */
switch (l_type) {
case LS_RDLCK:
if (!(lsp->flags & LSREAD)) {
errno = LSENOPEN;
return -1;
}
bl_type = B_RDLCK;
break;
case LS_RDLKW:
if (!(lsp->flags & LSREAD)) {
errno = LSENOPEN;
return -1;
}
bl_type = B_RDLKW;
break;
case LS_WRLCK:
if (!(lsp->flags & LSWRITE)) {
errno = LSENOPEN;
return -1;
}
bl_type = B_WRLCK;
break;
case LS_WRLKW:
if (!(lsp->flags & LSWRITE)) {
errno = LSENOPEN;
return -1;
}
bl_type = B_WRLKW;
break;
case LS_UNLCK:
/* flush buffers */
rs = bflush(lsp->bp);
if (rs == -1) {
LSEPRINT;
return -1;
}
bl_type = B_UNLCK;
break;
default:
errno = EINVAL;
return -1;
break;
}
/* lock lseq file */
rs = lockb(lsp->bp, bl_type, (bpos_t)0, (size_t)0);
if (rs == -1) {
if (errno != EAGAIN) LSEPRINT;
return -1;
}
/* set status bits in lseq control structure */
switch (l_type) {
case LS_UNLCK:
if (lsgetlck(lsp) == LS_WRLCK) { /* update header */
rs = bputh(lsp->bp, (void *)&lsp->lshdr);
if (rs == -1) {
LSEPRINT;
return -1;
}
}
lsp->flags &= ~LSLOCKS;
break; /* case LS_UNLCK: */
case LS_RDLCK:
case LS_RDLKW:
if (lsgetlck(lsp) == LS_UNLCK) {
rs = bgeth(lsp->bp, (void *)&lsp->lshdr);
if (rs == -1) {
LSEPRINT;
return -1;
}
if (lsp->lshdr.flags & LSHMOD) {
errno = LSECORRUPT;
return -1;
}
}
lsp->flags |= LSRDLCK;
lsp->flags &= ~LSWRLCK;
break; /* case LS_RDLCK: */
case LS_WRLCK:
case LS_WRLKW:
if (lsgetlck(lsp) == LS_UNLCK) {
rs = bgeth(lsp->bp, (void *)&lsp->lshdr);
if (rs == -1) {
LSEPRINT;
return -1;
}
if (lsp->lshdr.flags & LSHMOD) {
errno = LSECORRUPT;
return -1;
}
}
lsp->flags |= (LSRDLCK | LSWRLCK);
break; /* case LS_WRLCK: */
default:
LSEPRINT;
errno = LSEPANIC;
return -1;
break; /* default: */
}
errno = 0;
return 0;
}