]> code.delx.au - gnu-emacs/blobdiff - lib-src/pop.c
Neutralize language specific to a repository type.
[gnu-emacs] / lib-src / pop.c
index 9eaefa4cb830180fddd351fd533ae1c8be0a1e61..ffe16c5f91123f6c766b7e97a9325ba95a726572 100644 (file)
@@ -1,6 +1,7 @@
 /* pop.c: client routines for talking to a POP3-protocol post-office server
-   Copyright (C) 1991, 1993, 1996, 1997, 1999, 2001, 2002, 2003, 2004,
-                 2005, 2006, 2007, 2008, 2009, 2010  Free Software Foundation, Inc.
+
+Copyright (C) 1991, 1993, 1996-1997, 1999, 2001-2014 Free Software
+Foundation, Inc.
 
 Author: Jonathan Kamens <jik@security.ov.com>
 
@@ -20,11 +21,7 @@ You should have received a copy of the GNU General Public License
 along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 
-#ifdef HAVE_CONFIG_H
 #include <config.h>
-#else
-#define MAIL_USE_POP
-#endif
 
 #ifdef MAIL_USE_POP
 
@@ -33,15 +30,15 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include "ntlib.h"
 #include <winsock.h>
 #undef SOCKET_ERROR
-#define RECV(s,buf,len,flags) recv(s,buf,len,flags)
-#define SEND(s,buf,len,flags) send(s,buf,len,flags)
-#define CLOSESOCKET(s) closesocket(s)
+#define RECV(s,buf,len,flags) recv (s,buf,len,flags)
+#define SEND(s,buf,len,flags) send (s,buf,len,flags)
+#define CLOSESOCKET(s) closesocket (s)
 #else
 #include <netinet/in.h>
 #include <sys/socket.h>
-#define RECV(s,buf,len,flags) read(s,buf,len)
-#define SEND(s,buf,len,flags) write(s,buf,len)
-#define CLOSESOCKET(s) close(s)
+#define RECV(s,buf,len,flags) read (s,buf,len)
+#define SEND(s,buf,len,flags) write (s,buf,len)
+#define CLOSESOCKET(s) close (s)
 #endif
 #include <pop.h>
 
@@ -64,13 +61,8 @@ extern struct servent *hes_getservbyname (/* char *, char * */);
 #include <netdb.h>
 #include <errno.h>
 #include <stdio.h>
-#ifdef STDC_HEADERS
 #include <string.h>
-#define index strchr
-#endif
-#ifdef HAVE_UNISTD_H
 #include <unistd.h>
-#endif
 
 #ifdef KERBEROS
 # ifdef HAVE_KRB5_H
@@ -92,6 +84,8 @@ extern struct servent *hes_getservbyname (/* char *, char * */);
 # endif
 #endif /* KERBEROS */
 
+#include <min-max.h>
+
 #ifdef KERBEROS
 #ifndef KERBEROS5
 extern int krb_sendauth (/* long, int, KTEXT, char *, char *, char *,
@@ -103,14 +97,14 @@ extern char *krb_realmofhost (/* char * */);
 #endif /* KERBEROS */
 
 #ifndef WINDOWSNT
-#if !defined(HAVE_H_ERRNO) || !defined(HAVE_CONFIG_H)
+#ifndef HAVE_H_ERRNO
 extern int h_errno;
 #endif
 #endif
 
 static int socket_connection (char *, int);
 static int pop_getline (popserver, char **);
-static int sendline (popserver, char *);
+static int sendline (popserver, const char *);
 static int fullwrite (int, char *, int);
 static int getok (popserver);
 #if 0
@@ -123,18 +117,14 @@ static char *find_crlf (char *, int);
                                   to be bigger than the original
                                   value of 80 */
 #define POP_PORT 110
-#define KPOP_PORT 1109
 #define POP_SERVICE "pop3"     /* we don't want the POP2 port! */
 #ifdef KERBEROS
+#define KPOP_PORT 1109
 #define KPOP_SERVICE "kpop"    /* never used: look for 20060515 to see why */
 #endif
 
 char pop_error[ERROR_MAX];
