Fix out-of-bounds error when incorporating email from stdin
[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 #include <sysexits.h>
13
14
15 /*
16 **  `which' should either be the cur sequence to use the current draft
17 **  or the beyond sequence to start with a new draft.
18 */
19 char *
20 m_draft(char *which)
21 {
22         struct msgs *mp;
23         static char buffer[BUFSIZ];
24         char *folder;
25
26         folder = mh_xstrdup(toabsdir(draftfolder));
27         create_folder(folder, 0, exit);
28         if (!(mp = folder_read(folder))) {
29                 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
30         }
31         mh_free0(&folder);
32
33         /*
34         ** Make sure we have enough message status space for all
35         ** the message numbers from 1 to one beyond last, since we might
36         ** select an empty slot.  If we add more space at the
37         ** end, go ahead and add 10 additional slots.
38         */
39         if (mp->hghmsg >= mp->hghoff) {
40                 if (!(mp = folder_realloc(mp, 1, mp->hghmsg + 10)))
41                         adios(EX_OSERR, NULL, "unable to allocate folder storage");
42         } else if (mp->lowoff > 1) {
43                 if (!(mp = folder_realloc(mp, 1, mp->hghoff)))
44                         adios(EX_OSERR, NULL, "unable to allocate folder storage");
45         }
46
47         mp->msgflags |= ALLOW_BEYOND;  /* allow the beyond sequence */
48
49         /*
50         ** The draft message name to return is defined by `which'.
51         ** Usually it is seq_cur (for the current draft) or seq_beyond
52         ** (to start a new draft).
53         */
54         if (!m_convert(mp, which))
55                 exit(EX_SOFTWARE);
56         seq_setprev(mp);
57
58         snprintf(buffer, sizeof(buffer), "%s/%s", mp->foldpath,
59                         m_name(mp->lowsel));
60         seq_setcur(mp, mp->lowsel);
61         seq_save(mp);
62         folder_free(mp);
63
64         return buffer;
65 }