]>
diplodocus.org Git - nmh/blob - sbr/folder_delmsgs.c
3 * folder_delmsgs.c -- "remove" SELECTED messages from a folder
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.
14 * 1) If we are using an external rmmproc, then exec it.
15 * 2) Else if unlink_msgs is non-zero, then unlink the
17 * 3) Else rename SELECTED messages by prefixing name
18 * with a standard prefix.
20 * If there is an error, return -1, else return 0.
24 folder_delmsgs (struct msgs
*mp
, int unlink_msgs
, int nohook
)
27 int msgnum
, vecp
, retval
= 0;
28 char buf
[100], *dp
, **vec
, *prog
;
32 * If "rmmproc" is defined, exec it to remove messages.
35 /* Unset the EXISTS flag for each message to be removed */
36 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
37 if (is_selected (mp
, msgnum
))
38 unset_exists (mp
, msgnum
);
41 /* Mark that the sequence information has changed */
42 mp
->msgflags
|= SEQMOD
;
45 * Write out the sequence and context files; this will release
46 * any locks before the rmmproc is called.
52 vec
= argsplit(rmmproc
, &prog
, &vecp
);
55 * argsplit allocates a MAXARGS vector by default, If we need
56 * something bigger, allocate it ourselves
59 if (mp
->numsel
+ vecp
+ 1 > MAXARGS
)
60 vec
= (char **) mh_xrealloc (vec
,
61 (size_t) ((mp
->numsel
+ vecp
+ 1) *
64 adios (NULL
, "unable to allocate exec vector");
65 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
66 if (is_selected (mp
, msgnum
) &&
67 !(vec
[vecp
++] = strdup (m_name (msgnum
))))
68 adios (NULL
, "strdup failed");
74 switch (pid
= fork()) {
76 advise ("fork", "unable to");
81 fprintf (stderr
, "unable to exec ");
86 arglist_free(prog
, vec
);
87 return (pidwait (pid
, -1));
92 * Either unlink or rename the SELECTED messages
94 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
95 if (is_selected (mp
, msgnum
)) {
96 /* unselect message */
97 unset_selected (mp
, msgnum
);
101 * Run the external hook on the message if one was specified in the context.
102 * All we have is the message number; we have changed to the directory
103 * containing the message. So, we need to extract that directory to form
104 * the complete path. Note that the caller knows the directory, but has
105 * no way of passing that to us.
109 (void)snprintf(msgpath
, sizeof (msgpath
), "%s/%d", mp
->foldpath
, msgnum
);
110 (void)ext_hook("del-hook", msgpath
, (char *)0);
113 dp
= m_name (msgnum
);
116 /* just unlink the messages */
117 if (m_unlink (dp
) == -1) {
118 admonish (dp
, "unable to unlink");
123 /* or rename messages with standard prefix */
124 strncpy (buf
, m_backup (dp
), sizeof(buf
));
125 if (rename (dp
, buf
) == -1) {
126 admonish (buf
, "unable to rename %s to", dp
);
132 /* If removal was successful, decrement message count */
133 unset_exists (mp
, msgnum
);
140 adios (NULL
, "oops, mp->numsel should be 0");
142 /* Mark that the sequence information has changed */
143 mp
->msgflags
|= SEQMOD
;
146 * Write out sequence and context files