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
25 * We allocate space to record the names of folders
26 * (foldersToDo array), this number of elements at a time.
28 #define MAXFOLDERS 100
31 static struct swit switches[] = {
33 { "sequence name", 0 },
66 char *name; /* name of folder */
68 int error; /* error == 1 for unreadable folder */
69 int nMsgs; /* number of messages in folder */
70 int nSeq[NUMATTRS]; /* number of messages in each sequence */
71 int private[NUMATTRS]; /* is given sequence, public or private */
74 static struct Folder *orders = NULL;
75 static int nOrders = 0;
76 static int nOrdersAlloced = 0;
77 static struct Folder *folders = NULL;
78 static int nFolders = 0;
79 static int nFoldersAlloced = 0;
81 /* info on folders to search */
82 static char **foldersToDo;
83 static int numfolders;
84 static int maxfolders;
86 /* info on sequences to search for */
87 static char *sequencesToDo[NUMATTRS];
88 static int numsequences;
90 static int all = FALSE; /* scan all folders in top level? */
91 static int alphaOrder = FALSE; /* want alphabetical order only */
92 static int recurse = FALSE; /* show nested folders? */
93 static int showzero = TRUE; /* show folders even if no messages in seq? */
94 static int Total = TRUE; /* display info on number of messages in *
95 * sequence found, and total num messages */
97 static char curfolder[BUFSIZ]; /* name of the current folder */
98 static char *nmhdir; /* base nmh mail directory */
101 * Type for a compare function for qsort. This keeps
102 * the compiler happy.
104 typedef int (*qsort_comp) (const void *, const void *);
109 int CompareFolders(struct Folder *, struct Folder *);
110 void GetFolderOrder(void);
111 void ScanFolders(void);
112 int AddFolder(char *, int);
113 void BuildFolderList(char *, int);
114 void BuildFolderListRecurse(char *, struct stat *, int);
115 void PrintFolders(void);
116 static int num_digits (int);
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 if (!(foldersToDo = (char **) malloc ((size_t) (maxfolders * sizeof(*foldersToDo)))))
152 adios (NULL, "unable to allocate folder storage");
154 /* no sequences yet */
157 /* parse arguments */
158 while ((cp = *argp++)) {
160 switch (smatch(++cp, switches)) {
162 ambigsw(cp, switches);
165 adios(NULL, "-%s unknown", cp);
168 snprintf(buf, sizeof(buf), "%s [+folder1 [+folder2 ...]][switches]",
170 print_help(buf, switches, 1);
173 print_version(invo_name);
177 if (!(cp = *argp++) || *cp == '-')
178 adios (NULL, "missing argument to %s", argp[-2]);
180 /* check if too many sequences specified */
181 if (numsequences >= NUMATTRS)
182 adios (NULL, "too many sequences (more than %d) specified", NUMATTRS);
183 sequencesToDo[numsequences++] = cp;
226 * Check if we need to allocate more space
229 if (numfolders >= maxfolders) {
230 maxfolders += MAXFOLDERS;
231 if (!(foldersToDo = (char **) realloc (foldersToDo,
232 (size_t) (maxfolders * sizeof(*foldersToDo)))))
233 adios (NULL, "unable to reallocate folder name storage");
235 if (*cp == '+' || *cp == '@') {
236 foldersToDo[numfolders++] =
237 path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
239 foldersToDo[numfolders++] = cp;
243 if (!context_find ("path"))
244 free (path ("./", TFOLDER));
246 /* get current folder */
247 strncpy (curfolder, getfolder(1), sizeof(curfolder));
249 /* get nmh base directory */
250 nmhdir = m_maildir ("");
253 * If we didn't specify any sequences, we search
254 * for the "Unseen-Sequence" profile entry and use
255 * all the sequences defined there. We check to
256 * make sure that the Unseen-Sequence entry doesn't
257 * contain more than NUMATTRS sequences.
259 if (numsequences == 0) {
260 if ((cp = context_find(usequence)) && *cp) {
264 ap = brkstring (dp, " ", "\n");
265 for (; ap && *ap; ap++) {
266 if (numsequences >= NUMATTRS)
267 adios (NULL, "too many sequences (more than %d) in %s profile entry",
268 NUMATTRS, usequence);
270 sequencesToDo[numsequences++] = *ap;
273 adios (NULL, "no sequence specified or %s profile entry found", usequence);
279 qsort(folders, nFolders, sizeof(struct Folder), (qsort_comp) CompareFolders);
285 * Read the Flist-Order profile entry to determine
286 * how to sort folders for output.
296 if (!(p = context_find("Flist-Order")))
302 while (*p && !isspace(*p))
306 AllocFolders(&orders, &nOrdersAlloced, nOrders + 1);
307 o = &orders[nOrders++];
308 o->priority = priority++;
309 o->name = (char *) malloc(p - s + 1);
310 strncpy(o->name, s, p - s);
318 * Scan all the necessary folders
327 * change directory to base of nmh directory
329 if (chdir (nmhdir) == NOTOK)
330 adios (nmhdir, "unable to change directory to");
332 if (numfolders > 0) {
334 strncpy (curfolder, foldersToDo[numfolders - 1], sizeof(curfolder));
335 context_replace (pfolder, curfolder);/* update current folder */
336 context_save (); /* save the context file */
339 * Scan each given folder. If -all is given,
340 * then also scan the 1st level subfolders under
343 for (i = 0; i < numfolders; ++i)
344 BuildFolderList(foldersToDo[i], all ? 1 : 0);
348 * Do the readonly folders
350 do_readonly_folders();
353 * Now scan the entire nmh directory for folders
355 BuildFolderList(".", 0);
358 * Else scan current folder
360 BuildFolderList(curfolder, 0);
366 * Initial building of folder list for
367 * the top of our search tree.
371 BuildFolderList(char *dirName, int searchdepth)
375 /* Make sure we have a directory */
376 if ((stat(dirName, &st) == -1) || !S_ISDIR(st.st_mode))
380 * If base directory, don't add it to the
381 * folder list. We just recurse into it.
383 if (!strcmp (dirName, ".")) {
384 BuildFolderListRecurse (".", &st, 0);
389 * Add this folder to the list.
390 * If recursing and directory has subfolders,
391 * then build folder list for subfolders.
393 if (AddFolder(dirName, showzero) && (recurse || searchdepth) && st.st_nlink > 2)
394 BuildFolderListRecurse(dirName, &st, searchdepth - 1);
398 * Recursive building of folder list
402 BuildFolderListRecurse(char *dirName, struct stat *s, int searchdepth)
404 char *base, *n, name[PATH_MAX];
411 * Keep track of number of directories we've seen so we can
412 * stop stat'ing entries in this directory once we've seen
413 * them all. This optimization will fail if you have extra
414 * directories beginning with ".", since we don't bother to
415 * stat them. But that shouldn't generally be a problem.
417 nlinks = s->st_nlink;
419 if (!(dir = opendir(dirName)))
420 adios(dirName, "can't open directory");
423 * A hack so that we don't see a
424 * leading "./" in folder names.
426 base = strcmp (dirName, ".") ? dirName : dirName + 1;
428 while (nlinks && (dp = readdir(dir))) {
429 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) {
433 if (dp->d_name[0] == '.')
435 /* Check to see if the name of the file is a number
436 * if it is, we assume it's a mail file and skip it
438 for (n = dp->d_name; *n && isdigit(*n); n++);
441 strncpy (name, base, sizeof(name) - 2);
444 strncat(name, dp->d_name, sizeof(name) - strlen(name) - 1);
445 if ((stat(name, &st) != -1) && S_ISDIR(st.st_mode)) {
447 * Check if this was really a symbolic link pointing
448 * to a directory. If not, then decrement link count.
450 if (lstat (name, &st) == -1)
452 /* Add this folder to the list */
453 if (AddFolder(name, showzero) &&
454 (recurse || searchdepth) && st.st_nlink > 2)
455 BuildFolderListRecurse(name, &st, searchdepth - 1);
462 * Add this folder to our list, counting the total number of
463 * messages and the number of messages in each sequence.
467 AddFolder(char *name, int force)
469 int i, msgnum, nonzero;
470 int seqnum[NUMATTRS], nSeq[NUMATTRS];
474 /* Read folder and create message structure */
475 if (!(mp = folder_read (name))) {
476 /* Oops, error occurred. Record it and continue. */
477 AllocFolders(&folders, &nFoldersAlloced, nFolders + 1);
478 f = &folders[nFolders++];
479 f->name = getcpy(name);
481 f->priority = AssignPriority(f->name);
485 for (i = 0; i < numsequences; i++) {
486 /* Convert sequences to their sequence numbers */
487 if (sequencesToDo[i])
488 seqnum[i] = seq_getnum(mp, sequencesToDo[i]);
492 /* Now count messages in this sequence */
494 if (mp->nummsg > 0 && seqnum[i] != -1) {
495 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++) {
496 if (in_sequence(mp, seqnum[i], msgnum))
502 /* Check if any of the sequence checks were nonzero */
504 for (i = 0; i < numsequences; i++) {
511 if (nonzero || force) {
512 /* save general folder information */
513 AllocFolders(&folders, &nFoldersAlloced, nFolders + 1);
514 f = &folders[nFolders++];
515 f->name = getcpy(name);
516 f->nMsgs = mp->nummsg;
518 f->priority = AssignPriority(f->name);
520 /* record the sequence information */
521 for (i = 0; i < numsequences; i++) {
522 f->nSeq[i] = nSeq[i];
523 f->private[i] = (seqnum[i] != -1) ? is_seq_private(mp, seqnum[i]) : 0;
527 folder_free (mp); /* free folder/message structure */
532 * Print the folder/sequence information
538 char tmpname[BUFSIZ];
539 int i, j, len, has_private = 0;
540 int maxfolderlen = 0, maxseqlen = 0;
541 int maxnum = 0, maxseq = 0;
544 for (i = 0; i < nFolders; i++)
545 printf("%s\n", folders[i].name);
550 * Find the width we need for various fields
552 for (i = 0; i < nFolders; ++i) {
553 /* find the length of longest folder name */
554 len = strlen(folders[i].name);
555 if (len > maxfolderlen)
558 /* If folder had error, skip the rest */
559 if (folders[i].error)
562 /* find the maximum total messages */
563 if (folders[i].nMsgs > maxnum)
564 maxnum = folders[i].nMsgs;
566 for (j = 0; j < numsequences; j++) {
567 /* find maximum width of sequence name */
568 len = strlen (sequencesToDo[j]);
569 if ((folders[i].nSeq[j] > 0 || showzero) && (len > maxseqlen))
572 /* find the maximum number of messages in sequence */
573 if (folders[i].nSeq[j] > maxseq)
574 maxseq = folders[i].nSeq[j];
576 /* check if this sequence is private in any of the folders */
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(curfolder, folders[i].name))
588 snprintf(tmpname, sizeof(tmpname), "%s", folders[i].name);
590 snprintf(tmpname, sizeof(tmpname), "%s+", folders[i].name);
592 if (folders[i].error) {
593 printf("%-*s is unreadable\n", maxfolderlen+1, tmpname);
597 printf("%-*s has %*d in sequence %-*s%s; out of %*d\n",
598 maxfolderlen+1, tmpname,
599 num_digits(maxseq), folders[i].nSeq[j],
600 maxseqlen, sequencesToDo[j],
601 !has_private ? "" : folders[i].private[j] ? " (private)" : " ",
602 num_digits(maxnum), folders[i].nMsgs);
609 * Calculate the number of digits in a nonnegative integer
618 adios (NULL, "oops, num_digits called with negative value");
632 * Put them in priority order.
636 CompareFolders(struct Folder *f1, struct Folder *f2)
638 if (!alphaOrder && f1->priority != f2->priority)
639 return f1->priority - f2->priority;
641 return strcmp(f1->name, f2->name);
645 * Make sure we have at least n folders allocated.
649 AllocFolders(struct Folder **f, int *nfa, int n)
655 *f = (struct Folder *) malloc (*nfa * (sizeof(struct Folder)));
658 *f = (struct Folder *) realloc (*f, *nfa * (sizeof(struct Folder)));
663 * Return the priority for a name. The highest comes from an exact match.
664 * After that, the longest match (then first) assigns the priority.
667 AssignPriority(char *name)
675 for (i = 0; i < nOrders; ++i) {
677 if (!strcmp(name, o->name))
679 ol = strlen(o->name);
684 if (o->name[0] == '*' && !strcmp(o->name + 1, name + (nl - ol + 1))) {
687 } else if (o->name[ol - 1] == '*' && strncmp(o->name, name, ol - 1) == 0) {
696 * Do the read only folders
700 do_readonly_folders (void)
704 register struct node *np;
706 snprintf (atrcur, sizeof(atrcur), "atr-%s-", current);
707 atrlen = strlen (atrcur);
709 for (np = m_defs; np; np = np->n_next)
710 if (ssequal (atrcur, np->n_name)
711 && !ssequal (nmhdir, np->n_name + atrlen))
712 BuildFolderList (np->n_name + atrlen, 0);