]> diplodocus.org Git - nmh/blobdiff - uip/flist.c
Make sure to mark the sequence file as closed in the msgs struct.
[nmh] / uip / flist.c
index 408f60820dc4f4df6cdc7fdc4465a2be55832831..d0945fce76958ca60d9fdeb894f42ad207a4bf69 100644 (file)
  * control laws. This software is made available AS IS, and Xerox Corporation
  * makes no warranty about the software, its performance or its conformity to
  * any specification.
- *
- *  $Id$
  */
 
 #include <h/mh.h>
+#include <h/utils.h>
 
 #define FALSE   0
 #define TRUE    1
 #define MAXFOLDERS  100
 
 
-static struct swit switches[] = {
-#define SEQSW           0
-    { "sequence name", 0 },
-#define ALLSW           1
-    { "all", 0 },
-#define NOALLSW         2
-    { "noall", 0 },
-#define RECURSE         3
-    { "recurse", 0 },
-#define NORECURSE       4
-    { "norecurse", 0 },
-#define SHOWZERO        5
-    { "showzero", 0 },
-#define NOSHOWZERO      6
-    { "noshowzero", 0 },
-#define ALPHASW         7
-    { "alpha", 0 },
-#define NOALPHASW       8
-    { "noalpha", 0 },
-#define FASTSW          9
-    { "fast", 0 },
-#define NOFASTSW        10
-    { "nofast", 0 },
-#define TOTALSW         11
-    { "total", -5 },
-#define NOTOTALSW       12
-    { "nototal", -7 },
-#define VERSIONSW      13
-    { "version", 0 },
-#define HELPSW          14
-    { "help", 0 },
-    { NULL, 0 }
-};
+#define FLIST_SWITCHES \
+    X("sequence name", 0, SEQSW) \
+    X("all", 0, ALLSW) \
+    X("noall", 0, NOALLSW) \
+    X("recurse", 0, RECURSE) \
+    X("norecurse", 0, NORECURSE) \
+    X("showzero", 0, SHOWZERO) \
+    X("noshowzero", 0, NOSHOWZERO) \
+    X("alpha", 0, ALPHASW) \
+    X("noalpha", 0, NOALPHASW) \
+    X("fast", 0, FASTSW) \
+    X("nofast", 0, NOFASTSW) \
+    X("total", -5, TOTALSW) \
+    X("nototal", -7, NOTOTALSW) \
+    X("version", 0, VERSIONSW) \
+    X("help", 0, HELPSW) \
+
+#define X(sw, minchars, id) id,
+DEFINE_SWITCH_ENUM(FLIST);
+#undef X
+
+#define X(sw, minchars, id) { sw, minchars, id },
+DEFINE_SWITCH_ARRAY(FLIST, switches);
+#undef X
 
 struct Folder {
     char *name;                        /* name of folder */
@@ -75,7 +65,7 @@ static struct Folder *orders = NULL;
 static int nOrders = 0;
 static int nOrdersAlloced = 0;
 static struct Folder *folders = NULL;
-static int nFolders = 0;
+static unsigned int nFolders = 0;
 static int nFoldersAlloced = 0;
 
 /* info on folders to search */
@@ -85,7 +75,7 @@ static int maxfolders;
 
 /* info on sequences to search for */
 static char *sequencesToDo[NUMATTRS];
-static int numsequences;
+static unsigned int numsequences;
 
 static int all        = FALSE;  /* scan all folders in top level?           */
 static int alphaOrder = FALSE; /* want alphabetical order only             */
@@ -113,7 +103,6 @@ int AddFolder(char *, int);
 void BuildFolderList(char *, int);
 void BuildFolderListRecurse(char *, struct stat *, int);
 void PrintFolders(void);
-static int num_digits (int);
 void AllocFolders(struct Folder **, int *, int);
 int AssignPriority(char *);
 static void do_readonly_folders(void);
@@ -148,8 +137,7 @@ main(int argc, char **argv)
     /* allocate the initial space to record the folder names */
     numfolders = 0;
     maxfolders = MAXFOLDERS;
-    if (!(foldersToDo = (char **) malloc ((size_t) (maxfolders * sizeof(*foldersToDo)))))
-       adios (NULL, "unable to allocate folder storage");
+    foldersToDo = (char **) mh_xmalloc ((size_t) (maxfolders * sizeof(*foldersToDo)));
 
     /* no sequences yet */
     numsequences = 0;
@@ -168,10 +156,10 @@ main(int argc, char **argv)
                snprintf(buf, sizeof(buf), "%s [+folder1 [+folder2 ...]][switches]",
                        invo_name);
                print_help(buf, switches, 1);
-               done(1);
+               done(0);
            case VERSIONSW:
                print_version(invo_name);
-               done (1);
+               done (0);
 
            case SEQSW:
                if (!(cp = *argp++) || *cp == '-')
@@ -228,13 +216,12 @@ main(int argc, char **argv)
             */
            if (numfolders >= maxfolders) {
                maxfolders += MAXFOLDERS;
-               if (!(foldersToDo = (char **) realloc (foldersToDo,
-                       (size_t) (maxfolders * sizeof(*foldersToDo)))))
-                   adios (NULL, "unable to reallocate folder name storage");
+               foldersToDo = (char **) mh_xrealloc (foldersToDo,
+                   (size_t) (maxfolders * sizeof(*foldersToDo)));
            }
            if (*cp == '+' || *cp == '@') {
                foldersToDo[numfolders++] =
-                   path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
+                   pluspath (cp);
            } else
                foldersToDo[numfolders++] = cp;
        }
