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 },
64 char *name; /* name of folder */
66 int error; /* error == 1 for unreadable folder */
67 int nMsgs; /* number of messages in folder */
68 int nSeq[NUMATTRS]; /* number of messages in each sequence */
69 int private[NUMATTRS]; /* is given sequence, public or private */
72 static struct Folder *orders = NULL;
73 static int nOrders = 0;
74 static int nOrdersAlloced = 0;
75 static struct Folder *folders = NULL;
76 static unsigned int nFolders = 0;
77 static int nFoldersAlloced = 0;
79 /* info on folders to search */
80 static char **foldersToDo;
81 static int numfolders;
82 static int maxfolders;
84 /* info on sequences to search for */
85 static char *sequencesToDo[NUMATTRS];
86 static unsigned int numsequences;
88 static int all = FALSE; /* scan all folders in top level? */
89 static int alphaOrder = FALSE; /* want alphabetical order only */
90 static int recurse = FALSE; /* show nested folders? */
91 static int showzero = TRUE; /* show folders even if no messages in seq? */
92 static int fastsw = FALSE; /* display info on number of messages in
93 * sequence found, and total num messages */
95 static char curfol[BUFSIZ]; /* name of the current folder */
96 static char *nmhdir; /* base nmh mail directory */
99 ** Type for a compare function for qsort. This keeps
100 ** the compiler happy.
102 typedef int (*qsort_comp) (const void *, const void *);
107 int CompareFolders(struct Folder *, struct Folder *);
108 void GetFolderOrder(void);
109 void ScanFolders(void);
110 int AddFolder(char *, int);
111 void BuildFolderList(char *, int);
112 void BuildFolderListRecurse(char *, struct stat *, int);
113 void PrintFolders(void);
114 void AllocFolders(struct Folder **, int *, int);
115 int AssignPriority(char *);
116 static void do_readonly_folders(void);
121 main(int argc, char **argv)
127 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(EX_USAGE, NULL, "-%s unknown", cp);
163 snprintf(buf, sizeof(buf), "%s [+folder1 [+folder2 ...]][switches]", invo_name);
164 print_help(buf, switches, 1);
165 exit(argc == 2 ? EX_OK : EX_USAGE);
168 print_version(invo_name);
169 exit(argc == 2 ? EX_OK : EX_USAGE);
172 if (!(cp = *argp++) || *cp == '-')
173 adios(EX_USAGE, NULL, "missing argument to %s",
176 /* check if too many sequences specified */
177 if (numsequences >= NUMATTRS)
178 adios(EX_USAGE, NULL, "too many sequences (more than %d) specified", NUMATTRS);
179 sequencesToDo[numsequences++] = cp;
219 ** Check if we need to allocate more space
222 if (numfolders >= maxfolders) {
223 maxfolders += MAXFOLDERS;
224 foldersToDo = (char **) mh_xrealloc(foldersToDo, (size_t) (maxfolders * sizeof(*foldersToDo)));
226 if (*cp == '+' || *cp == '@') {
227 foldersToDo[numfolders++] = getcpy(expandfol(cp));
229 foldersToDo[numfolders++] = cp;
233 /* get current folder */
234 strncpy(curfol, getcurfol(), sizeof(curfol));
236 /* get nmh base directory */
237 nmhdir = toabsdir("+");
240 ** If no sequences specified, we use the `unseen' sequence(s)
241 ** We check to make sure that the Unseen-Sequence entry doesn't
242 ** contain too many sequences.
244 if (numsequences == 0) {
247 if ((cp = context_find(usequence))) {
249 adios(EX_CONFIG, NULL, "profile entry %s set, but empty, and no sequence given", usequence);
252 cp = seq_unseen; /* use default */
255 ap = brkstring(dp, " ", "\n");
256 for (; ap && *ap; ap++) {
257 if (numsequences >= NUMATTRS) {
258 adios(EX_USAGE, NULL, "too many sequences (more than %d) in %s profile entry", NUMATTRS, usequence);
260 sequencesToDo[numsequences++] = *ap;
267 qsort(folders, nFolders, sizeof(struct Folder),
268 (qsort_comp) CompareFolders);
274 ** Read the Flist-Order profile entry to determine
275 ** how to sort folders for output.
281 unsigned char *p, *s;
285 if (!(p = context_find("Flist-Order")))
291 while (*p && !isspace(*p))
295 AllocFolders(&orders, &nOrdersAlloced, nOrders + 1);
296 o = &orders[nOrders++];
297 o->priority = priority++;
298 o->name = (char *) mh_xmalloc(p - s + 1);
299 strncpy(o->name, s, p - s);
307 ** Scan all the necessary folders
316 * change directory to base of nmh directory
318 if (chdir(nmhdir) == NOTOK)
319 adios(EX_OSERR, nmhdir, "unable to change directory to");
321 if (numfolders > 0) {
323 strncpy(curfol, foldersToDo[numfolders - 1], sizeof(curfol));
324 context_replace(curfolder, curfol); /* update current folder */
325 context_save(); /* save the context file */
328 ** Scan each given folder. If -all is given,
329 ** then also scan the 1st level subfolders under
330 ** each given folder.
332 for (i = 0; i < numfolders; ++i)
333 BuildFolderList(foldersToDo[i], all ? 1 : 0);
336 /* Do the readonly folders */
337 do_readonly_folders();
339 /* Now scan the entire nmh directory for folders */
340 BuildFolderList(".", 0);
342 /* Else scan current folder */
343 BuildFolderList(curfol, 0);
349 ** Initial building of folder list for
350 ** the top of our search tree.
354 BuildFolderList(char *dirName, int searchdepth)
358 /* Make sure we have a directory */
359 if ((stat(dirName, &st) == -1) || !S_ISDIR(st.st_mode))
363 ** If base directory, don't add it to the
364 ** folder list. We just recurse into it.
366 if (strcmp(dirName, ".")==0) {
367 BuildFolderListRecurse(".", &st, 0);
372 ** Add this folder to the list.
373 ** If recursing and directory has subfolders,
374 ** then build folder list for subfolders.
376 if (AddFolder(dirName, showzero) && (recurse || searchdepth) &&
378 BuildFolderListRecurse(dirName, &st, searchdepth - 1);
382 ** Recursive building of folder list
386 BuildFolderListRecurse(char *dirName, struct stat *s, int searchdepth)
388 char *base, name[PATH_MAX];
396 ** Keep track of number of directories we've seen so we can
397 ** stop stat'ing entries in this directory once we've seen
398 ** them all. This optimization will fail if you have extra
399 ** directories beginning with ".", since we don't bother to
400 ** stat them. But that shouldn't generally be a problem.
402 nlinks = s->st_nlink;
404 if (!(dir = opendir(dirName)))
405 adios(EX_IOERR, dirName, "can't open directory");
408 ** A hack so that we don't see a
409 ** leading "./" in folder names.
411 base = (strcmp(dirName, ".")==0) ? dirName + 1 : dirName;
413 while (nlinks && (dp = readdir(dir))) {
414 if (strcmp(dp->d_name, ".")==0 ||
415 strcmp(dp->d_name, "..")==0) {
419 if (dp->d_name[0] == '.')
421 /* Check to see if the name of the file is a number
422 ** if it is, we assume it's a mail file and skip it
424 for (n = dp->d_name; *n && isdigit(*n); n++);
427 strncpy(name, base, sizeof(name) - 2);
430 strncat(name, dp->d_name, sizeof(name) - strlen(name) - 1);
431 if ((stat(name, &st) != -1) && S_ISDIR(st.st_mode)) {
433 ** Check if this was really a symbolic link pointing
434 ** to a directory. If not, then decrement link count.
436 if (lstat(name, &st) == -1)
438 /* Add this folder to the list */
439 if (AddFolder(name, showzero) &&
440 (recurse || searchdepth) &&
442 BuildFolderListRecurse(name, &st,
450 ** Add this folder to our list, counting the total number of
451 ** messages and the number of messages in each sequence.
455 AddFolder(char *name, int force)
459 int seqnum[NUMATTRS], nSeq[NUMATTRS];
463 /* Read folder and create message structure */
464 if (!(mp = folder_read(name))) {
465 /* Oops, error occurred. Record it and continue. */
466 AllocFolders(&folders, &nFoldersAlloced, nFolders + 1);
467 f = &folders[nFolders++];
468 f->name = getcpy(name);
470 f->priority = AssignPriority(f->name);
474 for (i = 0; i < numsequences; i++) {
475 /* Convert sequences to their sequence numbers */
476 if (sequencesToDo[i])
477 seqnum[i] = seq_getnum(mp, sequencesToDo[i]);
481 /* Now count messages in this sequence */
483 if (mp->nummsg > 0 && seqnum[i] != -1) {
484 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg;
486 if (in_sequence(mp, seqnum[i], msgnum))
492 /* Check if any of the sequence checks were nonzero */
494 for (i = 0; i < numsequences; i++) {
501 if (nonzero || force) {
502 /* save general folder information */
503 AllocFolders(&folders, &nFoldersAlloced, nFolders + 1);
504 f = &folders[nFolders++];
505 f->name = getcpy(name);
506 f->nMsgs = mp->nummsg;
508 f->priority = AssignPriority(f->name);
510 /* record the sequence information */
511 for (i = 0; i < numsequences; i++) {
512 f->nSeq[i] = nSeq[i];
513 f->private[i] = (seqnum[i] != -1) ?
514 is_seq_private(mp, seqnum[i]) : 0;
518 folder_free(mp); /* free folder/message structure */
523 ** Print the folder/sequence information
529 char tmpname[BUFSIZ];
530 unsigned int i, j, len, has_private = 0;
531 unsigned int maxfolderlen = 0, maxseqlen = 0;
532 int maxnum = 0, maxseq = 0;
535 for (i = 0; i < nFolders; i++)
536 printf("%s\n", folders[i].name);
541 ** Find the width we need for various fields
543 for (i = 0; i < nFolders; ++i) {
544 /* find the length of longest folder name */
545 len = strlen(folders[i].name);
546 if (len > maxfolderlen)
549 /* If folder had error, skip the rest */
550 if (folders[i].error)
553 /* find the maximum total messages */
554 if (folders[i].nMsgs > maxnum)
555 maxnum = folders[i].nMsgs;
557 for (j = 0; j < numsequences; j++) {
558 /* find maximum width of sequence name */
559 len = strlen(sequencesToDo[j]);
560 if ((folders[i].nSeq[j] > 0 || showzero) &&
564 /* find the maximum number of messages in sequence */
565 if (folders[i].nSeq[j] > maxseq)
566 maxseq = folders[i].nSeq[j];
569 ** check if this sequence is private in any of
572 if (folders[i].private[j])
577 /* Now print all the folder/sequence information */
578 for (i = 0; i < nFolders; i++) {
579 for (j = 0; j < numsequences; j++) {
580 if (folders[i].nSeq[j] > 0 || showzero) {
581 /* Add `+' to end of name of current folder */
582 if (strcmp(curfol, folders[i].name)!=0)
583 snprintf(tmpname, sizeof(tmpname),
584 "%s", folders[i].name);
586 snprintf(tmpname, sizeof(tmpname),
590 if (folders[i].error) {
591 printf("%-*s is unreadable\n",
597 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);
604 ** Put them in priority order.
608 CompareFolders(struct Folder *f1, struct Folder *f2)
610 if (!alphaOrder && f1->priority != f2->priority)
611 return f1->priority - f2->priority;
613 return strcmp(f1->name, f2->name);
617 ** Make sure we have at least n folders allocated.
621 AllocFolders(struct Folder **f, int *nfa, int n)
627 *f = (struct Folder *) mh_xmalloc(
628 *nfa * (sizeof(struct Folder)));
631 *f = (struct Folder *) mh_xrealloc(
632 *f, *nfa * (sizeof(struct Folder)));
637 ** Return the priority for a name. The highest comes from an exact match.
638 ** After that, the longest match (then first) assigns the priority.
641 AssignPriority(char *name)
649 for (i = 0; i < nOrders; ++i) {
651 if (strcmp(name, o->name)==0)
653 ol = strlen(o->name);
658 if (o->name[0] == '*' &&
659 strcmp(o->name + 1, name + (nl - ol + 1))==0) {
662 } else if (o->name[ol - 1] == '*' &&
663 strncmp(o->name, name, ol - 1) == 0) {
672 ** Do the read only folders
676 do_readonly_folders(void)
682 snprintf(atrcur, sizeof(atrcur), "atr-%s-", seq_cur);
683 atrlen = strlen(atrcur);
685 for (np = m_defs; np; np = np->n_next)
686 if (strncmp(np->n_name, atrcur, atrlen)==0
687 && strncmp(np->n_name+atrlen, nmhdir, strlen(nmhdir))!=0)
688 /* Why do we exclude absolute path names? --meillo */
689 BuildFolderList(np->n_name + atrlen, 0);