#include "ioqueue.h"
#ifdef _WIN32
-typedef struct iovec {
- char *iov_base;
- int iov_len;
+typedef struct iovec {
+ char *iov_base;
+ int iov_len;
} iovec_t;
#endif
void
ioq_init(ioq, bsize)
- struct ioqueue *ioq;
- int bsize;
+struct ioqueue *ioq;
+int bsize;
{
- extern s_char num_teles[];
+ extern s_char num_teles[];
- initque(&ioq->queue);
- ioq->cc = 0;
- ioq->bsize = bsize;
- *num_teles = '\0';
+ initque(&ioq->queue);
+ ioq->cc = 0;
+ ioq->bsize = bsize;
+ *num_teles = '\0';
}
/*
*/
int
ioq_peekiov(ioq, iov, max)
- struct ioqueue *ioq;
- struct iovec *iov;
- int max;
+struct ioqueue *ioq;
+struct iovec *iov;
+int max;
{
- if (ioq->cc <= 0)
- return 0;
- return ioqtoiov(ioq, iov, max);
+ if (ioq->cc <= 0)
+ return 0;
+ return ioqtoiov(ioq, iov, max);
}
/*
*/
int
ioq_peek(ioq, buf, cc)
- struct ioqueue *ioq;
- s_char *buf;
- int cc;
+struct ioqueue *ioq;
+s_char *buf;
+int cc;
{
- return ioqtobuf(ioq, buf, cc);
+ return ioqtobuf(ioq, buf, cc);
}
int
ioq_dequeue(ioq, cc)
- struct ioqueue *ioq;
- int cc;
+struct ioqueue *ioq;
+int cc;
{
- if (dequeuecc(ioq, cc) != cc)
- return 0;
- return cc;
+ if (dequeuecc(ioq, cc) != cc)
+ return 0;
+ return cc;
}
int
ioq_read(ioq, buf, cc)
- struct ioqueue *ioq;
- s_char *buf;
- int cc;
+struct ioqueue *ioq;
+s_char *buf;
+int cc;
{
- int n;
+ int n;
- n = ioqtobuf(ioq, buf, cc);
- if (n > 0)
- dequeuecc(ioq, n);
- return n;
+ n = ioqtobuf(ioq, buf, cc);
+ if (n > 0)
+ dequeuecc(ioq, n);
+ return n;
}
void
ioq_write(ioq, buf, cc)
- struct ioqueue *ioq;
- s_char *buf;
- int cc;
+struct ioqueue *ioq;
+s_char *buf;
+int cc;
{
- enqueuecc(ioq, buf, cc);
+ enqueuecc(ioq, buf, cc);
}
int
ioq_qsize(ioq)
- struct ioqueue *ioq;
+struct ioqueue *ioq;
{
- return ioq->cc;
+ return ioq->cc;
}
void
ioq_drain(ioq)
- struct ioqueue *ioq;
+struct ioqueue *ioq;
{
- struct io *io;
- struct qelem *qp;
+ struct io *io;
+ struct qelem *qp;
- while ((qp = ioq->queue.q_forw) != &ioq->queue) {
- io = (struct io *) qp;
- free(io->data);
- (void) remque(&io->queue);
- (void) free(io);
- }
- ioq->cc = 0;
+ while ((qp = ioq->queue.q_forw) != &ioq->queue) {
+ io = (struct io *)qp;
+ free(io->data);
+ (void)remque(&io->queue);
+ (void)free(io);
+ }
+ ioq->cc = 0;
}
s_char *
ioq_gets(ioq, buf, cc)
- struct ioqueue *ioq;
- s_char *buf;
- int cc;
+struct ioqueue *ioq;
+s_char *buf;
+int cc;
{
- register s_char *p;
- register s_char *end;
- int nbytes;
+ register s_char *p;
+ register s_char *end;
+ int nbytes;
- nbytes = ioqtobuf(ioq, buf, cc);
- if (nbytes < cc)
- cc = nbytes;
- end = &buf[cc];
- for (p = buf; p < end && *p; p++) {
- if (*p == '\n') {
- *p = '\0';
- dequeuecc(ioq, (p - buf) + 1);
- return buf;
- }
+ nbytes = ioqtobuf(ioq, buf, cc);
+ if (nbytes < cc)
+ cc = nbytes;
+ end = &buf[cc];
+ for (p = buf; p < end && *p; p++) {
+ if (*p == '\n') {
+ *p = '\0';
+ dequeuecc(ioq, (p - buf) + 1);
+ return buf;
}
- return 0;
+ }
+ return 0;
}
/*
* left for a higher level.
*/
static
-int
+ int
ioqtobuf(ioq, buf, cc)
- register struct ioqueue *ioq;
- s_char *buf;
- int cc;
+register struct ioqueue *ioq;
+s_char *buf;
+int cc;
{
- register struct io *io;
- struct qelem *qp;
- s_char *offset;
- int nbytes;
- int nleft;
+ register struct io *io;
+ struct qelem *qp;
+ s_char *offset;
+ int nbytes;
+ int nleft;
- nleft = cc;
- offset = buf;
- for (qp = ioq->queue.q_forw; qp != &ioq->queue; qp = qp->q_forw) {
- io = (struct io *) qp;
- if ((nbytes = io->nbytes - io->offset) < 0) {
- fprintf(stderr, "ioqtobuf: offset %d nbytes %d\n",
- io->offset, io->nbytes);
- continue;
- }
- if (nbytes > 0) {
- if (nleft < nbytes)
- nbytes = nleft;
- bcopy(io->data + io->offset, offset, nbytes);
- offset += nbytes;
- nleft -= nbytes;
- }
+ nleft = cc;
+ offset = buf;
+ for (qp = ioq->queue.q_forw; qp != &ioq->queue; qp = qp->q_forw) {
+ io = (struct io *)qp;
+ if ((nbytes = io->nbytes - io->offset) < 0) {
+ fprintf(stderr, "ioqtobuf: offset %d nbytes %d\n",
+ io->offset, io->nbytes);
+ continue;
}
- return offset - buf;
+ if (nbytes > 0) {
+ if (nleft < nbytes)
+ nbytes = nleft;
+ bcopy(io->data + io->offset, offset, nbytes);
+ offset += nbytes;
+ nleft -= nbytes;
+ }
+ }
+ return offset - buf;
}
/*
* of a full ioqueue still be quick.
*/
static
-int
+ int
ioqtoiov(ioq, iov, max)
- register struct ioqueue *ioq;
- register struct iovec *iov;
- register int max;
+register struct ioqueue *ioq;
+register struct iovec *iov;
+register int max;
{
- register struct io *io;
- register int cc;
- register int niov;
- struct qelem *qp;
+ register struct io *io;
+ register int cc;
+ register int niov;
+ struct qelem *qp;
- cc = 0;
- niov = 0;
- qp = ioq->queue.q_forw;
- for (qp = ioq->queue.q_forw; qp != &ioq->queue; qp = qp->q_forw) {
- io = (struct io *) qp;
- if (niov >= MAXIOV || cc >= max)
- break;
- iov->iov_base = io->data + io->offset;
- iov->iov_len = io->nbytes - io->offset;
- cc += io->nbytes - io->offset;
- niov++;
- iov++;
- }
- return niov;
+ cc = 0;
+ niov = 0;
+ qp = ioq->queue.q_forw;
+ for (qp = ioq->queue.q_forw; qp != &ioq->queue; qp = qp->q_forw) {
+ io = (struct io *)qp;
+ if (niov >= MAXIOV || cc >= max)
+ break;
+ iov->iov_base = io->data + io->offset;
+ iov->iov_len = io->nbytes - io->offset;
+ cc += io->nbytes - io->offset;
+ niov++;
+ iov++;
+ }
+ return niov;
}
/*
*/
static void
enqueuecc(ioq, buf, cc)
- struct ioqueue *ioq;
- s_char *buf;
- int cc;
+struct ioqueue *ioq;
+s_char *buf;
+int cc;
{
- struct io *io;
+ struct io *io;
- io = (struct io *) malloc(sizeof(*io));
- io->nbytes = cc;
- io->offset = 0;
- io->data = buf;
- insque(&io->queue, ioq->queue.q_back);
- ioq->cc += cc;
+ io = (struct io *)malloc(sizeof(*io));
+ io->nbytes = cc;
+ io->offset = 0;
+ io->data = buf;
+ insque(&io->queue, ioq->queue.q_back);
+ ioq->cc += cc;
}
/*
* which are no longer used.
*/
static
-int
+ int
dequeuecc(ioq, cc)
- register struct ioqueue *ioq;
- register int cc;
+register struct ioqueue *ioq;
+register int cc;
{
- register struct io *io;
- register struct qelem *qp;
- register int nbytes;
- register int there;
+ register struct io *io;
+ register struct qelem *qp;
+ register int nbytes;
+ register int there;
- nbytes = 0;
- while ((qp = ioq->queue.q_forw) != &ioq->queue) {
- io = (struct io *) qp;
- there = io->nbytes - io->offset;
- if (there < 0) {
- fprintf(stderr, "dequeuecc: nbytes %d, offset %d\n",
- io->nbytes, io->offset);
- continue;
- }
- if (cc > there) {
- cc -= there;
- nbytes += there;
- (void) remque(&io->queue);
- free(io->data);
- } else {
- io->offset += cc;
- nbytes += cc;
- break;
- }
+ nbytes = 0;
+ while ((qp = ioq->queue.q_forw) != &ioq->queue) {
+ io = (struct io *)qp;
+ there = io->nbytes - io->offset;
+ if (there < 0) {
+ fprintf(stderr, "dequeuecc: nbytes %d, offset %d\n",
+ io->nbytes, io->offset);
+ continue;
+ }
+ if (cc > there) {
+ cc -= there;
+ nbytes += there;
+ (void)remque(&io->queue);
+ free(io->data);
+ } else {
+ io->offset += cc;
+ nbytes += cc;
+ break;
}
- ioq->cc -= nbytes;
- return nbytes;
+ }
+ ioq->cc -= nbytes;
+ return nbytes;
}