3 * folder_delmsgs.c -- "remove" SELECTED messages from a folder
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
15 * 1) If we are using an external rmmproc, then exec it.
16 * 2) Else if unlink_msgs is non-zero, then unlink the
18 * 3) Else rename SELECTED messages by prefixing name
19 * with a standard prefix.
21 * If there is an error, return -1, else return 0.
25 folder_delmsgs (struct msgs *mp, int unlink_msgs, int nohook)
28 int msgnum, vecp, retval = 0;
29 char buf[100], *dp, **vec;
33 * If "rmmproc" is defined, exec it to remove messages.
36 /* Unset the EXISTS flag for each message to be removed */
37 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
38 if (is_selected (mp, msgnum))
39 unset_exists (mp, msgnum);
42 /* Mark that the sequence information has changed */
43 mp->msgflags |= SEQMOD;
45 if (mp->numsel > MAXARGS - 2)
46 adios (NULL, "more than %d messages for %s exec", MAXARGS - 2,
48 vec = (char **) calloc ((size_t) (mp->numsel + 2), sizeof(*vec));
50 adios (NULL, "unable to allocate exec vector");
52 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
53 if (is_selected (mp, msgnum) &&
54 !(vec[vecp++] = strdup (m_name (msgnum))))
55 adios (NULL, "strdup failed");
60 vec[0] = r1bindex (rmmproc, '/');
62 switch (pid = vfork()) {
64 advise ("fork", "unable to");
68 execvp (rmmproc, vec);
69 fprintf (stderr, "unable to exec ");
74 return (pidwait (pid, -1));
79 * Either unlink or rename the SELECTED messages
81 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
82 if (is_selected (mp, msgnum)) {
83 /* unselect message */
84 unset_selected (mp, msgnum);
88 * Run the external hook on the message if one was specified in the context.
89 * All we have is the message number; we have changed to the directory
90 * containing the message. So, we need to extract that directory to form
91 * the complete path. Note that the caller knows the directory, but has
92 * no way of passing that to us.
96 (void)snprintf(msgpath, sizeof (msgpath), "%s/%d", getcwd(msgpath, sizeof (msgpath)), msgnum);
97 (void)ext_hook("del-hook", msgpath, (char *)0);
100 dp = m_name (msgnum);
103 /* just unlink the messages */
104 if (unlink (dp) == -1) {
105 admonish (dp, "unable to unlink");
110 /* or rename messages with standard prefix */
111 strncpy (buf, m_backup (dp), sizeof(buf));
112 if (rename (dp, buf) == -1) {
113 admonish (buf, "unable to rename %s to", dp);
119 /* If removal was successful, decrement message count */
120 unset_exists (mp, msgnum);
127 adios (NULL, "oops, mp->numsel should be 0");
129 /* Mark that the sequence information has changed */
130 mp->msgflags |= SEQMOD;