]>
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
24 * We allocate space to record the names of folders
25 * (foldersToDo array), this number of elements at a time.
27 #define MAXFOLDERS 100
30 #define FLIST_SWITCHES \
31 X("sequence name", 0, SEQSW) \
33 X("noall", 0, NOALLSW) \
34 X("recurse", 0, RECURSE) \
35 X("norecurse", 0, NORECURSE) \
36 X("showzero", 0, SHOWZERO) \
37 X("noshowzero", 0, NOSHOWZERO) \
38 X("alpha", 0, ALPHASW) \
39 X("noalpha", 0, NOALPHASW) \
40 X("fast", 0, FASTSW) \
41 X("nofast", 0, NOFASTSW) \
42 X("total", -5, TOTALSW) \
43 X("nototal", -7, NOTOTALSW) \
44 X("version", 0, VERSIONSW) \
45 X("help", 0, HELPSW) \
47 #define X(sw, minchars, id) id,
48 DEFINE_SWITCH_ENUM(FLIST
);
51 #define X(sw, minchars, id) { sw, minchars, id },
52 DEFINE_SWITCH_ARRAY(FLIST
, switches
);
56 char *name
; /* name of folder */
58 int error
; /* error == 1 for unreadable folder */
59 int nMsgs
; /* number of messages in folder */
60 ivector_t nSeq
; /* number of messages in each sequence */
61 ivector_t
private; /* is given sequence, public or private */
64 static struct Folder
*orders
= NULL
;
65 static int nOrders
= 0;
66 static int nOrdersAlloced
= 0;
67 static struct Folder
*folders
= NULL
;
68 static unsigned int nFolders
= 0;
69 static int nFoldersAlloced
= 0;
71 /* info on folders to search */
72 static char **foldersToDo
;
73 static int numfolders
;
74 static int maxfolders
;
76 /* info on sequences to search for */
77 static svector_t sequencesToDo
;
79 static int all
= FALSE
; /* scan all folders in top level? */
80 static int alphaOrder
= FALSE
; /* want alphabetical order only */
81 static int recurse
= FALSE
; /* show nested folders? */
82 static int showzero
= TRUE
; /* show folders even if no messages in seq? */
83 static int Total
= TRUE
; /* display info on number of messages in *
84 * sequence found, and total num messages */
86 static char curfolder
[BUFSIZ
]; /* name of the current folder */
87 static char *nmhdir
; /* base nmh mail directory */
90 * Type for a compare function for qsort. This keeps
93 typedef int (*qsort_comp
) (const void *, const void *);
98 int CompareFolders(struct Folder
*, struct Folder
*);
99 void GetFolderOrder(void);
100 void ScanFolders(void);
101 int AddFolder(char *, int);
102 void BuildFolderList(char *, int);
103 void BuildFolderListRecurse(char *, struct stat
*, int);
104 void PrintFolders(void);
105 void AllocFolders(struct Folder
**, int *, int);
106 int AssignPriority(char *);
107 static void do_readonly_folders(void);
112 main(int argc
, char **argv
)
118 if (nmh_init(argv
[0], 1)) { return 1; }
121 * If program was invoked with name ending
122 * in `s', then add switch `-all'.
124 if (argv
[0][strlen (argv
[0]) - 1] == 's')
127 arguments
= getarguments (invo_name
, argc
, argv
, 1);
130 /* allocate the initial space to record the folder names */
132 maxfolders
= MAXFOLDERS
;
133 foldersToDo
= (char **) mh_xmalloc ((size_t) (maxfolders
* sizeof(*foldersToDo
)));
135 /* no sequences yet */
136 sequencesToDo
= svector_create (0);
138 /* parse arguments */
139 while ((cp
= *argp
++)) {
141 switch (smatch(++cp
, switches
)) {
143 ambigsw(cp
, switches
);
146 adios(NULL
, "-%s unknown", cp
);
149 snprintf(buf
, sizeof(buf
), "%s [+folder1 [+folder2 ...]][switches]",
151 print_help(buf
, switches
, 1);
154 print_version(invo_name
);
158 if (!(cp
= *argp
++) || *cp
== '-')
159 adios (NULL
, "missing argument to %s", argp
[-2]);
161 svector_push_back (sequencesToDo
, cp
);
204 * Check if we need to allocate more space
207 if (numfolders
>= maxfolders
) {
208 maxfolders
+= MAXFOLDERS
;
209 foldersToDo
= (char **) mh_xrealloc (foldersToDo
,
210 (size_t) (maxfolders
* sizeof(*foldersToDo
)));
212 if (*cp
== '+' || *cp
== '@') {
213 foldersToDo
[numfolders
++] =
216 foldersToDo
[numfolders
++] = cp
;
220 if (!context_find ("path"))
221 free (path ("./", TFOLDER
));
223 /* get current folder */
224 strncpy (curfolder
, getfolder(1), sizeof(curfolder
));
226 /* get nmh base directory */
227 nmhdir
= m_maildir ("");
230 * If we didn't specify any sequences, we search
231 * for the "Unseen-Sequence" profile entry and use
232 * all the sequences defined there.
234 if (svector_size (sequencesToDo
) == 0) {
235 if ((cp
= context_find(usequence
)) && *cp
) {
239 ap
= brkstring (dp
, " ", "\n");
240 for (; ap
&& *ap
; ap
++)
241 svector_push_back (sequencesToDo
, *ap
);
243 adios (NULL
, "no sequence specified or %s profile entry found", usequence
);
249 qsort(folders
, nFolders
, sizeof(struct Folder
), (qsort_comp
) CompareFolders
);
251 svector_free (sequencesToDo
);
257 * Read the Flist-Order profile entry to determine
258 * how to sort folders for output.
268 if (!(p
= context_find("Flist-Order")))
271 while (isspace((unsigned char) *p
))
274 while (*p
&& !isspace((unsigned char) *p
))
278 AllocFolders(&orders
, &nOrdersAlloced
, nOrders
+ 1);
279 o
= &orders
[nOrders
++];
280 o
->priority
= priority
++;
281 o
->name
= (char *) mh_xmalloc(p
- s
+ 1);
282 strncpy(o
->name
, s
, p
- s
);
290 * Scan all the necessary folders
299 * change directory to base of nmh directory
301 if (chdir (nmhdir
) == NOTOK
)
302 adios (nmhdir
, "unable to change directory to");
304 if (numfolders
> 0) {
306 strncpy (curfolder
, foldersToDo
[numfolders
- 1], sizeof(curfolder
));
307 context_replace (pfolder
, curfolder
);/* update current folder */
308 context_save (); /* save the context file */
311 * Scan each given folder. If -all is given,
312 * then also scan the 1st level subfolders under
315 for (i
= 0; i
< numfolders
; ++i
)
316 BuildFolderList(foldersToDo
[i
], all
? 1 : 0);
320 * Do the readonly folders
322 do_readonly_folders();
325 * Now scan the entire nmh directory for folders
327 BuildFolderList(".", 0);
330 * Else scan current folder
332 BuildFolderList(curfolder
, 0);
338 * Initial building of folder list for
339 * the top of our search tree.
343 BuildFolderList(char *dirName
, int searchdepth
)
347 /* Make sure we have a directory */
348 if ((stat(dirName
, &st
) == -1) || !S_ISDIR(st
.st_mode
))
352 * If base directory, don't add it to the
353 * folder list. We just recurse into it.
355 if (!strcmp (dirName
, ".")) {
356 BuildFolderListRecurse (".", &st
, 0);
361 * Add this folder to the list.
362 * If recursing and directory has subfolders,
363 * then build folder list for subfolders.
365 if (AddFolder(dirName
, showzero
) && (recurse
|| searchdepth
) && st
.st_nlink
> 2)
366 BuildFolderListRecurse(dirName
, &st
, searchdepth
- 1);
370 * Recursive building of folder list
374 BuildFolderListRecurse(char *dirName
, struct stat
*s
, int searchdepth
)
376 char *base
, name
[PATH_MAX
];
384 * Keep track of number of directories we've seen so we can
385 * stop stat'ing entries in this directory once we've seen
386 * them all. This optimization will fail if you have extra
387 * directories beginning with ".", since we don't bother to
388 * stat them. But that shouldn't generally be a problem.
390 nlinks
= s
->st_nlink
;
392 /* Disable the optimization under conditions where st_nlink
393 is set to 1. That happens on Cygwin, for example:
394 http://cygwin.com/ml/cygwin-apps/2008-08/msg00264.html */
398 if (!(dir
= opendir(dirName
)))
399 adios(dirName
, "can't open directory");
402 * A hack so that we don't see a
403 * leading "./" in folder names.
405 base
= strcmp (dirName
, ".") ? dirName
: dirName
+ 1;
407 while (nlinks
&& (dp
= readdir(dir
))) {
408 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, "..")) {
412 if (dp
->d_name
[0] == '.')
414 /* Check to see if the name of the file is a number
415 * if it is, we assume it's a mail file and skip it
417 for (n
= dp
->d_name
; *n
&& isdigit((unsigned char) *n
); n
++);
420 strncpy (name
, base
, sizeof(name
) - 2);
423 strncat(name
, dp
->d_name
, sizeof(name
) - strlen(name
) - 1);
424 if ((stat(name
, &st
) != -1) && S_ISDIR(st
.st_mode
)) {
426 * Check if this was really a symbolic link pointing
427 * to a directory. If not, then decrement link count.
429 if (lstat (name
, &st
) == -1)
431 /* Add this folder to the list */
432 if (AddFolder(name
, showzero
) &&
433 (recurse
|| searchdepth
) && st
.st_nlink
> 2)
434 BuildFolderListRecurse(name
, &st
, searchdepth
- 1);
441 * Add this folder to our list, counting the total number of
442 * messages and the number of messages in each sequence.
446 AddFolder(char *name
, int force
)
450 ivector_t seqnum
= ivector_create (0), nSeq
= ivector_create (0);
455 /* Read folder and create message structure */
456 if (!(mp
= folder_read (name
, 0))) {
457 /* Oops, error occurred. Record it and continue. */
458 AllocFolders(&folders
, &nFoldersAlloced
, nFolders
+ 1);
459 f
= &folders
[nFolders
++];
460 f
->name
= getcpy(name
);
462 f
->priority
= AssignPriority(f
->name
);
466 for (i
= 0; i
< svector_size (sequencesToDo
); i
++) {
467 /* Convert sequences to their sequence numbers */
468 if ((cp
= svector_at (sequencesToDo
, i
)))
469 ivector_push_back (seqnum
, seq_getnum(mp
, cp
));
471 ivector_push_back (seqnum
, -1);
473 /* Now count messages in this sequence */
474 ivector_push_back (nSeq
, 0);
475 if (mp
->nummsg
> 0 && ivector_at (seqnum
, i
) != -1) {
476 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++) {
477 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
))
478 (*ivector_atp (nSeq
, i
))++;
483 /* Check if any of the sequence checks were nonzero */
485 for (i
= 0; i
< svector_size (sequencesToDo
); i
++) {
486 if (ivector_at (nSeq
, i
) > 0) {
492 if (nonzero
|| force
) {
493 /* save general folder information */
494 AllocFolders(&folders
, &nFoldersAlloced
, nFolders
+ 1);
495 f
= &folders
[nFolders
++];
496 f
->name
= getcpy(name
);
497 f
->nMsgs
= mp
->nummsg
;
498 f
->nSeq
= ivector_create (0);
499 f
->private = ivector_create (0);
501 f
->priority
= AssignPriority(f
->name
);
503 /* record the sequence information */
504 for (i
= 0; i
< svector_size (sequencesToDo
); i
++) {
505 *ivector_atp (f
->nSeq
, i
) = ivector_at (nSeq
, i
);
506 ivector_push_back (f
->private,
507 ivector_at (seqnum
, i
) != -1
508 ? is_seq_private(mp
, ivector_at (seqnum
, i
))
514 ivector_free (seqnum
);
515 folder_free (mp
); /* free folder/message structure */
520 * Print the folder/sequence information
526 char tmpname
[BUFSIZ
];
527 unsigned int i
, j
, len
, has_private
= 0;
528 unsigned int maxfolderlen
= 0, maxseqlen
= 0;
529 int maxnum
= 0, maxseq
= 0;
532 for (i
= 0; i
< nFolders
; i
++)
533 printf("%s\n", folders
[i
].name
);
538 * Find the width we need for various fields
540 for (i
= 0; i
< nFolders
; ++i
) {
541 /* find the length of longest folder name */
542 len
= strlen(folders
[i
].name
);
543 if (len
> maxfolderlen
)
546 /* If folder had error, skip the rest */
547 if (folders
[i
].error
)
550 /* find the maximum total messages */
551 if (folders
[i
].nMsgs
> maxnum
)
552 maxnum
= folders
[i
].nMsgs
;
554 for (j
= 0; j
< svector_size (sequencesToDo
); j
++) {
555 /* find maximum width of sequence name */
556 len
= strlen (svector_at (sequencesToDo
, j
));
557 if ((ivector_at (folders
[i
].nSeq
, j
) > 0 || showzero
) &&
561 /* find the maximum number of messages in sequence */
562 if (ivector_at (folders
[i
].nSeq
, j
) > maxseq
)
563 maxseq
= ivector_at (folders
[i
].nSeq
, j
);
565 /* check if this sequence is private in any of the folders */
566 if (ivector_at (folders
[i
].private, j
))
571 /* Now print all the folder/sequence information */
572 for (i
= 0; i
< nFolders
; i
++) {
573 for (j
= 0; j
< svector_size (sequencesToDo
); j
++) {
574 if (ivector_at (folders
[i
].nSeq
, j
) > 0 || showzero
) {
575 /* Add `+' to end of name of current folder */
576 if (strcmp(curfolder
, folders
[i
].name
))
577 snprintf(tmpname
, sizeof(tmpname
), "%s", folders
[i
].name
);
579 snprintf(tmpname
, sizeof(tmpname
), "%s+", folders
[i
].name
);
581 if (folders
[i
].error
) {
582 printf("%-*s is unreadable\n", maxfolderlen
+1, tmpname
);
586 printf("%-*s has %*d in sequence %-*s%s; out of %*d\n",
587 maxfolderlen
+1, tmpname
,
588 num_digits(maxseq
), ivector_at (folders
[i
].nSeq
, j
),
589 maxseqlen
, svector_at (sequencesToDo
, j
),
590 !has_private
? "" : ivector_at (folders
[i
].private, j
)
591 ? " (private)" : " ",
592 num_digits(maxnum
), folders
[i
].nMsgs
);
599 * Put them in priority order.
603 CompareFolders(struct Folder
*f1
, struct Folder
*f2
)
605 if (!alphaOrder
&& f1
->priority
!= f2
->priority
)
606 return f1
->priority
- f2
->priority
;
608 return strcmp(f1
->name
, f2
->name
);
612 * Make sure we have at least n folders allocated.
616 AllocFolders(struct Folder
**f
, int *nfa
, int n
)
622 *f
= (struct Folder
*) mh_xmalloc (*nfa
* (sizeof(struct Folder
)));
625 *f
= (struct Folder
*) mh_xrealloc (*f
, *nfa
* (sizeof(struct Folder
)));
630 * Return the priority for a name. The highest comes from an exact match.
631 * After that, the longest match (then first) assigns the priority.
634 AssignPriority(char *name
)
642 for (i
= 0; i
< nOrders
; ++i
) {
644 if (!strcmp(name
, o
->name
))
646 ol
= strlen(o
->name
);
651 if (o
->name
[0] == '*' && !strcmp(o
->name
+ 1, name
+ (nl
- ol
+ 1))) {
654 } else if (o
->name
[ol
- 1] == '*' && strncmp(o
->name
, name
, ol
- 1) == 0) {
663 * Do the read only folders
667 do_readonly_folders (void)
671 register struct node
*np
;
673 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
674 atrlen
= strlen (atrcur
);
676 for (np
= m_defs
; np
; np
= np
->n_next
)
677 if (ssequal (atrcur
, np
->n_name
)
678 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
679 BuildFolderList (np
->n_name
+ atrlen
, 0);