]> git.pond.sub.org Git - empserver/blobdiff - src/client/ioqueue.c
(main): Don't bother to close socket before exit().
[empserver] / src / client / ioqueue.c
index 6804412637445feb0b68abe8c91ded3ccd1beb16..872e6ad58521c5a1100f96bd7b10317fc0d6cfba 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -19,9 +19,9 @@
  *
  *  ---
  *
- *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- *  related information and legal notices. It is expected that any future
- *  projects/authors will amend these files as needed.
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
  *
  *  ---
  *
  *     Steve McClure, 1998
  */
 
+#include <config.h>
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/types.h>
+#include "ioqueue.h"
 #include "misc.h"
 #include "queue.h"
-#include "ioqueue.h"
 
-static int ioqtobuf(register struct ioqueue *ioq, s_char *buf, int cc);
-static void enqueuecc(struct ioqueue *ioq, s_char *buf, int cc);
-static int dequeuecc(register struct ioqueue *ioq, register int cc);
+static int ioqtobuf(struct ioqueue *ioq, char *buf, int cc);
+static void enqueuecc(struct ioqueue *ioq, char *buf, int cc);
+static int dequeuecc(struct ioqueue *ioq, int cc);
 
-void insque(struct qelem *, struct qelem *);
-void remque(struct qelem *);
-void initque(struct qelem *p);
-struct qelem *makeqt(int nelem);
 
 void
 ioq_init(struct ioqueue *ioq, int bsize)
 {
-    extern s_char num_teles[];
-
     initque(&ioq->queue);
     ioq->cc = 0;
     ioq->bsize = bsize;
-    *num_teles = '\0';
 }
 
 /*
@@ -65,7 +59,7 @@ ioq_init(struct ioqueue *ioq, int bsize)
  * number of bytes actually found.
  */
 int
-ioq_peek(struct ioqueue *ioq, s_char *buf, int cc)
+ioq_peek(struct ioqueue *ioq, char *buf, int cc)
 {
     return ioqtobuf(ioq, buf, cc);
 }
@@ -79,7 +73,7 @@ ioq_dequeue(struct ioqueue *ioq, int cc)
 }
 
 int
-ioq_read(struct ioqueue *ioq, s_char *buf, int cc)
+ioq_read(struct ioqueue *ioq, char *buf, int cc)
 {
     int n;
 
@@ -90,7 +84,7 @@ ioq_read(struct ioqueue *ioq, s_char *buf, int cc)
 }
 
 void
-ioq_write(struct ioqueue *ioq, s_char *buf, int cc)
+ioq_write(struct ioqueue *ioq, char *buf, int cc)
 {
     enqueuecc(ioq, buf, cc);
 }
@@ -116,25 +110,33 @@ ioq_drain(struct ioqueue *ioq)
     ioq->cc = 0;
 }
 
-s_char *
-ioq_gets(struct ioqueue *ioq, s_char *buf, int cc)
+char *
+ioq_gets(struct ioqueue *ioq, char *buf, int cc, int *eol)
 {
-    register s_char *p;
-    register s_char *end;
+    char *p;
+    char *end;
     int nbytes;
 
+    *eol = 0;
+
+    cc--;
+
     nbytes = ioqtobuf(ioq, buf, cc);
-    if (nbytes < cc)
-       cc = nbytes;
-    end = &buf[cc];
+    end = &buf[nbytes];
     for (p = buf; p < end && *p; p++) {
        if (*p == '\n') {
-           *p = '\0';
-           dequeuecc(ioq, (p - buf) + 1);
+           *++p = '\0';
+           *eol = 1;
+           dequeuecc(ioq, p - buf);
            return buf;
        }
     }
-    return 0;
+    if (cc && (p - buf) == cc) {
+       dequeuecc(ioq, cc);
+       buf[cc] = '\0';
+       return buf;
+    }
+    return NULL;
 }
 
 /*
@@ -148,11 +150,11 @@ ioq_gets(struct ioqueue *ioq, s_char *buf, int cc)
  * left for a higher level.
  */
 static int
-ioqtobuf(register struct ioqueue *ioq, s_char *buf, int cc)
+ioqtobuf(struct ioqueue *ioq, char *buf, int cc)
 {
-    register struct io *io;
+    struct io *io;
     struct qelem *qp;
-    s_char *offset;
+    char *offset;
     int nbytes;
     int nleft;
 
@@ -180,11 +182,11 @@ ioqtobuf(register struct ioqueue *ioq, s_char *buf, int cc)
  * append a buffer to the end of the ioq.
  */
 static void
-enqueuecc(struct ioqueue *ioq, s_char *buf, int cc)
+enqueuecc(struct ioqueue *ioq, char *buf, int cc)
 {
     struct io *io;
 
-    io = (struct io *)malloc(sizeof(*io));
+    io = malloc(sizeof(*io));
     io->nbytes = cc;
     io->offset = 0;
     io->data = buf;
@@ -198,12 +200,12 @@ enqueuecc(struct ioqueue *ioq, s_char *buf, int cc)
  * which are no longer used.
  */
 static int
-dequeuecc(register struct ioqueue *ioq, register int cc)
+dequeuecc(struct ioqueue *ioq, int cc)
 {
-    register struct io *io;
-    register struct qelem *qp;
-    register int nbytes;
-    register int there;
+    struct io *io;
+    struct qelem *qp;
+    int nbytes;
+    int there;
 
     nbytes = 0;
     while ((qp = ioq->queue.q_forw) != &ioq->queue) {