]> code.citadel.org Git - citadel.git/blobdiff - citadel/ipc_c_tcp.c
Fixes for Cygwin (see ChangeLog)
[citadel.git] / citadel / ipc_c_tcp.c
index 94924bc73775b918b359238cfdae2ae7c5fb2e14..7f99b246403079c016b87eef6da67a440b16cf27 100644 (file)
@@ -3,7 +3,7 @@
  * 
  * Citadel/UX client/server IPC - client module using TCP/IP
  *
- * version 1.3
+ * version 1.3 $Id$
  *
  */
 
 #include <string.h>
 #include <pwd.h>
 #include <errno.h>
+#include <stdarg.h>
+#include "citadel.h"
 #include "citadel_decls.h"
 #include "ipc.h"
+#ifndef HAVE_SNPRINTF
+#include "snprintf.h"
+#endif
 
 /*
  * If server_is_local is set to nonzero, the client assumes that it is running
@@ -59,7 +64,7 @@ int connectsock(char *host, char *service, char *protocol)
        struct sockaddr_in sin;
        int s,type;
 
-       bzero((char *)&sin,sizeof(sin));
+       memset(&sin,0,sizeof(sin));
        sin.sin_family = AF_INET;
 
        pse=getservbyname(service,protocol);
@@ -127,7 +132,7 @@ void numericize(char *buf, char *host, char *service, char *protocol)
        struct servent *pse;
        struct sockaddr_in sin;
 
-       bzero((char *)&sin,sizeof(sin));
+       memset(&sin,0,sizeof(sin));
        sin.sin_family = AF_INET;
 
        pse=getservbyname(service,protocol);
@@ -169,8 +174,8 @@ void serv_read(char *buf, int bytes)
        while(len<bytes) {
                rlen = read(serv_sock,&buf[len],bytes-len);
                if (rlen<1) {
-                       printf("\rNetwork error - connection terminated.\n");
-                       printf("%s\n", strerror(errno));
+                       /* printf("\rNetwork error - connection terminated.\n");
+                       printf("%s\n", strerror(errno)); */
                        logoff(3);
                        }
                len = len + rlen;
@@ -189,8 +194,9 @@ void serv_write(char *buf, int nbytes)
                retval = write(serv_sock, &buf[bytes_written],
                        nbytes - bytes_written);
                if (retval < 1) {
+                       /*
                        printf("\rNetwork error - connection terminated.\n");
-                       printf("%s\n", strerror(errno));
+                       printf("%s\n", strerror(errno)); */
                        logoff(3);
                        }
                bytes_written = bytes_written + retval;
@@ -204,13 +210,25 @@ void serv_write(char *buf, int nbytes)
  */
 void serv_gets(char *buf)
 {
-       buf[0] = 0;
-       do {
-               buf[strlen(buf) + 1] = 0;
-               if (strlen(buf) < 255) serv_read(&buf[strlen(buf)], 1);
-               } while (buf[strlen(buf)-1] != 10);
-       if (strlen(buf) > 0) buf[strlen(buf)-1] = 0;
-       /* printf("> %s\n", buf); */
+       int i;
+
+       /* Read one character at a time.
+        */
+       for (i = 0;;i++) {
+               serv_read(&buf[i], 1);
+               if (buf[i] == '\n' || i == 255)
+                       break;
+               }
+
+       /* If we got a long line, discard characters until the newline.
+        */
+       if (i == 255)
+               while (buf[i] != '\n')
+                       serv_read(&buf[i], 1);
+
+       /* Strip the trailing newline.
+        */
+       buf[i] = 0;
        }
 
 
@@ -283,7 +301,7 @@ void attach_to_server(int argc, char **argv)
        printf("Attaching to server...\r");
        fflush(stdout);
 
-       sprintf(buf,"%c%c",
+       snprintf(buf,sizeof buf,"%c%c",
                4,                      /* version 4 */
                1);                     /* method = connect */
        serv_write(buf,2);