From f4243cc6788f63f5ea8fa6d950063bf686a8a28c Mon Sep 17 00:00:00 2001 From: Thomas Roessler Date: Tue, 20 Oct 1998 09:31:15 +0000 Subject: [PATCH] Fix the multiple connection code. From Brandon Long. --- mutt_socket.h | 3 ++- socket.c | 42 +++++++++++++++++++----------------------- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/mutt_socket.h b/mutt_socket.h index 0d9b823f..9e284b19 100644 --- a/mutt_socket.h +++ b/mutt_socket.h @@ -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); diff --git a/socket.c b/socket.c index 06be158d..1d033019 100644 --- 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; } -- 2.40.0