@@ -278,7 +265,8 @@ main(int argc, char **argv)
     ScanFolders();
     qsort(folders, nFolders, sizeof(struct Folder), (qsort_comp) CompareFolders);
     PrintFolders();
-    return done (0);
+    done (0);
+    return 1;
 }
 
 /*
@@ -296,17 +284,17 @@ GetFolderOrder(void)
     if (!(p = context_find("Flist-Order")))
        return;
     for (;;) {
-       while (isspace(*p))
+       while (isspace((unsigned char) *p))
            ++p;
        s = p;
-       while (*p && !isspace(*p))
+       while (*p && !isspace((unsigned char) *p))
            ++p;
        if (p != s) {
            /* Found one. */
            AllocFolders(&orders, &nOrdersAlloced, nOrders + 1);
            o = &orders[nOrders++];
            o->priority = priority++;
-           o->name = (char *) malloc(p - s + 1);
+           o->name = (char *) mh_xmalloc(p - s + 1);
            strncpy(o->name, s, p - s);
            o->name[p - s] = 0;
        } else
@@ -401,7 +389,8 @@ BuildFolderList(char *dirName, int searchdepth)
 void
 BuildFolderListRecurse(char *dirName, struct stat *s, int searchdepth)
 {
-    char *base, *n, name[PATH_MAX];
+    char *base, name[PATH_MAX];
+    char *n;
     int nlinks;
     DIR *dir;
     struct dirent *dp;
@@ -415,6 +404,12 @@ BuildFolderListRecurse(char *dirName, struct stat *s, int searchdepth)
      * stat them.  But that shouldn't generally be a problem.
      */
     nlinks = s->st_nlink;
+    if (nlinks == 1) {
+      /* Disable the optimization under conditions where st_nlink
+         is set to 1.  That happens on Cygwin, for example:
+         http://cygwin.com/ml/cygwin-apps/2008-08/msg00264.html */
+      nlinks = INT_MAX;
+    }
 
     if (!(dir = opendir(dirName)))
        adios(dirName, "can't open directory");
@@ -435,7 +430,7 @@ BuildFolderListRecurse(char *dirName, struct stat *s, int searchdepth)
        /* Check to see if the name of the file is a number
         * if it is, we assume it's a mail file and skip it
         */
-       for (n = dp->d_name; *n && isdigit(*n); n++);
+       for (n = dp->d_name; *n && isdigit((unsigned char) *n); n++);
        if (!*n)
            continue;
        strncpy (name, base, sizeof(name) - 2);
@@ -466,13 +461,14 @@ BuildFolderListRecurse(char *dirName, struct stat *s, int searchdepth)
 int
 AddFolder(char *name, int force)
 {
-    int i, msgnum, nonzero;
+    unsigned int i;
+    int msgnum, nonzero;
     int seqnum[NUMATTRS], nSeq[NUMATTRS];
     struct Folder *f;
     struct msgs *mp;
 
     /* Read folder and create message structure */
-    if (!(mp = folder_read (name))) {
+    if (!(mp = folder_read (name, 0))) {
        /* Oops, error occurred.  Record it and continue. */
        AllocFolders(&folders, &nFoldersAlloced, nFolders + 1);
        f = &folders[nFolders++];
@@ -536,8 +532,8 @@ void
 PrintFolders(void)
 {
     char tmpname[BUFSIZ];
-    int i, j, len, has_private = 0;
-    int maxfolderlen = 0, maxseqlen = 0;
+    unsigned int i, j, len, has_private = 0;
+    unsigned int maxfolderlen = 0, maxseqlen = 0;
     int maxnum = 0, maxseq = 0;
 
     if (!Total) {
@@ -605,29 +601,6 @@ PrintFolders(void)
     }
 }
 
-/*
- * Calculate the number of digits in a nonnegative integer
- */
-static int
-num_digits (int n)
-{
-    int ndigits = 0;
-
-    /* Sanity check */
-    if (n < 0)
-       adios (NULL, "oops, num_digits called with negative value");
-
-    if (n == 0)
-       return 1;
-
-    while (n) {
-       n /= 10;
-       ndigits++;
-    }
-
-    return ndigits;
-}
-
 /*
  * Put them in priority order.
  */
@@ -652,10 +625,10 @@ AllocFolders(struct Folder **f, int *nfa, int n)
        return;
     if (*f == NULL) {
        *nfa = 10;
-       *f = (struct Folder *) malloc (*nfa * (sizeof(struct Folder)));
+       *f = (struct Folder *) mh_xmalloc (*nfa * (sizeof(struct Folder)));
     } else {
        *nfa *= 2;
-       *f = (struct Folder *) realloc (*f, *nfa * (sizeof(struct Folder)));
+       *f = (struct Folder *) mh_xrealloc (*f, *nfa * (sizeof(struct Folder)));
     }
 }