]> diplodocus.org Git - nmh/blob - sbr/folder_delmsgs.c
More cleaned and conversion to the new parameter API.
[nmh] / sbr / folder_delmsgs.c
1
2 /*
3 * folder_delmsgs.c -- "remove" SELECTED messages from a folder
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 * 1) If we are using an external rmmproc, then exec it.
14 * 2) Else if unlink_msgs is non-zero, then unlink the
15 * SELECTED messages.
16 * 3) Else rename SELECTED messages by prefixing name
17 * with a standard prefix.
18 *
19 * If there is an error, return -1, else return 0.
20 */
21
22 int
23 folder_delmsgs (struct msgs *mp, int unlink_msgs, int nohook)
24 {
25 pid_t pid;
26 int msgnum, vecp, retval = 0;
27 char buf[100], *dp, **vec, *prog;
28 char msgpath[BUFSIZ];
29
30 /*
31 * If "rmmproc" is defined, exec it to remove messages.
32 */
33 if (rmmproc) {
34 /* Unset the EXISTS flag for each message to be removed */
35 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
36 if (is_selected (mp, msgnum))
37 unset_exists (mp, msgnum);
38 }
39
40 /* Mark that the sequence information has changed */
41 mp->msgflags |= SEQMOD;
42
43 /*
44 * Write out the sequence and context files; this will release
45 * any locks before the rmmproc is called.
46 */
47
48 seq_save (mp);
49 context_save ();
50
51 vec = argsplit(rmmproc, &prog, &vecp);
52
53 /*
54 * argsplit allocates a MAXARGS vector by default, If we need
55 * something bigger, allocate it ourselves
56 */
57
58 if (mp->numsel + vecp + 1 > MAXARGS)
59 vec = (char **) realloc (vec, (size_t) ((mp->numsel + vecp + 1) *
60 sizeof(*vec)));
61 if (vec == NULL)
62 adios (NULL, "unable to allocate exec vector");
63 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
64 if (is_selected (mp, msgnum) &&
65 !(vec[vecp++] = strdup (m_name (msgnum))))
66 adios (NULL, "strdup failed");
67 }
68 vec[vecp] = NULL;
69
70 fflush (stdout);
71
72 switch (pid = fork()) {
73 case -1:
74 advise ("fork", "unable to");
75 return -1;
76
77 case 0:
78 execvp (prog, vec);
79 fprintf (stderr, "unable to exec ");
80 perror (rmmproc);
81 _exit (-1);
82
83 default:
84 arglist_free(prog, vec);
85 return (pidwait (pid, -1));
86 }
87 }
88
89 /*
90 * Either unlink or rename the SELECTED messages
91 */
92 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
93 if (is_selected (mp, msgnum)) {
94 /* unselect message */
95 unset_selected (mp, msgnum);
96 mp->numsel--;
97
98 /*
99 * Run the external hook on the message if one was specified in the context.
100 * All we have is the message number; we have changed to the directory
101 * containing the message. So, we need to extract that directory to form
102 * the complete path. Note that the caller knows the directory, but has
103 * no way of passing that to us.
104 */
105
106 if (!nohook) {
107 (void)snprintf(msgpath, sizeof (msgpath), "%s/%d", mp->foldpath, msgnum);
108 (void)ext_hook("del-hook", msgpath, (char *)0);
109 }
110
111 dp = m_name (msgnum);
112
113 if (unlink_msgs) {
114 /* just unlink the messages */
115 if (m_unlink (dp) == -1) {
116 admonish (dp, "unable to unlink");
117 retval = -1;
118 continue;
119 }
120 } else {
121 /* or rename messages with standard prefix */
122 strncpy (buf, m_backup (dp), sizeof(buf));
123 if (rename (dp, buf) == -1) {
124 admonish (buf, "unable to rename %s to", dp);
125 retval = -1;
126 continue;
127 }
128 }
129
130 /* If removal was successful, decrement message count */
131 unset_exists (mp, msgnum);
132 mp->nummsg--;
133 }
134 }
135
136 /* Sanity check */
137 if (mp->numsel != 0)
138 adios (NULL, "oops, mp->numsel should be 0");
139
140 /* Mark that the sequence information has changed */
141 mp->msgflags |= SEQMOD;
142
143 /*
144 * Write out sequence and context files
145 */
146
147 seq_save (mp);
148 context_save ();
149
150 return retval;
151 }