drop mem_alloc and mem_free, use my own hand crafted optmized macros that
[apps/madmutt.git] / nntp / newsrc.c
index ef9537e..3f707ab 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"
@@ -32,6 +34,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)
@@ -86,7 +89,7 @@ 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));
@@ -148,10 +151,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 +202,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;
@@ -304,7 +307,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 +318,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,6 +340,8 @@ 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!");
 
@@ -351,11 +356,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)
@@ -406,9 +411,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);
@@ -632,9 +637,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 +654,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 +733,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;
@@ -759,7 +775,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 +946,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 +982,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,
@@ -1065,26 +1081,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 &&
+        str_cmp (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 &&
+            !str_cmp (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;
+        }
+        strncpy (dst, data->group, dstlen);
+        break;
       }
-      strcpy (s, data->group);
-      break;
     }
+    /* done if found */
+    if (dst && *dst)
+      return;
+    count++;
   }
+  *dst = '\0';
 }