3 * packf.c -- pack a nmh folder into a file
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 #include <h/dropsbr.h>
18 static struct swit switches[] = {
32 static int md = NOTOK;
33 static int mbx_style = MBOX_FORMAT;
34 static int mapping = 0;
36 static void mbxclose_done(int) NORETURN;
42 main (int argc, char **argv)
45 char *cp, *maildir, *msgnam, *folder = NULL, buf[BUFSIZ];
46 char **argp, **arguments;
47 struct msgs_array msgs = { 0, 0, NULL };
54 setlocale(LC_ALL, "");
56 invo_name = r1bindex (argv[0], '/');
58 /* read user profile/context */
61 arguments = getarguments (invo_name, argc, argv, 1);
67 while ((cp = *argp++)) {
69 switch (smatch (++cp, switches)) {
71 ambigsw (cp, switches);
74 adios (NULL, "-%s unknown", cp);
77 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
79 print_help (buf, switches, 1);
82 print_version(invo_name);
87 adios (NULL, "only one file at a time!");
88 if (!(file = *argp++) || *file == '-')
89 adios (NULL, "missing argument to %s", argp[-2]);
93 mbx_style = MBOX_FORMAT;
97 mbx_style = MMDF_FORMAT;
102 if (*cp == '+' || *cp == '@') {
104 adios (NULL, "only one folder at a time!");
105 folder = pluspath (cp);
107 app_msgarg(&msgs, cp);
112 file = path (file, TFILE);
115 * Check if file to be created (or appended to)
116 * exists. If not, ask for confirmation.
118 if (stat (file, &st) == NOTOK) {
120 adios (file, "error on file");
121 cp = concat ("Create file \"", file, "\"? ", NULL);
127 if (!context_find ("path"))
128 free (path ("./", TFOLDER));
130 /* default is to pack whole folder */
132 app_msgarg(&msgs, "all");
135 folder = getfolder (1);
136 maildir = m_maildir (folder);
138 if (chdir (maildir) == NOTOK)
139 adios (maildir, "unable to change directory to ");
141 /* read folder and create message structure */
142 if (!(mp = folder_read (folder)))
143 adios (NULL, "unable to read folder %s", folder);
145 /* check for empty folder */
147 adios (NULL, "no messages in %s", folder);
149 /* parse all the message ranges/sequences and set SELECTED */
150 for (msgnum = 0; msgnum < msgs.size; msgnum++)
151 if (!m_convert (mp, msgs.msgs[msgnum]))
153 seq_setprev (mp); /* set the previous-sequence */
155 /* open and lock new maildrop file */
156 if ((md = mbx_open(file, mbx_style, getuid(), getgid(), m_gmprot())) == NOTOK)
157 adios (file, "unable to open");
159 /* copy all the SELECTED messages to the file */
160 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
161 if (is_selected(mp, msgnum)) {
162 if ((fd = open (msgnam = m_name (msgnum), O_RDONLY)) == NOTOK) {
163 admonish (msgnam, "unable to read message");
167 if (mbx_copy (file, mbx_style, md, fd, mapping, NULL, 1) == NOTOK)
168 adios (file, "error writing to file");
173 /* close and unlock maildrop file */
174 mbx_close (file, md);
176 context_replace (pfolder, folder); /* update current folder */
177 if (mp->hghsel != mp->curmsg)
178 seq_setcur (mp, mp->lowsel);
180 context_save (); /* save the context file */
181 folder_free (mp); /* free folder/message structure */
187 mbxclose_done (int status)
189 mbx_close (file, md);