[Pkg-wmaker-commits] [wmbiff] 49/77: refactor mailbox stat()'ting to reduce duplicated code and supprort backticks.

Doug Torrance dtorrance-guest at moszumanska.debian.org
Thu Aug 20 03:01:12 UTC 2015


This is an automated email from the git hooks/post-receive script.

dtorrance-guest pushed a commit to tag wmbiff_0_4_0
in repository wmbiff.

commit fada037222c6456c1edfc503012b820f2acad66b
Author: bluehal <bluehal>
Date:   Sat Apr 20 07:53:20 2002 +0000

    refactor mailbox stat()'ting to reduce duplicated code and supprort backticks.
---
 wmbiff/LicqClient.c |  52 ++++++++--------
 wmbiff/mboxClient.c | 166 ++++++++++++++++++++++++++++++----------------------
 2 files changed, 119 insertions(+), 99 deletions(-)

diff --git a/wmbiff/LicqClient.c b/wmbiff/LicqClient.c
index 25c834d..18e93ff 100644
--- a/wmbiff/LicqClient.c
+++ b/wmbiff/LicqClient.c
@@ -1,10 +1,10 @@
-/* $Id: LicqClient.c,v 1.7 2002/04/16 07:37:38 bluehal Exp $ */
+/* $Id: LicqClient.c,v 1.8 2002/04/20 07:53:20 bluehal Exp $ */
 /* Author : Yong-iL Joh ( tolkien at mizi.com )
    Modified: Jorge Garc�a ( Jorge.Garcia at uv.es )
  * 
  * LICQ checker.
  *
- * Last Updated : Mar 20, 05:32:35 CET 2001     
+ * Last Updated : $Date: 2002/04/20 07:53:20 $
  *
  */
 
