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[] = {
25 static int md = NOTOK;
27 static void mbxclose_done(int) NORETURN;
33 main(int argc, char **argv)
36 char *cp, *maildir, *msgnam, *folder = NULL, buf[BUFSIZ];
37 char **argp, **arguments;
38 struct msgs_array msgs = { 0, 0, NULL };
45 setlocale(LC_ALL, "");
47 invo_name = mhbasename(argv[0]);
49 /* read user profile/context */
52 arguments = getarguments(invo_name, argc, argv, 1);
58 while ((cp = *argp++)) {
60 switch (smatch(++cp, switches)) {
62 ambigsw(cp, switches);
65 adios(NULL, "-%s unknown", cp);
68 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
69 print_help(buf, switches, 1);
72 print_version(invo_name);
77 adios(NULL, "only one file at a time!");
78 if (!(file = *argp++) || *file == '-')
79 adios(NULL, "missing argument to %s",
84 if (*cp == '+' || *cp == '@') {
86 adios(NULL, "only one folder at a time!");
87 folder = getcpy(expandfol(cp));
89 app_msgarg(&msgs, cp);
94 file = getcpy(expanddir(file));
97 ** Check if file to be created (or appended to)
98 ** exists. If not, ask for confirmation.
100 if (stat (file, &st) == NOTOK) {
102 adios(file, "error on file");
103 cp = concat("Create file \"", file, "\"? ", NULL);
109 /* default is to pack whole folder */
111 app_msgarg(&msgs, seq_all);
114 folder = getcurfol();
115 maildir = toabsdir(folder);
117 if (chdir(maildir) == NOTOK)
118 adios(maildir, "unable to change directory to ");
120 /* read folder and create message structure */
121 if (!(mp = folder_read(folder)))
122 adios(NULL, "unable to read folder %s", folder);
124 /* check for empty folder */
126 adios(NULL, "no messages in %s", folder);
128 /* parse all the message ranges/sequences and set SELECTED */
129 for (msgnum = 0; msgnum < msgs.size; msgnum++)
130 if (!m_convert(mp, msgs.msgs[msgnum]))
132 seq_setprev(mp); /* set the previous-sequence */
134 /* open and lock new maildrop file */
135 if ((md = mbx_open(file, getuid(), getgid(), m_gmprot()))
137 adios(file, "unable to open");
139 /* copy all the SELECTED messages to the file */
140 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
141 if (is_selected(mp, msgnum)) {
142 if ((fd = open(msgnam = m_name(msgnum), O_RDONLY))
144 admonish(msgnam, "unable to read message");
148 if (mbx_copy(file, md, fd, 1) == NOTOK)
149 adios(file, "error writing to file");
154 /* close and unlock maildrop file */
157 context_replace(curfolder, folder); /* update current folder */
158 if (mp->hghsel != mp->curmsg)
159 seq_setcur(mp, mp->lowsel);
161 context_save(); /* save the context file */
162 folder_free(mp); /* free folder/message structure */
168 mbxclose_done(int status)