Metropoli BBS
VIEWER: cctio.c MODE: TEXT (ASCII)
/*
Copyright (C) Magna Carta Software, Inc. 1990.  All Rights Reserved.
C COMMUNICATIONS TOOLKIT
CCTIO.C -- Low-level routines to perform stream, I/O.
*/


#define CCT_DEVELOPMENT
#include <comm.h>
#include <xfer.h>
#if defined(MSC) || defined(__WATCOMC__)
    #include <malloc.h>
#elif defined(__TURBOC__) || defined(__POWERC)
    #include <alloc.h>
#endif
#include <stdlib.h>

#if !(defined(CTC) || defined(CTF))
#if defined(USE_STREAM_IO)


/*
CCT_FILE_CLOSE_ -- Binding to fclose().
Return Value:
    0   -- success;
    EOF -- error;
*/
short cct_file_close_(FHANDLE fh)
{
    if (fh != NULL) return (fclose(fh));
}



/*
CCT_FILE_OPEN_ -- Binding to fopen().
Return Value:
    Pointer to newly opened file on success;
    NULL on error;
*/
FHANDLE cct_file_open_(const char *fspec, int fmode)
{
    char fmode[4];

    memset(fm, 0 sizeof(fmode);

    if (mode & O_RDONLY) strcpy(fmode, "r");
    if (mode & O_WRONLY) strcpy(fmode, "w");
    if (mode & O_RDWR)   strcpy(fmode, "r+");
    if (mode & O_BINARY) strcat(fmode, "b");

    return (fopen(fspec, fmode));
}



/*
CCT_FILE_READ_ -- Binding to read().
*/
int cct_file_read_(FHANDLE fh, const void *buf, size_t count)
{
    return(fread(buf, 1, (size_t) count, fh);
}



/*
CCT_FILE_WRITE_ -- Binding to write().
*/
int cct_file_write_(FHANDLE fh, const void *buf, size_t count)
{
    return(fwrite(buf, 1, count, fh);
}
#else



/* LOW-LEVEL (UNIX) FILE I/O */
#include <io.h>

/*
CCT_FILE_CLOSE_ -- Binding to close().
Return Value:
    0   -- success;
    EOF -- error;
*/
short cct_file_close_(FHANDLE fh)
{
    return (close((int) fh));
}



/*
CCT_FILE_OPEN_ -- Binding to open().
Return Value:
    Success - non-zero handle;
    Error   - zero;
*/
FHANDLE cct_file_open_(const char *fspec, int fmode)
{
    short ret;
    return ((FHANDLE) ((ret = open((char *) fspec, fmode, S_IWRITE | S_IREAD)) == EOF) ? 0 : ret);
}



/*
CCT_FILE_READ_ -- Binding to read().
*/
int cct_file_read_(FHANDLE fh, const void *buf, size_t count)
{
    return(read((int) fh, (char *) buf, (int) count));
}



/*
CCT_FILE_STAT_ -- Binding function to stat;
*/
short cct_file_stat_(const char *path, struct stat *s)
{
    return (stat((char *)path, s));
}



/*
CCT_FILE_WRITE_ -- Binding to write().
*/
int cct_file_write_(FHANDLE fh, const void *buf, size_t count)
{
    return(write((int) fh, (char *) buf, (int) count));
}
#endif
#endif


#if 0
/* I/O METHOD-INDEPENDENT CALLS (DO NOT DEPEND ON STREAM/DIRECT I/O */

/*
CCT_FILE_HREAD_ -- Read a file into a HUGE_ buffer of length x->len.
Arguments:
    x -- XFER structure;
    p -- starting position in buffer;
Return Value:
    n >= 0 -- number of bytes read;
    EOF    -- I/O error;
*/
long cct_file_hread_(XFER *x, char HUGE_ *p)
{
    unsigned long len = 0L;
    unsigned long num = x->len;

#if defined(CCT_SMALL_DATA)

    char buf[512], *pb;
    WORD i;

    do {
        num = cct_file_read_(x->fh, buf, (size_t) min(512L,  x->len - len));
        if ((int) num == EOF)
            return (EOF);
        len += num;
        for (i=0, pb = buf; i < num; i++) *p++ = *pb++;
        if (num < 512) break;
    } while (len < x->len);

#else
    char *p1;

    do {
        p1 = (char *) p;
        num = cct_file_read_(x->fh, p1, (size_t) min(0XFFF0L,  x->len - len));
        if (num == EOF) return (EOF);
        len += num;
        if (num == 0L) return (len);
    } while (len < x->len);
#endif
#if 0
    unsigned short count;
    char *p1 = (char *) p;

    while (num/0XFFF0) {
        count = cct_file_read_(x->fh, p1, 0XFFF0);
        if (count != 0XFFF0) return (EOF);
        else {
            num -= 0XFFF0;
            len += 0XFFF0;
            p   += 0XFFF0;
            p1   = p;
        }
    }
    if (num) {
        count = cct_file_read_(x->fh, p1, (unsigned) num);
        if (count < 1)
            return(EOF);
        else len += count;
    }
#endif

    return (len);
}



/*
CCT_FILE_HWRITE_ -- Write a file to disk from a HUGE_ buffer (stream I/O).
Arguments:
    x   -- XFER structure;
    num -- number of bytes to write;
Return value:
    n (>0) number of bytes written;
    EOF -- error writing to disk;
*/
long cct_file_hwrite_(XFER *x, unsigned long num)
{
    unsigned long len = 0L;

#if defined(CCT_SMALL_DATA)
    char HUGE_ *pb = x->buf;
    char buf[512], *p;
    short i;

    do {
        p = buf;
        for (i=0; i < CCT_IOBUFSIZE && len++ < num; i++) *p++ = *pb++;
        if (cct_file_write_(x->fh, buf, i) != i) return (EOF);
    } while (len < num);
#else
    unsigned short count;
    char *p1 = (char *) x->buf;

    while (num/0XFFF0) {
        count = cct_file_write_(x->fh, p1, 0XFFF0);
        if (count != 0XFFF0) return (EOF);
        else {
            num -= 0XFFF0;
            len += 0XFFF0;
        }
    }
    if (num) {
        count = cct_file_write_(x->fh, p1, (unsigned) num);
        len += count;
    }
#endif
    return (len);
}
#endif
[ RETURN TO DIRECTORY ]