]>
diplodocus.org Git - nmh/blob - uip/packf.c
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>
19 * We allocate space for messages (msgs array)
20 * this number of elements at a time.
25 static struct swit switches
[] = {
39 static int md
= NOTOK
;
40 static int mbx_style
= MBOX_FORMAT
;
41 static int mapping
= 0;
47 main (int argc
, char **argv
)
49 int nummsgs
, maxmsgs
, fd
, msgnum
;
50 char *cp
, *maildir
, *msgnam
, *folder
= NULL
, buf
[BUFSIZ
];
51 char **argp
, **arguments
, **msgs
;
56 setlocale(LC_ALL
, "");
58 invo_name
= r1bindex (argv
[0], '/');
60 /* read user profile/context */
63 arguments
= getarguments (invo_name
, argc
, argv
, 1);
66 /* Allocate the initial space to record message
71 msgs
= (char **) mh_xmalloc ((size_t) (maxmsgs
* sizeof(*msgs
)));
76 while ((cp
= *argp
++)) {
78 switch (smatch (++cp
, switches
)) {
80 ambigsw (cp
, switches
);
83 adios (NULL
, "-%s unknown", cp
);
86 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
88 print_help (buf
, switches
, 1);
91 print_version(invo_name
);
96 adios (NULL
, "only one file at a time!");
97 if (!(file
= *argp
++) || *file
== '-')
98 adios (NULL
, "missing argument to %s", argp
[-2]);
102 mbx_style
= MBOX_FORMAT
;
106 mbx_style
= MMDF_FORMAT
;
111 if (*cp
== '+' || *cp
== '@') {
113 adios (NULL
, "only one folder at a time!");
114 folder
= path (cp
+ 1, *cp
== '+' ? TFOLDER
: TSUBCWF
);
117 * Check if we need to allocate more space
118 * for message name/ranges.
120 if (nummsgs
>= maxmsgs
) {
122 msgs
= (char **) mh_xrealloc (msgs
,
123 (size_t) (maxmsgs
* sizeof(*msgs
)));
125 msgs
[nummsgs
++] = cp
;
131 file
= path (file
, TFILE
);
134 * Check if file to be created (or appended to)
135 * exists. If not, ask for confirmation.
137 if (stat (file
, &st
) == NOTOK
) {
139 adios (file
, "error on file");
140 cp
= concat ("Create file \"", file
, "\"? ", NULL
);
146 if (!context_find ("path"))
147 free (path ("./", TFOLDER
));
149 /* default is to pack whole folder */
151 msgs
[nummsgs
++] = "all";
154 folder
= getfolder (1);
155 maildir
= m_maildir (folder
);
157 if (chdir (maildir
) == NOTOK
)
158 adios (maildir
, "unable to change directory to ");
160 /* read folder and create message structure */
161 if (!(mp
= folder_read (folder
)))
162 adios (NULL
, "unable to read folder %s", folder
);
164 /* check for empty folder */
166 adios (NULL
, "no messages in %s", folder
);
168 /* parse all the message ranges/sequences and set SELECTED */
169 for (msgnum
= 0; msgnum
< nummsgs
; msgnum
++)
170 if (!m_convert (mp
, msgs
[msgnum
]))
172 seq_setprev (mp
); /* set the previous-sequence */
174 /* open and lock new maildrop file */
175 if ((md
= mbx_open(file
, mbx_style
, getuid(), getgid(), m_gmprot())) == NOTOK
)
176 adios (file
, "unable to open");
178 /* copy all the SELECTED messages to the file */
179 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
180 if (is_selected(mp
, msgnum
)) {
181 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
182 admonish (msgnam
, "unable to read message");
186 if (mbx_copy (file
, mbx_style
, md
, fd
, mapping
, NULL
, 1) == NOTOK
)
187 adios (file
, "error writing to file");
192 /* close and unlock maildrop file */
193 mbx_close (file
, md
);
195 context_replace (pfolder
, folder
); /* update current folder */
196 if (mp
->hghsel
!= mp
->curmsg
)
197 seq_setcur (mp
, mp
->lowsel
);
199 context_save (); /* save the context file */
200 folder_free (mp
); /* free folder/message structure */
207 mbx_close (file
, md
);
209 return 1; /* dead code to satisfy the compiler */