2 ** flist.c -- list nmh folders containing messages
3 ** -- in a given sequence
6 ** David Nichols, Xerox-PARC, November, 1992
8 ** Copyright (c) 1994 Xerox Corporation.
9 ** Use and copying of this software and preparation of derivative works based
10 ** upon this software are permitted. Any distribution of this software or
11 ** derivative works must comply with all applicable United States export
12 ** control laws. This software is made available AS IS, and Xerox Corporation
13 ** makes no warranty about the software, its performance or its conformity to
27 ** We allocate space to record the names of folders
28 ** (foldersToDo array), this number of elements at a time.
30 #define MAXFOLDERS 100
33 static struct swit switches[] = {
35 { "sequence name", 0 },
63 char *version=VERSION;
66 char *name; /* name of folder */
68 int error; /* error == 1 for unreadable folder */
69 int nMsgs; /* number of messages in folder */
70 int nSeq[NUMATTRS]; /* number of messages in each sequence */
71 int private[NUMATTRS]; /* is given sequence, public or private */
74 static struct Folder *orders = NULL;
75 static int nOrders = 0;
76 static int nOrdersAlloced = 0;
77 static struct Folder *folders = NULL;
78 static unsigned int nFolders = 0;
79 static int nFoldersAlloced = 0;
81 /* info on folders to search */
82 static char **foldersToDo;
83 static int numfolders;
84 static int maxfolders;
86 /* info on sequences to search for */
87 static char *sequencesToDo[NUMATTRS];
88 static unsigned int numsequences;
90 static int all = FALSE; /* scan all folders in top level? */
91 static int alphaOrder = FALSE; /* want alphabetical order only */
92 static int recurse = FALSE; /* show nested folders? */
93 static int showzero = TRUE; /* show folders even if no messages in seq? */
94 static int fastsw = FALSE; /* display info on number of messages in
95 * sequence found, and total num messages */
97 static char curfol[BUFSIZ]; /* name of the current folder */
98 static char *nmhdir; /* base nmh mail directory */
101 ** Type for a compare function for qsort. This keeps
102 ** the compiler happy.
104 typedef int (*qsort_comp) (const void *, const void *);
109 int CompareFolders(struct Folder *, struct Folder *);
110 void GetFolderOrder(void);
111 void ScanFolders(void);
112 int AddFolder(char *, int);
113 void BuildFolderList(char *, int);
114 void BuildFolderListRecurse(char *, struct stat *, int);
115 void PrintFolders(void);
116 void AllocFolders(struct Folder **, int *, int);
117 int AssignPriority(char *);
118 static void do_readonly_folders(void);
123 main(int argc, char **argv)
129 setlocale(LC_ALL, "");
130 invo_name = mhbasename(argv[0]);
132 /* read user profile/context */
136 ** If program was invoked with name ending
137 ** in `s', then add switch `-all'.
139 if (argv[0][strlen(argv[0]) - 1] == 's')
142 arguments = getarguments(invo_name, argc, argv, 1);
145 /* allocate the initial space to record the folder names */
147 maxfolders = MAXFOLDERS;
148 foldersToDo = mh_xcalloc(maxfolders, sizeof(*foldersToDo));
150 /* no sequences yet */
153 /* parse arguments */
154 while ((cp = *argp++)) {
156 switch (smatch(++cp, switches)) {
158 ambigsw(cp, switches);
161 adios(EX_USAGE, NULL, "-%s unknown", cp);
164 snprintf(buf, sizeof(buf), "%s [+folder1 [+folder2 ...]][switches]", invo_name);
165 print_help(buf, switches, 1);
166 exit(argc == 2 ? EX_OK : EX_USAGE);
169 print_version(invo_name);
170 exit(argc == 2 ? EX_OK : EX_USAGE);
173 if (!(cp = *argp++) || *cp == '-')
174 adios(EX_USAGE, NULL, "missing argument to %s",
177 /* check if too many sequences specified */
178 if (numsequences >= NUMATTRS)
179 adios(EX_USAGE, NULL, "too many sequences (more than %d) specified", NUMATTRS);
180 sequencesToDo[numsequences++] = cp;
220 ** Check if we need to allocate more space
223 if (numfolders >= maxfolders) {
224 maxfolders += MAXFOLDERS;
225 foldersToDo = mh_xrealloc(foldersToDo, maxfolders * sizeof(*foldersToDo));
227 if (*cp == '+' || *cp == '@') {
228 foldersToDo[numfolders++] = mh_xstrdup(expandfol(cp));
230 foldersToDo[numfolders++] = cp;
234 /* get current folder */
235 strncpy(curfol, getcurfol(), sizeof(curfol));
237 /* get nmh base directory */
238 nmhdir = toabsdir("+");
241 ** If no sequences specified, we use the `unseen' sequence(s)
242 ** We check to make sure that the Unseen-Sequence entry doesn't
243 ** contain too many sequences.
245 if (numsequences == 0) {
248 if ((cp = context_find(usequence))) {
250 adios(EX_CONFIG, NULL, "profile entry %s set, but empty, and no sequence given", usequence);
253 cp = seq_unseen; /* use default */
256 ap = brkstring(dp, " ", "\n");
257 for (; ap && *ap; ap++) {
258 if (numsequences >= NUMATTRS) {
259 adios(EX_USAGE, NULL, "too many sequences (more than %d) in %s profile entry", NUMATTRS, usequence);
261 sequencesToDo[numsequences++] = *ap;
268 qsort(folders, nFolders, sizeof(struct Folder),
269 (qsort_comp) CompareFolders);
275 ** Read the Flist-Order profile entry to determine
276 ** how to sort folders for output.
282 unsigned char *p, *s;
286 if (!(p = context_find("Flist-Order")))
292 while (*p && !isspace(*p))
296 AllocFolders(&orders, &nOrdersAlloced, nOrders + 1);
297 o = &orders[nOrders++];
298 o->priority = priority++;
299 o->name = mh_xcalloc(p - s + 1, sizeof(char));
300 strncpy(o->name, s, p - s);
308 ** Scan all the necessary folders
317 * change directory to base of nmh directory
319 if (chdir(nmhdir) == NOTOK)
320 adios(EX_OSERR, nmhdir, "unable to change directory to");
322 if (numfolders > 0) {
324 strncpy(curfol, foldersToDo[numfolders - 1], sizeof(curfol));
325 context_replace(curfolder, curfol); /* update current folder */
326 context_save(); /* save the context file */
329 ** Scan each given folder. If -all is given,
330 ** then also scan the 1st level subfolders under
331 ** each given folder.
333 for (i = 0; i < numfolders; ++i)
334 BuildFolderList(foldersToDo[i], all ? 1 : 0);
337 /* Do the readonly folders */
338 do_readonly_folders();
340 /* Now scan the entire nmh directory for folders */
341 BuildFolderList(".", 0);
343 /* Else scan current folder */
344 BuildFolderList(curfol, 0);
350 ** Initial building of folder list for
351 ** the top of our search tree.
355 BuildFolderList(char *dirName, int searchdepth)
359 /* Make sure we have a directory */
360 if ((stat(dirName, &st) == -1) || !S_ISDIR(st.st_mode))
364 ** If base directory, don't add it to the
365 ** folder list. We just recurse into it.
367 if (strcmp(dirName, ".")==0) {
368 BuildFolderListRecurse(".", &st, 0);
373 ** Add this folder to the list.
374 ** If recursing and directory has subfolders,
375 ** then build folder list for subfolders.
377 if (AddFolder(dirName, showzero) && (recurse || searchdepth) &&
379 BuildFolderListRecurse(dirName, &st, searchdepth - 1);
383 ** Recursive building of folder list
387 BuildFolderListRecurse(char *dirName, struct stat *s, int searchdepth)
389 char *base, name[PATH_MAX];
397 ** Keep track of number of directories we've seen so we can
398 ** stop stat'ing entries in this directory once we've seen
399 ** them all. This optimization will fail if you have extra
400 ** directories beginning with ".", since we don't bother to
401 ** stat them. But that shouldn't generally be a problem.
403 nlinks = s->st_nlink;
405 if (!(dir = opendir(dirName)))
406 adios(EX_IOERR, dirName, "can't open directory");
409 ** A hack so that we don't see a
410 ** leading "./" in folder names.
412 base = (strcmp(dirName, ".")==0) ? dirName + 1 : dirName;
414 while (nlinks && (dp = readdir(dir))) {
415 if (strcmp(dp->d_name, ".")==0 ||
416 strcmp(dp->d_name, "..")==0) {
420 if (dp->d_name[0] == '.')
422 /* Check to see if the name of the file is a number
423 ** if it is, we assume it's a mail file and skip it
425 for (n = dp->d_name; *n && isdigit(*n); n++);
428 strncpy(name, base, sizeof(name) - 2);
431 strncat(name, dp->d_name, sizeof(name) - strlen(name) - 1);
432 if ((stat(name, &st) != -1) && S_ISDIR(st.st_mode)) {
434 ** Check if this was really a symbolic link pointing
435 ** to a directory. If not, then decrement link count.
437 if (lstat(name, &st) == -1)
439 /* Add this folder to the list */
440 if (AddFolder(name, showzero) &&
441 (recurse || searchdepth) &&
443 BuildFolderListRecurse(name, &st,
451 ** Add this folder to our list, counting the total number of
452 ** messages and the number of messages in each sequence.
456 AddFolder(char *name, int force)
460 int seqnum[NUMATTRS], nSeq[NUMATTRS];
464 /* Read folder and create message structure */
465 if (!(mp = folder_read(name))) {
466 /* Oops, error occurred. Record it and continue. */
467 AllocFolders(&folders, &nFoldersAlloced, nFolders + 1);
468 f = &folders[nFolders++];
469 f->name = mh_xstrdup(name);
471 f->priority = AssignPriority(f->name);
475 for (i = 0; i < numsequences; i++) {
476 /* Convert sequences to their sequence numbers */
477 if (sequencesToDo[i])
478 seqnum[i] = seq_getnum(mp, sequencesToDo[i]);
482 /* Now count messages in this sequence */
484 if (mp->nummsg > 0 && seqnum[i] != -1) {
485 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg;
487 if (in_sequence(mp, seqnum[i], msgnum))
493 /* Check if any of the sequence checks were nonzero */
495 for (i = 0; i < numsequences; i++) {
502 if (nonzero || force) {
503 /* save general folder information */
504 AllocFolders(&folders, &nFoldersAlloced, nFolders + 1);
505 f = &folders[nFolders++];
506 f->name = mh_xstrdup(name);
507 f->nMsgs = mp->nummsg;
509 f->priority = AssignPriority(f->name);
511 /* record the sequence information */
512 for (i = 0; i < numsequences; i++) {
513 f->nSeq[i] = nSeq[i];
514 f->private[i] = (seqnum[i] != -1) ?
515 is_seq_private(mp, seqnum[i]) : 0;
519 folder_free(mp); /* free folder/message structure */
524 ** Print the folder/sequence information
530 char tmpname[BUFSIZ];
531 unsigned int i, j, len, has_private = 0;
532 unsigned int maxfolderlen = 0, maxseqlen = 0;
533 int maxnum = 0, maxseq = 0;
536 for (i = 0; i < nFolders; i++)
537 printf("%s\n", folders[i].name);
542 ** Find the width we need for various fields
544 for (i = 0; i < nFolders; ++i) {
545 /* find the length of longest folder name */
546 len = strlen(folders[i].name);
547 if (len > maxfolderlen)
550 /* If folder had error, skip the rest */
551 if (folders[i].error)
554 /* find the maximum total messages */
555 if (folders[i].nMsgs > maxnum)
556 maxnum = folders[i].nMsgs;
558 for (j = 0; j < numsequences; j++) {
559 /* find maximum width of sequence name */
560 len = strlen(sequencesToDo[j]);
561 if ((folders[i].nSeq[j] > 0 || showzero) &&
565 /* find the maximum number of messages in sequence */
566 if (folders[i].nSeq[j] > maxseq)
567 maxseq = folders[i].nSeq[j];
570 ** check if this sequence is private in any of
573 if (folders[i].private[j])
578 /* Now print all the folder/sequence information */
579 for (i = 0; i < nFolders; i++) {
580 for (j = 0; j < numsequences; j++) {
581 if (folders[i].nSeq[j] > 0 || showzero) {
582 /* Add `+' to end of name of current folder */
583 if (strcmp(curfol, folders[i].name)!=0)
584 snprintf(tmpname, sizeof(tmpname),
585 "%s", folders[i].name);
587 snprintf(tmpname, sizeof(tmpname),
591 if (folders[i].error) {
592 printf("%-*s is unreadable\n",
598 printf("%-*s has %*d in sequence %-*s%s; out of %*d\n", maxfolderlen+1, tmpname, num_digits(maxseq), folders[i].nSeq[j], maxseqlen, sequencesToDo[j], !has_private ? "" : folders[i].private[j] ? " (private)" : " ", num_digits(maxnum), folders[i].nMsgs);
605 ** Put them in priority order.
609 CompareFolders(struct Folder *f1, struct Folder *f2)
611 if (!alphaOrder && f1->priority != f2->priority)
612 return f1->priority - f2->priority;
614 return strcmp(f1->name, f2->name);
618 ** Make sure we have at least n folders allocated.
622 AllocFolders(struct Folder **f, int *nfa, int n)
628 *f = mh_xcalloc(*nfa, sizeof(struct Folder));
631 *f = mh_xrealloc(*f, *nfa * sizeof(struct Folder));
636 ** Return the priority for a name. The highest comes from an exact match.
637 ** After that, the longest match (then first) assigns the priority.
640 AssignPriority(char *name)
648 for (i = 0; i < nOrders; ++i) {
650 if (strcmp(name, o->name)==0)
652 ol = strlen(o->name);
657 if (o->name[0] == '*' &&
658 strcmp(o->name + 1, name + (nl - ol + 1))==0) {
661 } else if (o->name[ol - 1] == '*' &&
662 strncmp(o->name, name, ol - 1) == 0) {
671 ** Do the read only folders
675 do_readonly_folders(void)
681 snprintf(atrcur, sizeof(atrcur), "atr-%s-", seq_cur);
682 atrlen = strlen(atrcur);
684 for (np = m_defs; np; np = np->n_next)
685 if (strncmp(np->n_name, atrcur, atrlen)==0
686 && strncmp(np->n_name+atrlen, nmhdir, strlen(nmhdir))!=0)
687 /* Why do we exclude absolute path names? --meillo */
688 BuildFolderList(np->n_name + atrlen, 0);