Add %(unmailto) format function for List-Post headers
[mmh] / sbr / folder_realloc.c
1 /*
2 ** folder_realloc.c -- realloc a folder/msgs structure
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <sysexits.h>
10 #include <h/mh.h>
11 #include <h/utils.h>
12
13 /*
14 ** Reallocate some of the space in the folder
15 ** structure (currently just message status array).
16 **
17 ** Return pointer to new folder structure.
18 ** If error, return NULL.
19 */
20
21 struct msgs *
22 folder_realloc(struct msgs *mp, int lo, int hi)
23 {
24         int msgnum;
25
26         /* sanity checks */
27         if (lo < 1)
28                 adios(EX_SOFTWARE, NULL, "BUG: called folder_realloc with lo (%d) < 1", lo);
29         if (hi < 1)
30                 adios(EX_SOFTWARE, NULL, "BUG: called folder_realloc with hi (%d) < 1", hi);
31         if (mp->nummsg > 0 && lo > mp->lowmsg)
32                 adios(EX_SOFTWARE, NULL, "BUG: called folder_realloc with lo (%d) > mp->lowmsg (%d)",
33                                 lo, mp->lowmsg);
34         if (mp->nummsg > 0 && hi < mp->hghmsg)
35                 adios(EX_SOFTWARE, NULL, "BUG: called folder_realloc with hi (%d) < mp->hghmsg (%d)",
36                                 hi, mp->hghmsg);
37
38         /* Check if we really need to reallocate anything */
39         if (lo == mp->lowoff && hi == mp->hghoff)
40                 return mp;
41
42         if (lo == mp->lowoff) {
43                 /*
44                 ** We are just extending (or shrinking) the end of message
45                 ** status array.  So we don't have to move anything and can
46                 ** just realloc the message status array.
47                 */
48                 mp->msgstats = mh_xrealloc(mp->msgstats, MSGSTATSIZE(mp, lo, hi));
49         } else {
50                 /*
51                 ** We are changing the offset of the message status
52                 ** array.  So we will need to shift everything.
53                 */
54                 seqset_t *tmpstats;
55
56                 /* first allocate the new message status space */
57                 tmpstats = mh_xcalloc(MSGSTATSIZE(mp, lo, hi), 1);
58
59                 /* then copy messages status array with shift */
60                 if (mp->nummsg > 0) {
61                         for (msgnum=mp->lowmsg; msgnum<=mp->hghmsg; msgnum++) {
62                                 tmpstats[msgnum - lo] = mp->msgstats[msgnum - mp->lowoff];
63                         }
64                 }
65                 mh_free0(&(mp->msgstats));
66                 mp->msgstats = tmpstats;
67         }
68
69         mp->lowoff = lo;
70         mp->hghoff = hi;
71
72         /*
73         ** Clear all the flags for entries outside
74         ** the current message range for this folder.
75         */
76         if (mp->nummsg > 0) {
77                 for (msgnum = mp->lowoff; msgnum < mp->lowmsg; msgnum++)
78                         clear_msg_flags(mp, msgnum);
79                 for (msgnum = mp->hghmsg + 1; msgnum <= mp->hghoff; msgnum++)
80                         clear_msg_flags(mp, msgnum);
81         } else {
82                 /* no messages, so clear entire range */
83                 for (msgnum = mp->lowoff; msgnum <= mp->hghoff; msgnum++)
84                         clear_msg_flags(mp, msgnum);
85         }
86
87         return mp;
88 }