Removed the space between function names and the opening parenthesis.
[mmh] / sbr / folder_pack.c
1 /*
2 ** folder_pack.c -- pack (renumber) the messages in a folder
3 **               -- into a contiguous range from 1 to n.
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
12 /*
13 ** Pack the message in a folder.
14 ** Return -1 if error, else return 0.
15 */
16
17 int
18 folder_pack(struct msgs **mpp, int verbose)
19 {
20         int hole, msgnum, newcurrent = 0;
21         char newmsg[BUFSIZ], oldmsg[BUFSIZ];
22         struct msgs *mp;
23
24         mp = *mpp;
25
26         /*
27         ** Just return if folder is empty.
28         */
29         if (mp->nummsg == 0)
30                 return 0;
31
32         /*
33         ** Make sure we have message status space allocated
34         ** for all numbers from 1 to current high message.
35         */
36         if (mp->lowoff > 1) {
37                 if ((mp = folder_realloc(mp, 1, mp->hghmsg)))
38                         *mpp = mp;
39                 else {
40                         advise(NULL, "unable to allocate folder storage");
41                         return -1;
42                 }
43         }
44
45         for (msgnum = mp->lowmsg, hole = 1; msgnum <= mp->hghmsg; msgnum++) {
46                 if (does_exist(mp, msgnum)) {
47                         if (msgnum != hole) {
48                                 strncpy(newmsg, m_name(hole), sizeof(newmsg));
49                                 strncpy(oldmsg, m_name(msgnum), sizeof(oldmsg));
50                                 if (verbose)
51                                         printf("message %s becomes %s\n", oldmsg, newmsg);
52
53                                 /*
54                                 ** Invoke the external refile hook for each
55                                 ** message being renamed.  This is done
56                                 ** before the file is renamed so that the
57                                 ** old message file is around for the hook.
58                                 */
59
60                                 (void)snprintf(oldmsg, sizeof (oldmsg), "%s/%d", mp->foldpath, msgnum);
61                                 (void)snprintf(newmsg, sizeof (newmsg), "%s/%d", mp->foldpath, hole);
62                                 ext_hook("ref-hook", oldmsg, newmsg);
63
64                                 /* move the message file */
65                                 if (rename(oldmsg, newmsg) == -1) {
66                                         advise(newmsg, "unable to rename %s to", oldmsg);
67                                         return -1;
68                                 }
69
70                                 /* check if this is the current message */
71                                 if (msgnum == mp->curmsg)
72                                         newcurrent = hole;
73
74                                 /* copy the attribute flags for this message */
75                                 copy_msg_flags(mp, hole, msgnum);
76
77                                 if (msgnum == mp->lowsel)
78                                         mp->lowsel = hole;
79                                 if (msgnum == mp->hghsel)
80                                         mp->hghsel = hole;
81
82                                 /*
83                                 ** mark that sequence information has
84                                 ** been modified
85                                 */
86                                 mp->msgflags |= SEQMOD;
87                         }
88                         hole++;
89                 }
90         }
91
92         /* record the new number for the high/low message */
93         mp->lowmsg = 1;
94         mp->hghmsg = hole - 1;
95
96         /* update the "cur" sequence */
97         if (newcurrent != 0)
98                 seq_setcur(mp, newcurrent);
99
100         return 0;
101 }