@@ -20,51 +20,40 @@
 
 int licqCheckHistory(Pop3 pc)
 {
-	struct stat st;
 	struct utimbuf ut;
-	FILE *F;
-	int count_status = 0;
-	char buf[1024];
 
 	DM(pc, DEBUG_INFO, ">Mailbox: '%s'\n", pc->path);
 
-	/* licq file */
-	if (stat(pc->path, &st)) {
-		DM(pc, DEBUG_ERROR, "Can't stat mailbox '%s': %s\n",
-		   pc->path, strerror(errno));
-		return -1;				/* Error stating mailbox */
-	}
+    if(fileHasChanged(pc->path, &ut.actime, &PCM.mtime, &PCM.size) 
+       || pc->OldMsgs < 0) {
+        FILE *F;
+        char buf[1024];
+        int count_status = 0;
 
-	if (st.st_mtime != PCM.mtime || st.st_size != PCM.size
-		|| pc->OldMsgs < 0) {
 		/* file was changed OR initially read */
-		DM(pc, DEBUG_INFO,
-		   "  was changed,"
-		   " TIME: old %lu, new %lu"
-		   " SIZE: old %lu, new %lu\n",
-		   PCM.mtime, (unsigned long) st.st_mtime,
-		   (unsigned long) PCM.size, (unsigned long) st.st_size);
-		ut.actime = st.st_atime;
-		ut.modtime = st.st_mtime;
-		F = openMailbox(pc);
+		DM(pc, DEBUG_INFO, "  was changed,"
+		   " TIME: new %lu SIZE: new %lu\n",
+		   PCM.mtime, (unsigned long) PCM.size);
 
+        F = openMailbox(pc, pc->path);
 		/* count message */
 		while (fgets(buf, BUF_SIZE, F)) {
 			if ((buf[0] == '[') || (buf[0] == '-')) {	/* new, or old licq */
 				count_status++;
 			}
 		}
+		fclose(F);
+
 		pc->TotalMsgs = count_status * 2;
 		pc->UnreadMsgs = pc->TotalMsgs - count_status;
 		DM(pc, DEBUG_INFO, "from: %d status: %d\n", pc->TotalMsgs,
 		   pc->UnreadMsgs);
 
-		fclose(F);
-
+		/* Not clear that resetting the mtime is useful, as mutt
+           is not involved.  Unfortunately, I can't tell whether
+           this cut-and-pasted code is useful */
+		ut.modtime = PCM.mtime;
 		utime(pc->path, &ut);
-		/* Reset atime for MUTT and something others correctly work */
-		PCM.mtime = st.st_mtime;	/* Store new mtime */
-		PCM.size = st.st_size;	/* Store new size */
 	}
 
 	return 0;
@@ -89,3 +78,10 @@ int licqCreate(Pop3 pc, char *str)
 }
 
 /* vim:set ts=4: */
+/*
+ * Local Variables:
+ * tab-width: 4
+ * c-indent-level: 4
+ * c-basic-offset: 4
+ * End:
+ */
diff --git a/wmbiff/mboxClient.c b/wmbiff/mboxClient.c
index b04a36e..c6bc67d 100644
--- a/wmbiff/mboxClient.c
+++ b/wmbiff/mboxClient.c
@@ -1,4 +1,4 @@
-/* $Id: mboxClient.c,v 1.9 2002/04/16 07:37:38 bluehal Exp $ */
+/* $Id: mboxClient.c,v 1.10 2002/04/20 07:53:20 bluehal Exp $ */
 /* Author:		Yong-iL Joh <tolkien at mizi.com>
    Modified:	Jorge Garc�a <Jorge.Garcia at uv.es>
    			 	Rob Funk <rfunk at funknet.net>
@@ -21,96 +21,113 @@
 #define FROM_STR   "From "
 #define STATUS_STR "Status: "
 
-FILE *openMailbox(Pop3 pc)
+FILE *openMailbox(Pop3 pc, const char *mbox_filename)
 {
 	FILE *mailbox;
 
-	if ((mailbox = fopen(pc->path, "r")) == NULL) {
+	if ((mailbox = fopen(mbox_filename, "r")) == NULL) {
 		DM(pc, DEBUG_ERROR, "Error opening mailbox '%s': %s\n",
-		   pc->path, strerror(errno));
+		   mbox_filename, strerror(errno));
 	}
 	return (mailbox);
 }
 
-int mboxCheckHistory(Pop3 pc)
-{
-	struct stat st;
-	struct utimbuf ut;
-	FILE *F;
+/* count the messages in a mailbox */
+static void countMessages(Pop3 pc, const char *mbox_filename) {
+    FILE *F = openMailbox(pc, mbox_filename);
 	char buf[BUF_SIZE];
-
-
 	int is_header = 0;
 	int next_from_is_start_of_header = 1;
 	int count_from = 0, count_status = 0;
 	int len_from = strlen(FROM_STR), len_status = strlen(STATUS_STR);
 
-	DM(pc, DEBUG_INFO, ">Mailbox: '%s'\n", pc->path);
-
-	/* mbox file */
-	if (stat(pc->path, &st)) {
-		DM(pc, DEBUG_ERROR, "Can't stat mailbox '%s': %s\n",
-		   pc->path, strerror(errno));
-		return -1;				/* Error stating mailbox */
-	}
+    if(F == NULL) {
+        pc->TotalMsgs = -1;
+        pc->UnreadMsgs = -1;
+        return;
+    }
+
+    /* count message */
+    while (fgets(buf, BUF_SIZE, F)) {
+        if (buf[0] == '\n') {
+            /* a newline by itself terminates the header */
+            if (is_header)
+                is_header = 0;
+            else
+                next_from_is_start_of_header = 1;
+        } else if (!strncmp(buf, FROM_STR, len_from)) {
+            /* A line starting with "From" is the beginning of a new header.
+               "From" in the text of the mail should get escaped by the MDA.
+               If your MDA doesn't do that, it is broken.
+            */
+            if (next_from_is_start_of_header)
+                is_header = 1;
+            if (is_header)
+                count_from++;
+        } else {
+            next_from_is_start_of_header = 0;
+            if (is_header
+                && !strncmp(buf, STATUS_STR, len_status)
+                && strrchr(buf, 'R')) {
+                count_status++;
+            }
+        }
+    }
+    
+    DM(pc, DEBUG_INFO, "from: %d status: %d\n", count_from,
+       count_status);
+    pc->TotalMsgs = count_from;
+    pc->UnreadMsgs = count_from - count_status;
+    fclose(F);
+}
 
-	if (st.st_mtime != PCM.mtime || st.st_size != PCM.size
-		|| pc->OldMsgs < 0) {
+/* check file status; hold on to file information used 
+   to restore access time */
+int fileHasChanged(const char *mbox_filename,  time_t *atime,
+                   time_t *mtime, off_t *size) {
+    struct stat st;
+    
+    /* mbox file */
+    if (stat(mbox_filename, &st)) {
+		DMA(DEBUG_ERROR, "Can't stat '%s': %s\n",
+		   mbox_filename, strerror(errno));
+	} else if (st.st_mtime != *mtime || st.st_size != *size) {
 		/* file was changed OR initially read */
-		DM(pc, DEBUG_INFO,
-		   "  was changed,"
-		   " TIME: old %lu, new %lu"
-		   " SIZE: old %lu, new %lu\n",
-		   PCM.mtime, (unsigned long) st.st_mtime,
-		   (unsigned long) PCM.size, (unsigned long) st.st_size);
-		ut.actime = st.st_atime;
-		ut.modtime = st.st_mtime;
-		F = openMailbox(pc);
-
-		/* count message */
-		while (fgets(buf, BUF_SIZE, F)) {
-			if (buf[0] == '\n') {
-				/* a newline by itself terminates the header */
-				if (is_header)
-					is_header = 0;
-				else
-					next_from_is_start_of_header = 1;
-			} else if (!strncmp(buf, FROM_STR, len_from)) {
-				/* A line starting with "From" is the beginning of a new header.
-				   "From" in the text of the mail should get escaped by the MDA.
-				   If your MDA doesn't do that, it is broken.
-				 */
-				if (next_from_is_start_of_header)
-					is_header = 1;
-				if (is_header)
-					count_from++;
-			} else {
-				next_from_is_start_of_header = 0;
-				if (!strncmp(buf, STATUS_STR, len_status)) {
-					if (strrchr(buf, 'R')) {
-						if (is_header)
-							count_status++;
-					}
-				}
-			}
-		}
-
-		DM(pc, DEBUG_INFO, "from: %d status: %d\n", count_from,
-		   count_status);
-		pc->TotalMsgs = count_from;
-		pc->UnreadMsgs = count_from - count_status;
-		fclose(F);
-
-		utime(pc->path, &ut);
-		/* Reset atime for MUTT and something others correctly work */
-		PCM.mtime = st.st_mtime;	/* Store new mtime */
-		PCM.size = st.st_size;	/* Store new size */
-	}
+		DMA(DEBUG_INFO, " %s was changed,"
+		   " mTIME: %lu -> %lu; SIZE: %lu -> %lu\n",
+           mbox_filename, *mtime, st.st_mtime,
+           (unsigned long) *size, st.st_size);
+
+        *atime = st.st_atime;
+        *mtime = st.st_mtime;
+        *size = st.st_size;
+        return 1;
+    }
+    return 0;	
+}
 
+int mboxCheckHistory(Pop3 pc)
+{
+    char *mbox_filename = backtickExpand(pc, pc->path);
+    struct utimbuf ut;
+
+	DM(pc, DEBUG_INFO, ">Mailbox: '%s'\n", mbox_filename);
+
+    if(fileHasChanged(mbox_filename, &ut.actime, &PCM.mtime, &PCM.size) 
+       || pc->OldMsgs < 0) {
+
+        countMessages(pc, mbox_filename);
+
+		/* Reset atime for (at least) MUTT to work */
+        /* ut.actime is set above */
+		ut.modtime = PCM.mtime;
+		utime(mbox_filename, &ut);
+	}
+    free(mbox_filename);
 	return 0;
 }
 
-int mboxCreate(Pop3 pc, char *str)
+int mboxCreate(Pop3 pc, const char *str)
 {
 	/* MBOX format: mbox:fullpathname */
 
@@ -131,3 +148,10 @@ int mboxCreate(Pop3 pc, char *str)
 }
 
 /* vim:set ts=4: */
+/*
+ * Local Variables:
+ * tab-width: 4
+ * c-indent-level: 4
+ * c-basic-offset: 4
+ * End:
+ */

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-wmaker/wmbiff.git



More information about the Pkg-wmaker-commits mailing list