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
26 * We allocate space to record the names of folders
27 * (foldersToDo array), this number of elements at a time.
29 #define MAXFOLDERS 100
32 static struct swit switches[] = {
34 { "sequence name", 0 },
67 char *name; /* name of folder */
69 int error; /* error == 1 for unreadable folder */
70 int nMsgs; /* number of messages in folder */
71 int nSeq[NUMATTRS]; /* number of messages in each sequence */
72 int private[NUMATTRS]; /* is given sequence, public or private */
75 static struct Folder *orders = NULL;
76 static int nOrders = 0;
77 static int nOrdersAlloced = 0;
78 static struct Folder *folders = NULL;
79 static int nFolders = 0;
80 static int nFoldersAlloced = 0;
82 /* info on folders to search */
83 static char **foldersToDo;
84 static int numfolders;
85 static int maxfolders;
87 /* info on sequences to search for */
88 static char *sequencesToDo[NUMATTRS];
89 static int numsequences;
91 static int all = FALSE; /* scan all folders in top level? */
92 static int alphaOrder = FALSE; /* want alphabetical order only */
93 static int recurse = FALSE; /* show nested folders? */
94 static int showzero = TRUE; /* show folders even if no messages in seq? */
95 static int Total = TRUE; /* display info on number of messages in *
96 * sequence found, and total num messages */
98 static char curfolder[BUFSIZ]; /* name of the current folder */
99 static char *nmhdir; /* base nmh mail directory */
102 * Type for a compare function for qsort. This keeps
103 * the compiler happy.
105 typedef int (*qsort_comp) (const void *, const void *);
110 int CompareFolders(struct Folder *, struct Folder *);
111 void GetFolderOrder(void);
112 void ScanFolders(void);
113 int AddFolder(char *, int);
114 void BuildFolderList(char *, int);
115 void BuildFolderListRecurse(char *, struct stat *, int);
116 void PrintFolders(void);
117 void AllocFolders(struct Folder **, int *, int);
118 int AssignPriority(char *);
119 static void do_readonly_folders(void);
124 main(int argc, char **argv)
131 setlocale(LC_ALL, "");
133 invo_name = r1bindex(argv[0], '/');
135 /* read user profile/context */
139 * If program was invoked with name ending
140 * in `s', then add switch `-all'.
142 if (argv[0][strlen (argv[0]) - 1] == 's')
145 arguments = getarguments (invo_name, argc, argv, 1);
148 /* allocate the initial space to record the folder names */
150 maxfolders = MAXFOLDERS;
151 foldersToDo = (char **) mh_xmalloc ((size_t) (maxfolders * sizeof(*foldersToDo)));
153 /* no sequences yet */
156 /* parse arguments */
157 while ((cp = *argp++)) {
159 switch (smatch(++cp, switches)) {
161 ambigsw(cp, switches);
164 adios(NULL, "-%s unknown", cp);
167 snprintf(buf, sizeof(buf), "%s [+folder1 [+folder2 ...]][switches]",
169 print_help(buf, switches, 1);
172 print_version(invo_name);
176 if (!(cp = *argp++) || *cp == '-')
177 adios (NULL, "missing argument to %s", argp[-2]);
179 /* check if too many sequences specified */
180 if (numsequences >= NUMATTRS)
181 adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
182 sequencesToDo[numsequences++] = cp;
225 * Check if we need to allocate more space
228 if (numfolders >= maxfolders) {
229 maxfolders += MAXFOLDERS;
230 foldersToDo = (char **) mh_xrealloc (foldersToDo,
231 (size_t) (maxfolders * sizeof(*foldersToDo)));
233 if (*cp == '+' || *cp == '@') {
234 foldersToDo[numfolders++] =
237 foldersToDo[numfolders++] = cp;
241 if (!context_find ("path"))
242 free (path ("./", TFOLDER));
244 /* get current folder */
245 strncpy (curfolder, getfolder(1), sizeof(curfolder));
247 /* get nmh base directory */
248 nmhdir = m_maildir ("");
251 * If we didn't specify any sequences, we search
252 * for the "Unseen-Sequence" profile entry and use
253 * all the sequences defined there. We check to
254 * make sure that the Unseen-Sequence entry doesn't
255 * contain more than NUMATTRS sequences.
257 if (numsequences == 0) {
258 if ((cp = context_find(usequence)) && *cp) {
262 ap = brkstring (dp, " ", "\n");
263 for (; ap && *ap; ap++) {
264 if (numsequences >= NUMATTRS)
265 adios (NULL, "too many sequences (more than %d) in %s profile entry",
266 NUMATTRS, usequence);
268 sequencesToDo[numsequences++] = *ap;
271 adios (NULL, "no sequence specified or %s profile entry found", usequence);
277 qsort(folders, nFolders, sizeof(struct Folder), (qsort_comp) CompareFolders);
283 * Read the Flist-Order profile entry to determine
284 * how to sort folders for output.
290 unsigned char *p, *s;
294 if (!(p = context_find("Flist-Order")))
300 while (*p && !isspace(*p))
304 AllocFolders(&orders, &nOrdersAlloced, nOrders + 1);
305 o = &orders[nOrders++];
306 o->priority = priority++;
307 o->name = (char *) mh_xmalloc(p - s + 1);
308 strncpy(o->name, s, p - s);
316 * Scan all the necessary folders
325 * change directory to base of nmh directory
327 if (chdir (nmhdir) == NOTOK)
328 adios (nmhdir, "unable to change directory to");
330 if (numfolders > 0) {
332 strncpy (curfolder, foldersToDo[numfolders - 1], sizeof(curfolder));
333 context_replace (pfolder, curfolder);/* update current folder */
334 context_save (); /* save the context file */
337 * Scan each given folder. If -all is given,
338 * then also scan the 1st level subfolders under
341 for (i = 0; i < numfolders; ++i)
342 BuildFolderList(foldersToDo[i], all ? 1 : 0);
346 * Do the readonly folders
348 do_readonly_folders();
351 * Now scan the entire nmh directory for folders
353 BuildFolderList(".", 0);
356 * Else scan current folder
358 BuildFolderList(curfolder, 0);
364 * Initial building of folder list for
365 * the top of our search tree.
369 BuildFolderList(char *dirName, int searchdepth)
373 /* Make sure we have a directory */
374 if ((stat(dirName, &st) == -1) || !S_ISDIR(st.st_mode))
378 * If base directory, don't add it to the
379 * folder list. We just recurse into it.
381 if (!strcmp (dirName, ".")) {
382 BuildFolderListRecurse (".", &st, 0);
387 * Add this folder to the list.
388 * If recursing and directory has subfolders,
389 * then build folder list for subfolders.
391 if (AddFolder(dirName, showzero) && (recurse || searchdepth) && st.st_nlink > 2)
392 BuildFolderListRecurse(dirName, &st, searchdepth - 1);
396 * Recursive building of folder list
400 BuildFolderListRecurse(char *dirName, struct stat *s, int searchdepth)
402 char *base, name[PATH_MAX];
410 * Keep track of number of directories we've seen so we can
411 * stop stat'ing entries in this directory once we've seen
412 * them all. This optimization will fail if you have extra
413 * directories beginning with ".", since we don't bother to
414 * stat them. But that shouldn't generally be a problem.
416 nlinks = s->st_nlink;
418 if (!(dir = opendir(dirName)))
419 adios(dirName, "can't open directory");
422 * A hack so that we don't see a
423 * leading "./" in folder names.
425 base = strcmp (dirName, ".") ? dirName : dirName + 1;
427 while (nlinks && (dp = readdir(dir))) {
428 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) {
432 if (dp->d_name[0] == '.')
434 /* Check to see if the name of the file is a number
435 * if it is, we assume it's a mail file and skip it
437 for (n = dp->d_name; *n && isdigit(*n); n++);
440 strncpy (name, base, sizeof(name) - 2);
443 strncat(name, dp->d_name, sizeof(name) - strlen(name) - 1);
444 if ((stat(name, &st) != -1) && S_ISDIR(st.st_mode)) {
446 * Check if this was really a symbolic link pointing
447 * to a directory. If not, then decrement link count.
449 if (lstat (name, &st) == -1)
451 /* Add this folder to the list */
452 if (AddFolder(name, showzero) &&
453 (recurse || searchdepth) && st.st_nlink > 2)
454 BuildFolderListRecurse(name, &st, searchdepth - 1);
461 * Add this folder to our list, counting the total number of
462 * messages and the number of messages in each sequence.
466 AddFolder(char *name, int force)
468 int i, msgnum, nonzero;
469 int seqnum[NUMATTRS], nSeq[NUMATTRS];
473 /* Read folder and create message structure */
474 if (!(mp = folder_read (name))) {
475 /* Oops, error occurred. Record it and continue. */
476 AllocFolders(&folders, &nFoldersAlloced, nFolders + 1);
477 f = &folders[nFolders++];
478 f->name = getcpy(name);
480 f->priority = AssignPriority(f->name);
484 for (i = 0; i < numsequences; i++) {
485 /* Convert sequences to their sequence numbers */
486 if (sequencesToDo[i])
487 seqnum[i] = seq_getnum(mp, sequencesToDo[i]);
491 /* Now count messages in this sequence */
493 if (mp->nummsg > 0 && seqnum[i] != -1) {
494 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++) {
495 if (in_sequence(mp, seqnum[i], msgnum))
501 /* Check if any of the sequence checks were nonzero */
503 for (i = 0; i < numsequences; i++) {
510 if (nonzero || force) {
511 /* save general folder information */
512 AllocFolders(&folders, &nFoldersAlloced, nFolders + 1);
513 f = &folders[nFolders++];
514 f->name = getcpy(name);
515 f->nMsgs = mp->nummsg;
517 f->priority = AssignPriority(f->name);
519 /* record the sequence information */
520 for (i = 0; i < numsequences; i++) {
521 f->nSeq[i] = nSeq[i];
522 f->private[i] = (seqnum[i] != -1) ? is_seq_private(mp, seqnum[i]) : 0;
526 folder_free (mp); /* free folder/message structure */
531 * Print the folder/sequence information
537 char tmpname[BUFSIZ];
538 int i, j, len, has_private = 0;
539 int maxfolderlen = 0, maxseqlen = 0;
540 int maxnum = 0, maxseq = 0;
543 for (i = 0; i < nFolders; i++)
544 printf("%s\n", folders[i].name);
549 * Find the width we need for various fields
551 for (i = 0; i < nFolders; ++i) {
552 /* find the length of longest folder name */
553 len = strlen(folders[i].name);
554 if (len > maxfolderlen)
557 /* If folder had error, skip the rest */
558 if (folders[i].error)
561 /* find the maximum total messages */
562 if (folders[i].nMsgs > maxnum)
563 maxnum = folders[i].nMsgs;
565 for (j = 0; j < numsequences; j++) {
566 /* find maximum width of sequence name */
567 len = strlen (sequencesToDo[j]);
568 if ((folders[i].nSeq[j] > 0 || showzero) && (len > maxseqlen))
571 /* find the maximum number of messages in sequence */
572 if (folders[i].nSeq[j] > maxseq)
573 maxseq = folders[i].nSeq[j];
575 /* check if this sequence is private in any of the folders */
576 if (folders[i].private[j])
581 /* Now print all the folder/sequence information */
582 for (i = 0; i < nFolders; i++) {
583 for (j = 0; j < numsequences; j++) {
584 if (folders[i].nSeq[j] > 0 || showzero) {
585 /* Add `+' to end of name of current folder */
586 if (strcmp(curfolder, folders[i].name))
587 snprintf(tmpname, sizeof(tmpname), "%s", folders[i].name);
589 snprintf(tmpname, sizeof(tmpname), "%s+", folders[i].name);
591 if (folders[i].error) {
592 printf("%-*s is unreadable\n", maxfolderlen+1, tmpname);
596 printf("%-*s has %*d in sequence %-*s%s; out of %*d\n",
597 maxfolderlen+1, tmpname,
598 num_digits(maxseq), folders[i].nSeq[j],
599 maxseqlen, sequencesToDo[j],
600 !has_private ? "" : folders[i].private[j] ? " (private)" : " ",
601 num_digits(maxnum), folders[i].nMsgs);
608 * Put them in priority order.
612 CompareFolders(struct Folder *f1, struct Folder *f2)
614 if (!alphaOrder && f1->priority != f2->priority)
615 return f1->priority - f2->priority;
617 return strcmp(f1->name, f2->name);
621 * Make sure we have at least n folders allocated.
625 AllocFolders(struct Folder **f, int *nfa, int n)
631 *f = (struct Folder *) mh_xmalloc (*nfa * (sizeof(struct Folder)));
634 *f = (struct Folder *) mh_xrealloc (*f, *nfa * (sizeof(struct Folder)));
639 * Return the priority for a name. The highest comes from an exact match.
640 * After that, the longest match (then first) assigns the priority.
643 AssignPriority(char *name)
651 for (i = 0; i < nOrders; ++i) {
653 if (!strcmp(name, o->name))
655 ol = strlen(o->name);
660 if (o->name[0] == '*' && !strcmp(o->name + 1, name + (nl - ol + 1))) {
663 } else if (o->name[ol - 1] == '*' && strncmp(o->name, name, ol - 1) == 0) {
672 * Do the read only folders
676 do_readonly_folders (void)
680 register struct node *np;
682 snprintf (atrcur, sizeof(atrcur), "atr-%s-", current);
683 atrlen = strlen (atrcur);
685 for (np = m_defs; np; np = np->n_next)
686 if (ssequal (atrcur, np->n_name)
687 && !ssequal (nmhdir, np->n_name + atrlen))
688 BuildFolderList (np->n_name + atrlen, 0);