a whole lot of size_t -> ssize_t.
[apps/madmutt.git] / nntp / newsrc.c
index 1de5085..e9709f7 100644 (file)
 #include <config.h>
 #endif
 
+#include <unistd.h>
+#include <string.h>
+#include <ctype.h>
+#include <stdlib.h>
+#include <libgen.h>
+#include <sys/stat.h>
+
 #include <lib-lib/mem.h>
 #include <lib-lib/str.h>
+#include <lib-lib/file.h>
 #include <lib-lib/macros.h>
+#include <lib-lib/debug.h>
+
+#include <lib-mime/mime.h>
+
+#include <lib-ui/curses.h>
 
 #include "mutt.h"
-#include "mutt_curses.h"
 #include "sort.h"
 #include "mx.h"
-#include "mime.h"
 #include "nntp.h"
-#include "rfc822.h"
 #include "rfc1524.h"
-#include "rfc2047.h"
-
-#include "lib/debug.h"
-
-#include <unistd.h>
-#include <string.h>
-#include <ctype.h>
-#include <stdlib.h>
-#include <libgen.h>
-#include <sys/stat.h>
 
 void nntp_add_to_list (NNTP_SERVER * s, NNTP_DATA * d)
 {
-  LIST *l;
+  string_list_t *l;
 
   if (!s || !d)
     return;
 
-  l = p_new(LIST, 1);
+  l = p_new(string_list_t, 1);
   if (s->list)
     s->tail->next = l;
   else
@@ -58,7 +58,7 @@ static int nntp_parse_newsrc_line (NNTP_SERVER * news, char *line)
   char group[LONG_STRING];
   int x = 1;
   char *p = line, *b, *h;
-  size_t len;
+  ssize_t len;
 
   while (*p) {
     if (*p++ == ',')
@@ -73,7 +73,7 @@ static int nntp_parse_newsrc_line (NNTP_SERVER * news, char *line)
   len = p + 1 - line;
   if (len > sizeof (group))
     len = sizeof (group);
-  strfcpy (group, line, len);
+  m_strcpy(group, len, line);
   if ((data = (NNTP_DATA *) hash_find (news->newsgroups, group)) == NULL) {
     data = xmalloc(sizeof(NNTP_DATA) + m_strlen(group) + 1);
     data->group = (char *) data + sizeof (NNTP_DATA);
@@ -172,7 +172,7 @@ static int nntp_parse_cacheindex (NNTP_SERVER * news)
   struct stat st;
   char buf[HUGE_STRING], *cp;
   char dir[_POSIX_PATH_MAX], file[_POSIX_PATH_MAX];
-  FILE *index;
+  FILE *idx;
   NNTP_DATA *data;
   int l, m, t;
 
@@ -183,7 +183,7 @@ static int nntp_parse_cacheindex (NNTP_SERVER * news)
   if (!NewsCacheDir || !*NewsCacheDir)
     return 0;
 
-  strfcpy (dir, NewsCacheDir, sizeof (dir));
+  m_strcpy(dir, sizeof(dir), NewsCacheDir);
   mutt_expand_path (dir, sizeof (dir));
 
   if (lstat (dir, &st) || (st.st_mode & S_IFDIR) == 0) {
@@ -202,26 +202,26 @@ static int nntp_parse_cacheindex (NNTP_SERVER * news)
 
   p_delete(&news->cache);
   snprintf (buf, sizeof (buf), "%s/.index", dir);
-  if (!(index = safe_fopen (buf, "a+")))
+  if (!(idx = safe_fopen (buf, "a+")))
     return 0;
-  rewind (index);
-  while (fgets (buf, sizeof (buf), index)) {
+  rewind (idx);
+  while (fgets (buf, sizeof (buf), idx)) {
     buf[m_strlen(buf) - 1] = 0;  /* strip ending '\n' */
-    if (!str_ncmp (buf, "#: ", 3) &&
-        !str_casecmp (buf + 3, news->conn->account.host))
+    if (!m_strncmp(buf, "#: ", 3) &&
+        !m_strcasecmp(buf + 3, news->conn->account.host))
       break;
   }
-  while (fgets (buf, sizeof (buf), index)) {
+  while (fgets (buf, sizeof (buf), idx)) {
     cp = buf;
     while (*cp && *cp != ' ')
       cp++;
     if (!*cp)
       continue;
     cp[0] = 0;
-    if (!str_cmp (buf, "#:"))
+    if (!m_strcmp(buf, "#:"))
       break;
     sscanf (cp + 1, "%s %d %d", file, &l, &m);
-    if (!str_cmp (buf, "ALL")) {
+    if (!m_strcmp(buf, "ALL")) {
       news->cache = m_strdup(file);
       news->newgroups_time = m;
     }
@@ -251,11 +251,11 @@ static int nntp_parse_cacheindex (NNTP_SERVER * news)
         mutt_newsgroup_stat (data);
     }
   }
-  fclose (index);
+  fclose (idx);
   return 0;
 }
 
-const char *nntp_format_str (char *dest, size_t destlen, char op,
+const char *nntp_format_str (char *dest, ssize_t destlen, char op,
                              const char *src, const char *fmt,
                              const char *ifstring, const char *elsestring,
                              unsigned long data, format_flag flags)
@@ -264,8 +264,8 @@ const char *nntp_format_str (char *dest, size_t destlen, char op,
 
   switch (op) {
   case 's':
-    strncpy (fn, NewsServer, sizeof (fn) - 1);
-    str_tolower (fn);
+    m_strcpy(fn, sizeof (fn), NewsServer);
+    m_strtolower(fn);
     snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
     snprintf (dest, destlen, tmp, fn);
     break;
@@ -275,44 +275,44 @@ const char *nntp_format_str (char *dest, size_t destlen, char op,
 
 /* nntp_parse_url: given an NNPT URL, return host, port,
  * username, password and newsgroup will recognise. */
-int nntp_parse_url (const char *server, ACCOUNT * acct,
-                    char *group, size_t group_len)
+int nntp_parse_url (const char *server, ACCOUNT * act,
+                    char *group, ssize_t group_len)
 {
   ciss_url_t url;
   char *c;
   int ret = -1;
 
   /* Defaults */
-  acct->flags = 0;
-  acct->port = NNTP_PORT;
-  acct->type = M_ACCT_TYPE_NNTP;
+  act->flags = 0;
+  act->port = NNTP_PORT;
+  act->type = M_ACCT_TYPE_NNTP;
 
   c = m_strdup(server);
   url_parse_ciss (&url, c);
 
   if (url.scheme == U_NNTP || url.scheme == U_NNTPS) {
     if (url.scheme == U_NNTPS) {
-      acct->flags |= M_ACCT_SSL;
-      acct->port = NNTP_SSL_PORT;
+      act->flags |= M_ACCT_SSL;
+      act->port = NNTP_SSL_PORT;
     }
 
     *group = '\0';
     if (url.path)
-      strfcpy (group, url.path, group_len);
+      m_strcpy(group, group_len, url.path);
 
-    ret = mutt_account_fromurl (acct, &url);
+    ret = mutt_account_fromurl (act, &url);
   }
 
   p_delete(&c);
   return ret;
 }
 
-void nntp_expand_path (char *line, size_t len, ACCOUNT * acct)
+void nntp_expand_path (char *line, ssize_t len, ACCOUNT * act)
 {
   ciss_url_t url;
 
   url.path = m_strdup(line);
-  mutt_account_tourl (acct, &url);
+  mutt_account_tourl (act, &url);
   url_ciss_tostring (&url, line, len, 0);
   p_delete(&url.path);
 }
@@ -331,12 +331,12 @@ NNTP_SERVER *mutt_select_newsserver (char *server)
 {
   char file[_POSIX_PATH_MAX];
   char *buf, *p;
-  LIST *list;
-  ACCOUNT acct;
+  string_list_t *list;
+  ACCOUNT act;
   NNTP_SERVER *serv;
   CONNECTION *conn;
 
-  memset (&acct, 0, sizeof (ACCOUNT));
+  p_clear(&act, 1);
 
   if (!server || !*server) {
     mutt_error _("No newsserver defined!");
@@ -351,14 +351,14 @@ NNTP_SERVER *mutt_select_newsserver (char *server)
   }
   strcpy (p, server);
 
-  if ((nntp_parse_url (buf, &acct, file, sizeof (file))) < 0 || *file) {
+  if ((nntp_parse_url (buf, &act, file, sizeof (file))) < 0 || *file) {
     p_delete(&buf);
     mutt_error (_("%s is an invalid newsserver specification!"), server);
     return NULL;
   }
   p_delete(&buf);
 
-  conn = mutt_conn_find (NULL, &acct);
+  conn = mutt_conn_find (NULL, &act);
   if (!conn)
     return NULL;
 
@@ -406,7 +406,7 @@ NNTP_SERVER *mutt_select_newsserver (char *server)
     hash_destroy (&serv->newsgroups, nntp_delete_data);
     for (list = serv->list; list; list = list->next)
       list->data = NULL;
-    mutt_free_list (&serv->list);
+    string_list_wipe(&serv->list);
     p_delete(&serv->newsrc);
     p_delete(&serv->cache);
     p_delete(&serv);
@@ -499,10 +499,10 @@ static int puti (char *line, int num)
 }
 
 static void nntp_create_newsrc_line (NNTP_DATA * data, char **buf,
-                                     char **pline, size_t * buflen)
+                                     char **pline, ssize_t * buflen)
 {
   char *line = *pline;
-  size_t len = *buflen - (*pline - *buf);
+  ssize_t len = *buflen - (*pline - *buf);
   int x, i;
 
   if (len < LONG_STRING * 10) {
@@ -629,7 +629,7 @@ void newsrc_gen_entries (CONTEXT * ctx)
 }
 
 static int mutt_update_list_file (char *filename, char *section,
-                                  char *key, char *line) {
+                                  const char *key, char *line) {
   FILE *ifp;
   FILE *ofp;
   char buf[HUGE_STRING];
@@ -655,8 +655,8 @@ static int mutt_update_list_file (char *filename, char *section,
    * via rename(2); as dirname(2) may modify its argument,
    * temporarily use buf as copy of it
    */
-  strncpy (buf, filename, sizeof (buf));
-  strncpy (tmpfile, basename (filename), sizeof (tmpfile));
+  m_strcpy(buf, sizeof(buf), filename);
+  m_strcpy(tmpfile, sizeof(tmpfile), basename(filename));
   mutt_adv_mktemp ((const char*) dirname (buf), tmpfile, sizeof (tmpfile));
   debug_print (1, ("Opening %s\n", tmpfile));
   if (!(ofp = fopen (tmpfile, "w"))) {
@@ -671,7 +671,7 @@ static int mutt_update_list_file (char *filename, char *section,
       c = buf;
       while (*c && *c != '\n') c++;
       c[0] = 0;        /* strip EOL */
-      if (!strncmp (buf, "#: ", 3) && !str_casecmp (buf+3, section))
+      if (!strncmp (buf, "#: ", 3) && !m_strcasecmp(buf+3, section))
         done++;
     }
     if (r != EOF && !done) {
@@ -745,9 +745,9 @@ int mutt_newsrc_update (NNTP_SERVER * news)
 {
   char *buf, *line;
   NNTP_DATA *data;
-  LIST *tmp;
+  string_list_t *tmp;
   int r = -1;
-  size_t len, llen;
+  ssize_t len, llen;
 
   if (!news)
     return -1;
@@ -764,7 +764,7 @@ int mutt_newsrc_update (NNTP_SERVER * news)
   }
   /* newrc being fully rewritten */
   if (news->newsrc &&
-      (r = mutt_update_list_file (news->newsrc, NULL, "", buf)) == 0) {
+      (r = mutt_update_list_file(news->newsrc, NULL, "", buf)) == 0) {
     struct stat st;
 
     stat (news->newsrc, &st);
@@ -796,8 +796,9 @@ static FILE *mutt_mkname (char *s)
 /* Updates info into .index file: ALL or about selected newsgroup */
 static int nntp_update_cacheindex (NNTP_SERVER * serv, NNTP_DATA * data)
 {
-  char buf[LONG_STRING], *key = "ALL";
+  char buf[LONG_STRING];
   char file[_POSIX_PATH_MAX];
+  const char *key = "ALL";
 
   if (!serv || !serv->conn || !serv->conn->account.host)
     return -1;
@@ -808,7 +809,7 @@ static int nntp_update_cacheindex (NNTP_SERVER * serv, NNTP_DATA * data)
               data->firstMessage, data->lastLoaded);
   }
   else {
-    strfcpy (file, serv->cache, sizeof (file));
+    m_strcpy(file, sizeof(file), serv->cache);
     snprintf (buf, sizeof (buf), "ALL %s 0 %d", file,
               (int) serv->newgroups_time);
   }
@@ -820,7 +821,7 @@ static int nntp_update_cacheindex (NNTP_SERVER * serv, NNTP_DATA * data)
 void nntp_clear_cacheindex (NNTP_SERVER * news)
 {
   NNTP_DATA *data;
-  LIST *tmp;
+  string_list_t *tmp;
 
   if (option (OPTSAVEUNSUB) || !news)
     return;
@@ -841,7 +842,7 @@ int nntp_save_cache_index (NNTP_SERVER * news)
   char file[_POSIX_PATH_MAX];
   NNTP_DATA *d;
   FILE *f;
-  LIST *l;
+  string_list_t *l;
 
   if (!news || !news->newsgroups)
     return -1;
@@ -854,7 +855,7 @@ int nntp_save_cache_index (NNTP_SERVER * news)
     f = safe_fopen (file, "w");
   }
   else {
-    strfcpy (buf, news->conn->account.host, sizeof (buf));
+    m_strcpy(buf, sizeof(buf), news->conn->account.host);
     f = mutt_mkname (buf);
     news->cache = m_strdup(buf);
     nntp_cache_expand (file, buf);
@@ -1076,8 +1077,8 @@ NNTP_DATA *mutt_newsgroup_uncatchup (NNTP_SERVER * news, char *group)
 }
 
 /* this routine gives the first newsgroup with new messages */
-void nntp_buffy (char* dst, size_t dstlen) {
-  LIST *list;
+void nntp_buffy (char* dst, ssize_t dstlen) {
+  string_list_t *list;
   int count = 0;
 
   /* forward to current group */
@@ -1085,7 +1086,7 @@ void nntp_buffy (char* dst, size_t dstlen) {
     NNTP_DATA *data = (NNTP_DATA *) list->data;
     if (data && data->subscribed && data->unread && 
         Context && Context->magic == M_NNTP &&
-        str_cmp (data->group, ((NNTP_DATA *) Context->data)->group) == 0) {
+        m_strcmp(data->group, ((NNTP_DATA *) Context->data)->group) == 0) {
       list = list->next;
       break;
     }
@@ -1103,7 +1104,7 @@ void nntp_buffy (char* dst, size_t dstlen) {
 
       if (data && data->subscribed && data->unread) {
         if (Context && Context->magic == M_NNTP &&
-            !str_cmp (data->group, ((NNTP_DATA *) Context->data)->group)) {
+            !m_strcmp(data->group, ((NNTP_DATA *) Context->data)->group)) {
           unsigned int i, unread = 0;
 
           for (i = 0; i < Context->msgcount; i++)
@@ -1112,7 +1113,7 @@ void nntp_buffy (char* dst, size_t dstlen) {
           if (!unread)
             continue;
         }
-        strncpy (dst, data->group, dstlen);
+        m_strcpy(dst, dstlen, data->group);
         break;
       }
     }