2 ** packf.c -- pack a nmh folder into a file
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.
11 #include <h/dropsbr.h>
15 static struct swit switches[] = {
29 static int md = NOTOK;
30 static int mbx_style = MBOX_FORMAT;
31 static int mapping = 0;
33 static void mbxclose_done(int) NORETURN;
39 main(int argc, char **argv)
42 char *cp, *maildir, *msgnam, *folder = NULL, buf[BUFSIZ];
43 char **argp, **arguments;
44 struct msgs_array msgs = { 0, 0, NULL };
51 setlocale(LC_ALL, "");
53 invo_name = mhbasename(argv[0]);
55 /* read user profile/context */
58 arguments = getarguments(invo_name, argc, argv, 1);
64 while ((cp = *argp++)) {
66 switch (smatch(++cp, switches)) {
68 ambigsw(cp, switches);
71 adios(NULL, "-%s unknown", cp);
74 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
75 print_help(buf, switches, 1);
78 print_version(invo_name);
83 adios(NULL, "only one file at a time!");
84 if (!(file = *argp++) || *file == '-')
85 adios(NULL, "missing argument to %s", argp[-2]);
89 mbx_style = MBOX_FORMAT;
93 mbx_style = MMDF_FORMAT;
98 if (*cp == '+' || *cp == '@') {
100 adios(NULL, "only one folder at a time!");
101 folder = getcpy(expandfol(cp));
103 app_msgarg(&msgs, cp);
108 file = getcpy(expanddir(file));
111 ** Check if file to be created (or appended to)
112 ** exists. If not, ask for confirmation.
114 if (stat (file, &st) == NOTOK) {
116 adios(file, "error on file");
117 cp = concat("Create file \"", file, "\"? ", NULL);
123 /* default is to pack whole folder */
125 app_msgarg(&msgs, "all");
128 folder = getcurfol();
129 maildir = toabsdir(folder);
131 if (chdir(maildir) == NOTOK)
132 adios(maildir, "unable to change directory to ");
134 /* read folder and create message structure */
135 if (!(mp = folder_read(folder)))
136 adios(NULL, "unable to read folder %s", folder);
138 /* check for empty folder */
140 adios(NULL, "no messages in %s", folder);
142 /* parse all the message ranges/sequences and set SELECTED */
143 for (msgnum = 0; msgnum < msgs.size; msgnum++)
144 if (!m_convert(mp, msgs.msgs[msgnum]))
146 seq_setprev(mp); /* set the previous-sequence */
148 /* open and lock new maildrop file */
149 if ((md = mbx_open(file, mbx_style, getuid(), getgid(), m_gmprot()))
151 adios(file, "unable to open");
153 /* copy all the SELECTED messages to the file */
154 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
155 if (is_selected(mp, msgnum)) {
156 if ((fd = open(msgnam = m_name(msgnum), O_RDONLY))
158 admonish(msgnam, "unable to read message");
162 if (mbx_copy(file, mbx_style, md, fd, mapping, NULL, 1)
164 adios(file, "error writing to file");
169 /* close and unlock maildrop file */
172 context_replace(curfolder, folder); /* update current folder */
173 if (mp->hghsel != mp->curmsg)
174 seq_setcur(mp, mp->lowsel);
176 context_save(); /* save the context file */
177 folder_free(mp); /* free folder/message structure */
183 mbxclose_done(int status)