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.
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;
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;
43 vec = (char **) calloc ((size_t) (mp->numsel + 2), sizeof(*vec));
45 adios (NULL, "unable to allocate exec vector");
47 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
48 if (is_selected (mp, msgnum) &&
49 !(vec[vecp++] = strdup (m_name (msgnum))))
50 adios (NULL, "strdup failed");
55 vec[0] = r1bindex (rmmproc, '/');
57 switch (pid = vfork()) {
59 advise ("fork", "unable to");
63 execvp (rmmproc, vec);
64 fprintf (stderr, "unable to exec ");
69 return (pidwait (pid, -1));
74 * Either unlink or rename the SELECTED messages
76 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
77 if (is_selected (mp, msgnum)) {
78 /* unselect message */
79 unset_selected (mp, msgnum);
83 * Run the external hook on the message if one was specified in the context.
84 * All we have is the message number; we have changed to the directory
85 * containing the message. So, we need to extract that directory to form
86 * the complete path. Note that the caller knows the directory, but has
87 * no way of passing that to us.
91 (void)snprintf(msgpath, sizeof (msgpath), "%s/%d", mp->foldpath, msgnum);
92 (void)ext_hook("del-hook", msgpath, (char *)0);
98 /* just unlink the messages */
99 if (unlink (dp) == -1) {
100 admonish (dp, "unable to unlink");
105 /* or rename messages with standard prefix */
106 strncpy (buf, m_backup (dp), sizeof(buf));
107 if (rename (dp, buf) == -1) {
108 admonish (buf, "unable to rename %s to", dp);
114 /* If removal was successful, decrement message count */
115 unset_exists (mp, msgnum);
122 adios (NULL, "oops, mp->numsel should be 0");
124 /* Mark that the sequence information has changed */
125 mp->msgflags |= SEQMOD;