]>
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.
13 * 1) If we are using an external rmmproc, then exec it.
14 * 2) Else if unlink_msgs is non-zero, then unlink the
16 * 3) Else rename SELECTED messages by prefixing name
17 * with a standard prefix.
19 * If there is an error, return -1, else return 0.
23 folder_delmsgs (struct msgs
*mp
, int unlink_msgs
, int nohook
)
26 int msgnum
, vecp
, retval
= 0;
27 char buf
[100], *dp
, **vec
, *prog
;
31 * If "rmmproc" is defined, exec it to remove messages.
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
);
40 /* Mark that the sequence information has changed */
41 mp
->msgflags
|= SEQMOD
;
44 * Write out the sequence and context files; this will release
45 * any locks before the rmmproc is called.
51 vec
= argsplit(rmmproc
, &prog
, &vecp
);
54 * argsplit allocates a MAXARGS vector by default, If we need
55 * something bigger, allocate it ourselves
58 if (mp
->numsel
+ vecp
+ 1 > MAXARGS
)
59 vec
= mh_xrealloc(vec
, (mp
->numsel
+ vecp
+ 1) * sizeof *vec
);
60 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
61 if (is_selected (mp
, msgnum
) &&
62 !(vec
[vecp
++] = strdup (m_name (msgnum
))))
63 adios (NULL
, "strdup failed");
69 switch (pid
= fork()) {
71 advise ("fork", "unable to");
76 fprintf (stderr
, "unable to exec ");
81 arglist_free(prog
, vec
);
82 return pidwait(pid
, -1);
87 * Either unlink or rename the SELECTED messages
89 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
90 if (is_selected (mp
, msgnum
)) {
91 /* unselect message */
92 unset_selected (mp
, msgnum
);
96 * Run the external hook on the message if one was specified in the context.
97 * All we have is the message number; we have changed to the directory
98 * containing the message. So, we need to extract that directory to form
99 * the complete path. Note that the caller knows the directory, but has
100 * no way of passing that to us.
104 (void)snprintf(msgpath
, sizeof (msgpath
), "%s/%d", mp
->foldpath
, msgnum
);
105 (void)ext_hook("del-hook", msgpath
, NULL
);
108 dp
= m_name (msgnum
);
111 /* just unlink the messages */
112 if (m_unlink (dp
) == -1) {
113 admonish (dp
, "unable to unlink");
118 /* or rename messages with standard prefix */
119 strncpy (buf
, m_backup (dp
), sizeof(buf
));
120 if (rename (dp
, buf
) == -1) {
121 admonish (buf
, "unable to rename %s to", dp
);
127 /* If removal was successful, decrement message count */
128 unset_exists (mp
, msgnum
);
135 adios (NULL
, "oops, mp->numsel should be 0");
137 /* Mark that the sequence information has changed */
138 mp
->msgflags
|= SEQMOD
;
141 * Write out sequence and context files