home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frostbyte's 1980s DOS Shareware Collection
/
floppyshareware.zip
/
floppyshareware
/
DOOG
/
CBASE09.ZIP
/
BLKIO10.ZIP
/
BUOPS.C
< prev
next >
Wrap
Text File
|
1989-08-30
|
12KB
|
505 lines
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "buops.c 1.1 - 89/07/03" */
#include "blkio_.h"
#ifndef HOST /* check host definition */
/* #error HOST macro not defined. */
#endif
#if HOST != UNIX && HOST != MSDOS
/* #error HOST macro not defined as supported operating system. */
#endif
#include <errno.h>
#if HOST == UNIX
#include <fcntl.h>
#include <unistd.h>
int close(/* int fildes */);
long lseek(/* int fildes, long offset, int whence */);
int open(/* const char *path, int oflag, .../*int mode*\/*/);
int read(/* int fildes, char *buf, unsigned n */);
int write(/* int fildes, char *buf, unsigned n */);
/* file permissions to create file with */
#define MODE (0666)
/*(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)*/
#elif HOST == MSDOS
#include <fcntl.h>
#if MSDOSC == TURBOC
#include <io.h> /* fct prototypes, SEEK_??? */
/* file permissions to create file with */
#define MODE (0x100 | 0x080)
/*(S_IREAD | S_IWRITE)*/
#elif MSDOSC == MSC
#include <io.h> /* fct prototypes, SEEK_??? */
/* file permissions to create file with */
#define MODE (0x100 | 0x080)
/*(S_IREAD | S_IWRITE)*/
#endif
#endif
/*man---------------------------------------------------------------------------
NAME
b_uclose - close block file
SYNOPSIS
#include "blkio_.h"
int b_uclose(bp)
BLKFILE *bp;
DESCRIPTION
The b_uclose function closes the physical file associated with the
BLKFILE pointer bp.
b_uclose will fail if one or more of the following is true:
[EINVAL] bp is not a valid BLKFILE pointer.
[BENOPEN] bp is not open.
SEE ALSO
b_uopen.
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 b_uclose(bp)
BLKFILE *bp;
{
int rs = 0;
#ifdef DEBUG
/* validate arguments */
if (!b_valid(bp)) {
BEPRINT;
errno = EINVAL;
return -1;
}
/* check if not open */
if (!(bp->flags & BIOOPEN)) {
BEPRINT;
errno = BENOPEN;
return -1;
}
#endif
/* close file */
#if HOST == UNIX || HOST == MSDOS
rs = close(bp->fd.ifd);
if (rs == -1) {
BEPRINT;
return -1;
}
#endif
errno = 0;
return 0;
}
/*man---------------------------------------------------------------------------
NAME
b_uendblk - unbuffered end block
SYNOPSIS
#include "blkio_.h"
int b_uendblk(bp, endblk_p)
BLKFILE *bp;
bpos_t *endblk_p;
DESCRIPTION
The b_uendblk function finds the block number of the first block past
the end of file of the block file associated with BLKFILE pointer bp.
This value is returned in the storage location pointed to by endblk_p.
Blocks in buffer storage and not yet written to the file are not
counted, so this function should normally be used only when the file
is first opened or preceded by a call to bsync.
b_uendblk will fail if one or more of the following is true:
[EINVAL] bp is not a valid BLKFILE pointer.
[BEBOUND] The file does not end on a block boundary.
[BENOPEN] bp is not open.
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 b_uendblk(bp, endblk_p)
BLKFILE *bp;
bpos_t *endblk_p;
{
#if HOST == UNIX || HOST == MSDOS
long pos = 0;
#endif
errno = 0;
#ifdef DEBUG
/* validate arguments */
if (!b_valid(bp)) {
BEPRINT;
errno = EINVAL;
return -1;
}
/* check if not open */
if (!(bp->flags & BIOOPEN)) {
BEPRINT;
errno = BENOPEN;
return -1;
}
#endif
#if HOST == UNIX || HOST == MSDOS
/* find position of end of file */
pos = lseek(bp->fd.ifd, (long)0, SEEK_END);
if (pos == -1) {
BEPRINT;
return -1;
}
/* check if empty file */
if (pos == 0) {
*endblk_p = 0;
errno = 0;
return 0;
}
/* find length past end of header */
pos -= bp->hdrsize;
/* check if file does not end on block boundary */
if ((pos < 0) || ((pos % bp->blksize) != 0)) {
errno = BEBOUND;
return -1;
}
/* set return argument */
*endblk_p = (pos / bp->blksize) + 1;
#endif
errno = 0;
return 0;
}
/*man---------------------------------------------------------------------------
NAME
b_ugetf - unbuffered get field from block file
SYNOPSIS
#include "blkio_.h"
int b_ugetf(bp, bn, offset, buf, bufsize)
BLKFILE *bp;
bpos_t bn;
size_t offset;
void *buf;
size_t bufsize;
DESCRIPTION
The b_ugetf function reads bufsize characters from block number bn
of the block file associated with BLKFILE pointer bp into the buffer
pointed to by buf. A value of 0 for bn indicates the file header.
The read starts offset characters from the beginning of the block.
The sum of the offset and the bufsize must not exceed the header size
if bn is equal to 0, or the block size if bn is not equal to 0.
b_ugetf will fail if one or more of the following is true:
[EINVAL] bp is not a valid BLKFILE pointer.
[EINVAL] buf is NULL.
[EINVAL] bufsize is less than 1.
[BEBOUND] offset + bufsize extends across a block
boundary.
[BEBOUND] End of file not on block boundary.
[BEEOF] Block bn is past the end of file.
[BENOPEN] bp is not open.
SEE ALSO
b_uputf.
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 b_ugetf(bp, bn, offset, buf, bufsize)
BLKFILE * bp;
bpos_t bn;
size_t offset;
void * buf;
size_t bufsize;
{
#if HOST == UNIX || HOST == MSDOS
long pos = 0;
int nr = 0;
#endif
#ifdef DEBUG
/* validate arguments */
if ((!b_valid(bp)) || (buf == NULL) || (bufsize < 1)) {
BEPRINT;
errno = EINVAL;
return -1;
}
/* check if not open */
if (!(bp->flags & BIOOPEN)) {
BEPRINT;
errno = BENOPEN;
return -1;
}
/* check if block boundary is crossed */
if (bn == 0) {
if ((offset + bufsize) > bp->hdrsize) {
BEPRINT;
errno = BEBOUND;
return -1;
}
} else {
if ((offset + bufsize) > bp->blksize) {
BEPRINT;
errno = BEBOUND;
return -1;
}
}
#endif
#if HOST == UNIX || HOST == MSDOS
/* read from file into buffer */
if (bn == 0) { /* header */
pos = 0;
} else { /* block */
pos = bp->hdrsize + (bn - 1) * bp->blksize;
}
pos += offset;
if (lseek(bp->fd.ifd, pos, SEEK_SET) == -1) {
BEPRINT;
return -1;
}
nr = read(bp->fd.ifd, (char *)buf, (unsigned)bufsize);
if (nr == -1) {
BEPRINT;
return -1;
}
if (nr != bufsize) {
BEPRINT;
errno = BEBOUND;
return -1;
}
#endif
errno = 0;
return 0;
}
/*man---------------------------------------------------------------------------
NAME
b_uopen - open block file
SYNOPSIS
#include "blkio_.h"
int b_uopen(bp, filename, type)
BLKFILE *bp;
char *filename;
char *type;
DESCRIPTION
The b_uopen function opens the physical file associated with the
BLKFILE pointer bp.
b_uopen will fail if one or more of the following is true:
[EINVAL] bp is not a valid BLKFILE pointer.
[EINVAL] filename or type is the NULL pointer.
SEE ALSO
b_uclose.
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 b_uopen(bp, filename, type)
BLKFILE * bp;
char * filename;
char * type;
{
int rs = 0;
#if HOST == UNIX || HOST == MSDOS
int oflag = 0;
int fd = 0;
#endif
#ifdef DEBUG
/* validate arguments */
if ((!b_valid(bp)) || (filename == NULL) || (type == NULL)) {
BEPRINT;
errno = EINVAL;
return -1;
}
#endif
/* open file */
#if HOST == UNIX
oflag = 0;
#elif HOST == MSDOS
oflag = O_BINARY;
#endif
#if HOST == UNIX || HOST == MSDOS
if (strcmp(type, BF_READ) == 0) {
oflag |= O_RDONLY;
} else if (strcmp(type, BF_RDWR) == 0) {
oflag |= O_RDWR;
} else if (strcmp(type, BF_CREATE) == 0) {
oflag |= O_RDWR | O_CREAT | O_EXCL;
} else if (strcmp(type, BF_CRTR) == 0) {
oflag |= O_RDWR | O_CREAT | O_TRUNC;
} else {
errno = EINVAL;
return -1;
}
fd = open(filename, oflag, MODE);
if (fd == -1) {
if ((errno != EACCES) && (errno != EEXIST) && (errno != ENOENT)) BEPRINT;
return -1;
}
bp->fd.ifd = fd;
#endif
errno = 0;
return 0;
}
/*man---------------------------------------------------------------------------
NAME
b_uputf - unbuffered put field to block file
SYNOPSIS
#include "blkio_.h"
int b_uputf(bp, bn, offset, buf, bufsize)
BLKFILE *bp;
bpos_t bn;
size_t offset;
void *buf;
size_t bufsize;
DESCRIPTION
The b_uputf function writes bufsize characters from the buffer pointed
to by buf to block number bn of the block file associated with BLKFILE
pointer bp. A value of 0 for bn indicates the file header. The write
starts offset characters from the beginning of the block. The sum of
offset bufsize must not exceed the header size if bn is equal to 0, or
the block size if bn is not equal to 0.
b_uputf will fail if one or more of the following is true:
[EINVAL] bp is not a valid BLKFILE pointer.
[EINVAL] buf is the NULL pointer.
[EINVAL] bufsize is less than 1.
[BEBOUND] offset + bufsize extends beyond the block
boundary.
[BEEOF] Partial block being written and block bn
is past the end of file.
[BEEOF] Complete block being written and block bn
is more than 1 past the end of file.
[BENOPEN] bp is not open for writing.
SEE ALSO
b_ugetf.
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 b_uputf(bp, bn, offset, buf, bufsize)
BLKFILE * bp;
bpos_t bn;
size_t offset;
void * buf;
size_t bufsize;
{
#if HOST == UNIX || HOST == MSDOS
long pos = 0;
int nw = 0;
#endif
#ifdef DEBUG
/* validate arguments */
if ((!b_valid(bp)) || (buf == NULL) || (bufsize < 1)) {
BEPRINT;
errno = EINVAL;
return -1;
}
/* check if not open */
if (!(bp->flags & BIOWRITE)) {
BEPRINT;
errno = BENOPEN;
return -1;
}
/* check if block boundary is crossed */
if (bn == 0) {
if ((offset + bufsize) > bp->hdrsize) {
BEPRINT;
errno = BEBOUND;
return -1;
}
} else {
if ((offset + bufsize) > bp->blksize) {
BEPRINT;
errno = BEBOUND;
return -1;
}
}
#endif
#if HOST == UNIX || HOST == MSDOS
/* write buffer to file */
if (bn == 0) { /* header */
pos = 0;
} else { /* block */
pos = bp->hdrsize + (bn - 1) * bp->blksize;
}
pos += offset;
if (lseek(bp->fd.ifd, pos, SEEK_SET) == -1) {
BEPRINT;
return -1;
}
nw = write(bp->fd.ifd, (char *)buf, (unsigned)bufsize);
if (nw == -1) {
BEPRINT;
return -1;
}
if (nw != bufsize) {
BEPRINT;
errno = BEPANIC;
return -1;
}
#endif
errno = 0;
return 0;
}