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 },
58 char *name; /* name of folder */
60 int error; /* error == 1 for unreadable folder */
61 int nMsgs; /* number of messages in folder */
62 int nSeq[NUMATTRS]; /* number of messages in each sequence */
63 int private[NUMATTRS]; /* is given sequence, public or private */
66 static struct Folder *orders = NULL;
67 static int nOrders = 0;
68 static int nOrdersAlloced = 0;
69 static struct Folder *folders = NULL;
70 static unsigned int nFolders = 0;
71 static int nFoldersAlloced = 0;
73 /* info on folders to search */
74 static char **foldersToDo;
75 static int numfolders;
76 static int maxfolders;
78 /* info on sequences to search for */
79 static char *sequencesToDo[NUMATTRS];
80 static unsigned int numsequences;
82 static int all = FALSE; /* scan all folders in top level? */
83 static int alphaOrder = FALSE; /* want alphabetical order only */
84 static int recurse = FALSE; /* show nested folders? */
85 static int showzero = TRUE; /* show folders even if no messages in seq? */
86 static int fastsw = FALSE; /* display info on number of messages in
87 * sequence found, and total num messages */
89 static char curfol[BUFSIZ]; /* name of the current folder */
90 static char *nmhdir; /* base nmh mail directory */
93 ** Type for a compare function for qsort. This keeps
94 ** the compiler happy.
96 typedef int (*qsort_comp) (const void *, const void *);
101 int CompareFolders(struct Folder *, struct Folder *);
102 void GetFolderOrder(void);
103 void ScanFolders(void);
104 int AddFolder(char *, int);
105 void BuildFolderList(char *, int);
106 void BuildFolderListRecurse(char *, struct stat *, int);
107 void PrintFolders(void);
108 void AllocFolders(struct Folder **, int *, int);
109 int AssignPriority(char *);
110 static void do_readonly_folders(void);
115 main(int argc, char **argv)
122 setlocale(LC_ALL, "");
124 invo_name = mhbasename(argv[0]);
126 /* read user profile/context */
130 ** If program was invoked with name ending
131 ** in `s', then add switch `-all'.
133 if (argv[0][strlen(argv[0]) - 1] == 's')
136 arguments = getarguments(invo_name, argc, argv, 1);
139 /* allocate the initial space to record the folder names */
141 maxfolders = MAXFOLDERS;
142 foldersToDo = (char **) mh_xmalloc((size_t)
143 (maxfolders * sizeof(*foldersToDo)));
145 /* no sequences yet */
148 /* parse arguments */
149 while ((cp = *argp++)) {
151 switch (smatch(++cp, switches)) {
153 ambigsw(cp, switches);
156 adios(NULL, "-%s unknown", cp);
159 snprintf(buf, sizeof(buf), "%s [+folder1 [+folder2 ...]][switches]", invo_name);
160 print_help(buf, switches, 1);
163 print_version(invo_name);
167 if (!(cp = *argp++) || *cp == '-')
168 adios(NULL, "missing argument to %s",
171 /* check if too many sequences specified */
172 if (numsequences >= NUMATTRS)
173 adios(NULL, "too many sequences (more than %d) specified", NUMATTRS);
174 sequencesToDo[numsequences++] = cp;
214 ** Check if we need to allocate more space
217 if (numfolders >= maxfolders) {
218 maxfolders += MAXFOLDERS;
219 foldersToDo = (char **) mh_xrealloc(foldersToDo, (size_t) (maxfolders * sizeof(*foldersToDo)));
221 if (*cp == '+' || *cp == '@') {
222 foldersToDo[numfolders++] = getcpy(expandfol(cp));
224 foldersToDo[numfolders++] = cp;
228 /* get current folder */
229 strncpy(curfol, getcurfol(), sizeof(curfol));
231 /* get nmh base directory */
232 nmhdir = toabsdir("+");
235 ** If no sequences specified, we use the `unseen' sequence(s)
236 ** We check to make sure that the Unseen-Sequence entry doesn't
237 ** contain too many sequences.
239 if (numsequences == 0) {
242 if ((cp = context_find(usequence))) {
244 adios(NULL, "profile entry %s set, but empty, and no sequence given", usequence);
247 cp = seq_unseen; /* use default */
250 ap = brkstring(dp, " ", "\n");
251 for (; ap && *ap; ap++) {
252 if (numsequences >= NUMATTRS) {
253 adios(NULL, "too many sequences (more than %d) in %s profile entry", NUMATTRS, usequence);
255 sequencesToDo[numsequences++] = *ap;
262 qsort(folders, nFolders, sizeof(struct Folder),
263 (qsort_comp) CompareFolders);
270 ** Read the Flist-Order profile entry to determine
271 ** how to sort folders for output.
277 unsigned char *p, *s;
281 if (!(p = context_find("Flist-Order")))
287 while (*p && !isspace(*p))
291 AllocFolders(&orders, &nOrdersAlloced, nOrders + 1);
292 o = &orders[nOrders++];
293 o->priority = priority++;
294 o->name = (char *) mh_xmalloc(p - s + 1);
295 strncpy(o->name, s, p - s);
303 ** Scan all the necessary folders
312 * change directory to base of nmh directory
314 if (chdir(nmhdir) == NOTOK)
315 adios(nmhdir, "unable to change directory to");
317 if (numfolders > 0) {
319 strncpy(curfol, foldersToDo[numfolders - 1], sizeof(curfol));
320 context_replace(curfolder, curfol); /* update current folder */
321 context_save(); /* save the context file */
324 ** Scan each given folder. If -all is given,
325 ** then also scan the 1st level subfolders under
326 ** each given folder.
328 for (i = 0; i < numfolders; ++i)
329 BuildFolderList(foldersToDo[i], all ? 1 : 0);
332 /* Do the readonly folders */
333 do_readonly_folders();
335 /* Now scan the entire nmh directory for folders */
336 BuildFolderList(".", 0);
338 /* Else scan current folder */
339 BuildFolderList(curfol, 0);
345 ** Initial building of folder list for
346 ** the top of our search tree.
350 BuildFolderList(char *dirName, int searchdepth)
354 /* Make sure we have a directory */
355 if ((stat(dirName, &st) == -1) || !S_ISDIR(st.st_mode))
359 ** If base directory, don't add it to the
360 ** folder list. We just recurse into it.
362 if (strcmp(dirName, ".")==0) {
363 BuildFolderListRecurse(".", &st, 0);
368 ** Add this folder to the list.
369 ** If recursing and directory has subfolders,
370 ** then build folder list for subfolders.
372 if (AddFolder(dirName, showzero) && (recurse || searchdepth) &&
374 BuildFolderListRecurse(dirName, &st, searchdepth - 1);
378 ** Recursive building of folder list
382 BuildFolderListRecurse(char *dirName, struct stat *s, int searchdepth)
384 char *base, name[PATH_MAX];
392 ** Keep track of number of directories we've seen so we can
393 ** stop stat'ing entries in this directory once we've seen
394 ** them all. This optimization will fail if you have extra
395 ** directories beginning with ".", since we don't bother to
396 ** stat them. But that shouldn't generally be a problem.
398 nlinks = s->st_nlink;
400 if (!(dir = opendir(dirName)))
401 adios(dirName, "can't open directory");
404 ** A hack so that we don't see a
405 ** leading "./" in folder names.
407 base = (strcmp(dirName, ".")==0) ? dirName + 1 : dirName;
409 while (nlinks && (dp = readdir(dir))) {
410 if (strcmp(dp->d_name, ".")==0 ||
411 strcmp(dp->d_name, "..")==0) {
415 if (dp->d_name[0] == '.')
417 /* Check to see if the name of the file is a number
418 ** if it is, we assume it's a mail file and skip it
420 for (n = dp->d_name; *n && isdigit(*n); n++);
423 strncpy(name, base, sizeof(name) - 2);
426 strncat(name, dp->d_name, sizeof(name) - strlen(name) - 1);
427 if ((stat(name, &st) != -1) && S_ISDIR(st.st_mode)) {
429 ** Check if this was really a symbolic link pointing
430 ** to a directory. If not, then decrement link count.
432 if (lstat(name, &st) == -1)
434 /* Add this folder to the list */
435 if (AddFolder(name, showzero) &&
436 (recurse || searchdepth) &&
438 BuildFolderListRecurse(name, &st,
446 ** Add this folder to our list, counting the total number of
447 ** messages and the number of messages in each sequence.
451 AddFolder(char *name, int force)
455 int seqnum[NUMATTRS], nSeq[NUMATTRS];
459 /* Read folder and create message structure */
460 if (!(mp = folder_read(name))) {
461 /* Oops, error occurred. Record it and continue. */
462 AllocFolders(&folders, &nFoldersAlloced, nFolders + 1);
463 f = &folders[nFolders++];
464 f->name = getcpy(name);
466 f->priority = AssignPriority(f->name);
470 for (i = 0; i < numsequences; i++) {
471 /* Convert sequences to their sequence numbers */
472 if (sequencesToDo[i])
473 seqnum[i] = seq_getnum(mp, sequencesToDo[i]);
477 /* Now count messages in this sequence */
479 if (mp->nummsg > 0 && seqnum[i] != -1) {
480 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg;
482 if (in_sequence(mp, seqnum[i], msgnum))
488 /* Check if any of the sequence checks were nonzero */
490 for (i = 0; i < numsequences; i++) {
497 if (nonzero || force) {
498 /* save general folder information */
499 AllocFolders(&folders, &nFoldersAlloced, nFolders + 1);
500 f = &folders[nFolders++];
501 f->name = getcpy(name);
502 f->nMsgs = mp->nummsg;
504 f->priority = AssignPriority(f->name);
506 /* record the sequence information */
507 for (i = 0; i < numsequences; i++) {
508 f->nSeq[i] = nSeq[i];
509 f->private[i] = (seqnum[i] != -1) ?
510 is_seq_private(mp, seqnum[i]) : 0;
514 folder_free(mp); /* free folder/message structure */
519 ** Print the folder/sequence information
525 char tmpname[BUFSIZ];
526 unsigned int i, j, len, has_private = 0;
527 unsigned int maxfolderlen = 0, maxseqlen = 0;
528 int maxnum = 0, maxseq = 0;
531 for (i = 0; i < nFolders; i++)
532 printf("%s\n", folders[i].name);
537 ** Find the width we need for various fields
539 for (i = 0; i < nFolders; ++i) {
540 /* find the length of longest folder name */
541 len = strlen(folders[i].name);
542 if (len > maxfolderlen)
545 /* If folder had error, skip the rest */
546 if (folders[i].error)
549 /* find the maximum total messages */
550 if (folders[i].nMsgs > maxnum)
551 maxnum = folders[i].nMsgs;
553 for (j = 0; j < numsequences; j++) {
554 /* find maximum width of sequence name */
555 len = strlen(sequencesToDo[j]);
556 if ((folders[i].nSeq[j] > 0 || showzero) &&
560 /* find the maximum number of messages in sequence */
561 if (folders[i].nSeq[j] > maxseq)
562 maxseq = folders[i].nSeq[j];
565 ** check if this sequence is private in any of
568 if (folders[i].private[j])
573 /* Now print all the folder/sequence information */
574 for (i = 0; i < nFolders; i++) {
575 for (j = 0; j < numsequences; j++) {
576 if (folders[i].nSeq[j] > 0 || showzero) {
577 /* Add `+' to end of name of current folder */
578 if (strcmp(curfol, folders[i].name)!=0)
579 snprintf(tmpname, sizeof(tmpname),
580 "%s", folders[i].name);
582 snprintf(tmpname, sizeof(tmpname),
586 if (folders[i].error) {
587 printf("%-*s is unreadable\n",
593 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);
600 ** Put them in priority order.
604 CompareFolders(struct Folder *f1, struct Folder *f2)
606 if (!alphaOrder && f1->priority != f2->priority)
607 return f1->priority - f2->priority;
609 return strcmp(f1->name, f2->name);
613 ** Make sure we have at least n folders allocated.
617 AllocFolders(struct Folder **f, int *nfa, int n)
623 *f = (struct Folder *) mh_xmalloc(
624 *nfa * (sizeof(struct Folder)));
627 *f = (struct Folder *) mh_xrealloc(
628 *f, *nfa * (sizeof(struct Folder)));
633 ** Return the priority for a name. The highest comes from an exact match.
634 ** After that, the longest match (then first) assigns the priority.
637 AssignPriority(char *name)
645 for (i = 0; i < nOrders; ++i) {
647 if (strcmp(name, o->name)==0)
649 ol = strlen(o->name);
654 if (o->name[0] == '*' &&
655 strcmp(o->name + 1, name + (nl - ol + 1))==0) {
658 } else if (o->name[ol - 1] == '*' &&
659 strncmp(o->name, name, ol - 1) == 0) {
668 ** Do the read only folders
672 do_readonly_folders(void)
676 register struct node *np;
678 snprintf(atrcur, sizeof(atrcur), "atr-%s-", seq_cur);
679 atrlen = strlen(atrcur);
681 for (np = m_defs; np; np = np->n_next)
682 if (strncmp(np->n_name, atrcur, atrlen)==0
683 && strncmp(np->n_name+atrlen, nmhdir, strlen(nmhdir))!=0)
684 /* Why do we exclude absolute path names? --meillo */
685 BuildFolderList(np->n_name + atrlen, 0);