From 9edbb96ac60147e928ffd6d6226df8c010e1c430 Mon Sep 17 00:00:00 2001
From: Brendan Cully <brendan@kublai.com>
Date: Wed, 14 May 2008 16:23:52 -0700
Subject: [PATCH] Ensure alias file ends with \n before appending new aliases.
 Closes #1580.

---
 ChangeLog | 10 ++++++++++
 alias.c   | 27 ++++++++++++++++++++++++++-
 2 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index 69198c45..94b28cd8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2008-05-14 15:19 +0200  Samuel Tardieu  <sam@rfc1149.net>  (5518355bc6da)
+
+	* curs_main.c: Always update status bar after sorting method changes
+	(closes #2120).
+
+2008-05-12 23:15 -0700  Brendan Cully  <brendan@kublai.com>  (97aa20438c47)
+
+	* imap/imap.c: Terminate open IMAP message sets at end of mailbox
+	even if the last message is inactive. Closes #3002.
+
 2008-05-11 10:52 +0200  Matthias Andree  <matthias.andree@gmx.de>  (09b0363cdf54)
 
 	* rfc3676.c: In debug output, cast size_t arguments to long for
diff --git a/alias.c b/alias.c
index 0a5b38d8..e8696cdb 100644
--- a/alias.c
+++ b/alias.c
@@ -331,8 +331,26 @@ retry_name:
   if (mutt_get_field (_("Save to file: "), buf, sizeof (buf), M_FILE) != 0)
     return;
   mutt_expand_path (buf, sizeof (buf));
-  if ((rc = fopen (buf, "a")))
+  if ((rc = fopen (buf, "a+")))
   {
+    /* terminate existing file with \n if necessary */
+    if (fseek (rc, 0, SEEK_END))
+      goto fseek_err;
+    if (ftell(rc) > 0)
+    {
+      if (fseek (rc, -1, SEEK_CUR) < 0)
+	goto fseek_err;
+      if (fread(buf, 1, 1, rc) < 0)
+      {
+	mutt_perror (_("Error reading alias file"));
+	return;
+      }
+      if (fseek (rc, 0, SEEK_END) < 0)
+	goto fseek_err;
+      if (buf[0] != '\n')
+	fputc ('\n', rc);
+    }
+
     if (mutt_check_alias_name (new->name, NULL))
       mutt_quote_filename (buf, sizeof (buf), new->name);
     else
@@ -347,6 +365,13 @@ retry_name:
   }
   else
     mutt_perror (buf);
+
+  return;
+  
+  fseek_err:
+  mutt_perror (_("Error seeking in alias file"));
+  fclose(rc);
+  return;
 }
 
 /* 
-- 
2.40.0