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 = r1bindex (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]",
76 print_help (buf, switches, 1);
79 print_version(invo_name);
84 adios (NULL, "only one file at a time!");
85 if (!(file = *argp++) || *file == '-')
86 adios (NULL, "missing argument to %s", argp[-2]);
90 mbx_style = MBOX_FORMAT;
94 mbx_style = MMDF_FORMAT;
99 if (*cp == '+' || *cp == '@') {
101 adios (NULL, "only one folder at a time!");
102 folder = pluspath (cp);
104 app_msgarg(&msgs, cp);
109 file = path (file, TFILE);
112 * Check if file to be created (or appended to)
113 * exists. If not, ask for confirmation.
115 if (stat (file, &st) == NOTOK) {
117 adios (file, "error on file");
118 cp = concat ("Create file \"", file, "\"? ", NULL);
124 if (!context_find ("path"))
125 free (path ("./", TFOLDER));
127 /* default is to pack whole folder */
129 app_msgarg(&msgs, "all");
132 folder = getfolder (1);
133 maildir = m_maildir (folder);
135 if (chdir (maildir) == NOTOK)
136 adios (maildir, "unable to change directory to ");
138 /* read folder and create message structure */
139 if (!(mp = folder_read (folder)))
140 adios (NULL, "unable to read folder %s", folder);
142 /* check for empty folder */
144 adios (NULL, "no messages in %s", folder);
146 /* parse all the message ranges/sequences and set SELECTED */
147 for (msgnum = 0; msgnum < msgs.size; msgnum++)
148 if (!m_convert (mp, msgs.msgs[msgnum]))
150 seq_setprev (mp); /* set the previous-sequence */
152 /* open and lock new maildrop file */
153 if ((md = mbx_open(file, mbx_style, getuid(), getgid(), m_gmprot())) == NOTOK)
154 adios (file, "unable to open");
156 /* copy all the SELECTED messages to the file */
157 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
158 if (is_selected(mp, msgnum)) {
159 if ((fd = open (msgnam = m_name (msgnum), O_RDONLY)) == NOTOK) {
160 admonish (msgnam, "unable to read message");
164 if (mbx_copy (file, mbx_style, md, fd, mapping, NULL, 1) == NOTOK)
165 adios (file, "error writing to file");
170 /* close and unlock maildrop file */
171 mbx_close (file, md);
173 context_replace (pfolder, folder); /* update current folder */
174 if (mp->hghsel != mp->curmsg)
175 seq_setcur (mp, mp->lowsel);
177 context_save (); /* save the context file */
178 folder_free (mp); /* free folder/message structure */
184 mbxclose_done (int status)
186 mbx_close (file, md);