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 = mh_xcalloc(maxfolders, sizeof(*foldersToDo));
148 /* no sequences yet */
151 /* parse arguments */
152 while ((cp = *argp++)) {
154 switch (smatch(++cp, switches)) {
156 ambigsw(cp, switches);
159 adios(EX_USAGE, NULL, "-%s unknown", cp);
162 snprintf(buf, sizeof(buf), "%s [+folder1 [+folder2 ...]][switches]", invo_name);
163 print_help(buf, switches, 1);
164 exit(argc == 2 ? EX_OK : EX_USAGE);
167 print_version(invo_name);
168 exit(argc == 2 ? EX_OK : EX_USAGE);
171 if (!(cp = *argp++) || *cp == '-')
172 adios(EX_USAGE, NULL, "missing argument to %s",
175 /* check if too many sequences specified */
176 if (numsequences >= NUMATTRS)
177 adios(EX_USAGE, NULL, "too many sequences (more than %d) specified", NUMATTRS);
178 sequencesToDo[numsequences++] = cp;
218 ** Check if we need to allocate more space
221 if (numfolders >= maxfolders) {
222 maxfolders += MAXFOLDERS;
223 foldersToDo = mh_xrealloc(foldersToDo, maxfolders * sizeof(*foldersToDo));
225 if (*cp == '+' || *cp == '@') {
226 foldersToDo[numfolders++] = getcpy(expandfol(cp));
228 foldersToDo[numfolders++] = cp;
232 /* get current folder */
233 strncpy(curfol, getcurfol(), sizeof(curfol));
235 /* get nmh base directory */
236 nmhdir = toabsdir("+");
239 ** If no sequences specified, we use the `unseen' sequence(s)
240 ** We check to make sure that the Unseen-Sequence entry doesn't
241 ** contain too many sequences.
243 if (numsequences == 0) {
246 if ((cp = context_find(usequence))) {
248 adios(EX_CONFIG, NULL, "profile entry %s set, but empty, and no sequence given", usequence);
251 cp = seq_unseen; /* use default */
254 ap = brkstring(dp, " ", "\n");
255 for (; ap && *ap; ap++) {
256 if (numsequences >= NUMATTRS) {
257 adios(EX_USAGE, NULL, "too many sequences (more than %d) in %s profile entry", NUMATTRS, usequence);
259 sequencesToDo[numsequences++] = *ap;
266 qsort(folders, nFolders, sizeof(struct Folder),
267 (qsort_comp) CompareFolders);
273 ** Read the Flist-Order profile entry to determine
274 ** how to sort folders for output.
280 unsigned char *p, *s;
284 if (!(p = context_find("Flist-Order")))
290 while (*p && !isspace(*p))
294 AllocFolders(&orders, &nOrdersAlloced, nOrders + 1);
295 o = &orders[nOrders++];
296 o->priority = priority++;
297 o->name = mh_xcalloc(p - s + 1, sizeof(char));
298 strncpy(o->name, s, p - s);
306 ** Scan all the necessary folders
315 * change directory to base of nmh directory
317 if (chdir(nmhdir) == NOTOK)
318 adios(EX_OSERR, nmhdir, "unable to change directory to");
320 if (numfolders > 0) {
322 strncpy(curfol, foldersToDo[numfolders - 1], sizeof(curfol));
323 context_replace(curfolder, curfol); /* update current folder */
324 context_save(); /* save the context file */
327 ** Scan each given folder. If -all is given,
328 ** then also scan the 1st level subfolders under
329 ** each given folder.
331 for (i = 0; i < numfolders; ++i)
332 BuildFolderList(foldersToDo[i], all ? 1 : 0);
335 /* Do the readonly folders */
336 do_readonly_folders();
338 /* Now scan the entire nmh directory for folders */
339 BuildFolderList(".", 0);
341 /* Else scan current folder */
342 BuildFolderList(curfol, 0);
348 ** Initial building of folder list for
349 ** the top of our search tree.
353 BuildFolderList(char *dirName, int searchdepth)
357 /* Make sure we have a directory */
358 if ((stat(dirName, &st) == -1) || !S_ISDIR(st.st_mode))
362 ** If base directory, don't add it to the
363 ** folder list. We just recurse into it.
365 if (strcmp(dirName, ".")==0) {
366 BuildFolderListRecurse(".", &st, 0);
371 ** Add this folder to the list.
372 ** If recursing and directory has subfolders,
373 ** then build folder list for subfolders.
375 if (AddFolder(dirName, showzero) && (recurse || searchdepth) &&
377 BuildFolderListRecurse(dirName, &st, searchdepth - 1);
381 ** Recursive building of folder list
385 BuildFolderListRecurse(char *dirName, struct stat *s, int searchdepth)
387 char *base, name[PATH_MAX];
395 ** Keep track of number of directories we've seen so we can
396 ** stop stat'ing entries in this directory once we've seen
397 ** them all. This optimization will fail if you have extra
398 ** directories beginning with ".", since we don't bother to
399 ** stat them. But that shouldn't generally be a problem.
401 nlinks = s->st_nlink;
403 if (!(dir = opendir(dirName)))
404 adios(EX_IOERR, dirName, "can't open directory");
407 ** A hack so that we don't see a
408 ** leading "./" in folder names.
410 base = (strcmp(dirName, ".")==0) ? dirName + 1 : dirName;
412 while (nlinks && (dp = readdir(dir))) {
413 if (strcmp(dp->d_name, ".")==0 ||
414 strcmp(dp->d_name, "..")==0) {
418 if (dp->d_name[0] == '.')
420 /* Check to see if the name of the file is a number
421 ** if it is, we assume it's a mail file and skip it
423 for (n = dp->d_name; *n && isdigit(*n); n++);
426 strncpy(name, base, sizeof(name) - 2);
429 strncat(name, dp->d_name, sizeof(name) - strlen(name) - 1);
430 if ((stat(name, &st) != -1) && S_ISDIR(st.st_mode)) {
432 ** Check if this was really a symbolic link pointing
433 ** to a directory. If not, then decrement link count.
435 if (lstat(name, &st) == -1)
437 /* Add this folder to the list */
438 if (AddFolder(name, showzero) &&
439 (recurse || searchdepth) &&
441 BuildFolderListRecurse(name, &st,
449 ** Add this folder to our list, counting the total number of
450 ** messages and the number of messages in each sequence.
454 AddFolder(char *name, int force)
458 int seqnum[NUMATTRS], nSeq[NUMATTRS];
462 /* Read folder and create message structure */
463 if (!(mp = folder_read(name))) {
464 /* Oops, error occurred. Record it and continue. */
465 AllocFolders(&folders, &nFoldersAlloced, nFolders + 1);
466 f = &folders[nFolders++];
467 f->name = getcpy(name);
469 f->priority = AssignPriority(f->name);
473 for (i = 0; i < numsequences; i++) {
474 /* Convert sequences to their sequence numbers */
475 if (sequencesToDo[i])
476 seqnum[i] = seq_getnum(mp, sequencesToDo[i]);
480 /* Now count messages in this sequence */
482 if (mp->nummsg > 0 && seqnum[i] != -1) {
483 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg;
485 if (in_sequence(mp, seqnum[i], msgnum))
491 /* Check if any of the sequence checks were nonzero */
493 for (i = 0; i < numsequences; i++) {
500 if (nonzero || force) {
501 /* save general folder information */
502 AllocFolders(&folders, &nFoldersAlloced, nFolders + 1);
503 f = &folders[nFolders++];
504 f->name = getcpy(name);
505 f->nMsgs = mp->nummsg;
507 f->priority = AssignPriority(f->name);
509 /* record the sequence information */
510 for (i = 0; i < numsequences; i++) {
511 f->nSeq[i] = nSeq[i];
512 f->private[i] = (seqnum[i] != -1) ?
513 is_seq_private(mp, seqnum[i]) : 0;
517 folder_free(mp); /* free folder/message structure */
522 ** Print the folder/sequence information
528 char tmpname[BUFSIZ];
529 unsigned int i, j, len, has_private = 0;
530 unsigned int maxfolderlen = 0, maxseqlen = 0;
531 int maxnum = 0, maxseq = 0;
534 for (i = 0; i < nFolders; i++)
535 printf("%s\n", folders[i].name);
540 ** Find the width we need for various fields
542 for (i = 0; i < nFolders; ++i) {
543 /* find the length of longest folder name */
544 len = strlen(folders[i].name);
545 if (len > maxfolderlen)
548 /* If folder had error, skip the rest */
549 if (folders[i].error)
552 /* find the maximum total messages */
553 if (folders[i].nMsgs > maxnum)
554 maxnum = folders[i].nMsgs;
556 for (j = 0; j < numsequences; j++) {
557 /* find maximum width of sequence name */
558 len = strlen(sequencesToDo[j]);
559 if ((folders[i].nSeq[j] > 0 || showzero) &&
563 /* find the maximum number of messages in sequence */
564 if (folders[i].nSeq[j] > maxseq)
565 maxseq = folders[i].nSeq[j];
568 ** check if this sequence is private in any of
571 if (folders[i].private[j])
576 /* Now print all the folder/sequence information */
577 for (i = 0; i < nFolders; i++) {
578 for (j = 0; j < numsequences; j++) {
579 if (folders[i].nSeq[j] > 0 || showzero) {
580 /* Add `+' to end of name of current folder */
581 if (strcmp(curfol, folders[i].name)!=0)
582 snprintf(tmpname, sizeof(tmpname),
583 "%s", folders[i].name);
585 snprintf(tmpname, sizeof(tmpname),
589 if (folders[i].error) {
590 printf("%-*s is unreadable\n",
596 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);
603 ** Put them in priority order.
607 CompareFolders(struct Folder *f1, struct Folder *f2)
609 if (!alphaOrder && f1->priority != f2->priority)
610 return f1->priority - f2->priority;
612 return strcmp(f1->name, f2->name);
616 ** Make sure we have at least n folders allocated.
620 AllocFolders(struct Folder **f, int *nfa, int n)
626 *f = mh_xcalloc(*nfa, sizeof(struct Folder));
629 *f = mh_xrealloc(*f, *nfa * sizeof(struct Folder));
634 ** Return the priority for a name. The highest comes from an exact match.
635 ** After that, the longest match (then first) assigns the priority.
638 AssignPriority(char *name)
646 for (i = 0; i < nOrders; ++i) {
648 if (strcmp(name, o->name)==0)
650 ol = strlen(o->name);
655 if (o->name[0] == '*' &&
656 strcmp(o->name + 1, name + (nl - ol + 1))==0) {
659 } else if (o->name[ol - 1] == '*' &&
660 strncmp(o->name, name, ol - 1) == 0) {
669 ** Do the read only folders
673 do_readonly_folders(void)
679 snprintf(atrcur, sizeof(atrcur), "atr-%s-", seq_cur);
680 atrlen = strlen(atrcur);
682 for (np = m_defs; np; np = np->n_next)
683 if (strncmp(np->n_name, atrcur, atrlen)==0
684 && strncmp(np->n_name+atrlen, nmhdir, strlen(nmhdir))!=0)
685 /* Why do we exclude absolute path names? --meillo */
686 BuildFolderList(np->n_name + atrlen, 0);