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;
40 main (int argc, char **argv)
43 char *cp, *maildir, *msgnam, *folder = NULL, buf[BUFSIZ];
44 char **argp, **arguments;
45 struct msgs_array msgs = { 0, 0, NULL };
50 setlocale(LC_ALL, "");
52 invo_name = r1bindex (argv[0], '/');
54 /* read user profile/context */
57 arguments = getarguments (invo_name, argc, argv, 1);
63 while ((cp = *argp++)) {
65 switch (smatch (++cp, switches)) {
67 ambigsw (cp, switches);
70 adios (NULL, "-%s unknown", cp);
73 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
75 print_help (buf, switches, 1);
78 print_version(invo_name);
83 adios (NULL, "only one file at a time!");
84 if (!(file = *argp++) || *file == '-')
85 adios (NULL, "missing argument to %s", argp[-2]);
89 mbx_style = MBOX_FORMAT;
93 mbx_style = MMDF_FORMAT;
98 if (*cp == '+' || *cp == '@') {
100 adios (NULL, "only one folder at a time!");
101 folder = pluspath (cp);
103 app_msgarg(&msgs, cp);
108 file = path (file, TFILE);
111 * Check if file to be created (or appended to)
112 * exists. If not, ask for confirmation.
114 if (stat (file, &st) == NOTOK) {
116 adios (file, "error on file");
117 cp = concat ("Create file \"", file, "\"? ", NULL);
123 if (!context_find ("path"))
124 free (path ("./", TFOLDER));
126 /* default is to pack whole folder */
128 app_msgarg(&msgs, "all");
131 folder = getfolder (1);
132 maildir = m_maildir (folder);
134 if (chdir (maildir) == NOTOK)
135 adios (maildir, "unable to change directory to ");
137 /* read folder and create message structure */
138 if (!(mp = folder_read (folder)))
139 adios (NULL, "unable to read folder %s", folder);
141 /* check for empty folder */
143 adios (NULL, "no messages in %s", folder);
145 /* parse all the message ranges/sequences and set SELECTED */
146 for (msgnum = 0; msgnum < msgs.size; msgnum++)
147 if (!m_convert (mp, msgs.msgs[msgnum]))
149 seq_setprev (mp); /* set the previous-sequence */
151 /* open and lock new maildrop file */
152 if ((md = mbx_open(file, mbx_style, getuid(), getgid(), m_gmprot())) == NOTOK)
153 adios (file, "unable to open");
155 /* copy all the SELECTED messages to the file */
156 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
157 if (is_selected(mp, msgnum)) {
158 if ((fd = open (msgnam = m_name (msgnum), O_RDONLY)) == NOTOK) {
159 admonish (msgnam, "unable to read message");
163 if (mbx_copy (file, mbx_style, md, fd, mapping, NULL, 1) == NOTOK)
164 adios (file, "error writing to file");
169 /* close and unlock maildrop file */
170 mbx_close (file, md);
172 context_replace (pfolder, folder); /* update current folder */
173 if (mp->hghsel != mp->curmsg)
174 seq_setcur (mp, mp->lowsel);
176 context_save (); /* save the context file */
177 folder_free (mp); /* free folder/message structure */
184 mbx_close (file, md);
186 return 1; /* dead code to satisfy the compiler */