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 main(int argc, char **argv)
28 char *cp, *maildir, *msgnam, *folder = NULL, buf[BUFSIZ];
29 char **argp, **arguments;
30 struct msgs_array msgs = { 0, 0, NULL };
34 setlocale(LC_ALL, "");
36 invo_name = mhbasename(argv[0]);
38 /* read user profile/context */
41 arguments = getarguments(invo_name, argc, argv, 1);
47 while ((cp = *argp++)) {
49 switch (smatch(++cp, switches)) {
51 ambigsw(cp, switches);
54 adios(NULL, "-%s unknown", cp);
57 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
58 print_help(buf, switches, 1);
61 print_version(invo_name);
65 if (*cp == '+' || *cp == '@') {
67 adios(NULL, "only one folder at a time!");
68 folder = getcpy(expandfol(cp));
70 app_msgarg(&msgs, cp);
73 /* default is to pack whole folder */
75 app_msgarg(&msgs, seq_all);
79 maildir = toabsdir(folder);
81 if (chdir(maildir) == NOTOK)
82 adios(maildir, "unable to change directory to ");
84 /* read folder and create message structure */
85 if (!(mp = folder_read(folder)))
86 adios(NULL, "unable to read folder %s", folder);
88 /* check for empty folder */
90 adios(NULL, "no messages in %s", folder);
92 /* parse all the message ranges/sequences and set SELECTED */
93 for (msgnum = 0; msgnum < msgs.size; msgnum++)
94 if (!m_convert(mp, msgs.msgs[msgnum]))
96 seq_setprev(mp); /* set the previous-sequence */
98 /* copy all the SELECTED messages to stdout */
99 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
100 if (is_selected(mp, msgnum)) {
101 if ((fd = open(msgnam = m_name(msgnum), O_RDONLY))
103 admonish(msgnam, "unable to read message");
106 if (mbox_copy(fileno(stdout), fd) == NOTOK) {
107 adios(NULL, "error writing to stdout");
112 context_replace(curfolder, folder);
113 if (mp->hghsel != mp->curmsg)
114 seq_setcur(mp, mp->lowsel);