home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frostbyte's 1980s DOS Shareware Collection
/
floppyshareware.zip
/
floppyshareware
/
DOOG
/
CBASE09.ZIP
/
BLKIO10.ZIP
/
BOPEN.C
< prev
next >
Wrap
Text File
|
1989-08-30
|
4KB
|
170 lines
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "bopen.c 1.1 - 89/07/03" */
#include <errno.h>
/* #include <string.h> */
#include "blkio_.h"
/* block file control structure table definition */
BLKFILE biob[BOPEN_MAX];
/*man---------------------------------------------------------------------------
NAME
bopen - open a block file
SYNOPSIS
#include <blkio.h>
BLKFILE *bopen(filename, type, hdrsize, blksize, bufcnt)
char *filename;
char *type;
size_t hdrsize;
size_t blksize;
size_t bufcnt;
DESCRIPTION
The bopen function opens the file named by filename as a block file.
A pointer to the BLKFILE structure associated with file is returned.
filename points to a character string that contains the name of the
file 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)
"w+" truncate or create for update
"c" create for update
If type is "r" or "r+" and the file does not exist, bopen will
fail. If type is "c" and the file already exists, bopen will fail.
hdrsize is the size of the file header. If there is no file header,
specify a value of 0 for hdrsize.
blksize is the size of the blocks.
bufcnt is the number of blocks to for which to create buffer storage.
For unbuffered operation, specify a value of 0 for bufcnt.
bopen will fail if one or more of the following is true:
[EEXIST] type is "c" and the named file exists.
[EINVAL] filename or type is the NULL pointer.
[EINVAL] type is not "r", "r+", "w+", or "c".
[EINVAL] blksize is 0.
[ENOENT] type is "r" or "r+" and the named file
does not exist.
[BEMFILE] The maximum number of block files is
already open.
SEE ALSO
bclose.
DIAGNOSTICS
bopen returns a NULL pointer on failure, and errno is set to
indicate the error.
------------------------------------------------------------------------------*/
BLKFILE *bopen(filename, type, hdrsize, blksize, bufcnt)
char * filename;
char * type;
size_t hdrsize;
size_t blksize;
size_t bufcnt;
{
int rs = 0;
BLKFILE * bp = NULL;
int terrno = 0;
errno = 0;
/* validate arguments */
if ((filename == NULL) || (type == NULL) || (blksize == 0)) {
errno = EINVAL;
return NULL;
}
/* find free slot in biob table */
for (bp = biob; bp < (biob + BOPEN_MAX); bp++) {
if (!(bp->flags & BIOOPEN)) {
break; /* found */
}
}
if (bp > (biob + BOPEN_MAX - 1)) {
errno = BEMFILE;
return NULL; /* no free slots */
}
/* set biob flags */
if (strcmp(type, BF_READ) == 0) {
bp->flags = BIOREAD;
} else if (strcmp(type, BF_RDWR) == 0) {
bp->flags = BIOREAD | BIOWRITE;
} else if (strcmp(type, BF_CREATE) == 0) {
bp->flags = BIOREAD | BIOWRITE;
} else if (strcmp(type, BF_CRTR) == 0) {
bp->flags = BIOREAD | BIOWRITE;
} else {
errno = EINVAL;
return NULL;
}
/* open file */
rs = b_uopen(bp, filename, type);
if (rs == -1) {
if ((errno != EEXIST) && (errno != ENOENT)) BEPRINT;
memset((void *)bp, 0, sizeof(biob[0]));
bp->flags = 0;
return NULL;
}
/* initialize */
bp->hdrsize = hdrsize;
bp->blksize = blksize;
bp->bufcnt = bufcnt;
bp->endblk = 0;
bp->most = 0;
bp->least = 0;
bp->block_p = NULL;
bp->blkbuf = NULL;
rs = b_uendblk(bp, &(bp->endblk));
if (rs == -1) {
if ((errno != BEBOUND) && (errno != BEEOF)) BEPRINT;
terrno = errno;
b_uclose(bp);
memset((void *)bp, 0, sizeof(biob[0]));
bp->flags = 0;
errno = terrno;
return NULL;
}
/* allocate memory for bp */
rs = b_alloc(bp);
if (rs == -1) {
BEPRINT;
terrno = errno;
b_uclose(bp);
memset((void *)bp, 0, sizeof(biob[0]));
bp->flags = 0;
errno = terrno;
return NULL;
}
/* initialize buffer storage */
rs = b_init(bp);
if (rs == -1) {
BEPRINT;
terrno = errno;
b_free(bp);
b_uclose(bp);
memset((void *)bp, 0, sizeof(biob[0]));
bp->flags = 0;
errno = terrno;
return NULL;
}
errno = 0;
return bp;
}