exit mem_realloc, enters p_realloc/xrealloc.
[apps/madmutt.git] / nntp / newsrc.c
index a8e1576..8f0e514 100644 (file)
@@ -13,6 +13,8 @@
 #include <config.h>
 #endif
 
+#include <lib-lib/mem.h>
+
 #include "mutt.h"
 #include "mutt_curses.h"
 #include "sort.h"
@@ -23,7 +25,6 @@
 #include "rfc1524.h"
 #include "rfc2047.h"
 
-#include "lib/mem.h"
 #include "lib/str.h"
 #include "lib/intl.h"
 #include "lib/debug.h"
@@ -32,6 +33,7 @@
 #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 +43,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 +75,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) + str_len(group) + 1);
     data->group = (char *) data + sizeof (NNTP_DATA);
     strcpy (data->group, group);
     data->nserv = news;
@@ -86,10 +87,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 +149,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,7 +200,7 @@ 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;
@@ -226,9 +227,7 @@ static int nntp_parse_cacheindex (NNTP_SERVER * news)
     }
     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) + str_len(buf) + 1);
         data->group = (char *) data + sizeof (NNTP_DATA);
         strcpy (data->group, buf);
         data->nserv = news;
@@ -304,7 +303,7 @@ int nntp_parse_url (const char *server, ACCOUNT * acct,
     ret = mutt_account_fromurl (acct, &url);
   }
 
-  mem_free (&c);
+  p_delete(&c);
   return ret;
 }
 
@@ -315,7 +314,7 @@ void nntp_expand_path (char *line, size_t len, ACCOUNT * acct)
   url.path = str_dup (line);
   mutt_account_tourl (acct, &url);
   url_ciss_tostring (&url, line, len, 0);
-  mem_free (&url.path);
+  p_delete(&url.path);
 }
 
 /*
@@ -337,13 +336,15 @@ NNTP_SERVER *mutt_select_newsserver (char *server)
   NNTP_SERVER *serv;
   CONNECTION *conn;
 
+  memset (&acct, 0, sizeof (ACCOUNT));
+
   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, str_len (server) + 10);
   if (url_check_scheme (server) == U_UNKNOWN) {
     strcpy (buf, "nntp://");
     p = strchr (buf, '\0');
@@ -351,11 +352,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,7 +394,7 @@ 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->newsgroups = hash_create (1009);
@@ -406,9 +407,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,7 +509,7 @@ 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);
@@ -524,7 +525,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 +573,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 +595,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 +615,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;
@@ -632,9 +633,9 @@ static int mutt_update_list_file (char *filename, char *section,
   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 +650,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
+   */
+  strncpy (buf, filename, sizeof (buf));
+  strncpy (tmpfile, basename (filename), sizeof (tmpfile));
+  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 +729,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 +752,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;
@@ -759,7 +771,7 @@ int mutt_newsrc_update (NNTP_SERVER * news)
     news->size = st.st_size;
     news->mtime = st.st_mtime;
   }
-  mem_free (&buf);
+  p_delete(&buf);
   return r;
 }
 
@@ -930,7 +942,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 +978,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 +992,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) + str_len(group) + 1);
     data->group = (char *) data + sizeof (NNTP_DATA);
     strcpy (data->group, group);
     data->nserv = news;
@@ -1022,7 +1033,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 +1057,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;