]> granicus.if.org Git - neomutt/commitdiff
Fix the multiple connection code. From Brandon Long.
authorThomas Roessler <roessler@does-not-exist.org>
Tue, 20 Oct 1998 09:31:15 +0000 (09:31 +0000)
committerThomas Roessler <roessler@does-not-exist.org>
Tue, 20 Oct 1998 09:31:15 +0000 (09:31 +0000)
mutt_socket.h
socket.c

index 0d9b823f035fe1b3cb2402ad982b48dc4b363efd..9e284b195d4e03cb10d7938060806b31499dcaa1 100644 (file)
@@ -20,7 +20,7 @@
 #ifndef _MUTT_SOCKET_H_
 #define _MUTT_SOCKET_H_ 1
 
-typedef struct
+typedef struct _connection
 {
   char *server;
   int port;
@@ -30,6 +30,7 @@ typedef struct
   int bufpos;
   int available;
   void *data;
+  struct _connection *next;
 } CONNECTION;
 
 int mutt_socket_readchar (CONNECTION *conn, char *c);
index 06be158d1d90185c2f218750c7cfdd08ffa121d4..1d0330193679de12f926f35a151d2b5ae812ceee 100644 (file)
--- a/socket.c
+++ b/socket.c
@@ -31,7 +31,6 @@ static const char rcsid[]="$Id$";
 /* support for multiple socket connections */
 
 static CONNECTION *Connections = NULL;
-static int NumConnections = 0;
 
 /* simple read buffering to speed things up. */
 int mutt_socket_readchar (CONNECTION *conn, char *c)
@@ -61,7 +60,10 @@ int mutt_socket_read_line (char *buf, size_t buflen, CONNECTION *conn)
       break;
     buf[i] = ch;
   }
-  buf[i-1] = 0;
+  if (i)
+    buf[i-1] = '\0';
+  else
+    buf[i] = '\0';
   return (i + 1);
 }
 
@@ -80,33 +82,27 @@ int mutt_socket_write (CONNECTION *conn, const char *buf)
 
 CONNECTION *mutt_socket_select_connection (char *host, int port, int flags)
 {
-  int x;
+  CONNECTION *conn;
 
   if (flags != M_NEW_SOCKET)
   {
-    for (x = 0; x < NumConnections; x++)
+    conn = Connections;
+    while (conn)
     {
-      if (!strcmp (host, Connections[x].server) && 
-         (port == Connections[x].port))
-       return &Connections[x];
+      if (!strcmp (host, conn->server) && (port == conn->port))
+       return conn;
+      conn = conn->next;
     }
   }
-  if (NumConnections == 0)
-  {
-    NumConnections = 1;
-    Connections = (CONNECTION *) safe_malloc (sizeof (CONNECTION));
-  }
-  else
-  {
-    NumConnections++;
-    safe_realloc ((void *)&Connections, sizeof (CONNECTION) * NumConnections);
-  }
-  Connections[NumConnections - 1].bufpos = 0;
-  Connections[NumConnections - 1].available = 0;
-  Connections[NumConnections - 1].uses = 0;
-  Connections[NumConnections - 1].server = safe_strdup (host);
-  Connections[NumConnections - 1].port = port;
+  conn = (CONNECTION *) safe_calloc (1, sizeof (CONNECTION));
+  conn->bufpos = 0;
+  conn->available = 0;
+  conn->uses = 0;
+  conn->server = safe_strdup (host);
+  conn->port = port;
+  conn->next = Connections;
+  Connections = conn;
 
-  return &Connections[NumConnections - 1];
+  return conn;
 }