less horrible strncpy's
[apps/madmutt.git] / nntp / newsrc.c
index ef9537e..ff9df9e 100644 (file)
 #include <config.h>
 #endif
 
+#include <lib-lib/mem.h>
+#include <lib-lib/str.h>
+#include <lib-lib/file.h>
+#include <lib-lib/macros.h>
+
 #include "mutt.h"
 #include "mutt_curses.h"
 #include "sort.h"
 #include "rfc1524.h"
 #include "rfc2047.h"
 
-#include "lib/mem.h"
-#include "lib/str.h"
-#include "lib/intl.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)
@@ -41,7 +44,7 @@ void nntp_add_to_list (NNTP_SERVER * s, NNTP_DATA * d)
   if (!s || !d)
     return;
 
-  l = mem_calloc (1, sizeof (LIST));
+  l = p_new(LIST, 1);
   if (s->list)
     s->tail->next = l;
   else
@@ -73,8 +76,7 @@ static int nntp_parse_newsrc_line (NNTP_SERVER * news, char *line)
     len = sizeof (group);
   strfcpy (group, line, len);
   if ((data = (NNTP_DATA *) hash_find (news->newsgroups, group)) == NULL) {
-    data =
-      (NNTP_DATA *) mem_calloc (1, sizeof (NNTP_DATA) + str_len (group) + 1);
+    data = xmalloc(sizeof(NNTP_DATA) + m_strlen(group) + 1);
     data->group = (char *) data + sizeof (NNTP_DATA);
     strcpy (data->group, group);
     data->nserv = news;
@@ -86,10 +88,10 @@ static int nntp_parse_newsrc_line (NNTP_SERVER * news, char *line)
     nntp_add_to_list (news, data);
   }
   else
-    mem_free ((void **) &data->entries);
+    p_delete(&data->entries);
 
   data->rc = 1;
-  data->entries = mem_calloc (x * 2, sizeof (NEWSRC_ENTRY));
+  data->entries = p_new(NEWSRC_ENTRY, x * 2);
   data->max = x * 2;
 
   if (*p == ':')
@@ -148,10 +150,10 @@ static int slurp_newsrc (NNTP_SERVER * news)
     return -1;
   }
 
-  buf = mem_malloc (sb.st_size + 1);
+  buf = p_new(char, sb.st_size + 1);
   while (fgets (buf, sb.st_size + 1, fp))
     nntp_parse_newsrc_line (news, buf);
-  mem_free (&buf);
+  p_delete(&buf);
 
   mx_unlock_file (news->newsrc, fileno (fp), 0);
   fclose (fp);
@@ -199,13 +201,13 @@ static int nntp_parse_cacheindex (NNTP_SERVER * news)
 
   set_option (OPTNEWSCACHE);
 
