]>
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 int nSeq
[NUMATTRS
]; /* number of messages in each sequence */
61 int private[NUMATTRS
]; /* 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 char *sequencesToDo
[NUMATTRS
];
78 static unsigned int numsequences
;
80 static int all
= FALSE
; /* scan all folders in top level? */
81 static int alphaOrder
= FALSE
; /* want alphabetical order only */
82 static int recurse
= FALSE
; /* show nested folders? */
83 static int showzero
= TRUE
; /* show folders even if no messages in seq? */
84 static int Total
= TRUE
; /* display info on number of messages in *
85 * sequence found, and total num messages */
87 static char curfolder
[BUFSIZ
]; /* name of the current folder */
88 static char *nmhdir
; /* base nmh mail directory */
91 * Type for a compare function for qsort. This keeps
94 typedef int (*qsort_comp
) (const void *, const void *);
99 int CompareFolders(struct Folder
*, struct Folder
*);
100 void GetFolderOrder(void);
101 void ScanFolders(void);
102 int AddFolder(char *, int);
103 void BuildFolderList(char *, int);
104 void BuildFolderListRecurse(char *, struct stat
*, int);
105 void PrintFolders(void);
106 void AllocFolders(struct Folder
**, int *, int);
107 int AssignPriority(char *);
108 static void do_readonly_folders(void);
113 main(int argc
, char **argv
)
120 setlocale(LC_ALL
, "");
122 invo_name
= r1bindex(argv
[0], '/');
124 /* read user profile/context */
128 * If program was invoked with name ending
129 * in `s', then add switch `-all'.
131 if (argv
[0][strlen (argv
[0]) - 1] == 's')
134 arguments
= getarguments (invo_name
, argc
, argv
, 1);
137 /* allocate the initial space to record the folder names */
139 maxfolders
= MAXFOLDERS
;
140 foldersToDo
= (char **) mh_xmalloc ((size_t) (maxfolders
* sizeof(*foldersToDo
)));
142 /* no sequences yet */
145 /* parse arguments */
146 while ((cp
= *argp
++)) {
148 switch (smatch(++cp
, switches
)) {
150 ambigsw(cp
, switches
);
153 adios(NULL
, "-%s unknown", cp
);
156 snprintf(buf
, sizeof(buf
), "%s [+folder1 [+folder2 ...]][switches]",
158 print_help(buf
, switches
, 1);
161 print_version(invo_name
);
165 if (!(cp
= *argp
++) || *cp
== '-')
166 adios (NULL
, "missing argument to %s", argp
[-2]);
168 /* check if too many sequences specified */
169 if (numsequences
>= NUMATTRS
)
170 adios (NULL
, "too many sequences (more than %d) specified", NUMATTRS
);
171 sequencesToDo
[numsequences
++] = cp
;
214 * Check if we need to allocate more space
217 if (numfolders
>= maxfolders
) {
218 maxfolders
+= MAXFOLDERS
;
219 foldersToDo
= (char **) mh_xrealloc (foldersToDo
,
220 (size_t) (maxfolders
* sizeof(*foldersToDo
)));
222 if (*cp
== '+' || *cp
== '@') {
223 foldersToDo
[numfolders
++] =
226 foldersToDo
[numfolders
++] = cp
;
230 if (!context_find ("path"))
231 free (path ("./", TFOLDER
));
233 /* get current folder */
234 strncpy (curfolder
, getfolder(1), sizeof(curfolder
));
236 /* get nmh base directory */
237 nmhdir
= m_maildir ("");
240 * If we didn't specify any sequences, we search
241 * for the "Unseen-Sequence" profile entry and use
242 * all the sequences defined there. We check to
243 * make sure that the Unseen-Sequence entry doesn't
244 * contain more than NUMATTRS sequences.
246 if (numsequences
== 0) {
247 if ((cp
= context_find(usequence
)) && *cp
) {
251 ap
= brkstring (dp
, " ", "\n");
252 for (; ap
&& *ap
; ap
++) {
253 if (numsequences
>= NUMATTRS
)
254 adios (NULL
, "too many sequences (more than %d) in %s profile entry",
255 NUMATTRS
, usequence
);
257 sequencesToDo
[numsequences
++] = *ap
;
260 adios (NULL
, "no sequence specified or %s profile entry found", usequence
);
266 qsort(folders
, nFolders
, sizeof(struct Folder
), (qsort_comp
) CompareFolders
);
273 * Read the Flist-Order profile entry to determine
274 * how to sort folders for output.
284 if (!(p
= context_find("Flist-Order")))
287 while (isspace((unsigned char) *p
))
290 while (*p
&& !isspace((unsigned char) *p
))
294 AllocFolders(&orders
, &nOrdersAlloced
, nOrders
+ 1);
295 o
= &orders
[nOrders
++];
296 o
->priority
= priority
++;
297 o
->name
= (char *) mh_xmalloc(p
- s
+ 1);
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 (nmhdir
, "unable to change directory to");
320 if (numfolders
> 0) {
322 strncpy (curfolder
, foldersToDo
[numfolders
- 1], sizeof(curfolder
));
323 context_replace (pfolder
, curfolder
);/* 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
331 for (i
= 0; i
< numfolders
; ++i
)
332 BuildFolderList(foldersToDo
[i
], all
? 1 : 0);
336 * Do the readonly folders
338 do_readonly_folders();
341 * Now scan the entire nmh directory for folders
343 BuildFolderList(".", 0);
346 * Else scan current folder
348 BuildFolderList(curfolder
, 0);
354 * Initial building of folder list for
355 * the top of our search tree.
359 BuildFolderList(char *dirName
, int searchdepth
)
363 /* Make sure we have a directory */
364 if ((stat(dirName
, &st
) == -1) || !S_ISDIR(st
.st_mode
))
368 * If base directory, don't add it to the
369 * folder list. We just recurse into it.
371 if (!strcmp (dirName
, ".")) {
372 BuildFolderListRecurse (".", &st
, 0);
377 * Add this folder to the list.
378 * If recursing and directory has subfolders,
379 * then build folder list for subfolders.
381 if (AddFolder(dirName
, showzero
) && (recurse
|| searchdepth
) && st
.st_nlink
> 2)
382 BuildFolderListRecurse(dirName
, &st
, searchdepth
- 1);
386 * Recursive building of folder list
390 BuildFolderListRecurse(char *dirName
, struct stat
*s
, int searchdepth
)
392 char *base
, name
[PATH_MAX
];
400 * Keep track of number of directories we've seen so we can
401 * stop stat'ing entries in this directory once we've seen
402 * them all. This optimization will fail if you have extra
403 * directories beginning with ".", since we don't bother to
404 * stat them. But that shouldn't generally be a problem.
406 nlinks
= s
->st_nlink
;
408 /* Disable the optimization under conditions where st_nlink
409 is set to 1. That happens on Cygwin, for example:
410 http://cygwin.com/ml/cygwin-apps/2008-08/msg00264.html */
414 if (!(dir
= opendir(dirName
)))
415 adios(dirName
, "can't open directory");
418 * A hack so that we don't see a
419 * leading "./" in folder names.
421 base
= strcmp (dirName
, ".") ? dirName
: dirName
+ 1;
423 while (nlinks
&& (dp
= readdir(dir
))) {
424 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, "..")) {
428 if (dp
->d_name
[0] == '.')
430 /* Check to see if the name of the file is a number
431 * if it is, we assume it's a mail file and skip it
433 for (n
= dp
->d_name
; *n
&& isdigit((unsigned char) *n
); n
++);
436 strncpy (name
, base
, sizeof(name
) - 2);
439 strncat(name
, dp
->d_name
, sizeof(name
) - strlen(name
) - 1);
440 if ((stat(name
, &st
) != -1) && S_ISDIR(st
.st_mode
)) {
442 * Check if this was really a symbolic link pointing
443 * to a directory. If not, then decrement link count.
445 if (lstat (name
, &st
) == -1)
447 /* Add this folder to the list */
448 if (AddFolder(name
, showzero
) &&
449 (recurse
|| searchdepth
) && st
.st_nlink
> 2)
450 BuildFolderListRecurse(name
, &st
, searchdepth
- 1);
457 * Add this folder to our list, counting the total number of
458 * messages and the number of messages in each sequence.
462 AddFolder(char *name
, int force
)
466 int seqnum
[NUMATTRS
], nSeq
[NUMATTRS
];
470 /* Read folder and create message structure */
471 if (!(mp
= folder_read (name
))) {
472 /* Oops, error occurred. Record it and continue. */
473 AllocFolders(&folders
, &nFoldersAlloced
, nFolders
+ 1);
474 f
= &folders
[nFolders
++];
475 f
->name
= getcpy(name
);
477 f
->priority
= AssignPriority(f
->name
);
481 for (i
= 0; i
< numsequences
; i
++) {
482 /* Convert sequences to their sequence numbers */
483 if (sequencesToDo
[i
])
484 seqnum
[i
] = seq_getnum(mp
, sequencesToDo
[i
]);
488 /* Now count messages in this sequence */
490 if (mp
->nummsg
> 0 && seqnum
[i
] != -1) {
491 for (msgnum
= mp
->lowmsg
; msgnum
<= mp
->hghmsg
; msgnum
++) {
492 if (in_sequence(mp
, seqnum
[i
], msgnum
))
498 /* Check if any of the sequence checks were nonzero */
500 for (i
= 0; i
< numsequences
; i
++) {
507 if (nonzero
|| force
) {
508 /* save general folder information */
509 AllocFolders(&folders
, &nFoldersAlloced
, nFolders
+ 1);
510 f
= &folders
[nFolders
++];
511 f
->name
= getcpy(name
);
512 f
->nMsgs
= mp
->nummsg
;
514 f
->priority
= AssignPriority(f
->name
);
516 /* record the sequence information */
517 for (i
= 0; i
< numsequences
; i
++) {
518 f
->nSeq
[i
] = nSeq
[i
];
519 f
->private[i
] = (seqnum
[i
] != -1) ? is_seq_private(mp
, seqnum
[i
]) : 0;
523 folder_free (mp
); /* free folder/message structure */
528 * Print the folder/sequence information
534 char tmpname
[BUFSIZ
];
535 unsigned int i
, j
, len
, has_private
= 0;
536 unsigned int maxfolderlen
= 0, maxseqlen
= 0;
537 int maxnum
= 0, maxseq
= 0;
540 for (i
= 0; i
< nFolders
; i
++)
541 printf("%s\n", folders
[i
].name
);
546 * Find the width we need for various fields
548 for (i
= 0; i
< nFolders
; ++i
) {
549 /* find the length of longest folder name */
550 len
= strlen(folders
[i
].name
);
551 if (len
> maxfolderlen
)
554 /* If folder had error, skip the rest */
555 if (folders
[i
].error
)
558 /* find the maximum total messages */
559 if (folders
[i
].nMsgs
> maxnum
)
560 maxnum
= folders
[i
].nMsgs
;
562 for (j
= 0; j
< numsequences
; j
++) {
563 /* find maximum width of sequence name */
564 len
= strlen (sequencesToDo
[j
]);
565 if ((folders
[i
].nSeq
[j
] > 0 || showzero
) && (len
> maxseqlen
))
568 /* find the maximum number of messages in sequence */
569 if (folders
[i
].nSeq
[j
] > maxseq
)
570 maxseq
= folders
[i
].nSeq
[j
];
572 /* check if this sequence is private in any of the folders */
573 if (folders
[i
].private[j
])
578 /* Now print all the folder/sequence information */
579 for (i
= 0; i
< nFolders
; i
++) {
580 for (j
= 0; j
< numsequences
; j
++) {
581 if (folders
[i
].nSeq
[j
] > 0 || showzero
) {
582 /* Add `+' to end of name of current folder */
583 if (strcmp(curfolder
, folders
[i
].name
))
584 snprintf(tmpname
, sizeof(tmpname
), "%s", folders
[i
].name
);
586 snprintf(tmpname
, sizeof(tmpname
), "%s+", folders
[i
].name
);
588 if (folders
[i
].error
) {
589 printf("%-*s is unreadable\n", maxfolderlen
+1, tmpname
);
593 printf("%-*s has %*d in sequence %-*s%s; out of %*d\n",
594 maxfolderlen
+1, tmpname
,
595 num_digits(maxseq
), folders
[i
].nSeq
[j
],
596 maxseqlen
, sequencesToDo
[j
],
597 !has_private
? "" : folders
[i
].private[j
] ? " (private)" : " ",
598 num_digits(maxnum
), folders
[i
].nMsgs
);
605 * Put them in priority order.
609 CompareFolders(struct Folder
*f1
, struct Folder
*f2
)
611 if (!alphaOrder
&& f1
->priority
!= f2
->priority
)
612 return f1
->priority
- f2
->priority
;
614 return strcmp(f1
->name
, f2
->name
);
618 * Make sure we have at least n folders allocated.
622 AllocFolders(struct Folder
**f
, int *nfa
, int n
)
628 *f
= (struct Folder
*) mh_xmalloc (*nfa
* (sizeof(struct Folder
)));
631 *f
= (struct Folder
*) mh_xrealloc (*f
, *nfa
* (sizeof(struct Folder
)));
636 * Return the priority for a name. The highest comes from an exact match.
637 * After that, the longest match (then first) assigns the priority.
640 AssignPriority(char *name
)
648 for (i
= 0; i
< nOrders
; ++i
) {
650 if (!strcmp(name
, o
->name
))
652 ol
= strlen(o
->name
);
657 if (o
->name
[0] == '*' && !strcmp(o
->name
+ 1, name
+ (nl
- ol
+ 1))) {
660 } else if (o
->name
[ol
- 1] == '*' && strncmp(o
->name
, name
, ol
- 1) == 0) {
669 * Do the read only folders
673 do_readonly_folders (void)
677 register struct node
*np
;
679 snprintf (atrcur
, sizeof(atrcur
), "atr-%s-", current
);
680 atrlen
= strlen (atrcur
);
682 for (np
= m_defs
; np
; np
= np
->n_next
)
683 if (ssequal (atrcur
, np
->n_name
)
684 && !ssequal (nmhdir
, np
->n_name
+ atrlen
))
685 BuildFolderList (np
->n_name
+ atrlen
, 0);