]>
diplodocus.org Git - nmh/blob - uip/flist.c
1 /* flist.c -- list nmh folders containing messages
2 * -- in a given sequence
5 * David Nichols, Xerox-PARC, November, 1992
7 * Copyright (c) 1994 Xerox Corporation.
8 * Use and copying of this software and preparation of derivative works based
9 * upon this software are permitted. Any distribution of this software or
10 * derivative works must comply with all applicable United States export
11 * control laws. This software is made available AS IS, and Xerox Corporation
12 * makes no warranty about the software, its performance or its conformity to
19 #include "sbr/m_maildir.h"
22 * We allocate space to record the names of folders
23 * (foldersToDo array), this number of elements at a time.
25 #define MAXFOLDERS 100
28 #define FLIST_SWITCHES \
29 X("sequence name", 0, SEQSW) \
31 X("noall", 0, NOALLSW) \
32 X("recurse", 0, RECURSE) \
33 X("norecurse", 0, NORECURSE) \
34 X("showzero", 0, SHOWZERO) \
35 X("noshowzero", 0, NOSHOWZERO) \
36 X("alpha", 0, ALPHASW) \
37 X("noalpha", 0, NOALPHASW) \
38 X("fast", 0, FASTSW) \
39 X("nofast", 0, NOFASTSW) \
40 X("total", -5, TOTALSW) \
41 X("nototal", -7, NOTOTALSW) \
42 X("version", 0, VERSIONSW) \
43 X("help", 0, HELPSW) \
45 #define X(sw, minchars, id) id,
46 DEFINE_SWITCH_ENUM(FLIST
);
49 #define X(sw, minchars, id) { sw, minchars, id },
50 DEFINE_SWITCH_ARRAY(FLIST
, switches
);
54 char *name
; /* name of folder */
56 int error
; /* error == 1 for unreadable folder */
57 int nMsgs
; /* number of messages in folder */
58 ivector_t nSeq
; /* number of messages in each sequence */
59 ivector_t
private; /* is given sequence, public or private */
62 static struct Folder
*orders
= NULL
;
63 static int nOrders
= 0;
64 static int nOrdersAlloced
= 0;
65 static struct Folder
*folders
= NULL
;
66 static unsigned int nFolders
= 0;
67 static int nFoldersAlloced
= 0;
69 /* info on folders to search */
70 static char **foldersToDo
;
71 static int numfolders
;
72 static int maxfolders
;
74 /* info on sequences to search for */
75 static svector_t sequencesToDo
;
77 static bool all
; /* scan all folders in top level? */
78 static bool alphaOrder
; /* want alphabetical order only */
79 static bool recurse
; /* show nested folders? */
80 static bool showzero
= true; /* show folders even if no messages in seq? */
81 static bool Total
= true; /* display info on number of messages in
82 * sequence found, and total num messages */
84 static char curfolder
[BUFSIZ
]; /* name of the current folder */
85 static char *nmhdir
; /* base nmh mail directory */
88 * Type for a compare function for qsort. This keeps
91 typedef int (*qsort_comp
) (const void *, const void *);
96 static int CompareFolders(struct Folder
*, struct Folder
*);
97 static void GetFolderOrder(void);
98 static void ScanFolders(void);
99 static int AddFolder(char *, int);
100 static void BuildFolderList(char *, int);
101 static void BuildFolderListRecurse(char *, struct stat
*, int);
102 static void PrintFolders(void);
103 static void AllocFolders(struct Folder
**, int *, int);
104 static int AssignPriority(char *);
105 static void do_readonly_folders(void);
110 main(int argc
, char **argv
)
116 if (nmh_init(argv
[0], true, true)) { return 1; }
119 * If program was invoked with name ending
120 * in `s', then add switch `-all'.
122 all
= has_suffix_c(argv
[0], 's');
124 arguments
= getarguments (invo_name
, argc
, argv
, 1);
127 /* allocate the initial space to record the folder names */
129 maxfolders
= MAXFOLDERS
;
130 foldersToDo
= mh_xmalloc ((size_t) (maxfolders
* sizeof(*foldersToDo
)));
132 /* no sequences yet */
133 sequencesToDo
= svector_create (0);
135 /* parse arguments */
136 while ((cp
= *argp
++)) {
138 switch (smatch(++cp
, switches
)) {
140 ambigsw(cp
, switches
);
143 die("-%s unknown", cp
);
146 snprintf(buf
, sizeof(buf
), "%s [+folder1 [+folder2 ...]][switches]",
148 print_help(buf
, switches
, 1);
151 print_version(invo_name
);
155 if (!(cp
= *argp
++) || *cp
== '-')
156 die("missing argument to %s", argp
[-2]);
158 svector_push_back (sequencesToDo
, cp
);
201 * Check if we need to allocate more space
204 if (numfolders
>= maxfolders
) {
205 maxfolders
+= MAXFOLDERS
;
206 foldersToDo
= mh_xrealloc (foldersToDo
,
207 (size_t) (maxfolders
* sizeof(*foldersToDo
)));
209 if (*cp
== '+' || *cp
== '@') {
210 foldersToDo
[numfolders
++] =
213 foldersToDo
[numfolders
++] = cp
;
217 if (!context_find ("path"))
218 free (path ("./", TFOLDER
));
220 /* get current folder */
221 strncpy (curfolder
, getfolder(1), sizeof(curfolder
));
223 /* get nmh base directory */
224 nmhdir
= m_maildir ("");
227 * If we didn't specify any sequences, we search
228 * for the "Unseen-Sequence" profile entry and use
229 * all the sequences defined there.
231 if (svector_size (sequencesToDo
) == 0) {
232 if ((cp
= context_find(usequence
)) && *cp
) {
236 ap
= brkstring (dp
, " ", "\n");
237 for (; ap
&& *ap
; ap
++)
238 svector_push_back (sequencesToDo
, *ap
);
240 die("no sequence specified or %s profile entry found", usequence
);
246 qsort(folders
, nFolders
, sizeof(struct Folder
), (qsort_comp
) CompareFolders
);
248 svector_free (sequencesToDo
);
254 * Read the Flist-Order profile entry to determine
255 * how to sort folders for output.
265 if (!(p
= context_find("Flist-Order")))
268 while (isspace((unsigned char) *p
))
271 while (*p
&& !isspace((unsigned char) *p
))
275 AllocFolders(&orders
, &nOrdersAlloced
, nOrders
+ 1);
276 o
= &orders
[nOrders
++];
277 o
->priority
= priority
++;
278 o
->name
= mh_xmalloc(p
- s
+ 1);
279 strncpy(o
->name
, s
, p
- s
);
287 * Scan all the necessary folders
296 * change directory to base of nmh directory
298 if (chdir (nmhdir
) == NOTOK
)
299 adios (nmhdir
, "unable to change directory to");
301 if (numfolders
> 0) {
303 strncpy (curfolder
, foldersToDo
[numfolders
- 1], sizeof(curfolder
));
304 context_replace (pfolder
, curfolder
);/* update current folder */
305 context_save (); /* save the context file */
308 * Scan each given folder. If -all is given,
309 * then also scan the 1st level subfolders under
312 for (i
= 0; i
< numfolders
; ++i
)
313 BuildFolderList(foldersToDo
[i
], all
? 1 : 0);
317 * Do the readonly folders
319 do_readonly_folders();
322 * Now scan the entire nmh directory for folders
324 BuildFolderList(".", 0);
327 * Else scan current folder
329 BuildFolderList(curfolder
, 0);
335 * Initial building of folder list for
336 * the top of our search tree.
340 BuildFolderList(char *dirName
, int searchdepth
)
344 /* Make sure we have a directory */
345 if ((stat(dirName
, &st
) == -1) || !S_ISDIR(st
.st_mode
))
349 * If base directory, don't add it to the
350 * folder list. We just recurse into it.
352 if (!strcmp (dirName
, ".")) {
353 BuildFolderListRecurse (".", &st
, 0);
358 * Add this folder to the list.
359 * If recursing and directory has subfolders,
360 * then build folder list for subfolders.
362 if (AddFolder(dirName
, showzero
) && (recurse
|| searchdepth
) && st
.st_nlink
> 2)
363 BuildFolderListRecurse(dirName
, &st
, searchdepth
- 1);
367 * Recursive building of folder list
371 BuildFolderListRecurse(char *dirName
, struct stat
*s
, int searchdepth
)
373 char *base
, name
[PATH_MAX
];
381 * Keep track of number of directories we've seen so we can
382 * stop stat'ing entries in this directory once we've seen
383 * them all. This optimization will fail if you have extra
384 * directories beginning with ".", since we don't bother to
385 * stat them. But that shouldn't generally be a problem.
387 nlinks
= s
->st_nlink
;
389 /* Disable the optimization under conditions where st_nlink
390 is set to 1. That happens on Cygwin, for example:
391 http://cygwin.com/ml/cygwin-apps/2008-08/msg00264.html */
395 if (!(dir
= opendir(dirName
)))
396 adios(dirName
, "can't open directory");
399 * A hack so that we don't see a
400 * leading "./" in folder names.
402 base
= strcmp (dirName
, ".") ? dirName
: dirName
+ 1;
404 while (nlinks
&& (dp
= readdir(dir
))) {
405 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, "..")) {
409 if (dp
->d_name
[0] == '.')
411 /* Check to see if the name of the file is a number
412 * if it is, we assume it's a mail file and skip it
414 for (n
= dp
->d_name
; isdigit((unsigned char)*n
); n
++);
417 strncpy (name
, base
, sizeof(name
) - 2);
420 strncat(name
, dp
->d_name
, sizeof(name
) - strlen(name
) - 1);
421 if ((stat(name
, &st
) != -1) && S_ISDIR(st
.st_mode
)) {
423 * Check if this was really a symbolic link pointing
424 * to a directory. If not, then decrement link count.
426 if (lstat (name
, &st
) == -1)
428 /* Add this folder to the list */
429 if (AddFolder(name
, showzero
) &&
430 (recurse
|| searchdepth
) && st
.st_nlink
> 2)
431 BuildFolderListRecurse(name
, &st
, searchdepth
- 1);
438 * Add this folder to our list, counting the total number of
439 * messages and the number of messages in each sequence.
443 AddFolder(char *name
, int force
)
448 ivector_t seqnum
= ivector_create (0), nSeq
= ivector_create (0);
453 /* Read folder and create message structure */
454 if (!(mp
= folder_read (name
, 0))) {
455 /* Oops, error occurred. Record it and continue. */
456 AllocFolders(&folders
, &nFoldersAlloced
, nFolders
+ 1);
457 f
= &folders
[nFolders
++];
458 f
->name
= mh_xstrdup(name
);
460 f
->priority
= AssignPriority(f
->name
);
464 for (i
= 0; i
< svector_size (sequencesToDo
); i
++) {
465 /* Convert sequences to their sequence numbers */
466 if ((cp
= svector_at (sequencesToDo
, i
)))
467 ivector_push_back (seqnum
, seq_getnum(mp
, cp
));
469 ivector_push_back (seqnum
, -1);
471 /* Now count messages in this sequence */
472 ivector_push_back (nSeq
, 0);
473 if (mp
->nummsg
> 0 && ivector_at (seqnum
, i
) != -1) {
474 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++) {
475 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
))
476 (*ivector_atp (nSeq
, i
))++;
481 /* Check if any of the sequence checks were nonzero */
483 for (i
= 0; i
< svector_size (sequencesToDo
); i
++) {
484 if (ivector_at (nSeq
, i
) > 0) {
490 if (nonzero
|| force
) {
491 /* save general folder information */
492 AllocFolders(&folders
, &nFoldersAlloced
, nFolders
+ 1);
493 f
= &folders
[nFolders
++];
494 f
->name
= mh_xstrdup(name
);
495 f
->nMsgs
= mp
->nummsg
;
496 f
->nSeq
= ivector_create (0);
497 f
->private = ivector_create (0);
499 f
->priority
= AssignPriority(f
->name
);
501 /* record the sequence information */
502 for (i
= 0; i
< svector_size (sequencesToDo
); i
++) {
503 *ivector_atp (f
->nSeq
, i
) = ivector_at (nSeq
, i
);
504 ivector_push_back (f
->private,
505 ivector_at (seqnum
, i
) != -1
506 ? is_seq_private(mp
, ivector_at (seqnum
, i
))
512 ivector_free (seqnum
);
513 folder_free (mp
); /* free folder/message structure */
518 * Print the folder/sequence information
524 char tmpname
[BUFSIZ
];
525 unsigned int i
, j
, len
;
526 bool has_private
= false;
527 unsigned int maxfolderlen
= 0, maxseqlen
= 0;
528 int maxnum
= 0, maxseq
= 0;
531 for (i
= 0; i
< nFolders
; i
++)
532 puts(folders
[i
].name
);
537 * Find the width we need for various fields
539 for (i
= 0; i
< nFolders
; ++i
) {
540 /* find the length of longest folder name */
541 len
= strlen(folders
[i
].name
);
542 if (len
> maxfolderlen
)
545 /* If folder had error, skip the rest */
546 if (folders
[i
].error
)
549 /* find the maximum total messages */
550 if (folders
[i
].nMsgs
> maxnum
)
551 maxnum
= folders
[i
].nMsgs
;
553 for (j
= 0; j
< svector_size (sequencesToDo
); j
++) {
554 /* find maximum width of sequence name */
555 len
= strlen (svector_at (sequencesToDo
, j
));
556 if ((ivector_at (folders
[i
].nSeq
, j
) > 0 || showzero
) &&
560 /* find the maximum number of messages in sequence */
561 if (ivector_at (folders
[i
].nSeq
, j
) > maxseq
)
562 maxseq
= ivector_at (folders
[i
].nSeq
, j
);
564 /* check if this sequence is private in any of the folders */
565 if (ivector_at (folders
[i
].private, j
))
570 /* Now print all the folder/sequence information */
571 for (i
= 0; i
< nFolders
; i
++) {
572 for (j
= 0; j
< svector_size (sequencesToDo
); j
++) {
573 if (ivector_at (folders
[i
].nSeq
, j
) > 0 || showzero
) {
574 /* Add `+' to end of name of current folder */
575 if (strcmp(curfolder
, folders
[i
].name
))
576 snprintf(tmpname
, sizeof(tmpname
), "%s", folders
[i
].name
);
578 snprintf(tmpname
, sizeof(tmpname
), "%s+", folders
[i
].name
);
580 if (folders
[i
].error
) {
581 printf("%-*s is unreadable\n", maxfolderlen
+1, tmpname
);
585 printf("%-*s has %*d in sequence %-*s%s; out of %*d\n",
586 maxfolderlen
+1, tmpname
,
587 num_digits(maxseq
), ivector_at (folders
[i
].nSeq
, j
),
588 maxseqlen
, svector_at (sequencesToDo
, j
),
589 !has_private
? "" : ivector_at (folders
[i
].private, j
)
590 ? " (private)" : " ",
591 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
;
606 return strcmp(f1
->name
, f2
->name
);
610 * Make sure we have at least n folders allocated.
614 AllocFolders(struct Folder
**f
, int *nfa
, int n
)
620 *f
= mh_xmalloc (*nfa
* (sizeof(struct Folder
)));
623 *f
= mh_xrealloc (*f
, *nfa
* (sizeof(struct Folder
)));
628 * Return the priority for a name. The highest comes from an exact match.
629 * After that, the longest match (then first) assigns the priority.
632 AssignPriority(char *name
)
640 for (i
= 0; i
< nOrders
; ++i
) {
642 if (!strcmp(name
, o
->name
))
644 ol
= strlen(o
->name
);
649 if (o
->name
[0] == '*' && !strcmp(o
->name
+ 1, name
+ (nl
- ol
+ 1))) {
652 } else if (o
->name
[ol
- 1] == '*' && strncmp(o
->name
, name
, ol
- 1) == 0) {
661 * Do the read only folders
665 do_readonly_folders (void)
671 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
672 atrlen
= strlen (atrcur
);
674 for (np
= m_defs
; np
; np
= np
->n_next
)
675 if (ssequal (atrcur
, np
->n_name
)
676 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
677 BuildFolderList (np
->n_name
+ atrlen
, 0);