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 };
33 setlocale(LC_ALL, "");
34 invo_name = mhbasename(argv[0]);
36 /* read user profile/context */
39 arguments = getarguments(invo_name, argc, argv, 1);
45 while ((cp = *argp++)) {
47 switch (smatch(++cp, switches)) {
49 ambigsw(cp, switches);
52 adios(NULL, "-%s unknown", cp);
55 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
56 print_help(buf, switches, 1);
59 print_version(invo_name);
63 if (*cp == '+' || *cp == '@') {
65 adios(NULL, "only one folder at a time!");
66 folder = getcpy(expandfol(cp));
68 app_msgarg(&msgs, cp);
71 /* default is to pack whole folder */
73 app_msgarg(&msgs, seq_all);
77 maildir = toabsdir(folder);
79 if (chdir(maildir) == NOTOK)
80 adios(maildir, "unable to change directory to ");
82 /* read folder and create message structure */
83 if (!(mp = folder_read(folder)))
84 adios(NULL, "unable to read folder %s", folder);
86 /* check for empty folder */
88 adios(NULL, "no messages in %s", folder);
90 /* parse all the message ranges/sequences and set SELECTED */
91 for (msgnum = 0; msgnum < msgs.size; msgnum++)
92 if (!m_convert(mp, msgs.msgs[msgnum]))
94 seq_setprev(mp); /* set the previous-sequence */
96 /* copy all the SELECTED messages to stdout */
97 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
98 if (is_selected(mp, msgnum)) {
99 if ((fd = open(msgnam = m_name(msgnum), O_RDONLY))
101 admonish(msgnam, "unable to read message");
104 if (mbox_copy(fileno(stdout), fd) == NOTOK) {
105 adios(NULL, "error writing to stdout");
110 context_replace(curfolder, folder);
111 if (mp->hghsel != mp->curmsg)
112 seq_setcur(mp, mp->lowsel);