-int pop_debug = 0;
-
-#ifndef min
-#define min(a,b) (((a) < (b)) ? (a) : (b))
-#endif
+bool pop_debug = false;
 
 /*
  * Function: pop_open (char *host, char *username, char *password,
@@ -279,8 +269,8 @@ pop_open (char *host, char *username, char *password, int flags)
   server->data = 0;
   server->buffer_index = 0;
   server->buffer_size = GETLINE_MIN;
-  server->in_multi = 0;
-  server->trash_started = 0;
+  server->in_multi = false;
+  server->trash_started = false;
 
   if (getok (server))
     return (0);
@@ -350,9 +340,7 @@ pop_stat (popserver server, int *count, int *size)
   if (strncmp (fromserver, "+OK ", 4))
     {
       if (0 == strncmp (fromserver, "-ERR", 4))
-       {
-         strncpy (pop_error, fromserver, ERROR_MAX);
-       }
+       snprintf (pop_error, ERROR_MAX, "%s", fromserver);
       else
        {
          strcpy (pop_error,
@@ -453,7 +441,7 @@ pop_list (popserver server, int message, int **IDs, int **sizes)
       if (strncmp (fromserver, "+OK ", 4))
        {
          if (! strncmp (fromserver, "-ERR", 4))
-           strncpy (pop_error, fromserver, ERROR_MAX);
+           snprintf (pop_error, ERROR_MAX, "%s", fromserver);
          else
            {
              strcpy (pop_error,
@@ -465,7 +453,7 @@ pop_list (popserver server, int message, int **IDs, int **sizes)
          return (-1);
        }
       (*IDs)[0] = atoi (&fromserver[4]);
-      fromserver = index (&fromserver[4], ' ');
+      fromserver = strchr (&fromserver[4], ' ');
       if (! fromserver)
        {
          strcpy (pop_error,
@@ -496,7 +484,7 @@ pop_list (popserver server, int message, int **IDs, int **sizes)
              return (-1);
            }
          (*IDs)[i] = atoi (fromserver);
-         fromserver = index (fromserver, ' ');
+         fromserver = strchr (fromserver, ' ');
          if (! fromserver)
            {
              strcpy (pop_error,
@@ -676,7 +664,7 @@ pop_top_flush (popserver server)
 }
 
 int
-pop_multi_first (popserver server, char *command, char **response)
+pop_multi_first (popserver server, const char *command, char **response)
 {
   if (server->in_multi)
     {
@@ -692,13 +680,13 @@ pop_multi_first (popserver server, char *command, char **response)
 
   if (0 == strncmp (*response, "-ERR", 4))
     {
-      strncpy (pop_error, *response, ERROR_MAX);
+      snprintf (pop_error, ERROR_MAX, "%s", *response);
       return (-1);
     }
   else if (0 == strncmp (*response, "+OK", 3))
     {
       for (*response += 3; **response == ' '; (*response)++) /* empty */;
-      server->in_multi = 1;
+      server->in_multi = true;
       return (0);
     }
   else
@@ -740,7 +728,7 @@ pop_multi_next (popserver server, char **line)
       if (! fromserver[1])
        {
          *line = 0;
-         server->in_multi = 0;
+         server->in_multi = false;
          return (0);
        }
       else
@@ -865,7 +853,7 @@ pop_last (popserver server)
 
   if (! strncmp (fromserver, "-ERR", 4))
     {
-      strncpy (pop_error, fromserver, ERROR_MAX);
+      snprintf (pop_error, ERROR_MAX, "%s", fromserver);
       return (-1);
     }
   else if (strncmp (fromserver, "+OK ", 4))
@@ -988,7 +976,7 @@ socket_connection (char *host, int flags)
   struct servent *servent;
   struct sockaddr_in addr;
   char found_port = 0;
-  char *service;
+  const char *service;
   int sock;
   char *realhost;
 #ifdef KERBEROS
