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
24 ** We allocate space to record the names of folders
25 ** (foldersToDo array), this number of elements at a time.
27 #define MAXFOLDERS 100
30 static struct swit switches[] = {
32 { "sequence name", 0 },
65 char *name; /* name of folder */
67 int error; /* error == 1 for unreadable folder */
68 int nMsgs; /* number of messages in folder */
69 int nSeq[NUMATTRS]; /* number of messages in each sequence */
70 int private[NUMATTRS]; /* is given sequence, public or private */
73 static struct Folder *orders = NULL;
74 static int nOrders = 0;
75 static int nOrdersAlloced = 0;
76 static struct Folder *folders = NULL;
77 static int nFolders = 0;
78 static int nFoldersAlloced = 0;
80 /* info on folders to search */
81 static char **foldersToDo;
82 static int numfolders;
83 static int maxfolders;
85 /* info on sequences to search for */
86 static char *sequencesToDo[NUMATTRS];
87 static int numsequences;
89 static int all = FALSE; /* scan all folders in top level? */
90 static int alphaOrder = FALSE; /* want alphabetical order only */
91 static int recurse = FALSE; /* show nested folders? */
92 static int showzero = TRUE; /* show folders even if no messages in seq? */
93 static int Total = TRUE; /* display info on number of messages in
94 * sequence found, and total num messages */
96 static char curfol[BUFSIZ]; /* name of the current folder */
97 static char *nmhdir; /* base nmh mail directory */
100 ** Type for a compare function for qsort. This keeps
101 ** the compiler happy.
103 typedef int (*qsort_comp) (const void *, const void *);
108 int CompareFolders(struct Folder *, struct Folder *);
109 void GetFolderOrder(void);
110 void ScanFolders(void);
111 int AddFolder(char *, int);
112 void BuildFolderList(char *, int);
113 void BuildFolderListRecurse(char *, struct stat *, int);
114 void PrintFolders(void);
115 void AllocFolders(struct Folder **, int *, int);
116 int AssignPriority(char *);
117 static void do_readonly_folders(void);
122 main(int argc, char **argv)
129 setlocale(LC_ALL, "");
131 invo_name = mhbasename(argv[0]);
133 /* read user profile/context */
137 ** If program was invoked with name ending
138 ** in `s', then add switch `-all'.
140 if (argv[0][strlen(argv[0]) - 1] == 's')
143 arguments = getarguments(invo_name, argc, argv, 1);
146 /* allocate the initial space to record the folder names */
148 maxfolders = MAXFOLDERS;
149 foldersToDo = (char **) mh_xmalloc((size_t)
150 (maxfolders * sizeof(*foldersToDo)));
152 /* no sequences yet */
155 /* parse arguments */
156 while ((cp = *argp++)) {
158 switch (smatch(++cp, switches)) {
160 ambigsw(cp, switches);
163 adios(NULL, "-%s unknown", cp);
166 snprintf(buf, sizeof(buf), "%s [+folder1 [+folder2 ...]][switches]", invo_name);
167 print_help(buf, switches, 1);
170 print_version(invo_name);
174 if (!(cp = *argp++) || *cp == '-')
175 adios(NULL, "missing argument to %s",
178 /* check if too many sequences specified */
179 if (numsequences >= NUMATTRS)
180 adios(NULL, "too many sequences (more than %d) specified", NUMATTRS);
181 sequencesToDo[numsequences++] = cp;
224 ** Check if we need to allocate more space
227 if (numfolders >= maxfolders) {
228 maxfolders += MAXFOLDERS;
229 foldersToDo = (char **) mh_xrealloc(foldersToDo, (size_t) (maxfolders * sizeof(*foldersToDo)));
231 if (*cp == '+' || *cp == '@') {
232 foldersToDo[numfolders++] = getcpy(expandfol(cp));
234 foldersToDo[numfolders++] = cp;
238 /* get current folder */
239 strncpy(curfol, getcurfol(), sizeof(curfol));
241 /* get nmh base directory */
242 nmhdir = toabsdir("+");
245 ** If no sequences specified, we use the `unseen' sequence(s)
246 ** We check to make sure that the Unseen-Sequence entry doesn't
247 ** contain too many sequences.
249 if (numsequences == 0) {
252 if ((cp = context_find(usequence))) {
254 adios(NULL, "profile entry %s set, but empty, and no sequence given", usequence);
257 cp = seq_unseen; /* use default */
260 ap = brkstring(dp, " ", "\n");
261 for (; ap && *ap; ap++) {
262 if (numsequences >= NUMATTRS) {
263 adios(NULL, "too many sequences (more than %d) in %s profile entry", NUMATTRS, usequence);
265 sequencesToDo[numsequences++] = *ap;
272 qsort(folders, nFolders, sizeof(struct Folder),
273 (qsort_comp) CompareFolders);
280 ** Read the Flist-Order profile entry to determine
281 ** how to sort folders for output.
287 unsigned char *p, *s;
291 if (!(p = context_find("Flist-Order")))
297 while (*p && !isspace(*p))
301 AllocFolders(&orders, &nOrdersAlloced, nOrders + 1);
302 o = &orders[nOrders++];
303 o->priority = priority++;
304 o->name = (char *) mh_xmalloc(p - s + 1);
305 strncpy(o->name, s, p - s);
313 ** Scan all the necessary folders
322 * change directory to base of nmh directory
324 if (chdir(nmhdir) == NOTOK)
325 adios(nmhdir, "unable to change directory to");
327 if (numfolders > 0) {
329 strncpy(curfol, foldersToDo[numfolders - 1], sizeof(curfol));
330 context_replace(curfolder, curfol); /* update current folder */
331 context_save(); /* save the context file */
334 ** Scan each given folder. If -all is given,
335 ** then also scan the 1st level subfolders under
336 ** each given folder.
338 for (i = 0; i < numfolders; ++i)
339 BuildFolderList(foldersToDo[i], all ? 1 : 0);
342 /* Do the readonly folders */
343 do_readonly_folders();
345 /* Now scan the entire nmh directory for folders */
346 BuildFolderList(".", 0);
348 /* Else scan current folder */
349 BuildFolderList(curfol, 0);
355 ** Initial building of folder list for
356 ** the top of our search tree.
360 BuildFolderList(char *dirName, int searchdepth)
364 /* Make sure we have a directory */
365 if ((stat(dirName, &st) == -1) || !S_ISDIR(st.st_mode))
369 ** If base directory, don't add it to the
370 ** folder list. We just recurse into it.
372 if (strcmp(dirName, ".")==0) {
373 BuildFolderListRecurse(".", &st, 0);
378 ** Add this folder to the list.
379 ** If recursing and directory has subfolders,
380 ** then build folder list for subfolders.
382 if (AddFolder(dirName, showzero) && (recurse || searchdepth) &&
384 BuildFolderListRecurse(dirName, &st, searchdepth - 1);
388 ** Recursive building of folder list
392 BuildFolderListRecurse(char *dirName, struct stat *s, int searchdepth)
394 char *base, name[PATH_MAX];
402 ** Keep track of number of directories we've seen so we can
403 ** stop stat'ing entries in this directory once we've seen
404 ** them all. This optimization will fail if you have extra
405 ** directories beginning with ".", since we don't bother to
406 ** stat them. But that shouldn't generally be a problem.
408 nlinks = s->st_nlink;
410 if (!(dir = opendir(dirName)))
411 adios(dirName, "can't open directory");
414 ** A hack so that we don't see a
415 ** leading "./" in folder names.
417 base = (strcmp(dirName, ".")==0) ? dirName + 1 : dirName;
419 while (nlinks && (dp = readdir(dir))) {
420 if (strcmp(dp->d_name, ".")==0 ||
421 strcmp(dp->d_name, "..")==0) {
425 if (dp->d_name[0] == '.')
427 /* Check to see if the name of the file is a number
428 ** if it is, we assume it's a mail file and skip it
430 for (n = dp->d_name; *n && isdigit(*n); n++);
433 strncpy(name, base, sizeof(name) - 2);
436 strncat(name, dp->d_name, sizeof(name) - strlen(name) - 1);
437 if ((stat(name, &st) != -1) && S_ISDIR(st.st_mode)) {
439 ** Check if this was really a symbolic link pointing
440 ** to a directory. If not, then decrement link count.
442 if (lstat(name, &st) == -1)
444 /* Add this folder to the list */
445 if (AddFolder(name, showzero) &&
446 (recurse || searchdepth) &&
448 BuildFolderListRecurse(name, &st,
456 ** Add this folder to our list, counting the total number of
457 ** messages and the number of messages in each sequence.
461 AddFolder(char *name, int force)
463 int i, msgnum, nonzero;
464 int seqnum[NUMATTRS], nSeq[NUMATTRS];
468 /* Read folder and create message structure */
469 if (!(mp = folder_read(name))) {
470 /* Oops, error occurred. Record it and continue. */
471 AllocFolders(&folders, &nFoldersAlloced, nFolders + 1);
472 f = &folders[nFolders++];
473 f->name = getcpy(name);
475 f->priority = AssignPriority(f->name);
479 for (i = 0; i < numsequences; i++) {
480 /* Convert sequences to their sequence numbers */
481 if (sequencesToDo[i])
482 seqnum[i] = seq_getnum(mp, sequencesToDo[i]);
486 /* Now count messages in this sequence */
488 if (mp->nummsg > 0 && seqnum[i] != -1) {
489 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg;
491 if (in_sequence(mp, seqnum[i], msgnum))
497 /* Check if any of the sequence checks were nonzero */
499 for (i = 0; i < numsequences; i++) {
506 if (nonzero || force) {
507 /* save general folder information */
508 AllocFolders(&folders, &nFoldersAlloced, nFolders + 1);
509 f = &folders[nFolders++];
510 f->name = getcpy(name);
511 f->nMsgs = mp->nummsg;
513 f->priority = AssignPriority(f->name);
515 /* record the sequence information */
516 for (i = 0; i < numsequences; i++) {
517 f->nSeq[i] = nSeq[i];
518 f->private[i] = (seqnum[i] != -1) ?
519 is_seq_private(mp, seqnum[i]) : 0;
523 folder_free(mp); /* free folder/message structure */
528 ** Print the folder/sequence information
534 char tmpname[BUFSIZ];
535 int i, j, len, has_private = 0;
536 int maxfolderlen = 0, maxseqlen = 0;
537 int maxnum = 0, maxseq = 0;
540 for (i = 0; i < nFolders; i++)
541 printf("%s\n", folders[i].name);
546 ** Find the width we need for various fields
548 for (i = 0; i < nFolders; ++i) {
549 /* find the length of longest folder name */
550 len = strlen(folders[i].name);
551 if (len > maxfolderlen)
554 /* If folder had error, skip the rest */
555 if (folders[i].error)
558 /* find the maximum total messages */
559 if (folders[i].nMsgs > maxnum)
560 maxnum = folders[i].nMsgs;
562 for (j = 0; j < numsequences; j++) {
563 /* find maximum width of sequence name */
564 len = strlen(sequencesToDo[j]);
565 if ((folders[i].nSeq[j] > 0 || showzero) &&
569 /* find the maximum number of messages in sequence */
570 if (folders[i].nSeq[j] > maxseq)
571 maxseq = folders[i].nSeq[j];
574 ** check if this sequence is private in any of
577 if (folders[i].private[j])
582 /* Now print all the folder/sequence information */
583 for (i = 0; i < nFolders; i++) {
584 for (j = 0; j < numsequences; j++) {
585 if (folders[i].nSeq[j] > 0 || showzero) {
586 /* Add `+' to end of name of current folder */
587 if (strcmp(curfol, folders[i].name)!=0)
588 snprintf(tmpname, sizeof(tmpname),
589 "%s", folders[i].name);
591 snprintf(tmpname, sizeof(tmpname),
595 if (folders[i].error) {
596 printf("%-*s is unreadable\n",
602 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);
609 ** Put them in priority order.
613 CompareFolders(struct Folder *f1, struct Folder *f2)
615 if (!alphaOrder && f1->priority != f2->priority)
616 return f1->priority - f2->priority;
618 return strcmp(f1->name, f2->name);
622 ** Make sure we have at least n folders allocated.
626 AllocFolders(struct Folder **f, int *nfa, int n)
632 *f = (struct Folder *) mh_xmalloc(
633 *nfa * (sizeof(struct Folder)));
636 *f = (struct Folder *) mh_xrealloc(
637 *f, *nfa * (sizeof(struct Folder)));
642 ** Return the priority for a name. The highest comes from an exact match.
643 ** After that, the longest match (then first) assigns the priority.
646 AssignPriority(char *name)
654 for (i = 0; i < nOrders; ++i) {
656 if (strcmp(name, o->name)==0)
658 ol = strlen(o->name);
663 if (o->name[0] == '*' &&
664 strcmp(o->name + 1, name + (nl - ol + 1))==0) {
667 } else if (o->name[ol - 1] == '*' &&
668 strncmp(o->name, name, ol - 1) == 0) {
677 ** Do the read only folders
681 do_readonly_folders(void)
685 register struct node *np;
687 snprintf(atrcur, sizeof(atrcur), "atr-%s-", seq_cur);
688 atrlen = strlen(atrcur);
690 for (np = m_defs; np; np = np->n_next)
691 if (strncmp(np->n_name, atrcur, atrlen)==0
692 && strncmp(np->n_name+atrlen, nmhdir, strlen(nmhdir))!=0)
693 /* Why do we exclude absolute path names? --meillo */
694 BuildFolderList(np->n_name + atrlen, 0);