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