Improved pluspath() and used it instead of path() where appropriate.
[mmh] / sbr / m_draft.c
1 /*
2 ** m_draft.c -- construct the name of a draft message
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <h/utils.h>
11 #include <errno.h>
12
13
14 /*
15 **  `which' should either be "cur" to use the current draft
16 **  or "new" to start with a new draft.
17 */
18 char *
19 m_draft(char *which)
20 {
21         register char *cp;
22         register struct msgs *mp;
23         static char buffer[BUFSIZ];
24         char *folder;
25
26         cp = draftfolder;
27         folder = pluspath(cp);
28
29         chdir(m_maildir(""));
30         strncpy(buffer, m_maildir(folder), sizeof(buffer));
31
32         create_folder(buffer, 0, done);
33
34         if (chdir(buffer) == -1)
35                 adios(buffer, "unable to change directory to");
36
37         if (!(mp = folder_read(folder)))
38                 adios(NULL, "unable to read folder %s", folder);
39
40         /*
41         ** Make sure we have enough message status space for all
42         ** the message numbers from 1 to "new", since we might
43         ** select an empty slot.  If we add more space at the
44         ** end, go ahead and add 10 additional slots.
45         */
46         if (mp->hghmsg >= mp->hghoff) {
47                 if (!(mp = folder_realloc(mp, 1, mp->hghmsg + 10)))
48                         adios(NULL, "unable to allocate folder storage");
49         } else if (mp->lowoff > 1) {
50                 if (!(mp = folder_realloc(mp, 1, mp->hghoff)))
51                         adios(NULL, "unable to allocate folder storage");
52         }
53
54         mp->msgflags |= ALLOW_NEW;  /* allow the "new" sequence */
55
56         /*
57         ** The draft message name to return is defined by `which'.
58         ** Usually it is "cur" (for the current draft) or "new"
59         ** (to start a new draft).
60         */
61         if (!m_convert(mp, which))
62                 done(1);
63         seq_setprev(mp);
64
65         snprintf(buffer, sizeof(buffer), "%s/%s", mp->foldpath,
66                         m_name(mp->lowsel));
67         seq_setcur(mp, mp->lowsel);
68         seq_save(mp);
69         folder_free(mp);
70
71         return buffer;
72 }