]> diplodocus.org Git - nmh/blob - sbr/folder_realloc.c
Just reworded the bit about '%s' being safe not to quote (it's only safe not to
[nmh] / sbr / folder_realloc.c
1
2 /*
3 * folder_realloc.c -- realloc a folder/msgs structure
4 *
5 * $Id$
6 */
7
8 #include <h/mh.h>
9
10 /*
11 * Reallocate some of the space in the folder
12 * structure (currently just message status array).
13 *
14 * Return pointer to new folder structure.
15 * If error, return NULL.
16 */
17
18 struct msgs *
19 folder_realloc (struct msgs *mp, int lo, int hi)
20 {
21 int msgnum;
22
23 /* sanity checks */
24 if (lo < 1)
25 adios (NULL, "BUG: called folder_realloc with lo (%d) < 1", lo);
26 if (hi < 1)
27 adios (NULL, "BUG: called folder_realloc with hi (%d) < 1", hi);
28 if (mp->nummsg > 0 && lo > mp->lowmsg)
29 adios (NULL, "BUG: called folder_realloc with lo (%d) > mp->lowmsg (%d)",
30 lo, mp->lowmsg);
31 if (mp->nummsg > 0 && hi < mp->hghmsg)
32 adios (NULL, "BUG: called folder_realloc with hi (%d) < mp->hghmsg (%d)",
33 hi, mp->hghmsg);
34
35 /* Check if we really need to reallocate anything */
36 if (lo == mp->lowoff && hi == mp->hghoff)
37 return mp;
38
39 if (lo == mp->lowoff) {
40 /*
41 * We are just extending (or shrinking) the end of message
42 * status array. So we don't have to move anything and can
43 * just realloc the message status array.
44 */
45 if (!(mp->msgstats = realloc (mp->msgstats, MSGSTATSIZE(mp, lo, hi)))) {
46 advise (NULL, "unable to reallocate message storage");
47 return NULL;
48 }
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 if (!(tmpstats = malloc (MSGSTATSIZE(mp, lo, hi)))) {
58 advise (NULL, "unable to reallocate message storage");
59 return NULL;
60 }
61
62 /* then copy messages status array with shift */
63 if (mp->nummsg > 0) {
64 for (msgnum = mp->lowmsg; msgnum <= mp->hghmsg; msgnum++)
65 tmpstats[msgnum - lo] = mp->msgstats[msgnum - mp->lowoff];
66 }
67 free(mp->msgstats);
68 mp->msgstats = tmpstats;
69 }
70
71 mp->lowoff = lo;
72 mp->hghoff = hi;
73
74 /*
75 * Clear all the flags for entries outside
76 * the current message range for this folder.
77 */
78 if (mp->nummsg > 0) {
79 for (msgnum = mp->lowoff; msgnum < mp->lowmsg; msgnum++)
80 clear_msg_flags (mp, msgnum);
81 for (msgnum = mp->hghmsg + 1; msgnum <= mp->hghoff; msgnum++)
82 clear_msg_flags (mp, msgnum);
83 } else {
84 /* no messages, so clear entire range */
85 for (msgnum = mp->lowoff; msgnum <= mp->hghoff; msgnum++)
86 clear_msg_flags (mp, msgnum);
87 }
88
89 return mp;
90 }