@@ -1065,15 +1053,14 @@ socket_connection (char *host, int flags)
   sock = socket (PF_INET, SOCK_STREAM, 0);
   if (sock < 0)
     {
-      strcpy (pop_error, POP_SOCKET_ERROR);
-      strncat (pop_error, strerror (errno),
-              ERROR_MAX - sizeof (POP_SOCKET_ERROR));
+      snprintf (pop_error, ERROR_MAX, "%s%s",
+               POP_SOCKET_ERROR, strerror (errno));
       return (-1);
 
     }
 
 #ifdef HAVE_GETADDRINFO
-  memset (&hints, 0, sizeof(hints));
+  memset (&hints, 0, sizeof (hints));
   hints.ai_socktype = SOCK_STREAM;
   hints.ai_flags = AI_CANONNAME;
   hints.ai_family = AF_INET;
@@ -1088,28 +1075,22 @@ socket_connection (char *host, int flags)
        }
     } while (ret != 0);
 
-  if (ret == 0)
-    {
-      it = res;
-      while (it)
-        {
-          if (it->ai_addrlen == sizeof (addr))
-            {
-              struct sockaddr_in *in_a = (struct sockaddr_in *) it->ai_addr;
-              memcpy (&addr.sin_addr, &in_a->sin_addr, sizeof (addr.sin_addr));
-              if (! connect (sock, (struct sockaddr *) &addr, sizeof (addr)))
-                break;
-            }
-          it = it->ai_next;
-        }
-      connect_ok = it != NULL;
-      if (connect_ok)
-        {
-          realhost = alloca (strlen (it->ai_canonname) + 1);
-          strcpy (realhost, it->ai_canonname);
-        }
-      freeaddrinfo (res);
+  for (it = res; it; it = it->ai_next)
+    if (it->ai_addrlen == sizeof addr)
+      {
+       struct sockaddr_in *in_a = (struct sockaddr_in *) it->ai_addr;
+       addr.sin_addr = in_a->sin_addr;
+       if (! connect (sock, (struct sockaddr *) &addr, sizeof addr))
+         break;
+      }
+  connect_ok = it != NULL;
+  if (connect_ok)
+    {
+      realhost = alloca (strlen (it->ai_canonname) + 1);
+      strcpy (realhost, it->ai_canonname);
     }
+  freeaddrinfo (res);
+
 #else /* !HAVE_GETADDRINFO */
   do
     {
@@ -1143,9 +1124,7 @@ socket_connection (char *host, int flags)
   if (! connect_ok)
     {
       CLOSESOCKET (sock);
-      strcpy (pop_error, CONNECT_ERROR);
-      strncat (pop_error, strerror (errno),
-              ERROR_MAX - sizeof (CONNECT_ERROR));
+      snprintf (pop_error, ERROR_MAX, "%s%s", CONNECT_ERROR, strerror (errno));
       return (-1);
 
     }
@@ -1163,9 +1142,8 @@ socket_connection (char *host, int flags)
            krb5_auth_con_free (kcontext, auth_context);
          if (kcontext)
            krb5_free_context (kcontext);
-         strcpy (pop_error, KRB_ERROR);
-         strncat (pop_error, error_message (rem),
-                  ERROR_MAX - sizeof(KRB_ERROR));
+         snprintf (pop_error, ERROR_MAX, "%s%s",
+                   KRB_ERROR, error_message (rem));
          CLOSESOCKET (sock);
          return (-1);
        }
@@ -1203,30 +1181,19 @@ socket_connection (char *host, int flags)
       krb5_free_principal (kcontext, server);
       if (rem)
        {
-         strcpy (pop_error, KRB_ERROR);
-         strncat (pop_error, error_message (rem),
-                  ERROR_MAX - sizeof (KRB_ERROR));
+         int pop_error_len = snprintf (pop_error, ERROR_MAX, "%s%s",
+                                       KRB_ERROR, error_message (rem));
 #if defined HAVE_KRB5_ERROR_TEXT
          if (err_ret && err_ret->text.length)
            {
-             strncat (pop_error, " [server says '",
-                      ERROR_MAX - strlen (pop_error) - 1);
-             strncat (pop_error, err_ret->text.data,
-                      min (ERROR_MAX - strlen (pop_error) - 1,
-                           err_ret->text.length));
-             strncat (pop_error, "']",
-                      ERROR_MAX - strlen (pop_error) - 1);
+             int errlen = err_ret->text.length;
+             snprintf (pop_error + pop_error_len, ERROR_MAX - pop_error_len,
+                       " [server says '.*%s']", errlen, err_ret->text.data);
            }
 #elif defined HAVE_KRB5_ERROR_E_TEXT
-         if (err_ret && err_ret->e_text && strlen(*err_ret->e_text))
-           {
-             strncat (pop_error, " [server says '",
-                      ERROR_MAX - strlen (pop_error) - 1);
-             strncat (pop_error, *err_ret->e_text,
-                      ERROR_MAX - strlen (pop_error) - 1);
-             strncat (pop_error, "']",
-                      ERROR_MAX - strlen (pop_error) - 1);
-           }
+         if (err_ret && err_ret->e_text && **err_ret->e_text)
+           snprintf (pop_error + pop_error_len, ERROR_MAX - pop_error_len,
+                     " [server says '%s']", *err_ret->e_text);
 #endif
          if (err_ret)
            krb5_free_error (kcontext, err_ret);
@@ -1247,9 +1214,7 @@ socket_connection (char *host, int flags)
       free ((char *) ticket);
       if (rem != KSUCCESS)
        {
-         strcpy (pop_error, KRB_ERROR);
-         strncat (pop_error, krb_err_txt[rem],
-                  ERROR_MAX - sizeof (KRB_ERROR));
+         snprintf (pop_error, ERROR_MAX, "%s%s", KRB_ERROR, krb_err_txt[rem]);
          CLOSESOCKET (sock);
          return (-1);
        }
@@ -1354,9 +1319,8 @@ pop_getline (popserver server, char **line)
                  server->buffer_size - server->data - 1, 0);
       if (ret < 0)
        {
-         strcpy (pop_error, GETLINE_ERROR);
-         strncat (pop_error, strerror (errno),
-                  ERROR_MAX - sizeof (GETLINE_ERROR));
+         snprintf (pop_error, ERROR_MAX, "%s%s",
+                   GETLINE_ERROR, strerror (errno));
          pop_trash (server);
          return (-1);
        }
@@ -1414,7 +1378,7 @@ pop_getline (popserver server, char **line)
  * Side effects: Closes the connection on error.
  */
 static int
-sendline (popserver server, char *line)
+sendline (popserver server, const char *line)
 {
 #define SENDLINE_ERROR "Error writing to POP server: "
   int ret;
@@ -1440,9 +1404,7 @@ sendline (popserver server, char *line)
   if (ret < 0)
     {
       pop_trash (server);
-      strcpy (pop_error, SENDLINE_ERROR);
-      strncat (pop_error, strerror (errno),
-              ERROR_MAX - sizeof (SENDLINE_ERROR));
+      snprintf (pop_error, ERROR_MAX, "%s%s", SENDLINE_ERROR, strerror (errno));
       return (ret);
     }
 
@@ -1504,8 +1466,7 @@ getok (popserver server)
     return (0);
   else if (! strncmp (fromline, "-ERR", 4))
     {
-      strncpy (pop_error, fromline, ERROR_MAX);
-      pop_error[ERROR_MAX-1] = '\0';
+      snprintf (pop_error, ERROR_MAX, "%s", fromline);
       return (-1);
     }
   else
@@ -1585,7 +1546,7 @@ pop_trash (popserver server)
       /* avoid recursion; sendline can call pop_trash */
       if (server->trash_started)
        return;
-      server->trash_started = 1;
+      server->trash_started = true;
 
       sendline (server, "RSET");
       sendline (server, "QUIT");
@@ -1626,6 +1587,3 @@ find_crlf (char *in_string, int len)
 }
 
 #endif /* MAIL_USE_POP */
-
-/* arch-tag: ceb37041-b7ad-49a8-a63d-286618b8367d
-   (do not change this comment) */