
/*
 *  DWrite.c
 */

#include "lib.h"

long
DQueueFull(_chan)
void *_chan;
{
    CHANN *chan = (CHANN *)_chan;

    return (chan->qlen && (chan->queued > chan->qlen));
}

long
DWrite(_chan, _buf, bytes)
void *_chan;
void *_buf;
long bytes;
{
    int error = bytes;
    CHANN *chan = (CHANN *)_chan;
    char *buf = (char *)_buf;

    if (chan->qlen) {
	if (WaitQueue(chan, NULL) >= 0) {
	    IOSTD *ior = AllocMem(sizeof(IOSTD), MEMF_CLEAR|MEMF_PUBLIC);
	    ior->io_Command = DNCMD_WRITE;
	    ior->io_Unit = (void *)chan->chan;
	    ior->io_Offset = (long)chan;
	    ior->io_Message.mn_ReplyPort = (PORT *)chan;
	    ior->io_Data = AllocMem(bytes, MEMF_PUBLIC);
	    ior->io_Length = bytes;
	    BMov(buf, ior->io_Data, bytes);
	    PutMsg(chan->dnetport, (MSG *)ior);
	    ++chan->queued;
	} else {
	    error = -1;
	}
    } else {
	IOSTD ior;
	ior.io_Command = DNCMD_WRITE;
	ior.io_Unit = (void *)chan->chan;
	ior.io_Offset = (long)chan;
	ior.io_Message.mn_ReplyPort = (PORT *)chan;
	ior.io_Data = (APTR)buf;
	ior.io_Length = bytes;
	PutMsg(chan->dnetport, (MSG *)&ior);
	WaitMsg(&ior);
	if (ior.io_Error)
	    error = -1;
    }
    FixSignal(chan);
    return(error);
}

