]>
diplodocus.org Git - nmh/blob - sbr/folder_delmsgs.c
936dbf6a989b213c308d49307e836ce4a23925f5
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 #include "folder_delmsgs.h"
13 #include "context_save.h"
20 * 1) If we are using an external rmmproc, then exec it.
21 * 2) Else if unlink_msgs is non-zero, then unlink the
23 * 3) Else rename SELECTED messages by prefixing name
24 * with a standard prefix.
26 * If there is an error, return -1, else return 0.
30 folder_delmsgs (struct msgs
*mp
, int unlink_msgs
, int nohook
)
33 int msgnum
, vecp
, retval
= 0;
34 char buf
[100], *dp
, **vec
, *prog
;
38 * If "rmmproc" is defined, exec it to remove messages.
41 /* Unset the EXISTS flag for each message to be removed */
42 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
43 if (is_selected (mp
, msgnum
))
44 unset_exists (mp
, msgnum
);
47 /* Mark that the sequence information has changed */
48 mp
->msgflags
|= SEQMOD
;
51 * Write out the sequence and context files; this will release
52 * any locks before the rmmproc is called.
58 vec
= argsplit(rmmproc
, &prog
, &vecp
);
61 * argsplit allocates a MAXARGS vector by default, If we need
62 * something bigger, allocate it ourselves
65 if (mp
->numsel
+ vecp
+ 1 > MAXARGS
)
66 vec
= mh_xrealloc(vec
, (mp
->numsel
+ vecp
+ 1) * sizeof *vec
);
67 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
68 if (is_selected (mp
, msgnum
) &&
69 !(vec
[vecp
++] = strdup (m_name (msgnum
))))
76 switch (pid
= fork()) {
78 advise ("fork", "unable to");
83 fprintf (stderr
, "unable to exec ");
88 arglist_free(prog
, vec
);
89 return pidwait(pid
, -1);
94 * Either unlink or rename the SELECTED messages
96 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
97 if (is_selected (mp
, msgnum
)) {
98 /* unselect message */
99 unset_selected (mp
, msgnum
);
103 * Run the external hook on the message if one was specified in the context.
104 * All we have is the message number; we have changed to the directory
105 * containing the message. So, we need to extract that directory to form
106 * the complete path. Note that the caller knows the directory, but has
107 * no way of passing that to us.
111 (void)snprintf(msgpath
, sizeof (msgpath
), "%s/%d", mp
->foldpath
, msgnum
);
112 (void)ext_hook("del-hook", msgpath
, NULL
);
115 dp
= m_name (msgnum
);
118 /* just unlink the messages */
119 if (m_unlink (dp
) == -1) {
120 admonish (dp
, "unable to unlink");
125 /* or rename messages with standard prefix */
126 strncpy (buf
, m_backup (dp
), sizeof(buf
));
127 if (rename (dp
, buf
) == -1) {
128 admonish (buf
, "unable to rename %s to", dp
);
134 /* If removal was successful, decrement message count */
135 unset_exists (mp
, msgnum
);
142 die("oops, mp->numsel should be 0");
144 /* Mark that the sequence information has changed */
145 mp
->msgflags
|= SEQMOD
;
148 * Write out sequence and context files