]> code.citadel.org Git - citadel.git/blobdiff - citadel/clientsocket.c
- port to Cygwin (DLL support, etc.)
[citadel.git] / citadel / clientsocket.c
index 3c5e94caf928db5dd84bb3ce5bbc7a604784f722..286659aef946e8e4787c3253972ae740d196a51c 100644 (file)
@@ -8,6 +8,10 @@
  *
  */
 
+#ifdef DLL_EXPORT
+#define IN_LIBCIT
+#endif
+
 #include "sysdep.h"
 #include <stdlib.h>
 #include <unistd.h>
@@ -23,6 +27,8 @@
 #include <errno.h>
 #include <stdarg.h>
 #include "citadel.h"
+#include "server.h"
+#include "dynloader.h"
 #ifndef HAVE_SNPRINTF
 #include "snprintf.h"
 #endif
@@ -76,21 +82,11 @@ int sock_connect(char *host, char *service, char *protocol)
                return(-1);
        }
 
-/* FIX ... the alarm clock is a problem for multithreaded programs because all
- * threads receive the signal.
-       signal(SIGALRM, timeout);
-       alarm(30);
- */
-
        if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
                lprintf(3, "can't connect to %s.%s: %s\n",
                        host, service, strerror(errno));
                return(-1);
        }
-/*
-       alarm(0);
-       signal(SIGALRM, SIG_IGN);
- */
 
        return (s);
 }
@@ -135,6 +131,7 @@ int sock_write(int sock, char *buf, int nbytes)
 }
 
 
+
 /*
  * Input string from socket - implemented in terms of sock_read()
  * 
@@ -147,13 +144,13 @@ int sock_gets(int sock, char *buf)
         */
        for (i = 0;; i++) {
                if (sock_read(sock, &buf[i], 1) < 0) return(-1);
-               if (buf[i] == '\n' || i == 255)
+               if (buf[i] == '\n' || i == (SIZ-1))
                        break;
        }
 
        /* If we got a long line, discard characters until the newline.
         */
-       if (i == 255)
+       if (i == (SIZ-1))
                while (buf[i] != '\n')
                        if (sock_read(sock, &buf[i], 1) < 0) return(-1);
 
@@ -168,6 +165,27 @@ int sock_gets(int sock, char *buf)
        return(strlen(buf));
 }
 
+/*
+ * Multiline version of sock_gets() ... this is a convenience function for
+ * client side protocol implementations.  It only returns the first line of
+ * a multiline response, discarding the rest.
+ */
+int ml_sock_gets(int sock, char *buf) {
+       char bigbuf[1024];
+       int g;
+
+       g = sock_gets(sock, buf);
+       if (g < 4) return(g);
+       if (buf[3] != '-') return(g);
+
+       do {
+               g = sock_gets(sock, bigbuf);
+               if (g < 0) return(g);
+       } while ( (g >= 4) && (bigbuf[3] == '-') );
+
+       return(strlen(buf));
+}
+
 
 /*
  * sock_puts() - send line to server - implemented in terms of serv_write()
@@ -183,3 +201,19 @@ int sock_puts(int sock, char *buf)
        if (j<0) return(j);
        return(i+j);
 }
+
+
+/*
+ * sock_puts_crlf() - same as sock_puts() but ends line with CRLF, not LF
+ * Returns the number of bytes written, or -1 for error.
+ */
+int sock_puts_crlf(int sock, char *buf)
+{
+       int i, j;
+
+       i = sock_write(sock, buf, strlen(buf));
+       if (i<0) return(i);
+       j = sock_write(sock, "\r\n", 2);
+       if (j<0) return(j);
+       return(i+j);
+}