]> diplodocus.org Git - nmh/blob - uip/flist.c
Fixed three LC_ALL settings:
[nmh] / uip / flist.c
1 /*
2 * flist.c -- list nmh folders containing messages
3 * -- in a given sequence
4 *
5 * originally by
6 * David Nichols, Xerox-PARC, November, 1992
7 *
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
14 * any specification.
15 */
16
17 #include <h/mh.h>
18 #include <h/utils.h>
19
20 #define FALSE 0
21 #define TRUE 1
22
23 /*
24 * We allocate space to record the names of folders
25 * (foldersToDo array), this number of elements at a time.
26 */
27 #define MAXFOLDERS 100
28
29
30 #define FLIST_SWITCHES \
31 X("sequence name", 0, SEQSW) \
32 X("all", 0, ALLSW) \
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) \
46
47 #define X(sw, minchars, id) id,
48 DEFINE_SWITCH_ENUM(FLIST);
49 #undef X
50
51 #define X(sw, minchars, id) { sw, minchars, id },
52 DEFINE_SWITCH_ARRAY(FLIST, switches);
53 #undef X
54
55 struct Folder {
56 char *name; /* name of folder */
57 int priority;
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 */
62 };
63
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;
70
71 /* info on folders to search */
72 static char **foldersToDo;
73 static int numfolders;
74 static int maxfolders;
75
76 /* info on sequences to search for */
77 static svector_t sequencesToDo;
78
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 */
85
86 static char curfolder[BUFSIZ]; /* name of the current folder */
87 static char *nmhdir; /* base nmh mail directory */
88
89 /*
90 * Type for a compare function for qsort. This keeps
91 * the compiler happy.
92 */
93 typedef int (*qsort_comp) (const void *, const void *);
94
95 /*
96 * prototypes
97 */
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);
108
109
110
111 int
112 main(int argc, char **argv)
113 {
114 char *cp, **argp;
115 char **arguments;
116 char buf[BUFSIZ];
117
118 setlocale(LC_ALL, "");
119 invo_name = r1bindex(argv[0], '/');
120
121 /* read user profile/context */
122 context_read();
123
124 /*
125 * If program was invoked with name ending
126 * in `s', then add switch `-all'.
127 */
128 if (argv[0][strlen (argv[0]) - 1] == 's')
129 all = TRUE;
130
131 arguments = getarguments (invo_name, argc, argv, 1);
132 argp = arguments;
133
134 /* allocate the initial space to record the folder names */
135 numfolders = 0;
136 maxfolders = MAXFOLDERS;
137 foldersToDo = (char **) mh_xmalloc ((size_t) (maxfolders * sizeof(*foldersToDo)));
138
139 /* no sequences yet */
140 sequencesToDo = svector_create (0);
141
142 /* parse arguments */
143 while ((cp = *argp++)) {
144 if (*cp == '-') {
145 switch (smatch(++cp, switches)) {
146 case AMBIGSW:
147 ambigsw(cp, switches);
148 done(1);
149 case UNKWNSW:
150 adios(NULL, "-%s unknown", cp);
151
152 case HELPSW:
153 snprintf(buf, sizeof(buf), "%s [+folder1 [+folder2 ...]][switches]",
154 invo_name);
155 print_help(buf, switches, 1);
156 done(0);
157 case VERSIONSW:
158 print_version(invo_name);
159 done (0);
160
161 case SEQSW:
162 if (!(cp = *argp++) || *cp == '-')
163 adios (NULL, "missing argument to %s", argp[-2]);
164
165 svector_push_back (sequencesToDo, cp);
166 break;
167
168 case ALLSW:
169 all = TRUE;
170 break;
171 case NOALLSW:
172 all = FALSE;
173 break;
174
175 case SHOWZERO:
176 showzero = TRUE;
177 break;
178 case NOSHOWZERO:
179 showzero = FALSE;
180 break;
181
182 case ALPHASW:
183 alphaOrder = TRUE;
184 break;
185 case NOALPHASW:
186 alphaOrder = FALSE;
187 break;
188
189 case NOFASTSW:
190 case TOTALSW:
191 Total = TRUE;
192 break;
193
194 case FASTSW:
195 case NOTOTALSW:
196 Total = FALSE;
197 break;
198
199 case RECURSE:
200 recurse = TRUE;
201 break;
202 case NORECURSE:
203 recurse = FALSE;
204 break;
205 }
206 } else {
207 /*
208 * Check if we need to allocate more space
209 * for folder names.
210 */
211 if (numfolders >= maxfolders) {
212 maxfolders += MAXFOLDERS;
213 foldersToDo = (char **) mh_xrealloc (foldersToDo,
214 (size_t) (maxfolders * sizeof(*foldersToDo)));
215 }
216 if (*cp == '+' || *cp == '@') {
217 foldersToDo[numfolders++] =
218 pluspath (cp);
219 } else
220 foldersToDo[numfolders++] = cp;
221 }
222 }
223
224 if (!context_find ("path"))
225 free (path ("./", TFOLDER));
226
227 /* get current folder */
228 strncpy (curfolder, getfolder(1), sizeof(curfolder));
229
230 /* get nmh base directory */
231 nmhdir = m_maildir ("");
232
233 /*
234 * If we didn't specify any sequences, we search
235 * for the "Unseen-Sequence" profile entry and use
236 * all the sequences defined there.
237 */
238 if (svector_size (sequencesToDo) == 0) {
239 if ((cp = context_find(usequence)) && *cp) {
240 char **ap, *dp;
241
242 dp = getcpy(cp);
243 ap = brkstring (dp, " ", "\n");
244 for (; ap && *ap; ap++)
245 svector_push_back (sequencesToDo, *ap);
246 } else {
247 adios (NULL, "no sequence specified or %s profile entry found", usequence);
248 }
249 }
250
251 GetFolderOrder();
252 ScanFolders();
253 qsort(folders, nFolders, sizeof(struct Folder), (qsort_comp) CompareFolders);
254 PrintFolders();
255 svector_free (sequencesToDo);
256 done (0);
257 return 1;
258 }
259
260 /*
261 * Read the Flist-Order profile entry to determine
262 * how to sort folders for output.
263 */
264
265 void
266 GetFolderOrder(void)
267 {
268 char *p, *s;
269 int priority = 1;
270 struct Folder *o;
271
272 if (!(p = context_find("Flist-Order")))
273 return;
274 for (;;) {
275 while (isspace((unsigned char) *p))
276 ++p;
277 s = p;
278 while (*p && !isspace((unsigned char) *p))
279 ++p;
280 if (p != s) {
281 /* Found one. */
282 AllocFolders(&orders, &nOrdersAlloced, nOrders + 1);
283 o = &orders[nOrders++];
284 o->priority = priority++;
285 o->name = (char *) mh_xmalloc(p - s + 1);
286 strncpy(o->name, s, p - s);
287 o->name[p - s] = 0;
288 } else
289 break;
290 }
291 }
292
293 /*
294 * Scan all the necessary folders
295 */
296
297 void
298 ScanFolders(void)
299 {
300 int i;
301
302 /*
303 * change directory to base of nmh directory
304 */
305 if (chdir (nmhdir) == NOTOK)
306 adios (nmhdir, "unable to change directory to");
307
308 if (numfolders > 0) {
309 /* Update context */
310 strncpy (curfolder, foldersToDo[numfolders - 1], sizeof(curfolder));
311 context_replace (pfolder, curfolder);/* update current folder */
312 context_save (); /* save the context file */
313
314 /*
315 * Scan each given folder. If -all is given,
316 * then also scan the 1st level subfolders under
317 * each given folder.
318 */
319 for (i = 0; i < numfolders; ++i)
320 BuildFolderList(foldersToDo[i], all ? 1 : 0);
321 } else {
322 if (all) {
323 /*
324 * Do the readonly folders
325 */
326 do_readonly_folders();
327
328 /*
329 * Now scan the entire nmh directory for folders
330 */
331 BuildFolderList(".", 0);
332 } else {
333 /*
334 * Else scan current folder
335 */
336 BuildFolderList(curfolder, 0);
337 }
338 }
339 }
340
341 /*
342 * Initial building of folder list for
343 * the top of our search tree.
344 */
345
346 void
347 BuildFolderList(char *dirName, int searchdepth)
348 {
349 struct stat st;
350
351 /* Make sure we have a directory */
352 if ((stat(dirName, &st) == -1) || !S_ISDIR(st.st_mode))
353 return;
354
355 /*
356 * If base directory, don't add it to the
357 * folder list. We just recurse into it.
358 */
359 if (!strcmp (dirName, ".")) {
360 BuildFolderListRecurse (".", &st, 0);
361 return;
362 }
363
364 /*
365 * Add this folder to the list.
366 * If recursing and directory has subfolders,
367 * then build folder list for subfolders.
368 */
369 if (AddFolder(dirName, showzero) && (recurse || searchdepth) && st.st_nlink > 2)
370 BuildFolderListRecurse(dirName, &st, searchdepth - 1);
371 }
372
373 /*
374 * Recursive building of folder list
375 */
376
377 void
378 BuildFolderListRecurse(char *dirName, struct stat *s, int searchdepth)
379 {
380 char *base, name[PATH_MAX];
381 char *n;
382 int nlinks;
383 DIR *dir;
384 struct dirent *dp;
385 struct stat st;
386
387 /*
388 * Keep track of number of directories we've seen so we can
389 * stop stat'ing entries in this directory once we've seen
390 * them all. This optimization will fail if you have extra
391 * directories beginning with ".", since we don't bother to
392 * stat them. But that shouldn't generally be a problem.
393 */
394 nlinks = s->st_nlink;
395 if (nlinks == 1) {
396 /* Disable the optimization under conditions where st_nlink
397 is set to 1. That happens on Cygwin, for example:
398 http://cygwin.com/ml/cygwin-apps/2008-08/msg00264.html */
399 nlinks = INT_MAX;
400 }
401
402 if (!(dir = opendir(dirName)))
403 adios(dirName, "can't open directory");
404
405 /*
406 * A hack so that we don't see a
407 * leading "./" in folder names.
408 */
409 base = strcmp (dirName, ".") ? dirName : dirName + 1;
410
411 while (nlinks && (dp = readdir(dir))) {
412 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) {
413 nlinks--;
414 continue;
415 }
416 if (dp->d_name[0] == '.')
417 continue;
418 /* Check to see if the name of the file is a number
419 * if it is, we assume it's a mail file and skip it
420 */
421 for (n = dp->d_name; *n && isdigit((unsigned char) *n); n++);
422 if (!*n)
423 continue;
424 strncpy (name, base, sizeof(name) - 2);
425 if (*base)
426 strcat(name, "/");
427 strncat(name, dp->d_name, sizeof(name) - strlen(name) - 1);
428 if ((stat(name, &st) != -1) && S_ISDIR(st.st_mode)) {
429 /*
430 * Check if this was really a symbolic link pointing
431 * to a directory. If not, then decrement link count.
432 */
433 if (lstat (name, &st) == -1)
434 nlinks--;
435 /* Add this folder to the list */
436 if (AddFolder(name, showzero) &&
437 (recurse || searchdepth) && st.st_nlink > 2)
438 BuildFolderListRecurse(name, &st, searchdepth - 1);
439 }
440 }
441 closedir(dir);
442 }
443
444 /*
445 * Add this folder to our list, counting the total number of
446 * messages and the number of messages in each sequence.
447 */
448
449 int
450 AddFolder(char *name, int force)
451 {
452 unsigned int i;
453 int msgnum, nonzero;
454 ivector_t seqnum = ivector_create (0), nSeq = ivector_create (0);
455 struct Folder *f;
456 struct msgs *mp;
457 char *cp;
458
459 /* Read folder and create message structure */
460 if (!(mp = folder_read (name, 0))) {
461 /* Oops, error occurred. Record it and continue. */
462 AllocFolders(&folders, &nFoldersAlloced, nFolders + 1);
463 f = &folders[nFolders++];
464 f->name = getcpy(name);
465 f->error = 1;
466 f->priority = AssignPriority(f->name);
467 return 0;
468 }
469
470 for (i = 0; i < svector_size (sequencesToDo); i++) {
471 /* Convert sequences to their sequence numbers */
472 if ((cp = svector_at (sequencesToDo, i)))
473 ivector_push_back (seqnum, seq_getnum(mp, cp));
474 else
475 ivector_push_back (seqnum, -1);
476
477 /* Now count messages in this sequence */
478 ivector_push_back (nSeq, 0);
479 if (mp->nummsg > 0 && ivector_at (seqnum, i) != -1) {
480 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++) {
481 if (in_sequence(mp, ivector_at (seqnum, i), msgnum))
482 (*ivector_atp (nSeq, i))++;
483 }
484 }
485 }
486
487 /* Check if any of the sequence checks were nonzero */
488 nonzero = 0;
489 for (i = 0; i < svector_size (sequencesToDo); i++) {
490 if (ivector_at (nSeq, i) > 0) {
491 nonzero = 1;
492 break;
493 }
494 }
495
496 if (nonzero || force) {
497 /* save general folder information */
498 AllocFolders(&folders, &nFoldersAlloced, nFolders + 1);
499 f = &folders[nFolders++];
500 f->name = getcpy(name);
501 f->nMsgs = mp->nummsg;
502 f->nSeq = ivector_create (0);
503 f->private = ivector_create (0);
504 f->error = 0;
505 f->priority = AssignPriority(f->name);
506
507 /* record the sequence information */
508 for (i = 0; i < svector_size (sequencesToDo); i++) {
509 *ivector_atp (f->nSeq, i) = ivector_at (nSeq, i);
510 ivector_push_back (f->private,
511 ivector_at (seqnum, i) != -1
512 ? is_seq_private(mp, ivector_at (seqnum, i))
513 : 0);
514 }
515 }
516
517 ivector_free (nSeq);
518 ivector_free (seqnum);
519 folder_free (mp); /* free folder/message structure */
520 return 1;
521 }
522
523 /*
524 * Print the folder/sequence information
525 */
526
527 void
528 PrintFolders(void)
529 {
530 char tmpname[BUFSIZ];
531 unsigned int i, j, len, has_private = 0;
532 unsigned int maxfolderlen = 0, maxseqlen = 0;
533 int maxnum = 0, maxseq = 0;
534
535 if (!Total) {
536 for (i = 0; i < nFolders; i++)
537 printf("%s\n", folders[i].name);
538 return;
539 }
540
541 /*
542 * Find the width we need for various fields
543 */
544 for (i = 0; i < nFolders; ++i) {
545 /* find the length of longest folder name */
546 len = strlen(folders[i].name);
547 if (len > maxfolderlen)
548 maxfolderlen = len;
549
550 /* If folder had error, skip the rest */
551 if (folders[i].error)
552 continue;
553
554 /* find the maximum total messages */
555 if (folders[i].nMsgs > maxnum)
556 maxnum = folders[i].nMsgs;
557
558 for (j = 0; j < svector_size (sequencesToDo); j++) {
559 /* find maximum width of sequence name */
560 len = strlen (svector_at (sequencesToDo, j));
561 if ((ivector_at (folders[i].nSeq, j) > 0 || showzero) &&
562 (len > maxseqlen))
563 maxseqlen = len;
564
565 /* find the maximum number of messages in sequence */
566 if (ivector_at (folders[i].nSeq, j) > maxseq)
567 maxseq = ivector_at (folders[i].nSeq, j);
568
569 /* check if this sequence is private in any of the folders */
570 if (ivector_at (folders[i].private, j))
571 has_private = 1;
572 }
573 }
574
575 /* Now print all the folder/sequence information */
576 for (i = 0; i < nFolders; i++) {
577 for (j = 0; j < svector_size (sequencesToDo); j++) {
578 if (ivector_at (folders[i].nSeq, j) > 0 || showzero) {
579 /* Add `+' to end of name of current folder */
580 if (strcmp(curfolder, folders[i].name))
581 snprintf(tmpname, sizeof(tmpname), "%s", folders[i].name);
582 else
583 snprintf(tmpname, sizeof(tmpname), "%s+", folders[i].name);
584
585 if (folders[i].error) {
586 printf("%-*s is unreadable\n", maxfolderlen+1, tmpname);
587 continue;
588 }
589
590 printf("%-*s has %*d in sequence %-*s%s; out of %*d\n",
591 maxfolderlen+1, tmpname,
592 num_digits(maxseq), ivector_at (folders[i].nSeq, j),
593 maxseqlen, svector_at (sequencesToDo, j),
594 !has_private ? "" : ivector_at (folders[i].private, j)
595 ? " (private)" : " ",
596 num_digits(maxnum), folders[i].nMsgs);
597 }
598 }
599 }
600 }
601
602 /*
603 * Put them in priority order.
604 */
605
606 int
607 CompareFolders(struct Folder *f1, struct Folder *f2)
608 {
609 if (!alphaOrder && f1->priority != f2->priority)
610 return f1->priority - f2->priority;
611 else
612 return strcmp(f1->name, f2->name);
613 }
614
615 /*
616 * Make sure we have at least n folders allocated.
617 */
618
619 void
620 AllocFolders(struct Folder **f, int *nfa, int n)
621 {
622 if (n <= *nfa)
623 return;
624 if (*f == NULL) {
625 *nfa = 10;
626 *f = (struct Folder *) mh_xmalloc (*nfa * (sizeof(struct Folder)));
627 } else {
628 *nfa *= 2;
629 *f = (struct Folder *) mh_xrealloc (*f, *nfa * (sizeof(struct Folder)));
630 }
631 }
632
633 /*
634 * Return the priority for a name. The highest comes from an exact match.
635 * After that, the longest match (then first) assigns the priority.
636 */
637 int
638 AssignPriority(char *name)
639 {
640 int i, ol, nl;
641 int best = nOrders;
642 int bestLen = 0;
643 struct Folder *o;
644
645 nl = strlen(name);
646 for (i = 0; i < nOrders; ++i) {
647 o = &orders[i];
648 if (!strcmp(name, o->name))
649 return o->priority;
650 ol = strlen(o->name);
651 if (nl < ol - 1)
652 continue;
653 if (ol < bestLen)
654 continue;
655 if (o->name[0] == '*' && !strcmp(o->name + 1, name + (nl - ol + 1))) {
656 best = o->priority;
657 bestLen = ol;
658 } else if (o->name[ol - 1] == '*' && strncmp(o->name, name, ol - 1) == 0) {
659 best = o->priority;
660 bestLen = ol;
661 }
662 }
663 return best;
664 }
665
666 /*
667 * Do the read only folders
668 */
669
670 static void
671 do_readonly_folders (void)
672 {
673 int atrlen;
674 char atrcur[BUFSIZ];
675 register struct node *np;
676
677 snprintf (atrcur, sizeof(atrcur), "atr-%s-", current);
678 atrlen = strlen (atrcur);
679
680 for (np = m_defs; np; np = np->n_next)
681 if (ssequal (atrcur, np->n_name)
682 && !ssequal (nmhdir, np->n_name + atrlen))
683 BuildFolderList (np->n_name + atrlen, 0);
684 }