use memset to clear the msgstats in folder_realloc
[mmh] / sbr / seq_list.c
1 /*
2 ** seq_list.c -- Get all messages in a sequence and return them
3 **            -- as a space separated list of message ranges.
4 **
5 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
6 ** COPYRIGHT file in the root directory of the nmh distribution for
7 ** complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/utils.h>
12
13 /* allocate this much buffer space at a time */
14 #define MAXBUFFER 1024
15
16 /* static buffer to collect the sequence line */
17 static char *buffer = NULL;
18 static int len = 0;
19
20
21 char *
22 seq_list(struct msgs *mp, char *seqname)
23 {
24         int i, j, seqnum;
25         char *bp;
26
27         /* On first invocation, allocate initial buffer space */
28         if (!buffer) {
29                 len = MAXBUFFER;
30                 buffer = mh_xcalloc(len, sizeof(char));
31         }
32
33         /*
34         ** Special processing for the cur sequence.  We assume that the
35         ** cur sequence and mp->curmsg are in sync (see seq_add.c).
36         ** This is returned, even if message doesn't exist or the
37         ** folder is empty.
38         */
39         if (strcmp(seq_cur, seqname)==0) {
40                 if (mp->curmsg) {
41                         sprintf(buffer, "%s", m_name(mp->curmsg));
42                         return (buffer);
43                 } else
44                         return (NULL);
45         }
46
47         /* If the folder is empty, just return NULL */
48         if (mp->nummsg == 0)
49                 return NULL;
50
51         /* Get the index of the sequence */
52         if ((seqnum = seq_getnum(mp, seqname)) == -1)
53                 return NULL;
54
55         bp = buffer;
56
57         for (i = mp->lowoff; i <= mp->hghoff; ++i) {
58                 /* If message isn't in the sequence, then continue */
59                 if (!in_sequence(mp, seqnum, i))
60                         continue;
61
62                 /*
63                 ** See if we need to enlarge buffer.  Since we don't know
64                 ** exactly how many character this particular message range
65                 ** will need, we enlarge the buffer if we are within
66                 ** 50 characters of the end.
67                 */
68                 if (bp - buffer > len - 50) {
69                         char *newbuf;
70
71                         len += MAXBUFFER;
72                         newbuf = mh_xrealloc(buffer, len);
73                         bp = newbuf + (bp - buffer);
74                         buffer = newbuf;
75                 }
76
77                 /*
78                 ** If this is not the first message range in
79                 ** the list, first add a space.
80                 */
81                 if (bp > buffer)
82                         *bp++ = ' ';
83
84                 sprintf(bp, "%s", m_name(i));
85                 bp += strlen(bp);
86                 j = i;  /* Remember beginning of message range */
87
88                 /*
89                 ** Scan to the end of this message range
90                 */
91                 for (++i; i <= mp->hghoff && in_sequence(mp, seqnum, i); ++i)
92                         ;
93
94                 if (i - j > 1) {
95                         sprintf(bp, "-%s", m_name(i - 1));
96                         bp += strlen(bp);
97                 }
98         }
99         return (bp > buffer? buffer : NULL);
100 }