]>
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>
18 * We allocate space for messages (msgs array)
19 * this number of elements at a time.
24 static struct swit switches
[] = {
38 static int md
= NOTOK
;
39 static int mbx_style
= MBOX_FORMAT
;
40 static int mapping
= 0;
46 main (int argc
, char **argv
)
48 int nummsgs
, maxmsgs
, fd
, msgnum
;
49 char *cp
, *maildir
, *msgnam
, *folder
= NULL
, buf
[BUFSIZ
];
50 char **argp
, **arguments
, **msgs
;
55 setlocale(LC_ALL
, "");
57 invo_name
= r1bindex (argv
[0], '/');
59 /* read user profile/context */
62 arguments
= getarguments (invo_name
, argc
, argv
, 1);
65 /* Allocate the initial space to record message
70 if (!(msgs
= (char **) malloc ((size_t) (maxmsgs
* sizeof(*msgs
)))))
71 adios (NULL
, "unable to allocate storage");
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 if (!(msgs
= (char **) realloc (msgs
,
123 (size_t) (maxmsgs
* sizeof(*msgs
)))))
124 adios (NULL
, "unable to reallocate msgs storage");
126 msgs
[nummsgs
++] = cp
;
132 file
= path (file
, TFILE
);
135 * Check if file to be created (or appended to)
136 * exists. If not, ask for confirmation.
138 if (stat (file
, &st
) == NOTOK
) {
140 adios (file
, "error on file");
141 cp
= concat ("Create file \"", file
, "\"? ", NULL
);
147 if (!context_find ("path"))
148 free (path ("./", TFOLDER
));
150 /* default is to pack whole folder */
152 msgs
[nummsgs
++] = "all";
155 folder
= getfolder (1);
156 maildir
= m_maildir (folder
);
158 if (chdir (maildir
) == NOTOK
)
159 adios (maildir
, "unable to change directory to ");
161 /* read folder and create message structure */
162 if (!(mp
= folder_read (folder
)))
163 adios (NULL
, "unable to read folder %s", folder
);
165 /* check for empty folder */
167 adios (NULL
, "no messages in %s", folder
);
169 /* parse all the message ranges/sequences and set SELECTED */
170 for (msgnum
= 0; msgnum
< nummsgs
; msgnum
++)
171 if (!m_convert (mp
, msgs
[msgnum
]))
173 seq_setprev (mp
); /* set the previous-sequence */
175 /* open and lock new maildrop file */
176 if ((md
= mbx_open(file
, mbx_style
, getuid(), getgid(), m_gmprot())) == NOTOK
)
177 adios (file
, "unable to open");
179 /* copy all the SELECTED messages to the file */
180 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
181 if (is_selected(mp
, msgnum
)) {
182 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
183 admonish (msgnam
, "unable to read message");
187 if (mbx_copy (file
, mbx_style
, md
, fd
, mapping
, NULL
, 1) == NOTOK
)
188 adios (file
, "error writing to file");
193 /* close and unlock maildrop file */
194 mbx_close (file
, md
);
196 context_replace (pfolder
, folder
); /* update current folder */
197 if (mp
->hghsel
!= mp
->curmsg
)
198 seq_setcur (mp
, mp
->lowsel
);
200 context_save (); /* save the context file */
201 folder_free (mp
); /* free folder/message structure */
208 mbx_close (file
, md
);
210 return 1; /* dead code to satisfy the compiler */