]>
diplodocus.org Git - nmh/blob - uip/packf.c
3 * packf.c -- pack a nmh folder into a file
10 #include <h/dropsbr.h>
14 * We allocate space for messages (msgs array)
15 * this number of elements at a time.
20 static struct swit switches
[] = {
36 static int md
= NOTOK
;
37 static int mbx_style
= MBOX_FORMAT
;
38 static int mapping
= 0;
44 main (int argc
, char **argv
)
46 int nummsgs
, maxmsgs
, fd
, msgnum
;
47 char *cp
, *maildir
, *msgnam
, *folder
= NULL
, buf
[BUFSIZ
];
48 char **argp
, **arguments
, **msgs
;
53 setlocale(LC_ALL
, "");
55 invo_name
= r1bindex (argv
[0], '/');
57 /* read user profile/context */
60 arguments
= getarguments (invo_name
, argc
, argv
, 1);
63 /* Allocate the initial space to record message
68 if (!(msgs
= (char **) malloc ((size_t) (maxmsgs
* sizeof(*msgs
)))))
69 adios (NULL
, "unable to allocate storage");
74 while ((cp
= *argp
++)) {
76 switch (smatch (++cp
, switches
)) {
78 ambigsw (cp
, switches
);
81 adios (NULL
, "-%s unknown", cp
);
84 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
86 print_help (buf
, switches
, 1);
89 print_version(invo_name
);
94 adios (NULL
, "only one file at a time!");
95 if (!(file
= *argp
++) || *file
== '-')
96 adios (NULL
, "missing argument to %s", argp
[-2]);
100 mbx_style
= MBOX_FORMAT
;
104 mbx_style
= MMDF_FORMAT
;
109 if (*cp
== '+' || *cp
== '@') {
111 adios (NULL
, "only one folder at a time!");
112 folder
= path (cp
+ 1, *cp
== '+' ? TFOLDER
: TSUBCWF
);
115 * Check if we need to allocate more space
116 * for message name/ranges.
118 if (nummsgs
>= maxmsgs
) {
120 if (!(msgs
= (char **) realloc (msgs
,
121 (size_t) (maxmsgs
* sizeof(*msgs
)))))
122 adios (NULL
, "unable to reallocate msgs storage");
124 msgs
[nummsgs
++] = cp
;
130 file
= path (file
, TFILE
);
133 * Check if file to be created (or appended to)
134 * exists. If not, ask for confirmation.
136 if (stat (file
, &st
) == NOTOK
) {
138 adios (file
, "error on file");
139 cp
= concat ("Create file \"", file
, "\"? ", NULL
);
145 if (!context_find ("path"))
146 free (path ("./", TFOLDER
));
148 /* default is to pack whole folder */
150 msgs
[nummsgs
++] = "all";
153 folder
= getfolder (1);
154 maildir
= m_maildir (folder
);
156 if (chdir (maildir
) == NOTOK
)
157 adios (maildir
, "unable to change directory to ");
159 /* read folder and create message structure */
160 if (!(mp
= folder_read (folder
)))
161 adios (NULL
, "unable to read folder %s", folder
);
163 /* check for empty folder */
165 adios (NULL
, "no messages in %s", folder
);
167 /* parse all the message ranges/sequences and set SELECTED */
168 for (msgnum
= 0; msgnum
< nummsgs
; msgnum
++)
169 if (!m_convert (mp
, msgs
[msgnum
]))
171 seq_setprev (mp
); /* set the previous-sequence */
173 /* open and lock new maildrop file */
174 if ((md
= mbx_open(file
, mbx_style
, getuid(), getgid(), m_gmprot())) == NOTOK
)
175 adios (file
, "unable to open");
177 /* copy all the SELECTED messages to the file */
178 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
179 if (is_selected(mp
, msgnum
)) {
180 if ((fd
= open (msgnam
= m_name (msgnum
), O_RDONLY
)) == NOTOK
) {
181 admonish (msgnam
, "unable to read message");
185 if (mbx_copy (file
, mbx_style
, md
, fd
, mapping
, NULL
, 1) == NOTOK
)
186 adios (file
, "error writing to file");
191 /* close and unlock maildrop file */
192 mbx_close (file
, md
);
194 context_replace (pfolder
, folder
); /* update current folder */
195 if (mp
->hghsel
!= mp
->curmsg
)
196 seq_setcur (mp
, mp
->lowsel
);
198 context_save (); /* save the context file */
199 folder_free (mp
); /* free folder/message structure */
206 mbx_close (file
, md
);
208 return 1; /* dead code to satisfy the compiler */