-  mem_free (&news->cache);
+  p_delete(&news->cache);
   snprintf (buf, sizeof (buf), "%s/.index", dir);
   if (!(index = safe_fopen (buf, "a+")))
     return 0;
   rewind (index);
   while (fgets (buf, sizeof (buf), index)) {
-    buf[str_len (buf) - 1] = 0;  /* strip ending '\n' */
+    buf[m_strlen(buf) - 1] = 0;  /* strip ending '\n' */
     if (!str_ncmp (buf, "#: ", 3) &&
         !str_casecmp (buf + 3, news->conn->account.host))
       break;
@@ -217,18 +219,16 @@ static int nntp_parse_cacheindex (NNTP_SERVER * news)
     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")) {
-      news->cache = str_dup (file);
+    if (!m_strcmp(buf, "ALL")) {
+      news->cache = m_strdup(file);
       news->newgroups_time = m;
     }
     else if (news->newsgroups) {
       if ((data = (NNTP_DATA *) hash_find (news->newsgroups, buf)) == NULL) {
-        data =
-          (NNTP_DATA *) mem_calloc (1,
-                                     sizeof (NNTP_DATA) + str_len (buf) + 1);
+        data = xmalloc(sizeof(NNTP_DATA) + m_strlen(buf) + 1);
         data->group = (char *) data + sizeof (NNTP_DATA);
         strcpy (data->group, buf);
         data->nserv = news;
@@ -239,7 +239,7 @@ static int nntp_parse_cacheindex (NNTP_SERVER * news)
         hash_insert (news->newsgroups, data->group, data, 0);
         nntp_add_to_list (news, data);
       }
-      data->cache = str_dup (file);
+      data->cache = m_strdup(file);
       t = 0;
       if (!data->firstMessage || data->lastMessage < m)
         t = 1;
@@ -265,7 +265,7 @@ const char *nntp_format_str (char *dest, size_t destlen, char op,
 
   switch (op) {
   case 's':
-    strncpy (fn, NewsServer, sizeof (fn) - 1);
+    m_strcpy(fn, sizeof (fn), NewsServer);
     str_tolower (fn);
     snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
     snprintf (dest, destlen, tmp, fn);
@@ -288,7 +288,7 @@ int nntp_parse_url (const char *server, ACCOUNT * acct,
   acct->port = NNTP_PORT;
   acct->type = M_ACCT_TYPE_NNTP;
 
-  c = str_dup (server);
+  c = m_strdup(server);
   url_parse_ciss (&url, c);
 
   if (url.scheme == U_NNTP || url.scheme == U_NNTPS) {
@@ -304,7 +304,7 @@ int nntp_parse_url (const char *server, ACCOUNT * acct,
     ret = mutt_account_fromurl (acct, &url);
   }
 
-  mem_free (&c);
+  p_delete(&c);
   return ret;
 }
 
@@ -312,10 +312,10 @@ void nntp_expand_path (char *line, size_t len, ACCOUNT * acct)
 {
   ciss_url_t url;
 
-  url.path = str_dup (line);
+  url.path = m_strdup(line);
   mutt_account_tourl (acct, &url);
   url_ciss_tostring (&url, line, len, 0);
-  mem_free (&url.path);
+  p_delete(&url.path);
 }
 
 /*
@@ -337,13 +337,15 @@ NNTP_SERVER *mutt_select_newsserver (char *server)
   NNTP_SERVER *serv;
   CONNECTION *conn;
 
+  p_clear(&acct, 1);
+
   if (!server || !*server) {
     mutt_error _("No newsserver defined!");
 
     return NULL;
   }
 
-  buf = p = mem_calloc (str_len (server) + 10, sizeof (char));
+  buf = p = p_new(char, m_strlen(server) + 10);
   if (url_check_scheme (server) == U_UNKNOWN) {
     strcpy (buf, "nntp://");
     p = strchr (buf, '\0');
@@ -351,11 +353,11 @@ NNTP_SERVER *mutt_select_newsserver (char *server)
   strcpy (p, server);
 
   if ((nntp_parse_url (buf, &acct, file, sizeof (file))) < 0 || *file) {
-    mem_free (&buf);
+    p_delete(&buf);
     mutt_error (_("%s is an invalid newsserver specification!"), server);
     return NULL;
   }
-  mem_free (&buf);
+  p_delete(&buf);
 
   conn = mutt_conn_find (NULL, &acct);
   if (!conn)
@@ -393,9 +395,9 @@ NNTP_SERVER *mutt_select_newsserver (char *server)
   }
 
   /* New newsserver */
-  serv = mem_calloc (1, sizeof (NNTP_SERVER));
+  serv = p_new(NNTP_SERVER, 1);
   serv->conn = conn;
-  serv->newsrc = str_dup (file);
+  serv->newsrc = m_strdup(file);
   serv->newsgroups = hash_create (1009);
   slurp_newsrc (serv);          /* load .newsrc */
   nntp_parse_cacheindex (serv); /* load .index */
@@ -406,9 +408,9 @@ NNTP_SERVER *mutt_select_newsserver (char *server)
     for (list = serv->list; list; list = list->next)
       list->data = NULL;
     mutt_free_list (&serv->list);
-    mem_free (&serv->newsrc);
-    mem_free (&serv->cache);
-    mem_free (&serv);
+    p_delete(&serv->newsrc);
+    p_delete(&serv->cache);
+    p_delete(&serv);
     return NULL;
   }
   nntp_clear_cacheindex (serv);
@@ -508,12 +510,12 @@ static void nntp_create_newsrc_line (NNTP_DATA * data, char **buf,
     len += *buflen;
     *buflen *= 2;
     line = *buf;
-    mem_realloc (buf, *buflen);
+    p_realloc(buf, *buflen);
     line = *buf + (*pline - line);
   }
   strcpy (line, data->group);
-  len -= str_len (line) + 1;
-  line += str_len (line);
+  len -= m_strlen(line) + 1;
+  line += m_strlen(line);
   *line++ = data->subscribed ? ':' : '!';
   *line++ = ' ';
   *line = '\0';
@@ -524,7 +526,7 @@ static void nntp_create_newsrc_line (NNTP_DATA * data, char **buf,
       *buflen *= 2;
       *pline = line;
       line = *buf;
-      mem_realloc (buf, *buflen);
+      p_realloc(buf, *buflen);
       line = *buf + (*pline - line);
     }
     if (x) {
@@ -572,7 +574,7 @@ void newsrc_gen_entries (CONTEXT * ctx)
   }
 
   if (!data->max) {
-    data->entries = mem_calloc (5, sizeof (NEWSRC_ENTRY));
+    data->entries = p_new(NEWSRC_ENTRY, 5);
     data->max = 5;
   }
 
@@ -594,7 +596,7 @@ void newsrc_gen_entries (CONTEXT * ctx)
           !ctx->hdrs[x]->read) {
         if (data->num >= data->max) {
           data->max = data->max * 2;
-          mem_realloc (&data->entries, data->max * sizeof (NEWSRC_ENTRY));
+          p_realloc(&data->entries, data->max);
         }
         data->entries[data->num].first = first;
         data->entries[data->num].last = last - 1;
@@ -614,7 +616,7 @@ void newsrc_gen_entries (CONTEXT * ctx)
   if (series && first <= data->lastLoaded) {
     if (data->num >= data->max) {
       data->max = data->max * 2;
-      mem_realloc (&data->entries, data->max * sizeof (NEWSRC_ENTRY));
+      p_realloc(&data->entries, data->max);
     }
     data->entries[data->num].first = first;
     data->entries[data->num].last = data->lastLoaded;
@@ -628,13 +630,13 @@ 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];
-  char tmpfile[_POSIX_PATH_MAX];
+  char tmpfile[_POSIX_PATH_MAX], link[_POSIX_PATH_MAX];
   char *c;
-  int ext = 0, done = 0, r = 0;
+  int ext = 0, done = 0, r = 0, l = 0;
 
   /* if file not exist, create it */
   if ((ifp = safe_fopen (filename, "a")))
@@ -649,7 +651,14 @@ static int mutt_update_list_file (char *filename, char *section,
     mutt_error (_("Unable to lock %s"), filename);
     return -1;
   }
-  snprintf (tmpfile, sizeof(tmpfile), "%s.tmp", filename);
+  /* use mutt_adv_mktemp() to get a tempfile in the same
+   * directory as filename is so that we can follow symlinks
+   * via rename(2); as dirname(2) may modify its argument,
+   * temporarily use buf as copy of it
+   */
+  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"))) {
     fclose (ifp);
@@ -721,9 +730,13 @@ static int mutt_update_list_file (char *filename, char *section,
     mutt_error (_("Can't write %s"), tmpfile);
     return -1;
   }
-  if (rename (tmpfile, filename) < 0) {
+  link[0] = '\0';
+  if ((l = readlink (filename, link, sizeof (link)-1)) > 0)
+    link[l] = '\0';
+  debug_print (1, ("Renaming %s to %s\n",tmpfile, l > 0 ? link : filename));
+  if (rename (tmpfile, l > 0 ? link : filename) < 0) {
     unlink (tmpfile);
-    mutt_error (_("Can't rename %s to %s"), tmpfile, filename);
+    mutt_error (_("Can't rename %s to %s"), tmpfile, l > 0 ? link : filename);
     return -1;
   }
   return 0;
@@ -740,7 +753,7 @@ int mutt_newsrc_update (NNTP_SERVER * news)
   if (!news)
     return -1;
   llen = len = 10 * LONG_STRING;
-  line = buf = mem_calloc (1, len);
+  line = buf = p_new(char, len);
   /* we will generate full newsrc here */
   for (tmp = news->list; tmp; tmp = tmp->next) {
     data = (NNTP_DATA *) tmp->data;
@@ -748,18 +761,18 @@ int mutt_newsrc_update (NNTP_SERVER * news)
       continue;
     nntp_create_newsrc_line (data, &buf, &line, &llen);
     debug_print (2, ("Added to newsrc: %s\n", line));
-    line += str_len (line);
+    line += m_strlen(line);
   }
   /* 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);
     news->size = st.st_size;
     news->mtime = st.st_mtime;
   }
-  mem_free (&buf);
+  p_delete(&buf);
   return r;
 }
 
@@ -774,7 +787,7 @@ static FILE *mutt_mkname (char *s)
     return fp;
 
   nntp_cache_expand (buf, "cache-XXXXXX");
-  pc = buf + str_len (buf) - 12; /* positioning to "cache-XXXXXX" */
+  pc = buf + m_strlen(buf) - 12; /* positioning to "cache-XXXXXX" */
   if ((fd = mkstemp (buf)) == -1)
     return NULL;
   strcpy (s, pc);               /* generated name */
@@ -784,8 +797,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;
@@ -844,7 +858,7 @@ int nntp_save_cache_index (NNTP_SERVER * news)
   else {
     strfcpy (buf, news->conn->account.host, sizeof (buf));
     f = mutt_mkname (buf);
-    news->cache = str_dup (buf);
+    news->cache = m_strdup(buf);
     nntp_cache_expand (file, buf);
   }
   if (!f)
@@ -900,7 +914,7 @@ int nntp_save_cache_group (CONTEXT * ctx)
               ((NNTP_DATA *) ctx->data)->nserv->conn->account.host,
               ((NNTP_DATA *) ctx->data)->group);
     f = mutt_mkname (buf);
-    ((NNTP_DATA *) ctx->data)->cache = str_dup (buf);
+    ((NNTP_DATA *) ctx->data)->cache = m_strdup(buf);
     nntp_cache_expand (file, buf);
   }
   if (!f)
@@ -930,7 +944,7 @@ int nntp_save_cache_group (CONTEXT * ctx)
       fputs (buf, f);
       if (h->env->references)
         mutt_write_references (h->env->references, f);
-      snprintf (buf, sizeof (buf), "\t%ld\t%d\tXref: %s\n",
+      snprintf (buf, sizeof (buf), "\t%zd\t%d\tXref: %s\n",
                 h->content->length, h->lines, NONULL (h->env->xref));
       if (fputs (buf, f) == EOF) {
         fclose (f);
@@ -966,7 +980,7 @@ void nntp_delete_cache (NNTP_DATA * data)
 
   nntp_cache_expand (buf, data->cache);
   unlink (buf);
-  mem_free (&data->cache);
+  p_delete(&data->cache);
   data->lastCached = 0;
   nntp_cache_expand (buf, ".index");
   mutt_update_list_file (buf, data->nserv->conn->account.host, data->group,
@@ -980,8 +994,7 @@ NNTP_DATA *mutt_newsgroup_subscribe (NNTP_SERVER * news, char *group)
   if (!news || !news->newsgroups || !group || !*group)
     return NULL;
   if (!(data = (NNTP_DATA *) hash_find (news->newsgroups, group))) {
-    data =
-      (NNTP_DATA *) mem_calloc (1, sizeof (NNTP_DATA) + str_len (group) + 1);
+    data = xmalloc(sizeof(NNTP_DATA) + m_strlen(group) + 1);
     data->group = (char *) data + sizeof (NNTP_DATA);
     strcpy (data->group, group);
     data->nserv = news;
@@ -1022,7 +1035,7 @@ NNTP_DATA *mutt_newsgroup_catchup (NNTP_SERVER * news, char *group)
       !(data = (NNTP_DATA *) hash_find (news->newsgroups, group)))
     return NULL;
   if (!data->max) {
-    data->entries = mem_calloc (5, sizeof (NEWSRC_ENTRY));
+    data->entries = p_new(NEWSRC_ENTRY, 5);
     data->max = 5;
   }
   data->num = 1;
@@ -1046,7 +1059,7 @@ NNTP_DATA *mutt_newsgroup_uncatchup (NNTP_SERVER * news, char *group)
       !(data = (NNTP_DATA *) hash_find (news->newsgroups, group)))
     return NULL;
   if (!data->max) {
-    data->entries = mem_calloc (5, sizeof (NEWSRC_ENTRY));
+    data->entries = p_new(NEWSRC_ENTRY, 5);
     data->max = 5;
   }
   data->num = 1;
@@ -1065,26 +1078,50 @@ NNTP_DATA *mutt_newsgroup_uncatchup (NNTP_SERVER * news, char *group)
 }
 
 /* this routine gives the first newsgroup with new messages */
-void nntp_buffy (char *s)
-{
+void nntp_buffy (char* dst, size_t dstlen) {
   LIST *list;
+  int count = 0;
 
+  /* forward to current group */
   for (list = CurrentNewsSrv->list; list; list = list->next) {
     NNTP_DATA *data = (NNTP_DATA *) list->data;
+    if (data && data->subscribed && data->unread && 
+        Context && Context->magic == M_NNTP &&
+        m_strcmp(data->group, ((NNTP_DATA *) Context->data)->group) == 0) {
+      list = list->next;
+      break;
+    }
+  }
 
-    if (data && data->subscribed && data->unread) {
-      if (Context && Context->magic == M_NNTP &&
-          !str_cmp (data->group, ((NNTP_DATA *) Context->data)->group)) {
-        unsigned int i, unread = 0;
+  *dst = '\0';
 
-        for (i = 0; i < Context->msgcount; i++)
-          if (!Context->hdrs[i]->read && !Context->hdrs[i]->deleted)
-            unread++;
-        if (!unread)
-          continue;
+  while (count < 2) {
+
+    if (!list)
+      list = CurrentNewsSrv->list;
+
+    for (; list; list = list->next) {
+      NNTP_DATA *data = (NNTP_DATA *) list->data;
+
+      if (data && data->subscribed && data->unread) {
+        if (Context && Context->magic == M_NNTP &&
+            !m_strcmp(data->group, ((NNTP_DATA *) Context->data)->group)) {
+          unsigned int i, unread = 0;
+
+          for (i = 0; i < Context->msgcount; i++)
+            if (!Context->hdrs[i]->read && !Context->hdrs[i]->deleted)
+              unread++;
+          if (!unread)
+            continue;
+        }
+        m_strcpy(dst, dstlen, data->group);
+        break;
       }
-      strcpy (s, data->group);
-      break;
     }
+    /* done if found */
+    if (dst && *dst)
+      return;
+    count++;
   }
+  *dst = '\0';
 }