Move #include from h/mh.h to source files
[mmh] / sbr / folder_delmsgs.c
1 /*
2 ** folder_delmsgs.c -- remove (= unlink) SELECTED messages from a folder
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 <unistd.h>
10 #include <h/mh.h>
11
12 /*
13 ** Unlink the SELECTED messages.
14 **
15 ** If there is an error, return -1, else return 0.
16 */
17 int
18 folder_delmsgs(struct msgs *mp, int hook)
19 {
20         int msgnum, retval = 0;
21         char msgpath[BUFSIZ];
22
23         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
24                 if (!is_selected(mp, msgnum)) {
25                         continue;
26                 }
27
28                 /* unselect message */
29                 unset_selected(mp, msgnum);
30                 mp->numsel--;
31
32                 snprintf(msgpath, sizeof (msgpath), "%s/%d",
33                                 mp->foldpath, msgnum);
34
35                 if (hook) {
36                         /* Run the external hook on the message. */
37                         ext_hook("del-hook", msgpath, NULL);
38                 }
39
40                 /* just unlink the messages */
41                 if (unlink(msgpath) == -1) {
42                         admonish(msgpath, "unable to unlink");
43                         retval = -1;
44                         continue;
45                 }
46
47                 /* If removal was successful, decrement message count */
48                 unset_exists(mp, msgnum);
49                 mp->nummsg--;
50         }
51
52         /* Sanity check */
53         if (mp->numsel != 0)
54                 adios(NULL, "oops, mp->numsel should be 0");
55
56         /* Mark that the sequence information has changed */
57         mp->msgflags |= SEQMOD;
58
59         return retval;
60 }