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
21 ** We allocate space to record the names of folders
22 ** (foldersToDo array), this number of elements at a time.
24 #define MAXFOLDERS 100
27 static struct swit switches[] = {
29 { "sequence name", 0 },
62 char *name; /* name of folder */
64 int error; /* error == 1 for unreadable folder */
65 int nMsgs; /* number of messages in folder */
66 int nSeq[NUMATTRS]; /* number of messages in each sequence */
67 int private[NUMATTRS]; /* is given sequence, public or private */
70 static struct Folder *orders = NULL;
71 static int nOrders = 0;
72 static int nOrdersAlloced = 0;
73 static struct Folder *folders = NULL;
74 static unsigned int nFolders = 0;
75 static int nFoldersAlloced = 0;
77 /* info on folders to search */
78 static char **foldersToDo;
79 static int numfolders;
80 static int maxfolders;
82 /* info on sequences to search for */
83 static char *sequencesToDo[NUMATTRS];
84 static unsigned int numsequences;
86 static int all = FALSE; /* scan all folders in top level? */
87 static int alphaOrder = FALSE; /* want alphabetical order only */
88 static int recurse = FALSE; /* show nested folders? */
89 static int showzero = TRUE; /* show folders even if no messages in seq? */
90 static int Total = TRUE; /* display info on number of messages in
91 * sequence found, and total num messages */
93 static char curfol[BUFSIZ]; /* name of the current folder */
94 static char *nmhdir; /* base nmh mail directory */
97 ** Type for a compare function for qsort. This keeps
98 ** the compiler happy.
100 typedef int (*qsort_comp) (const void *, const void *);
105 int CompareFolders(struct Folder *, struct Folder *);
106 void GetFolderOrder(void);
107 void ScanFolders(void);
108 int AddFolder(char *, int);
109 void BuildFolderList(char *, int);
110 void BuildFolderListRecurse(char *, struct stat *, int);
111 void PrintFolders(void);
112 void AllocFolders(struct Folder **, int *, int);
113 int AssignPriority(char *);
114 static void do_readonly_folders(void);
119 main(int argc, char **argv)
126 setlocale(LC_ALL, "");
128 invo_name = mhbasename(argv[0]);
130 /* read user profile/context */
134 ** If program was invoked with name ending
135 ** in `s', then add switch `-all'.
137 if (argv[0][strlen(argv[0]) - 1] == 's')
140 arguments = getarguments(invo_name, argc, argv, 1);
143 /* allocate the initial space to record the folder names */
145 maxfolders = MAXFOLDERS;
146 foldersToDo = (char **) mh_xmalloc((size_t)
147 (maxfolders * sizeof(*foldersToDo)));
149 /* no sequences yet */
152 /* parse arguments */
153 while ((cp = *argp++)) {
155 switch (smatch(++cp, switches)) {
157 ambigsw(cp, switches);
160 adios(NULL, "-%s unknown", cp);
163 snprintf(buf, sizeof(buf), "%s [+folder1 [+folder2 ...]][switches]", invo_name);
164 print_help(buf, switches, 1);
167 print_version(invo_name);
171 if (!(cp = *argp++) || *cp == '-')
172 adios(NULL, "missing argument to %s",
175 /* check if too many sequences specified */
176 if (numsequences >= NUMATTRS)
177 adios(NULL, "too many sequences (more than %d) specified", NUMATTRS);
178 sequencesToDo[numsequences++] = cp;
221 ** Check if we need to allocate more space
224 if (numfolders >= maxfolders) {
225 maxfolders += MAXFOLDERS;
226 foldersToDo = (char **) mh_xrealloc(foldersToDo, (size_t) (maxfolders * sizeof(*foldersToDo)));
228 if (*cp == '+' || *cp == '@') {
229 foldersToDo[numfolders++] = getcpy(expandfol(cp));
231 foldersToDo[numfolders++] = cp;
235 /* get current folder */
236 strncpy(curfol, getcurfol(), sizeof(curfol));
238 /* get nmh base directory */
239 nmhdir = toabsdir("+");
242 ** If no sequences specified, we use the `unseen' sequence(s)
243 ** We check to make sure that the Unseen-Sequence entry doesn't
244 ** contain too many sequences.
246 if (numsequences == 0) {
249 if ((cp = context_find(usequence))) {
251 adios(NULL, "profile entry %s set, but empty, and no sequence given", usequence);
254 cp = seq_unseen; /* use default */
257 ap = brkstring(dp, " ", "\n");
258 for (; ap && *ap; ap++) {
259 if (numsequences >= NUMATTRS) {
260 adios(NULL, "too many sequences (more than %d) in %s profile entry", NUMATTRS, usequence);
262 sequencesToDo[numsequences++] = *ap;
269 qsort(folders, nFolders, sizeof(struct Folder),
270 (qsort_comp) CompareFolders);
277 ** Read the Flist-Order profile entry to determine
278 ** how to sort folders for output.
284 unsigned char *p, *s;
288 if (!(p = context_find("Flist-Order")))
294 while (*p && !isspace(*p))
298 AllocFolders(&orders, &nOrdersAlloced, nOrders + 1);
299 o = &orders[nOrders++];
300 o->priority = priority++;
301 o->name = (char *) mh_xmalloc(p - s + 1);
302 strncpy(o->name, s, p - s);
310 ** Scan all the necessary folders
319 * change directory to base of nmh directory
321 if (chdir(nmhdir) == NOTOK)
322 adios(nmhdir, "unable to change directory to");
324 if (numfolders > 0) {
326 strncpy(curfol, foldersToDo[numfolders - 1], sizeof(curfol));
327 context_replace(curfolder, curfol); /* update current folder */
328 context_save(); /* save the context file */
331 ** Scan each given folder. If -all is given,
332 ** then also scan the 1st level subfolders under
333 ** each given folder.
335 for (i = 0; i < numfolders; ++i)
336 BuildFolderList(foldersToDo[i], all ? 1 : 0);
339 /* Do the readonly folders */
340 do_readonly_folders();
342 /* Now scan the entire nmh directory for folders */
343 BuildFolderList(".", 0);
345 /* Else scan current folder */
346 BuildFolderList(curfol, 0);
352 ** Initial building of folder list for
353 ** the top of our search tree.
357 BuildFolderList(char *dirName, int searchdepth)
361 /* Make sure we have a directory */
362 if ((stat(dirName, &st) == -1) || !S_ISDIR(st.st_mode))
366 ** If base directory, don't add it to the
367 ** folder list. We just recurse into it.
369 if (strcmp(dirName, ".")==0) {
370 BuildFolderListRecurse(".", &st, 0);
375 ** Add this folder to the list.
376 ** If recursing and directory has subfolders,
377 ** then build folder list for subfolders.
379 if (AddFolder(dirName, showzero) && (recurse || searchdepth) &&
381 BuildFolderListRecurse(dirName, &st, searchdepth - 1);
385 ** Recursive building of folder list
389 BuildFolderListRecurse(char *dirName, struct stat *s, int searchdepth)
391 char *base, name[PATH_MAX];
399 ** Keep track of number of directories we've seen so we can
400 ** stop stat'ing entries in this directory once we've seen
401 ** them all. This optimization will fail if you have extra
402 ** directories beginning with ".", since we don't bother to
403 ** stat them. But that shouldn't generally be a problem.
405 nlinks = s->st_nlink;
407 if (!(dir = opendir(dirName)))
408 adios(dirName, "can't open directory");
411 ** A hack so that we don't see a
412 ** leading "./" in folder names.
414 base = (strcmp(dirName, ".")==0) ? dirName + 1 : dirName;
416 while (nlinks && (dp = readdir(dir))) {
417 if (strcmp(dp->d_name, ".")==0 ||
418 strcmp(dp->d_name, "..")==0) {
422 if (dp->d_name[0] == '.')
424 /* Check to see if the name of the file is a number
425 ** if it is, we assume it's a mail file and skip it
427 for (n = dp->d_name; *n && isdigit(*n); n++);
430 strncpy(name, base, sizeof(name) - 2);
433 strncat(name, dp->d_name, sizeof(name) - strlen(name) - 1);
434 if ((stat(name, &st) != -1) && S_ISDIR(st.st_mode)) {
436 ** Check if this was really a symbolic link pointing
437 ** to a directory. If not, then decrement link count.
439 if (lstat(name, &st) == -1)
441 /* Add this folder to the list */
442 if (AddFolder(name, showzero) &&
443 (recurse || searchdepth) &&
445 BuildFolderListRecurse(name, &st,
453 ** Add this folder to our list, counting the total number of
454 ** messages and the number of messages in each sequence.
458 AddFolder(char *name, int force)
462 int seqnum[NUMATTRS], nSeq[NUMATTRS];
466 /* Read folder and create message structure */
467 if (!(mp = folder_read(name))) {
468 /* Oops, error occurred. Record it and continue. */
469 AllocFolders(&folders, &nFoldersAlloced, nFolders + 1);
470 f = &folders[nFolders++];
471 f->name = getcpy(name);
473 f->priority = AssignPriority(f->name);
477 for (i = 0; i < numsequences; i++) {
478 /* Convert sequences to their sequence numbers */
479 if (sequencesToDo[i])
480 seqnum[i] = seq_getnum(mp, sequencesToDo[i]);
484 /* Now count messages in this sequence */
486 if (mp->nummsg > 0 && seqnum[i] != -1) {
487 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg;
489 if (in_sequence(mp, seqnum[i], msgnum))
495 /* Check if any of the sequence checks were nonzero */
497 for (i = 0; i < numsequences; i++) {
504 if (nonzero || force) {
505 /* save general folder information */
506 AllocFolders(&folders, &nFoldersAlloced, nFolders + 1);
507 f = &folders[nFolders++];
508 f->name = getcpy(name);
509 f->nMsgs = mp->nummsg;
511 f->priority = AssignPriority(f->name);
513 /* record the sequence information */
514 for (i = 0; i < numsequences; i++) {
515 f->nSeq[i] = nSeq[i];
516 f->private[i] = (seqnum[i] != -1) ?
517 is_seq_private(mp, seqnum[i]) : 0;
521 folder_free(mp); /* free folder/message structure */
526 ** Print the folder/sequence information
532 char tmpname[BUFSIZ];
533 unsigned int i, j, len, has_private = 0;
534 unsigned int maxfolderlen = 0, maxseqlen = 0;
535 int maxnum = 0, maxseq = 0;
538 for (i = 0; i < nFolders; i++)
539 printf("%s\n", folders[i].name);
544 ** Find the width we need for various fields
546 for (i = 0; i < nFolders; ++i) {
547 /* find the length of longest folder name */
548 len = strlen(folders[i].name);
549 if (len > maxfolderlen)
552 /* If folder had error, skip the rest */
553 if (folders[i].error)
556 /* find the maximum total messages */
557 if (folders[i].nMsgs > maxnum)
558 maxnum = folders[i].nMsgs;
560 for (j = 0; j < numsequences; j++) {
561 /* find maximum width of sequence name */
562 len = strlen(sequencesToDo[j]);
563 if ((folders[i].nSeq[j] > 0 || showzero) &&
567 /* find the maximum number of messages in sequence */
568 if (folders[i].nSeq[j] > maxseq)
569 maxseq = folders[i].nSeq[j];
572 ** check if this sequence is private in any of
575 if (folders[i].private[j])
580 /* Now print all the folder/sequence information */
581 for (i = 0; i < nFolders; i++) {
582 for (j = 0; j < numsequences; j++) {
583 if (folders[i].nSeq[j] > 0 || showzero) {
584 /* Add `+' to end of name of current folder */
585 if (strcmp(curfol, folders[i].name)!=0)
586 snprintf(tmpname, sizeof(tmpname),
587 "%s", folders[i].name);
589 snprintf(tmpname, sizeof(tmpname),
593 if (folders[i].error) {
594 printf("%-*s is unreadable\n",
600 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);
607 ** Put them in priority order.
611 CompareFolders(struct Folder *f1, struct Folder *f2)
613 if (!alphaOrder && f1->priority != f2->priority)
614 return f1->priority - f2->priority;
616 return strcmp(f1->name, f2->name);
620 ** Make sure we have at least n folders allocated.
624 AllocFolders(struct Folder **f, int *nfa, int n)
630 *f = (struct Folder *) mh_xmalloc(
631 *nfa * (sizeof(struct Folder)));
634 *f = (struct Folder *) mh_xrealloc(
635 *f, *nfa * (sizeof(struct Folder)));
640 ** Return the priority for a name. The highest comes from an exact match.
641 ** After that, the longest match (then first) assigns the priority.
644 AssignPriority(char *name)
652 for (i = 0; i < nOrders; ++i) {
654 if (strcmp(name, o->name)==0)
656 ol = strlen(o->name);
661 if (o->name[0] == '*' &&
662 strcmp(o->name + 1, name + (nl - ol + 1))==0) {
665 } else if (o->name[ol - 1] == '*' &&
666 strncmp(o->name, name, ol - 1) == 0) {
675 ** Do the read only folders
679 do_readonly_folders(void)
683 register struct node *np;
685 snprintf(atrcur, sizeof(atrcur), "atr-%s-", seq_cur);
686 atrlen = strlen(atrcur);
688 for (np = m_defs; np; np = np->n_next)
689 if (strncmp(np->n_name, atrcur, atrlen)==0
690 && strncmp(np->n_name+atrlen, nmhdir, strlen(nmhdir))!=0)
691 /* Why do we exclude absolute path names? --meillo */
692 BuildFolderList(np->n_name + atrlen, 0);