home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource4
/
295_01
/
buops.c
< prev
next >
Wrap
Text File
|
1989-12-30
|
14KB
|
565 lines
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "buops.c 1.2 - 89/10/31" */
#include "blkio_.h"
/* check host definition */
#ifndef HOST
/*#error HOST macro not defined.*/
#endif
#if HOST != UNIX && HOST != MSDOS
/*#error Invalid HOST macro definition.*/
#endif
#include <errno.h>
#if HOST == UNIX
#include <fcntl.h> /* open() macro definitions */
#include <unistd.h> /* lseek() macro definitions */
int close(/*int fd*/); /* system call declarations */
long lseek(/*int fd, long offset, int whence*/);
int open(/*char *path, int flags, ...*/);
int read(/*int fd, char *buf, unsigned n*/);
int write(/*int fd, char *buf, unsigned n*/);
#define MODE (0666) /* file permissions for new files */
#elif HOST == MSDOS
#if MSDOSC == TURBOC
#include <fcntl.h> /* open() macro definitions */
#include <io.h> /* system call declarations, lseek() macro defs */
#define MODE (0x100 | 0x080) /* file permissions for new files */
#elif MSDOSC == MSC
#include <fcntl.h> /* open() macro definitions */
#include <io.h> /* system call declarations, lseek() macro defs */
#define MODE (0x100 | 0x080) /* file permissions for new files */
#elif MSDOSC == MSQC
#include <fcntl.h> /* open() macro definitions */
#include <io.h> /* system call declarations, lseek() macro defs */
#define MODE (0x100 | 0x080) /* file permissions for new files */
#endif /* #if MSDOSC == TURBOC */
#endif /* #if HOST == MSDOS */
/*man---------------------------------------------------------------------------
NAME
b_uclose - unbuffered close block file
SYNOPSIS
#include "blkio_.h"
int b_uclose(bp)
BLKFILE *bp;
DESCRIPTION
The b_uclose function closes the 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;
{
#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
if (close(bp->fd.ifd) == -1) {
BEPRINT;
return -1;
}
#elif HOST == MSDOS
if (close(bp->fd.ifd) == -1) {
BEPRINT;
return -1;
}
#endif
errno = 0;
return 0;
}
/*man---------------------------------------------------------------------------
NAME
b_uendblk - unbuffered find 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 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
when the file is first opened or preceded by a call to bsync.
If the file does not end on a block boundary, the result is the
same as if the partial block (or header) at the end of the file
did not exist.
b_uendblk will fail if one or more of the following is true:
[EINVAL] bp is not a valid BLKFILE pointer.
[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
long pos = 0;
#elif HOST == MSDOS
long pos = 0;
#endif
#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 (or incomplete header) */
if (pos < bp->hdrsize) {
*endblk_p = 0;
errno = 0;
return 0;
}
/* find length past end of header */
pos -= bp->hdrsize;
/* 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 zero, or the block size if bn
is not equal to zero.
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.
[BEEOF] Block bn is past the end of file.
[BEEOF] End of file encountered within block bn.
[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
long pos = 0;
int nr = 0;
#elif 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
/* 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 = BEEOF;
return -1;
}
#elif 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, buf, (unsigned)bufsize);
if (nr == -1) {
BEPRINT;
return -1;
}
if (nr != bufsize) {
BEPRINT;
errno = BEEOF;
return -1;
}
#endif
errno = 0;
return 0;
}
/*man---------------------------------------------------------------------------
NAME
b_uopen - unbuffered open block file
SYNOPSIS
#include "blkio_.h"
int b_uop