]>
diplodocus.org Git - nmh/blob - sbr/folder_delmsgs.c
1 /* folder_delmsgs.c -- "remove" SELECTED messages from a folder
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
12 * 1) If we are using an external rmmproc, then exec it.
13 * 2) Else if unlink_msgs is non-zero, then unlink the
15 * 3) Else rename SELECTED messages by prefixing name
16 * with a standard prefix.
18 * If there is an error, return -1, else return 0.
22 folder_delmsgs (struct msgs
*mp
, int unlink_msgs
, int nohook
)
25 int msgnum
, vecp
, retval
= 0;
26 char buf
[100], *dp
, **vec
, *prog
;
30 * If "rmmproc" is defined, exec it to remove messages.
33 /* Unset the EXISTS flag for each message to be removed */
34 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
35 if (is_selected (mp
, msgnum
))
36 unset_exists (mp
, msgnum
);
39 /* Mark that the sequence information has changed */
40 mp
->msgflags
|= SEQMOD
;
43 * Write out the sequence and context files; this will release
44 * any locks before the rmmproc is called.
50 vec
= argsplit(rmmproc
, &prog
, &vecp
);
53 * argsplit allocates a MAXARGS vector by default, If we need
54 * something bigger, allocate it ourselves
57 if (mp
->numsel
+ vecp
+ 1 > MAXARGS
)
58 vec
= mh_xrealloc(vec
, (mp
->numsel
+ vecp
+ 1) * sizeof *vec
);
59 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
60 if (is_selected (mp
, msgnum
) &&
61 !(vec
[vecp
++] = strdup (m_name (msgnum
))))
62 adios (NULL
, "strdup failed");
68 switch (pid
= fork()) {
70 advise ("fork", "unable to");
75 fprintf (stderr
, "unable to exec ");
80 arglist_free(prog
, vec
);
81 return (pidwait (pid
, -1));
86 * Either unlink or rename the SELECTED messages
88 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
89 if (is_selected (mp
, msgnum
)) {
90 /* unselect message */
91 unset_selected (mp
, msgnum
);
95 * Run the external hook on the message if one was specified in the context.
96 * All we have is the message number; we have changed to the directory
97 * containing the message. So, we need to extract that directory to form
98 * the complete path. Note that the caller knows the directory, but has
99 * no way of passing that to us.
103 (void)snprintf(msgpath
, sizeof (msgpath
), "%s/%d", mp
->foldpath
, msgnum
);
104 (void)ext_hook("del-hook", msgpath
, NULL
);
107 dp
= m_name (msgnum
);
110 /* just unlink the messages */
111 if (m_unlink (dp
) == -1) {
112 admonish (dp
, "unable to unlink");
117 /* or rename messages with standard prefix */
118 strncpy (buf
, m_backup (dp
), sizeof(buf
));
119 if (rename (dp
, buf
) == -1) {
120 admonish (buf
, "unable to rename %s to", dp
);
126 /* If removal was successful, decrement message count */
127 unset_exists (mp
, msgnum
);
134 adios (NULL
, "oops, mp->numsel should be 0");
136 /* Mark that the sequence information has changed */
137 mp
->msgflags
|= SEQMOD
;
140 * Write out sequence and context files