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)
28 int msgnum, vecp, retval = 0;
29 char buf[100], *dp, **vec;
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;
44 if (mp->numsel > MAXARGS - 2)
45 adios (NULL, "more than %d messages for %s exec", MAXARGS - 2,
47 vec = (char **) calloc ((size_t) (mp->numsel + 2), sizeof(*vec));
49 adios (NULL, "unable to allocate exec vector");
51 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
52 if (is_selected (mp, msgnum) &&
53 !(vec[vecp++] = strdup (m_name (msgnum))))
54 adios (NULL, "strdup failed");
59 vec[0] = r1bindex (rmmproc, '/');
61 switch (pid = vfork()) {
63 advise ("fork", "unable to");
67 execvp (rmmproc, vec);
68 fprintf (stderr, "unable to exec ");
73 return (pidwait (pid, -1));
78 * Either unlink or rename the SELECTED messages
80 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
81 if (is_selected (mp, msgnum)) {
82 /* unselect message */
83 unset_selected (mp, msgnum);
89 /* just unlink the messages */
90 if (unlink (dp) == -1) {
91 admonish (dp, "unable to unlink");
96 /* or rename messages with standard prefix */
97 strncpy (buf, m_backup (dp), sizeof(buf));
98 if (rename (dp, buf) == -1) {
99 admonish (buf, "unable to rename %s to", dp);
105 /* If removal was successful, decrement message count */
106 unset_exists (mp, msgnum);
113 adios (NULL, "oops, mp->numsel should be 0");
115 /* Mark that the sequence information has changed */
116 mp->msgflags |= SEQMOD;