3 * packf.c -- pack a nmh folder into a file
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.
14 #include <h/dropsbr.h>
18 * We allocate space for messages (msgs array)
19 * this number of elements at a time.
24 static struct swit switches[] = {
40 static int md = NOTOK;
41 static int mbx_style = MBOX_FORMAT;
42 static int mapping = 0;
48 main (int argc, char **argv)
50 int nummsgs, maxmsgs, fd, msgnum;
51 char *cp, *maildir, *msgnam, *folder = NULL, buf[BUFSIZ];
52 char **argp, **arguments, **msgs;
57 setlocale(LC_ALL, "");
59 invo_name = r1bindex (argv[0], '/');
61 /* read user profile/context */
64 arguments = getarguments (invo_name, argc, argv, 1);
67 /* Allocate the initial space to record message
72 if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs)))))
73 adios (NULL, "unable to allocate storage");
78 while ((cp = *argp++)) {
80 switch (smatch (++cp, switches)) {
82 ambigsw (cp, switches);
85 adios (NULL, "-%s unknown", cp);
88 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
90 print_help (buf, switches, 1);
93 print_version(invo_name);
98 adios (NULL, "only one file at a time!");
99 if (!(file = *argp++) || *file == '-')
100 adios (NULL, "missing argument to %s", argp[-2]);
104 mbx_style = MBOX_FORMAT;
108 mbx_style = MMDF_FORMAT;
113 if (*cp == '+' || *cp == '@') {
115 adios (NULL, "only one folder at a time!");
116 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
119 * Check if we need to allocate more space
120 * for message name/ranges.
122 if (nummsgs >= maxmsgs) {
124 if (!(msgs = (char **) realloc (msgs,
125 (size_t) (maxmsgs * sizeof(*msgs)))))
126 adios (NULL, "unable to reallocate msgs storage");
128 msgs[nummsgs++] = cp;
134 file = path (file, TFILE);
137 * Check if file to be created (or appended to)
138 * exists. If not, ask for confirmation.
140 if (stat (file, &st) == NOTOK) {
142 adios (file, "error on file");
143 cp = concat ("Create file \"", file, "\"? ", NULL);
149 if (!context_find ("path"))
150 free (path ("./", TFOLDER));
152 /* default is to pack whole folder */
154 msgs[nummsgs++] = "all";
157 folder = getfolder (1);
158 maildir = m_maildir (folder);
160 if (chdir (maildir) == NOTOK)
161 adios (maildir, "unable to change directory to ");
163 /* read folder and create message structure */
164 if (!(mp = folder_read (folder)))
165 adios (NULL, "unable to read folder %s", folder);
167 /* check for empty folder */
169 adios (NULL, "no messages in %s", folder);
171 /* parse all the message ranges/sequences and set SELECTED */
172 for (msgnum = 0; msgnum < nummsgs; msgnum++)
173 if (!m_convert (mp, msgs[msgnum]))
175 seq_setprev (mp); /* set the previous-sequence */
177 /* open and lock new maildrop file */
178 if ((md = mbx_open(file, mbx_style, getuid(), getgid(), m_gmprot())) == NOTOK)
179 adios (file, "unable to open");
181 /* copy all the SELECTED messages to the file */
182 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
183 if (is_selected(mp, msgnum)) {
184 if ((fd = open (msgnam = m_name (msgnum), O_RDONLY)) == NOTOK) {
185 admonish (msgnam, "unable to read message");
189 if (mbx_copy (file, mbx_style, md, fd, mapping, NULL, 1) == NOTOK)
190 adios (file, "error writing to file");
195 /* close and unlock maildrop file */
196 mbx_close (file, md);
198 context_replace (pfolder, folder); /* update current folder */
199 if (mp->hghsel != mp->curmsg)
200 seq_setcur (mp, mp->lowsel);
202 context_save (); /* save the context file */
203 folder_free (mp); /* free folder/message structure */
210 mbx_close (file, md);
212 return 1; /* dead code to satisfy the compiler */