]>
diplodocus.org Git - nmh/blob - uip/flist.c
f2486f51e61cbac43b72ea186351192f80770bc8
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
17 #include "sbr/print_version.h"
18 #include "sbr/print_help.h"
19 #include "sbr/seq_getnum.h"
20 #include "sbr/error.h"
23 #include "sbr/m_maildir.h"
26 * We allocate space to record the names of folders
27 * (foldersToDo array), this number of elements at a time.
29 #define MAXFOLDERS 100
32 #define FLIST_SWITCHES \
33 X("sequence name", 0, SEQSW) \
35 X("noall", 0, NOALLSW) \
36 X("recurse", 0, RECURSE) \
37 X("norecurse", 0, NORECURSE) \
38 X("showzero", 0, SHOWZERO) \
39 X("noshowzero", 0, NOSHOWZERO) \
40 X("alpha", 0, ALPHASW) \
41 X("noalpha", 0, NOALPHASW) \
42 X("fast", 0, FASTSW) \
43 X("nofast", 0, NOFASTSW) \
44 X("total", -5, TOTALSW) \
45 X("nototal", -7, NOTOTALSW) \
46 X("version", 0, VERSIONSW) \
47 X("help", 0, HELPSW) \
49 #define X(sw, minchars, id) id,
50 DEFINE_SWITCH_ENUM(FLIST
);
53 #define X(sw, minchars, id) { sw, minchars, id },
54 DEFINE_SWITCH_ARRAY(FLIST
, switches
);
58 char *name
; /* name of folder */
60 int error
; /* error == 1 for unreadable folder */
61 int nMsgs
; /* number of messages in folder */
62 ivector_t nSeq
; /* number of messages in each sequence */
63 ivector_t
private; /* is given sequence, public or private */
66 static struct Folder
*orders
= NULL
;
67 static int nOrders
= 0;
68 static int nOrdersAlloced
= 0;
69 static struct Folder
*folders
= NULL
;
70 static unsigned int nFolders
= 0;
71 static int nFoldersAlloced
= 0;
73 /* info on folders to search */
74 static char **foldersToDo
;
75 static int numfolders
;
76 static int maxfolders
;
78 /* info on sequences to search for */
79 static svector_t sequencesToDo
;
81 static bool all
; /* scan all folders in top level? */
82 static bool alphaOrder
; /* want alphabetical order only */
83 static bool recurse
; /* show nested folders? */
84 static bool showzero
= true; /* show folders even if no messages in seq? */
85 static bool Total
= true; /* display info on number of messages in
86 * sequence found, and total num messages */
88 static char curfolder
[BUFSIZ
]; /* name of the current folder */
89 static char *nmhdir
; /* base nmh mail directory */
92 * Type for a compare function for qsort. This keeps
95 typedef int (*qsort_comp
) (const void *, const void *);
100 static int CompareFolders(struct Folder
*, struct Folder
*);
101 static void GetFolderOrder(void);
102 static void ScanFolders(void);
103 static int AddFolder(char *, int);
104 static void BuildFolderList(char *, int);
105 static void BuildFolderListRecurse(char *, struct stat
*, int);
106 static void PrintFolders(void);
107 static void AllocFolders(struct Folder
**, int *, int);
108 static int AssignPriority(char *);
109 static void do_readonly_folders(void);
114 main(int argc
, char **argv
)
120 if (nmh_init(argv
[0], true, true)) { return 1; }
123 * If program was invoked with name ending
124 * in `s', then add switch `-all'.
126 all
= has_suffix_c(argv
[0], 's');
128 arguments
= getarguments (invo_name
, argc
, argv
, 1);
131 /* allocate the initial space to record the folder names */
133 maxfolders
= MAXFOLDERS
;
134 foldersToDo
= mh_xmalloc ((size_t) (maxfolders
* sizeof(*foldersToDo
)));
136 /* no sequences yet */
137 sequencesToDo
= svector_create (0);
139 /* parse arguments */
140 while ((cp
= *argp
++)) {
142 switch (smatch(++cp
, switches
)) {
144 ambigsw(cp
, switches
);
147 die("-%s unknown", cp
);
150 snprintf(buf
, sizeof(buf
), "%s [+folder1 [+folder2 ...]][switches]",
152 print_help(buf
, switches
, 1);
155 print_version(invo_name
);
159 if (!(cp
= *argp
++) || *cp
== '-')
160 die("missing argument to %s", argp
[-2]);
162 svector_push_back (sequencesToDo
, cp
);
205 * Check if we need to allocate more space
208 if (numfolders
>= maxfolders
) {
209 maxfolders
+= MAXFOLDERS
;
210 foldersToDo
= mh_xrealloc (foldersToDo
,
211 (size_t) (maxfolders
* sizeof(*foldersToDo
)));
213 if (*cp
== '+' || *cp
== '@') {
214 foldersToDo
[numfolders
++] =
217 foldersToDo
[numfolders
++] = cp
;
221 if (!context_find ("path"))
222 free (path ("./", TFOLDER
));
224 /* get current folder */
225 strncpy (curfolder
, getfolder(1), sizeof(curfolder
));
227 /* get nmh base directory */
228 nmhdir
= m_maildir ("");
231 * If we didn't specify any sequences, we search
232 * for the "Unseen-Sequence" profile entry and use
233 * all the sequences defined there.
235 if (svector_size (sequencesToDo
) == 0) {
236 if ((cp
= context_find(usequence
)) && *cp
) {
240 ap
= brkstring (dp
, " ", "\n");
241 for (; ap
&& *ap
; ap
++)
242 svector_push_back (sequencesToDo
, *ap
);
244 die("no sequence specified or %s profile entry found", usequence
);
250 qsort(folders
, nFolders
, sizeof(struct Folder
), (qsort_comp
) CompareFolders
);
252 svector_free (sequencesToDo
);
258 * Read the Flist-Order profile entry to determine
259 * how to sort folders for output.
269 if (!(p
= context_find("Flist-Order")))
272 while (isspace((unsigned char) *p
))
275 while (*p
&& !isspace((unsigned char) *p
))
279 AllocFolders(&orders
, &nOrdersAlloced
, nOrders
+ 1);
280 o
= &orders
[nOrders
++];
281 o
->priority
= priority
++;
282 o
->name
= mh_xmalloc(p
- s
+ 1);
283 strncpy(o
->name
, s
, p
- s
);
291 * Scan all the necessary folders
300 * change directory to base of nmh directory
302 if (chdir (nmhdir
) == NOTOK
)
303 adios (nmhdir
, "unable to change directory to");
305 if (numfolders
> 0) {
307 strncpy (curfolder
, foldersToDo
[numfolders
- 1], sizeof(curfolder
));
308 context_replace (pfolder
, curfolder
);/* update current folder */
309 context_save (); /* save the context file */
312 * Scan each given folder. If -all is given,
313 * then also scan the 1st level subfolders under
316 for (i
= 0; i
< numfolders
; ++i
)
317 BuildFolderList(foldersToDo
[i
], all
? 1 : 0);
321 * Do the readonly folders
323 do_readonly_folders();
326 * Now scan the entire nmh directory for folders
328 BuildFolderList(".", 0);
331 * Else scan current folder
333 BuildFolderList(curfolder
, 0);
339 * Initial building of folder list for
340 * the top of our search tree.
344 BuildFolderList(char *dirName
, int searchdepth
)
348 /* Make sure we have a directory */
349 if ((stat(dirName
, &st
) == -1) || !S_ISDIR(st
.st_mode
))
353 * If base directory, don't add it to the
354 * folder list. We just recurse into it.
356 if (!strcmp (dirName
, ".")) {
357 BuildFolderListRecurse (".", &st
, 0);
362 * Add this folder to the list.
363 * If recursing and directory has subfolders,
364 * then build folder list for subfolders.
366 if (AddFolder(dirName
, showzero
) && (recurse
|| searchdepth
) && st
.st_nlink
> 2)
367 BuildFolderListRecurse(dirName
, &st
, searchdepth
- 1);
371 * Recursive building of folder list
375 BuildFolderListRecurse(char *dirName
, struct stat
*s
, int searchdepth
)
377 char *base
, name
[PATH_MAX
];
385 * Keep track of number of directories we've seen so we can
386 * stop stat'ing entries in this directory once we've seen
387 * them all. This optimization will fail if you have extra
388 * directories beginning with ".", since we don't bother to
389 * stat them. But that shouldn't generally be a problem.
391 nlinks
= s
->st_nlink
;
393 /* Disable the optimization under conditions where st_nlink
394 is set to 1. That happens on Cygwin, for example:
395 http://cygwin.com/ml/cygwin-apps/2008-08/msg00264.html */
399 if (!(dir
= opendir(dirName
)))
400 adios(dirName
, "can't open directory");
403 * A hack so that we don't see a
404 * leading "./" in folder names.
406 base
= strcmp (dirName
, ".") ? dirName
: dirName
+ 1;
408 while (nlinks
&& (dp
= readdir(dir
))) {
409 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, "..")) {
413 if (dp
->d_name
[0] == '.')
415 /* Check to see if the name of the file is a number
416 * if it is, we assume it's a mail file and skip it
418 for (n
= dp
->d_name
; isdigit((unsigned char)*n
); n
++);
421 strncpy (name
, base
, sizeof(name
) - 2);
424 strncat(name
, dp
->d_name
, sizeof(name
) - strlen(name
) - 1);
425 if ((stat(name
, &st
) != -1) && S_ISDIR(st
.st_mode
)) {
427 * Check if this was really a symbolic link pointing
428 * to a directory. If not, then decrement link count.
430 if (lstat (name
, &st
) == -1)
432 /* Add this folder to the list */
433 if (AddFolder(name
, showzero
) &&
434 (recurse
|| searchdepth
) && st
.st_nlink
> 2)
435 BuildFolderListRecurse(name
, &st
, searchdepth
- 1);
442 * Add this folder to our list, counting the total number of
443 * messages and the number of messages in each sequence.
447 AddFolder(char *name
, int force
)
452 ivector_t seqnum
= ivector_create (0), nSeq
= ivector_create (0);
457 /* Read folder and create message structure */
458 if (!(mp
= folder_read (name
, 0))) {
459 /* Oops, error occurred. Record it and continue. */
460 AllocFolders(&folders
, &nFoldersAlloced
, nFolders
+ 1);
461 f
= &folders
[nFolders
++];
462 f
->name
= mh_xstrdup(name
);
464 f
->priority
= AssignPriority(f
->name
);
468 for (i
= 0; i
< svector_size (sequencesToDo
); i
++) {
469 /* Convert sequences to their sequence numbers */
470 if ((cp
= svector_at (sequencesToDo
, i
)))
471 ivector_push_back (seqnum
, seq_getnum(mp
, cp
));
473 ivector_push_back (seqnum
, -1);
475 /* Now count messages in this sequence */
476 ivector_push_back (nSeq
, 0);
477 if (mp
->nummsg
> 0 && ivector_at (seqnum
, i
) != -1) {
478 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++) {
479 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
))
480 (*ivector_atp (nSeq
, i
))++;
485 /* Check if any of the sequence checks were nonzero */
487 for (i
= 0; i
< svector_size (sequencesToDo
); i
++) {
488 if (ivector_at (nSeq
, i
) > 0) {
494 if (nonzero
|| force
) {
495 /* save general folder information */
496 AllocFolders(&folders
, &nFoldersAlloced
, nFolders
+ 1);
497 f
= &folders
[nFolders
++];
498 f
->name
= mh_xstrdup(name
);
499 f
->nMsgs
= mp
->nummsg
;
500 f
->nSeq
= ivector_create (0);
501 f
->private = ivector_create (0);
503 f
->priority
= AssignPriority(f
->name
);
505 /* record the sequence information */
506 for (i
= 0; i
< svector_size (sequencesToDo
); i
++) {
507 *ivector_atp (f
->nSeq
, i
) = ivector_at (nSeq
, i
);
508 ivector_push_back (f
->private,
509 ivector_at (seqnum
, i
) != -1
510 ? is_seq_private(mp
, ivector_at (seqnum
, i
))
516 ivector_free (seqnum
);
517 folder_free (mp
); /* free folder/message structure */
522 * Print the folder/sequence information
528 char tmpname
[BUFSIZ
];
529 unsigned int i
, j
, len
;
530 bool has_private
= false;
531 unsigned int maxfolderlen
= 0, maxseqlen
= 0;
532 int maxnum
= 0, maxseq
= 0;
535 for (i
= 0; i
< nFolders
; i
++)
536 puts(folders
[i
].name
);
541 * Find the width we need for various fields
543 for (i
= 0; i
< nFolders
; ++i
) {
544 /* find the length of longest folder name */
545 len
= strlen(folders
[i
].name
);
546 if (len
> maxfolderlen
)
549 /* If folder had error, skip the rest */
550 if (folders
[i
].error
)
553 /* find the maximum total messages */
554 if (folders
[i
].nMsgs
> maxnum
)
555 maxnum
= folders
[i
].nMsgs
;
557 for (j
= 0; j
< svector_size (sequencesToDo
); j
++) {
558 /* find maximum width of sequence name */
559 len
= strlen (svector_at (sequencesToDo
, j
));
560 if ((ivector_at (folders
[i
].nSeq
, j
) > 0 || showzero
) &&
564 /* find the maximum number of messages in sequence */
565 if (ivector_at (folders
[i
].nSeq
, j
) > maxseq
)
566 maxseq
= ivector_at (folders
[i
].nSeq
, j
);
568 /* check if this sequence is private in any of the folders */
569 if (ivector_at (folders
[i
].private, j
))
574 /* Now print all the folder/sequence information */
575 for (i
= 0; i
< nFolders
; i
++) {
576 for (j
= 0; j
< svector_size (sequencesToDo
); j
++) {
577 if (ivector_at (folders
[i
].nSeq
, j
) > 0 || showzero
) {
578 /* Add `+' to end of name of current folder */
579 if (strcmp(curfolder
, folders
[i
].name
))
580 snprintf(tmpname
, sizeof(tmpname
), "%s", folders
[i
].name
);
582 snprintf(tmpname
, sizeof(tmpname
), "%s+", folders
[i
].name
);
584 if (folders
[i
].error
) {
585 printf("%-*s is unreadable\n", maxfolderlen
+1, tmpname
);
589 printf("%-*s has %*d in sequence %-*s%s; out of %*d\n",
590 maxfolderlen
+1, tmpname
,
591 num_digits(maxseq
), ivector_at (folders
[i
].nSeq
, j
),
592 maxseqlen
, svector_at (sequencesToDo
, j
),
593 !has_private
? "" : ivector_at (folders
[i
].private, j
)
594 ? " (private)" : " ",
595 num_digits(maxnum
), folders
[i
].nMsgs
);
602 * Put them in priority order.
606 CompareFolders(struct Folder
*f1
, struct Folder
*f2
)
608 if (!alphaOrder
&& f1
->priority
!= f2
->priority
)
609 return f1
->priority
- f2
->priority
;
610 return strcmp(f1
->name
, f2
->name
);
614 * Make sure we have at least n folders allocated.
618 AllocFolders(struct Folder
**f
, int *nfa
, int n
)
624 *f
= mh_xmalloc (*nfa
* (sizeof(struct Folder
)));
627 *f
= mh_xrealloc (*f
, *nfa
* (sizeof(struct Folder
)));
632 * Return the priority for a name. The highest comes from an exact match.
633 * After that, the longest match (then first) assigns the priority.
636 AssignPriority(char *name
)
644 for (i
= 0; i
< nOrders
; ++i
) {
646 if (!strcmp(name
, o
->name
))
648 ol
= strlen(o
->name
);
653 if (o
->name
[0] == '*' && !strcmp(o
->name
+ 1, name
+ (nl
- ol
+ 1))) {
656 } else if (o
->name
[ol
- 1] == '*' && strncmp(o
->name
, name
, ol
- 1) == 0) {
665 * Do the read only folders
669 do_readonly_folders (void)
675 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
676 atrlen
= strlen (atrcur
);
678 for (np
= m_defs
; np
; np
= np
->n_next
)
679 if (ssequal (atrcur
, np
->n_name
)
680 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
681 BuildFolderList (np
->n_name
+ atrlen
, 0);