Merge branch 'master' into nohook
[apps/madmutt.git] / lib-mx / mbox.c
index 57f287a..9fd7a40 100644 (file)
@@ -11,7 +11,7 @@
 
 #include <lib-lib/lib-lib.h>
 
-#include <lib-ui/curses.h>
+#include <lib-ui/lib-ui.h>
 #include <lib-sys/mutt_signal.h>
 
 #include "mutt.h"
 
 /* struct used by mutt_sync_mailbox() to store new offsets */
 struct m_update_t {
-  short valid;
-  off_t hdr;
-  off_t body;
-  long lines;
-  off_t length;
+    short valid;
+    off_t hdr;
+    off_t body;
+    long lines;
+    off_t length;
 };
 
-
-static int mbox_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr __attribute__ ((unused)))
+static int mbox_open_new_message(MESSAGE *msg, CONTEXT *dest, HEADER *hdr)
 {
-  msg->fp = dest->fp;
-  return 0;
+    msg->fp = dest->fp;
+    return 0;
 }
 
 /* prototypes */
@@ -47,11 +46,11 @@ static int mbox_reopen_mailbox (CONTEXT*, int*);
  * excl - exclusive lock?
  * retry - should retry if unable to lock?
  */
-int mbox_lock_mailbox (CONTEXT * ctx, int excl, int retry)
+int mbox_lock_mailbox(CONTEXT *ctx, int excl, int retry)
 {
   int r;
 
-  if ((r = mx_lock_file (ctx->path, fileno (ctx->fp), excl, 1, retry)) == 0)
+  if ((r = mx_lock_file(ctx->path, fileno(ctx->fp), excl, retry)) == 0)
     ctx->locked = 1;
   else if (retry && !excl) {
     ctx->readonly = 1;
@@ -66,7 +65,7 @@ static void mbox_unlock_mailbox (CONTEXT * ctx)
   if (ctx->locked) {
     fflush (ctx->fp);
 
-    mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
+    mx_unlock_file(ctx->path, fileno(ctx->fp));
     ctx->locked = 0;
   }
 }
@@ -100,7 +99,8 @@ static int mbox_parse_mailbox (CONTEXT * ctx)
 
   /* precompute the local timezone to speed up calculation of the
      date received */
-  tz = mutt_local_tz (0);
+  t = time(NULL);
+  tz = localtime(&t)->tm_gmtoff;
 
   loc = ftello (ctx->fp);
   while (fgets (buf, sizeof (buf), ctx->fp) != NULL) {
@@ -227,26 +227,24 @@ static int mbox_parse_mailbox (CONTEXT * ctx)
 /* open a mbox style mailbox */
 static int mbox_open_mailbox (CONTEXT * ctx)
 {
-  int rc;
+    int rc;
 
-  if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
-    mutt_perror (ctx->path);
-    return (-1);
-  }
-  mutt_block_signals ();
-  if (mbox_lock_mailbox (ctx, 0, 1) == -1) {
-    mutt_unblock_signals ();
-    return (-1);
-  }
+    if (!(ctx->fp = fopen(ctx->path, "r"))) {
+        mutt_perror(ctx->path);
+        return -1;
+    }
 
-  if (ctx->magic == M_MBOX)
-    rc = mbox_parse_mailbox (ctx);
-  else
-    rc = -1;
+    mutt_block_signals();
+    if (mbox_lock_mailbox(ctx, 0, 1) < 0) {
+        mutt_unblock_signals();
+        return -1;
+    }
 
-  mbox_unlock_mailbox (ctx);
-  mutt_unblock_signals ();
-  return (rc);
+    rc = ctx->magic == M_MBOX ? mbox_parse_mailbox(ctx) : -1;
+
+    mbox_unlock_mailbox(ctx);
+    mutt_unblock_signals();
+    return rc;
 }
 
 /* check to see if the mailbox has changed on disk.
@@ -344,24 +342,25 @@ static int _mbox_check_mailbox (CONTEXT * ctx, int *index_hint)
   return (-1);
 }
 
-static int mbox_check_mailbox (CONTEXT* ctx, int* index_hint, int lock) {
-  int rc = 0;
+static int mbox_check_mailbox(CONTEXT *ctx, int *index_hint, int lock)
+{
+    int rc = 0;
 
-  if (lock) {
-    mutt_block_signals ();
-    if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
-      mutt_unblock_signals ();
-      return M_LOCKED;
+    if (lock) {
+        mutt_block_signals();
+        if (mbox_lock_mailbox(ctx, 0, 0) < 0) {
+            mutt_unblock_signals();
+            return M_LOCKED;
+        }
     }
-  }
 
-  rc = _mbox_check_mailbox (ctx, index_hint);
+    rc = _mbox_check_mailbox(ctx, index_hint);
 
-  if (lock) {
-    mutt_unblock_signals ();
-    mbox_unlock_mailbox (ctx);
-  }
-  return rc;
+    if (lock) {
+        mutt_unblock_signals ();
+        mbox_unlock_mailbox (ctx);
+    }
+    return rc;
 }
 
 /* return values:
@@ -424,7 +423,7 @@ static int mbox_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused))
     return (-1);
 
   /* Create a temporary file to write the new version of the mailbox in. */
-  fp = m_tempfile(tempfile, _POSIX_PATH_MAX, NONULL(MCore.tmpdir), NULL);
+  fp = m_tempfile(tempfile, _POSIX_PATH_MAX, NONULL(mod_core.tmpdir), NULL);
   if (fp == NULL) {
     mutt_error _("Could not create temporary file!");
     mutt_sleep (5);
@@ -570,7 +569,7 @@ static int mbox_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused))
     char savefile[_POSIX_PATH_MAX];
 
     snprintf(savefile, sizeof (savefile), "%s/mutt.%s-%u",
-             NONULL(MCore.tmpdir), NONULL(MCore.username), (unsigned int)getpid());
+             NONULL(mod_core.tmpdir), NONULL(mod_core.username), (unsigned int)getpid());
     rename (tempfile, savefile);
     mutt_unblock_signals ();
     mx_fastclose_mailbox (ctx);
@@ -650,7 +649,7 @@ bail:                          /* Come here in case of disaster */
 /* close a mailbox opened in write-mode */
 int mbox_close_mailbox (CONTEXT * ctx)
 {
-  mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
+  mx_unlock_file(ctx->path, fileno(ctx->fp));
 
   if (ctx->cinfo)
     mutt_slow_close_compressed (ctx);
@@ -883,4 +882,3 @@ mx_t const mbox_mx = {
     mbox_sync_mailbox,
     mbox_commit_message,
 };
-