]>
diplodocus.org Git - nmh/blob - uip/packf.c
3 * packf.c -- pack a nmh folder into a file
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/dropsbr.h>
16 static struct swit switches
[] = {
30 static int md
= NOTOK
;
31 static int mbx_style
= MBOX_FORMAT
;
32 static int mapping
= 0;
34 static void mbxclose_done(int) NORETURN
;
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
};
52 setlocale(LC_ALL
, "");
54 invo_name
= r1bindex (argv
[0], '/');
56 /* read user profile/context */
59 arguments
= getarguments (invo_name
, argc
, argv
, 1);
65 while ((cp
= *argp
++)) {
67 switch (smatch (++cp
, switches
)) {
69 ambigsw (cp
, switches
);
72 adios (NULL
, "-%s unknown", cp
);
75 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
77 print_help (buf
, switches
, 1);
80 print_version(invo_name
);
85 adios (NULL
, "only one file at a time!");
86 if (!(file
= *argp
++) || *file
== '-')
87 adios (NULL
, "missing argument to %s", argp
[-2]);
91 mbx_style
= MBOX_FORMAT
;
95 mbx_style
= MMDF_FORMAT
;
100 if (*cp
== '+' || *cp
== '@') {
102 adios (NULL
, "only one folder at a time!");
103 folder
= pluspath (cp
);
105 app_msgarg(&msgs
, cp
);
110 file
= path (file
, TFILE
);
113 * Check if file to be created (or appended to)
114 * exists. If not, ask for confirmation.
116 if (stat (file
, &st
) == NOTOK
) {
118 adios (file
, "error on file");
119 cp
= concat ("Create file \"", file
, "\"? ", NULL
);
125 if (!context_find ("path"))
126 free (path ("./", TFOLDER
));
128 /* default is to pack whole folder */
130 app_msgarg(&msgs
, "all");
133 folder
= getfolder (1);
134 maildir
= m_maildir (folder
);
136 if (chdir (maildir
) == NOTOK
)
137 adios (maildir
, "unable to change directory to ");
139 /* read folder and create message structure */
140 if (!(mp
= folder_read (folder
)))
141 adios (NULL
, "unable to read folder %s", folder
);
143 /* check for empty folder */
145 adios (NULL
, "no messages in %s", folder
);
147 /* parse all the message ranges/sequences and set SELECTED */
148 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
149 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
151 seq_setprev (mp
); /* set the previous-sequence */
153 /* open and lock new maildrop file */
154 if ((md
= mbx_open(file
, mbx_style
, getuid(), getgid(), m_gmprot())) == NOTOK
)
155 adios (file
, "unable to open");
157 /* copy all the SELECTED messages to the file */
158 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
159 if (is_selected(mp
, msgnum
)) {
160 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
161 admonish (msgnam
, "unable to read message");
165 if (mbx_copy (file
, mbx_style
, md
, fd
, mapping
, NULL
, 1) == NOTOK
)
166 adios (file
, "error writing to file");
171 /* close and unlock maildrop file */
172 mbx_close (file
, md
);
174 context_replace (pfolder
, folder
); /* update current folder */
175 if (mp
->hghsel
!= mp
->curmsg
)
176 seq_setcur (mp
, mp
->lowsel
);
178 context_save (); /* save the context file */
179 folder_free (mp
); /* free folder/message structure */
185 mbxclose_done (int status
)
187 mbx_close (file
, md
);