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