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>
18 static struct swit switches[] = {
28 main(int argc, char **argv)
31 char *cp, *maildir, *msgnam, *folder = NULL, buf[BUFSIZ];
32 char **argp, **arguments;
33 struct msgs_array msgs = { 0, 0, NULL };
36 setlocale(LC_ALL, "");
37 invo_name = mhbasename(argv[0]);
39 /* read user profile/context */
42 arguments = getarguments(invo_name, argc, argv, 1);
48 while ((cp = *argp++)) {
50 switch (smatch(++cp, switches)) {
52 ambigsw(cp, switches);
55 adios(EX_USAGE, NULL, "-%s unknown", cp);
58 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
59 print_help(buf, switches, 1);
60 exit(argc == 2 ? EX_OK : EX_USAGE);
62 print_version(invo_name);
63 exit(argc == 2 ? EX_OK : EX_USAGE);
66 if (*cp == '+' || *cp == '@') {
68 adios(EX_USAGE, NULL, "only one folder at a time!");
69 folder = getcpy(expandfol(cp));
71 app_msgarg(&msgs, cp);
74 /* default is to pack whole folder */
76 app_msgarg(&msgs, seq_all);
80 maildir = toabsdir(folder);
82 if (chdir(maildir) == NOTOK)
83 adios(EX_OSERR, maildir, "unable to change directory to ");
85 /* read folder and create message structure */
86 if (!(mp = folder_read(folder)))
87 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
89 /* check for empty folder */
91 adios(EX_DATAERR, NULL, "no messages in %s", folder);
93 /* parse all the message ranges/sequences and set SELECTED */
94 for (msgnum = 0; msgnum < msgs.size; msgnum++)
95 if (!m_convert(mp, msgs.msgs[msgnum]))
97 seq_setprev(mp); /* set the previous-sequence */
99 /* copy all the SELECTED messages to stdout */
100 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
101 if (is_selected(mp, msgnum)) {
102 if ((fd = open(msgnam = m_name(msgnum), O_RDONLY))
104 admonish(msgnam, "unable to read message");
107 if (mbox_copy(fileno(stdout), fd) == NOTOK) {
108 adios(EX_IOERR, NULL, "error writing to stdout");
113 context_replace(curfolder, folder);
114 if (mp->hghsel != mp->curmsg)
115 seq_setcur(mp, mp->lowsel);