]>
diplodocus.org Git - nmh/blob - uip/flist.c
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 #define FLIST_SWITCHES \
28 X("sequence name", 0, SEQSW) \
30 X("noall", 0, NOALLSW) \
31 X("recurse", 0, RECURSE) \
32 X("norecurse", 0, NORECURSE) \
33 X("showzero", 0, SHOWZERO) \
34 X("noshowzero", 0, NOSHOWZERO) \
35 X("alpha", 0, ALPHASW) \
36 X("noalpha", 0, NOALPHASW) \
37 X("fast", 0, FASTSW) \
38 X("nofast", 0, NOFASTSW) \
39 X("total", -5, TOTALSW) \
40 X("nototal", -7, NOTOTALSW) \
41 X("version", 0, VERSIONSW) \
42 X("help", 0, HELPSW) \
44 #define X(sw, minchars, id) id,
45 DEFINE_SWITCH_ENUM(FLIST
);
48 #define X(sw, minchars, id) { sw, minchars, id },
49 DEFINE_SWITCH_ARRAY(FLIST
, switches
);
53 char *name
; /* name of folder */
55 int error
; /* error == 1 for unreadable folder */
56 int nMsgs
; /* number of messages in folder */
57 ivector_t nSeq
; /* number of messages in each sequence */
58 ivector_t
private; /* is given sequence, public or private */
61 static struct Folder
*orders
= NULL
;
62 static int nOrders
= 0;
63 static int nOrdersAlloced
= 0;
64 static struct Folder
*folders
= NULL
;
65 static unsigned int nFolders
= 0;
66 static int nFoldersAlloced
= 0;
68 /* info on folders to search */
69 static char **foldersToDo
;
70 static int numfolders
;
71 static int maxfolders
;
73 /* info on sequences to search for */
74 static svector_t sequencesToDo
;
76 static int all
= FALSE
; /* scan all folders in top level? */
77 static int alphaOrder
= FALSE
; /* want alphabetical order only */
78 static int recurse
= FALSE
; /* show nested folders? */
79 static int showzero
= TRUE
; /* show folders even if no messages in seq? */
80 static int Total
= TRUE
; /* display info on number of messages in *
81 * sequence found, and total num messages */
83 static char curfolder
[BUFSIZ
]; /* name of the current folder */
84 static char *nmhdir
; /* base nmh mail directory */
87 * Type for a compare function for qsort. This keeps
90 typedef int (*qsort_comp
) (const void *, const void *);
95 int CompareFolders(struct Folder
*, struct Folder
*);
96 void GetFolderOrder(void);
97 void ScanFolders(void);
98 int AddFolder(char *, int);
99 void BuildFolderList(char *, int);
100 void BuildFolderListRecurse(char *, struct stat
*, int);
101 void PrintFolders(void);
102 void AllocFolders(struct Folder
**, int *, int);
103 int AssignPriority(char *);
104 static void do_readonly_folders(void);
109 main(int argc
, char **argv
)
115 if (nmh_init(argv
[0], 1)) { return 1; }
118 * If program was invoked with name ending
119 * in `s', then add switch `-all'.
121 all
= HasSuffixC(argv
[0], 's');
123 arguments
= getarguments (invo_name
, argc
, argv
, 1);
126 /* allocate the initial space to record the folder names */
128 maxfolders
= MAXFOLDERS
;
129 foldersToDo
= (char **) mh_xmalloc ((size_t) (maxfolders
* sizeof(*foldersToDo
)));
131 /* no sequences yet */
132 sequencesToDo
= svector_create (0);
134 /* parse arguments */
135 while ((cp
= *argp
++)) {
137 switch (smatch(++cp
, switches
)) {
139 ambigsw(cp
, switches
);
142 adios(NULL
, "-%s unknown", cp
);
145 snprintf(buf
, sizeof(buf
), "%s [+folder1 [+folder2 ...]][switches]",
147 print_help(buf
, switches
, 1);
150 print_version(invo_name
);
154 if (!(cp
= *argp
++) || *cp
== '-')
155 adios (NULL
, "missing argument to %s", argp
[-2]);
157 svector_push_back (sequencesToDo
, cp
);
200 * Check if we need to allocate more space
203 if (numfolders
>= maxfolders
) {
204 maxfolders
+= MAXFOLDERS
;
205 foldersToDo
= (char **) mh_xrealloc (foldersToDo
,
206 (size_t) (maxfolders
* sizeof(*foldersToDo
)));
208 if (*cp
== '+' || *cp
== '@') {
209 foldersToDo
[numfolders
++] =
212 foldersToDo
[numfolders
++] = cp
;
216 if (!context_find ("path"))
217 free (path ("./", TFOLDER
));
219 /* get current folder */
220 strncpy (curfolder
, getfolder(1), sizeof(curfolder
));
222 /* get nmh base directory */
223 nmhdir
= m_maildir ("");
226 * If we didn't specify any sequences, we search
227 * for the "Unseen-Sequence" profile entry and use
228 * all the sequences defined there.
230 if (svector_size (sequencesToDo
) == 0) {
231 if ((cp
= context_find(usequence
)) && *cp
) {
235 ap
= brkstring (dp
, " ", "\n");
236 for (; ap
&& *ap
; ap
++)
237 svector_push_back (sequencesToDo
, *ap
);
239 adios (NULL
, "no sequence specified or %s profile entry found", usequence
);
245 qsort(folders
, nFolders
, sizeof(struct Folder
), (qsort_comp
) CompareFolders
);
247 svector_free (sequencesToDo
);
253 * Read the Flist-Order profile entry to determine
254 * how to sort folders for output.
264 if (!(p
= context_find("Flist-Order")))
267 while (isspace((unsigned char) *p
))
270 while (*p
&& !isspace((unsigned char) *p
))
274 AllocFolders(&orders
, &nOrdersAlloced
, nOrders
+ 1);
275 o
= &orders
[nOrders
++];
276 o
->priority
= priority
++;
277 o
->name
= (char *) mh_xmalloc(p
- s
+ 1);
278 strncpy(o
->name
, s
, p
- s
);
286 * Scan all the necessary folders
295 * change directory to base of nmh directory
297 if (chdir (nmhdir
) == NOTOK
)
298 adios (nmhdir
, "unable to change directory to");
300 if (numfolders
> 0) {
302 strncpy (curfolder
, foldersToDo
[numfolders
- 1], sizeof(curfolder
));
303 context_replace (pfolder
, curfolder
);/* update current folder */
304 context_save (); /* save the context file */
307 * Scan each given folder. If -all is given,
308 * then also scan the 1st level subfolders under
311 for (i
= 0; i
< numfolders
; ++i
)
312 BuildFolderList(foldersToDo
[i
], all
? 1 : 0);
316 * Do the readonly folders
318 do_readonly_folders();
321 * Now scan the entire nmh directory for folders
323 BuildFolderList(".", 0);
326 * Else scan current folder
328 BuildFolderList(curfolder
, 0);
334 * Initial building of folder list for
335 * the top of our search tree.
339 BuildFolderList(char *dirName
, int searchdepth
)
343 /* Make sure we have a directory */
344 if ((stat(dirName
, &st
) == -1) || !S_ISDIR(st
.st_mode
))
348 * If base directory, don't add it to the
349 * folder list. We just recurse into it.
351 if (!strcmp (dirName
, ".")) {
352 BuildFolderListRecurse (".", &st
, 0);
357 * Add this folder to the list.
358 * If recursing and directory has subfolders,
359 * then build folder list for subfolders.
361 if (AddFolder(dirName
, showzero
) && (recurse
|| searchdepth
) && st
.st_nlink
> 2)
362 BuildFolderListRecurse(dirName
, &st
, searchdepth
- 1);
366 * Recursive building of folder list
370 BuildFolderListRecurse(char *dirName
, struct stat
*s
, int searchdepth
)
372 char *base
, name
[PATH_MAX
];
380 * Keep track of number of directories we've seen so we can
381 * stop stat'ing entries in this directory once we've seen
382 * them all. This optimization will fail if you have extra
383 * directories beginning with ".", since we don't bother to
384 * stat them. But that shouldn't generally be a problem.
386 nlinks
= s
->st_nlink
;
388 /* Disable the optimization under conditions where st_nlink
389 is set to 1. That happens on Cygwin, for example:
390 http://cygwin.com/ml/cygwin-apps/2008-08/msg00264.html */
394 if (!(dir
= opendir(dirName
)))
395 adios(dirName
, "can't open directory");
398 * A hack so that we don't see a
399 * leading "./" in folder names.
401 base
= strcmp (dirName
, ".") ? dirName
: dirName
+ 1;
403 while (nlinks
&& (dp
= readdir(dir
))) {
404 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, "..")) {
408 if (dp
->d_name
[0] == '.')
410 /* Check to see if the name of the file is a number
411 * if it is, we assume it's a mail file and skip it
413 for (n
= dp
->d_name
; *n
&& isdigit((unsigned char) *n
); n
++);
416 strncpy (name
, base
, sizeof(name
) - 2);
419 strncat(name
, dp
->d_name
, sizeof(name
) - strlen(name
) - 1);
420 if ((stat(name
, &st
) != -1) && S_ISDIR(st
.st_mode
)) {
422 * Check if this was really a symbolic link pointing
423 * to a directory. If not, then decrement link count.
425 if (lstat (name
, &st
) == -1)
427 /* Add this folder to the list */
428 if (AddFolder(name
, showzero
) &&
429 (recurse
|| searchdepth
) && st
.st_nlink
> 2)
430 BuildFolderListRecurse(name
, &st
, searchdepth
- 1);
437 * Add this folder to our list, counting the total number of
438 * messages and the number of messages in each sequence.
442 AddFolder(char *name
, int force
)
446 ivector_t seqnum
= ivector_create (0), nSeq
= ivector_create (0);
451 /* Read folder and create message structure */
452 if (!(mp
= folder_read (name
, 0))) {
453 /* Oops, error occurred. Record it and continue. */
454 AllocFolders(&folders
, &nFoldersAlloced
, nFolders
+ 1);
455 f
= &folders
[nFolders
++];
456 f
->name
= mh_xstrdup(name
);
458 f
->priority
= AssignPriority(f
->name
);
462 for (i
= 0; i
< svector_size (sequencesToDo
); i
++) {
463 /* Convert sequences to their sequence numbers */
464 if ((cp
= svector_at (sequencesToDo
, i
)))
465 ivector_push_back (seqnum
, seq_getnum(mp
, cp
));
467 ivector_push_back (seqnum
, -1);
469 /* Now count messages in this sequence */
470 ivector_push_back (nSeq
, 0);
471 if (mp
->nummsg
> 0 && ivector_at (seqnum
, i
) != -1) {
472 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++) {
473 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
))
474 (*ivector_atp (nSeq
, i
))++;
479 /* Check if any of the sequence checks were nonzero */
481 for (i
= 0; i
< svector_size (sequencesToDo
); i
++) {
482 if (ivector_at (nSeq
, i
) > 0) {
488 if (nonzero
|| force
) {
489 /* save general folder information */
490 AllocFolders(&folders
, &nFoldersAlloced
, nFolders
+ 1);
491 f
= &folders
[nFolders
++];
492 f
->name
= mh_xstrdup(name
);
493 f
->nMsgs
= mp
->nummsg
;
494 f
->nSeq
= ivector_create (0);
495 f
->private = ivector_create (0);
497 f
->priority
= AssignPriority(f
->name
);
499 /* record the sequence information */
500 for (i
= 0; i
< svector_size (sequencesToDo
); i
++) {
501 *ivector_atp (f
->nSeq
, i
) = ivector_at (nSeq
, i
);
502 ivector_push_back (f
->private,
503 ivector_at (seqnum
, i
) != -1
504 ? is_seq_private(mp
, ivector_at (seqnum
, i
))
510 ivector_free (seqnum
);
511 folder_free (mp
); /* free folder/message structure */
516 * Print the folder/sequence information
522 char tmpname
[BUFSIZ
];
523 unsigned int i
, j
, len
, has_private
= 0;
524 unsigned int maxfolderlen
= 0, maxseqlen
= 0;
525 int maxnum
= 0, maxseq
= 0;
528 for (i
= 0; i
< nFolders
; i
++)
529 printf("%s\n", folders
[i
].name
);
534 * Find the width we need for various fields
536 for (i
= 0; i
< nFolders
; ++i
) {
537 /* find the length of longest folder name */
538 len
= strlen(folders
[i
].name
);
539 if (len
> maxfolderlen
)
542 /* If folder had error, skip the rest */
543 if (folders
[i
].error
)
546 /* find the maximum total messages */
547 if (folders
[i
].nMsgs
> maxnum
)
548 maxnum
= folders
[i
].nMsgs
;
550 for (j
= 0; j
< svector_size (sequencesToDo
); j
++) {
551 /* find maximum width of sequence name */
552 len
= strlen (svector_at (sequencesToDo
, j
));
553 if ((ivector_at (folders
[i
].nSeq
, j
) > 0 || showzero
) &&
557 /* find the maximum number of messages in sequence */
558 if (ivector_at (folders
[i
].nSeq
, j
) > maxseq
)
559 maxseq
= ivector_at (folders
[i
].nSeq
, j
);
561 /* check if this sequence is private in any of the folders */
562 if (ivector_at (folders
[i
].private, j
))
567 /* Now print all the folder/sequence information */
568 for (i
= 0; i
< nFolders
; i
++) {
569 for (j
= 0; j
< svector_size (sequencesToDo
); j
++) {
570 if (ivector_at (folders
[i
].nSeq
, j
) > 0 || showzero
) {
571 /* Add `+' to end of name of current folder */
572 if (strcmp(curfolder
, folders
[i
].name
))
573 snprintf(tmpname
, sizeof(tmpname
), "%s", folders
[i
].name
);
575 snprintf(tmpname
, sizeof(tmpname
), "%s+", folders
[i
].name
);
577 if (folders
[i
].error
) {
578 printf("%-*s is unreadable\n", maxfolderlen
+1, tmpname
);
582 printf("%-*s has %*d in sequence %-*s%s; out of %*d\n",
583 maxfolderlen
+1, tmpname
,
584 num_digits(maxseq
), ivector_at (folders
[i
].nSeq
, j
),
585 maxseqlen
, svector_at (sequencesToDo
, j
),
586 !has_private
? "" : ivector_at (folders
[i
].private, j
)
587 ? " (private)" : " ",
588 num_digits(maxnum
), folders
[i
].nMsgs
);
595 * Put them in priority order.
599 CompareFolders(struct Folder
*f1
, struct Folder
*f2
)
601 if (!alphaOrder
&& f1
->priority
!= f2
->priority
)
602 return f1
->priority
- f2
->priority
;
603 return strcmp(f1
->name
, f2
->name
);
607 * Make sure we have at least n folders allocated.
611 AllocFolders(struct Folder
**f
, int *nfa
, int n
)
617 *f
= (struct Folder
*) mh_xmalloc (*nfa
* (sizeof(struct Folder
)));
620 *f
= (struct Folder
*) mh_xrealloc (*f
, *nfa
* (sizeof(struct Folder
)));
625 * Return the priority for a name. The highest comes from an exact match.
626 * After that, the longest match (then first) assigns the priority.
629 AssignPriority(char *name
)
637 for (i
= 0; i
< nOrders
; ++i
) {
639 if (!strcmp(name
, o
->name
))
641 ol
= strlen(o
->name
);
646 if (o
->name
[0] == '*' && !strcmp(o
->name
+ 1, name
+ (nl
- ol
+ 1))) {
649 } else if (o
->name
[ol
- 1] == '*' && strncmp(o
->name
, name
, ol
- 1) == 0) {
658 * Do the read only folders
662 do_readonly_folders (void)
668 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
669 atrlen
= strlen (atrcur
);
671 for (np
= m_defs
; np
; np
= np
->n_next
)
672 if (ssequal (atrcur
, np
->n_name
)
673 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
674 BuildFolderList (np
->n_name
+ atrlen
, 0);