]> diplodocus.org Git - nmh/blob - sbr/folder_pack.c
Bring these changes over from the branch.
[nmh] / sbr / folder_pack.c
1
2 /*
3 * folder_pack.c -- pack (renumber) the messages in a folder
4 * -- into a contiguous range from 1 to n.
5 *
6 * $Id$
7 *
8 * This code is Copyright (c) 2002, by the authors of nmh. See the
9 * COPYRIGHT file in the root directory of the nmh distribution for
10 * complete copyright information.
11 */
12
13 #include <h/mh.h>
14
15 /*
16 * Pack the message in a folder.
17 * Return -1 if error, else return 0.
18 */
19
20 int
21 folder_pack (struct msgs **mpp, int verbose)
22 {
23 int hole, msgnum, newcurrent = 0;
24 char newmsg[BUFSIZ], oldmsg[BUFSIZ];
25 struct msgs *mp;
26
27 mp = *mpp;
28
29 /*
30 * Just return if folder is empty.
31 */
32 if (mp->nummsg == 0)
33 return 0;
34
35 /*
36 * Make sure we have message status space allocated
37 * for all numbers from 1 to current high message.
38 */
39 if (mp->lowoff > 1) {
40 if ((mp = folder_realloc (mp, 1, mp->hghmsg)))
41 *mpp = mp;
42 else {
43 advise (NULL, "unable to allocate folder storage");
44 return -1;
45 }
46 }
47
48 for (msgnum = mp->lowmsg, hole = 1; msgnum <= mp->hghmsg; msgnum++) {
49 if (does_exist (mp, msgnum)) {
50 if (msgnum != hole) {
51 strncpy (newmsg, m_name (hole), sizeof(newmsg));
52 strncpy (oldmsg, m_name (msgnum), sizeof(oldmsg));
53 if (verbose)
54 printf ("message %s becomes %s\n", oldmsg, newmsg);
55
56 /* move the message file */
57 if (rename (oldmsg, newmsg) == -1) {
58 advise (newmsg, "unable to rename %s to", oldmsg);
59 return -1;
60 }
61
62 /* check if this is the current message */
63 if (msgnum == mp->curmsg)
64 newcurrent = hole;
65
66 /* copy the attribute flags for this message */
67 copy_msg_flags (mp, hole, msgnum);
68
69 if (msgnum == mp->lowsel)
70 mp->lowsel = hole;
71 if (msgnum == mp->hghsel)
72 mp->hghsel = hole;
73
74 /* mark that sequence information has been modified */
75 mp->msgflags |= SEQMOD;
76 }
77 hole++;
78 }
79 }
80
81 /* record the new number for the high/low message */
82 mp->lowmsg = 1;
83 mp->hghmsg = hole - 1;
84
85 /* update the "cur" sequence */
86 if (newcurrent != 0)
87 seq_setcur (mp, newcurrent);
88
89 return 0;
90 }