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)
121 setlocale(LC_ALL, "");
122 invo_name = mhbasename(argv[0]);
124 /* read user profile/context */
128 ** If program was invoked with name ending
129 ** in `s', then add switch `-all'.
131 if (argv[0][strlen(argv[0]) - 1] == 's')
134 arguments = getarguments(invo_name, argc, argv, 1);
137 /* allocate the initial space to record the folder names */
139 maxfolders = MAXFOLDERS;
140 foldersToDo = (char **) mh_xmalloc((size_t)
141 (maxfolders * sizeof(*foldersToDo)));
143 /* no sequences yet */
146 /* parse arguments */
147 while ((cp = *argp++)) {
149 switch (smatch(++cp, switches)) {
151 ambigsw(cp, switches);
152 /* sysexits.h EX_USAGE */
155 adios(NULL, "-%s unknown", cp);
158 snprintf(buf, sizeof(buf), "%s [+folder1 [+folder2 ...]][switches]", invo_name);
159 print_help(buf, switches, 1);
162 print_version(invo_name);
166 if (!(cp = *argp++) || *cp == '-')
167 adios(NULL, "missing argument to %s",
170 /* check if too many sequences specified */
171 if (numsequences >= NUMATTRS)
172 adios(NULL, "too many sequences (more than %d) specified", NUMATTRS);
173 sequencesToDo[numsequences++] = cp;
213 ** Check if we need to allocate more space
216 if (numfolders >= maxfolders) {
217 maxfolders += MAXFOLDERS;
218 foldersToDo = (char **) mh_xrealloc(foldersToDo, (size_t) (maxfolders * sizeof(*foldersToDo)));
220 if (*cp == '+' || *cp == '@') {
221 foldersToDo[numfolders++] = getcpy(expandfol(cp));
223 foldersToDo[numfolders++] = cp;
227 /* get current folder */
228 strncpy(curfol, getcurfol(), sizeof(curfol));
230 /* get nmh base directory */
231 nmhdir = toabsdir("+");
234 ** If no sequences specified, we use the `unseen' sequence(s)
235 ** We check to make sure that the Unseen-Sequence entry doesn't
236 ** contain too many sequences.
238 if (numsequences == 0) {
241 if ((cp = context_find(usequence))) {
243 adios(NULL, "profile entry %s set, but empty, and no sequence given", usequence);
246 cp = seq_unseen; /* use default */
249 ap = brkstring(dp, " ", "\n");
250 for (; ap && *ap; ap++) {
251 if (numsequences >= NUMATTRS) {
252 adios(NULL, "too many sequences (more than %d) in %s profile entry", NUMATTRS, usequence);
254 sequencesToDo[numsequences++] = *ap;
261 qsort(folders, nFolders, sizeof(struct Folder),
262 (qsort_comp) CompareFolders);
268 ** Read the Flist-Order profile entry to determine
269 ** how to sort folders for output.
275 unsigned char *p, *s;
279 if (!(p = context_find("Flist-Order")))
285 while (*p && !isspace(*p))
289 AllocFolders(&orders, &nOrdersAlloced, nOrders + 1);
290 o = &orders[nOrders++];
291 o->priority = priority++;
292 o->name = (char *) mh_xmalloc(p - s + 1);
293 strncpy(o->name, s, p - s);
301 ** Scan all the necessary folders
310 * change directory to base of nmh directory
312 if (chdir(nmhdir) == NOTOK)
313 adios(nmhdir, "unable to change directory to");
315 if (numfolders > 0) {
317 strncpy(curfol, foldersToDo[numfolders - 1], sizeof(curfol));
318 context_replace(curfolder, curfol); /* update current folder */
319 context_save(); /* save the context file */
322 ** Scan each given folder. If -all is given,
323 ** then also scan the 1st level subfolders under
324 ** each given folder.
326 for (i = 0; i < numfolders; ++i)
327 BuildFolderList(foldersToDo[i], all ? 1 : 0);
330 /* Do the readonly folders */
331 do_readonly_folders();
333 /* Now scan the entire nmh directory for folders */
334 BuildFolderList(".", 0);
336 /* Else scan current folder */
337 BuildFolderList(curfol, 0);
343 ** Initial building of folder list for
344 ** the top of our search tree.
348 BuildFolderList(char *dirName, int searchdepth)
352 /* Make sure we have a directory */
353 if ((stat(dirName, &st) == -1) || !S_ISDIR(st.st_mode))
357 ** If base directory, don't add it to the
358 ** folder list. We just recurse into it.
360 if (strcmp(dirName, ".")==0) {
361 BuildFolderListRecurse(".", &st, 0);
366 ** Add this folder to the list.
367 ** If recursing and directory has subfolders,
368 ** then build folder list for subfolders.
370 if (AddFolder(dirName, showzero) && (recurse || searchdepth) &&
372 BuildFolderListRecurse(dirName, &st, searchdepth - 1);
376 ** Recursive building of folder list
380 BuildFolderListRecurse(char *dirName, struct stat *s, int searchdepth)
382 char *base, name[PATH_MAX];
390 ** Keep track of number of directories we've seen so we can
391 ** stop stat'ing entries in this directory once we've seen
392 ** them all. This optimization will fail if you have extra
393 ** directories beginning with ".", since we don't bother to
394 ** stat them. But that shouldn't generally be a problem.
396 nlinks = s->st_nlink;
398 if (!(dir = opendir(dirName)))
399 adios(dirName, "can't open directory");
402 ** A hack so that we don't see a
403 ** leading "./" in folder names.
405 base = (strcmp(dirName, ".")==0) ? dirName + 1 : dirName;
407 while (nlinks && (dp = readdir(dir))) {
408 if (strcmp(dp->d_name, ".")==0 ||
409 strcmp(dp->d_name, "..")==0) {
413 if (dp->d_name[0] == '.')
415 /* Check to see if the name of the file is a number
416 ** if it is, we assume it's a mail file and skip it
418 for (n = dp->d_name; *n && isdigit(*n); n++);
421 strncpy(name, base, sizeof(name) - 2);
424 strncat(name, dp->d_name, sizeof(name) - strlen(name) - 1);
425 if ((stat(name, &st) != -1) && S_ISDIR(st.st_mode)) {
427 ** Check if this was really a symbolic link pointing
428 ** to a directory. If not, then decrement link count.
430 if (lstat(name, &st) == -1)
432 /* Add this folder to the list */
433 if (AddFolder(name, showzero) &&
434 (recurse || searchdepth) &&
436 BuildFolderListRecurse(name, &st,
444 ** Add this folder to our list, counting the total number of
445 ** messages and the number of messages in each sequence.
449 AddFolder(char *name, int force)
453 int seqnum[NUMATTRS], nSeq[NUMATTRS];
457 /* Read folder and create message structure */
458 if (!(mp = folder_read(name))) {
459 /* Oops, error occurred. Record it and continue. */
460 AllocFolders(&folders, &nFoldersAlloced, nFolders + 1);
461 f = &folders[nFolders++];
462 f->name = getcpy(name);
464 f->priority = AssignPriority(f->name);
468 for (i = 0; i < numsequences; i++) {
469 /* Convert sequences to their sequence numbers */
470 if (sequencesToDo[i])
471 seqnum[i] = seq_getnum(mp, sequencesToDo[i]);
475 /* Now count messages in this sequence */
477 if (mp->nummsg > 0 && seqnum[i] != -1) {
478 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg;
480 if (in_sequence(mp, seqnum[i], msgnum))
486 /* Check if any of the sequence checks were nonzero */
488 for (i = 0; i < numsequences; i++) {
495 if (nonzero || force) {
496 /* save general folder information */
497 AllocFolders(&folders, &nFoldersAlloced, nFolders + 1);
498 f = &folders[nFolders++];
499 f->name = getcpy(name);
500 f->nMsgs = mp->nummsg;
502 f->priority = AssignPriority(f->name);
504 /* record the sequence information */
505 for (i = 0; i < numsequences; i++) {
506 f->nSeq[i] = nSeq[i];
507 f->private[i] = (seqnum[i] != -1) ?
508 is_seq_private(mp, seqnum[i]) : 0;
512 folder_free(mp); /* free folder/message structure */
517 ** Print the folder/sequence information
523 char tmpname[BUFSIZ];
524 unsigned int i, j, len, has_private = 0;
525 unsigned int maxfolderlen = 0, maxseqlen = 0;
526 int maxnum = 0, maxseq = 0;
529 for (i = 0; i < nFolders; i++)
530 printf("%s\n", folders[i].name);
535 ** Find the width we need for various fields
537 for (i = 0; i < nFolders; ++i) {
538 /* find the length of longest folder name */
539 len = strlen(folders[i].name);
540 if (len > maxfolderlen)
543 /* If folder had error, skip the rest */
544 if (folders[i].error)
547 /* find the maximum total messages */
548 if (folders[i].nMsgs > maxnum)
549 maxnum = folders[i].nMsgs;
551 for (j = 0; j < numsequences; j++) {
552 /* find maximum width of sequence name */
553 len = strlen(sequencesToDo[j]);
554 if ((folders[i].nSeq[j] > 0 || showzero) &&
558 /* find the maximum number of messages in sequence */
559 if (folders[i].nSeq[j] > maxseq)
560 maxseq = folders[i].nSeq[j];
563 ** check if this sequence is private in any of
566 if (folders[i].private[j])
571 /* Now print all the folder/sequence information */
572 for (i = 0; i < nFolders; i++) {
573 for (j = 0; j < numsequences; j++) {
574 if (folders[i].nSeq[j] > 0 || showzero) {
575 /* Add `+' to end of name of current folder */
576 if (strcmp(curfol, folders[i].name)!=0)
577 snprintf(tmpname, sizeof(tmpname),
578 "%s", folders[i].name);
580 snprintf(tmpname, sizeof(tmpname),
584 if (folders[i].error) {
585 printf("%-*s is unreadable\n",
591 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);
598 ** Put them in priority order.
602 CompareFolders(struct Folder *f1, struct Folder *f2)
604 if (!alphaOrder && f1->priority != f2->priority)
605 return f1->priority - f2->priority;
607 return strcmp(f1->name, f2->name);
611 ** Make sure we have at least n folders allocated.
615 AllocFolders(struct Folder **f, int *nfa, int n)
621 *f = (struct Folder *) mh_xmalloc(
622 *nfa * (sizeof(struct Folder)));
625 *f = (struct Folder *) mh_xrealloc(
626 *f, *nfa * (sizeof(struct Folder)));
631 ** Return the priority for a name. The highest comes from an exact match.
632 ** After that, the longest match (then first) assigns the priority.
635 AssignPriority(char *name)
643 for (i = 0; i < nOrders; ++i) {
645 if (strcmp(name, o->name)==0)
647 ol = strlen(o->name);
652 if (o->name[0] == '*' &&
653 strcmp(o->name + 1, name + (nl - ol + 1))==0) {
656 } else if (o->name[ol - 1] == '*' &&
657 strncmp(o->name, name, ol - 1) == 0) {
666 ** Do the read only folders
670 do_readonly_folders(void)
674 register struct node *np;
676 snprintf(atrcur, sizeof(atrcur), "atr-%s-", seq_cur);
677 atrlen = strlen(atrcur);
679 for (np = m_defs; np; np = np->n_next)
680 if (strncmp(np->n_name, atrcur, atrlen)==0
681 && strncmp(np->n_name+atrlen, nmhdir, strlen(nmhdir))!=0)
682 /* Why do we exclude absolute path names? --meillo */
683 BuildFolderList(np->n_name + atrlen, 0);