2 * mhfixmsg.c -- rewrite a message with various tranformations
4 * This code is Copyright (c) 2002 and 2013, by the authors of nmh.
5 * See the COPYRIGHT file in the root directory of the nmh
6 * distribution for complete copyright information.
11 #include <h/mhparse.h>
13 #include <h/signals.h>
16 #define MHFIXMSG_SWITCHES \
17 X("decodetext 8bit|7bit", 0, DECODETEXTSW) \
18 X("nodecodetext", 0, NDECODETEXTSW) \
19 X("textcharset", 0, TEXTCHARSETSW) \
20 X("notextcharset", 0, NTEXTCHARSETSW) \
21 X("reformat", 0, REFORMATSW) \
22 X("noreformat", 0, NREFORMATSW) \
23 X("replacetextplain", 0, REPLACETEXTPLAINSW) \
24 X("noreplacetextplain", 0, NREPLACETEXTPLAINSW) \
25 X("fixboundary", 0, FIXBOUNDARYSW) \
26 X("nofixboundary", 0, NFIXBOUNDARYSW) \
27 X("fixcte", 0, FIXCTESW) \
28 X("nofixcte", 0, NFIXCTESW) \
29 X("file file", 0, FILESW) \
30 X("outfile file", 0, OUTFILESW) \
31 X("rmmproc program", 0, RPROCSW) \
32 X("normmproc", 0, NRPRCSW) \
33 X("verbose", 0, VERBSW) \
34 X("noverbose", 0, NVERBSW) \
35 X("version", 0, VERSIONSW) \
36 X("help", 0, HELPSW) \
38 #define X(sw, minchars, id) id,
39 DEFINE_SWITCH_ENUM(MHFIXMSG
);
42 #define X(sw, minchars, id) { sw, minchars, id },
43 DEFINE_SWITCH_ARRAY(MHFIXMSG
, switches
);
48 int debugsw
; /* Needed by mhparse.c. */
50 #define quitser pipeser
53 extern int skip_mp_cte_check
; /* flag to InitMultiPart */
54 extern int suppress_bogus_mp_content_warning
; /* flag to InitMultiPart */
55 extern int bogus_mp_content
; /* flag from InitMultiPart */
56 void reverse_parts (CT
);
59 int output_message (CT
, char *);
62 void flush_errors (void);
66 void freects_done (int) NORETURN
;
71 typedef struct fix_transformations
{
78 } fix_transformations
;
80 int mhfixmsgsbr (CT
*, const fix_transformations
*, char *);
81 static void reverse_alternative_parts (CT
);
82 static int fix_boundary (CT
*, int *);
83 static int get_multipart_boundary (CT
, char **);
84 static int replace_boundary (CT
, char *, char *);
85 static int fix_multipart_cte (CT
, int *);
86 static int set_ce (CT
, int);
87 static int ensure_text_plain (CT
*, CT
, int *, int);
88 static CT
build_text_plain_part (CT
);
89 static CT
divide_part (CT
);
90 static void copy_ctinfo (CI
, CI
);
91 static int decode_part (CT
);
92 static int reformat_part (CT
, char *, char *, char *, int);
93 static int charset_encoding (CT
);
94 static CT
build_multipart_alt (CT
, CT
, int, int);
95 static int boundary_in_content (FILE **, char *, const char *);
96 static void transfer_noncontent_headers (CT
, CT
);
97 static int set_ct_type (CT
, int type
, int subtype
, int encoding
);
98 static int decode_text_parts (CT
, int, int *);
99 static int content_encoding (CT
, const char **);
100 static int strip_crs (CT
, int *);
101 static int convert_charsets (CT
, char *, int *);
102 static int write_content (CT
, char *, char *, int, int);
103 static int remove_file (char *);
104 static void report (char *, char *, char *, char *, ...);
105 static void pipeser (int);
109 main (int argc
, char **argv
) {
111 char *cp
, *file
= NULL
, *folder
= NULL
;
112 char *maildir
, buf
[100], *outfile
= NULL
;
113 char **argp
, **arguments
;
114 struct msgs_array msgs
= { 0, 0, NULL
};
115 struct msgs
*mp
= NULL
;
120 fix_transformations fx
;
121 fx
.reformat
= fx
.fixcte
= fx
.fixboundary
= 1;
122 fx
.replacetextplain
= 0;
123 fx
.decodetext
= CE_8BIT
;
124 fx
.textcharset
= NULL
;
126 if (nmh_init(argv
[0], 1)) { return 1; }
130 arguments
= getarguments (invo_name
, argc
, argv
, 1);
136 while ((cp
= *argp
++)) {
138 switch (smatch (++cp
, switches
)) {
140 ambigsw (cp
, switches
);
143 adios (NULL
, "-%s unknown", cp
);
146 snprintf (buf
, sizeof buf
, "%s [+folder] [msgs] [switches]",
148 print_help (buf
, switches
, 1);
151 print_version(invo_name
);
155 if (! (cp
= *argp
++) || *cp
== '-')
156 adios (NULL
, "missing argument to %s", argp
[-2]);
157 if (! strcasecmp (cp
, "8bit")) {
158 fx
.decodetext
= CE_8BIT
;
159 } else if (! strcasecmp (cp
, "7bit")) {
160 fx
.decodetext
= CE_7BIT
;
162 adios (NULL
, "invalid argument to %s", argp
[-2]);
169 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
170 adios (NULL
, "missing argument to %s", argp
[-2]);
194 case REPLACETEXTPLAINSW
:
195 fx
.replacetextplain
= 1;
197 case NREPLACETEXTPLAINSW
:
198 fx
.replacetextplain
= 0;
201 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
202 adios (NULL
, "missing argument to %s", argp
[-2]);
203 file
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
206 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
207 adios (NULL
, "missing argument to %s", argp
[-2]);
208 outfile
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
211 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
212 adios (NULL
, "missing argument to %s", argp
[-2]);
225 if (*cp
== '+' || *cp
== '@') {
227 adios (NULL
, "only one folder at a time!");
229 folder
= pluspath (cp
);
232 /* Interpret a full path as a filename, not a message. */
233 file
= add (cp
, NULL
);
235 app_msgarg (&msgs
, cp
);
240 SIGNAL (SIGQUIT
, quitser
);
241 SIGNAL (SIGPIPE
, pipeser
);
244 * Read the standard profile setup
246 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
247 readconfig ((struct node
**) 0, fp
, cp
, 0);
251 suppress_bogus_mp_content_warning
= skip_mp_cte_check
= 1;
253 if (! context_find ("path"))
254 free (path ("./", TFOLDER
));
256 if (file
&& msgs
.size
)
257 adios (NULL
, "cannot specify msg and file at same time!");
260 * check if message is coming from file
263 /* If file is stdin, create a tmp file name before parse_mime()
264 has a chance, because it might put in on a different
265 filesystem than the output file. Instead, put it in the
266 user's preferred tmp directory. */
269 if (! strcmp ("-", file
)) {
275 if ((cp
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
276 adios (NULL
, "unable to create temporary file in %s",
280 file
= add (cp
, NULL
);
281 cpydata (STDIN_FILENO
, fd
, "-", file
);
285 (void) m_unlink (file
);
286 adios (NULL
, "failed to write temporary file");
290 if (! (cts
= (CT
*) calloc ((size_t) 2, sizeof *cts
)))
291 adios (NULL
, "out of memory");
294 if ((ct
= parse_mime (file
))) *ctp
++ = ct
;
297 * message(s) are coming from a folder
302 app_msgarg(&msgs
, "cur");
304 folder
= getfolder (1);
305 maildir
= m_maildir (folder
);
307 if (chdir (maildir
) == NOTOK
)
308 adios (maildir
, "unable to change directory to");
310 /* read folder and create message structure */
311 if (! (mp
= folder_read (folder
, 1)))
312 adios (NULL
, "unable to read folder %s", folder
);
314 /* check for empty folder */
316 adios (NULL
, "no messages in %s", folder
);
318 /* parse all the message ranges/sequences and set SELECTED */
319 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
320 if (! m_convert (mp
, msgs
.msgs
[msgnum
]))
322 seq_setprev (mp
); /* set the previous-sequence */
324 if (! (cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof *cts
)))
325 adios (NULL
, "out of memory");
328 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
329 if (is_selected(mp
, msgnum
)) {
332 msgnam
= m_name (msgnum
);
333 if ((ct
= parse_mime (msgnam
))) *ctp
++ = ct
;
337 seq_setcur (mp
, mp
->hghsel
); /* update current message */
338 seq_save (mp
); /* synchronize sequences */
339 context_replace (pfolder
, folder
);/* update current folder */
340 context_save (); /* save the context file */
344 for (ctp
= cts
; *ctp
; ++ctp
) {
345 status
+= mhfixmsgsbr (ctp
, &fx
, outfile
);
348 (void) m_unlink (file
);
351 /* Just calling m_backup() unlinks the backup file. */
352 (void) m_backup (file
);
363 /* done is freects_done, which will clean up all of cts. */
370 mhfixmsgsbr (CT
*ctp
, const fix_transformations
*fx
, char *outfile
) {
371 /* Store input filename in case one of the transformations, i.e.,
372 fix_boundary(), rewrites to a tmp file. */
373 char *input_filename
= add ((*ctp
)->c_file
, NULL
);
374 int modify_inplace
= 0;
375 int message_mods
= 0;
378 if (outfile
== NULL
) {
381 if ((*ctp
)->c_file
) {
383 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
384 adios (NULL
, "unable to create temporary file in %s",
387 outfile
= add (tempfile
, NULL
);
389 adios (NULL
, "missing both input and output filenames\n");
393 reverse_alternative_parts (*ctp
);
394 if (status
== OK
&& fx
->fixboundary
) {
395 status
= fix_boundary (ctp
, &message_mods
);
397 if (status
== OK
&& fx
->fixcte
) {
398 status
= fix_multipart_cte (*ctp
, &message_mods
);
400 if (status
== OK
&& fx
->reformat
) {
402 ensure_text_plain (ctp
, NULL
, &message_mods
, fx
->replacetextplain
);
404 if (status
== OK
&& fx
->decodetext
) {
405 status
= decode_text_parts (*ctp
, fx
->decodetext
, &message_mods
);
407 if (status
== OK
&& fx
->textcharset
!= NULL
) {
408 status
= convert_charsets (*ctp
, fx
->textcharset
, &message_mods
);
411 if (! (*ctp
)->c_umask
) {
412 /* Set the umask for the contents file. This currently
413 isn't used but just in case it is in the future. */
416 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
417 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
419 (*ctp
)->c_umask
= ~m_gmprot();
424 * Write the content to a file
427 status
= write_content (*ctp
, input_filename
, outfile
, modify_inplace
,
429 } else if (! modify_inplace
) {
430 /* Something went wrong. Output might be expected, such
431 as if this were run as a filter. Just copy the input
433 int in
= open (input_filename
, O_RDONLY
);
434 int out
= strcmp (outfile
, "-")
435 ? open (outfile
, O_WRONLY
| O_CREAT
, m_gmprot ())
438 if (in
!= -1 && out
!= -1) {
439 cpydata (in
, out
, input_filename
, outfile
);
448 if (modify_inplace
) {
449 if (status
!= OK
) (void) m_unlink (outfile
);
454 free (input_filename
);
460 /* parse_mime() arranges alternates in reverse (priority) order, so
461 reverse them back. This will put a text/plain part at the front of
462 a multipart/alternative part, for example, where it belongs. */
464 reverse_alternative_parts (CT ct
) {
465 if (ct
->c_type
== CT_MULTIPART
) {
466 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
469 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
473 /* And call recursively on each part of a multipart. */
474 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
475 reverse_alternative_parts (part
->mp_part
);
482 fix_boundary (CT
*ct
, int *message_mods
) {
483 struct multipart
*mp
;
486 if (bogus_mp_content
) {
487 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
490 * 1) Get boundary at end of part.
491 * 2) Get boundary at beginning of part and compare to the end-of-part
493 * 3) Write out contents of ct to tmp file, replacing boundary in
494 * header with boundary from part. Set c_unlink to 1.
496 * 5) Call parse_mime() on the tmp file, replacing ct.
499 if (mp
&& mp
->mp_start
) {
502 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
505 if ((fixed
= m_mktemp2 (NULL
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
506 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
507 char *filename
= add ((*ct
)->c_file
, NULL
);
510 if ((*ct
= parse_mime (fixed
))) {
515 report (NULL
, NULL
, filename
,
516 "fix multipart boundary");
521 advise (NULL
, "unable to replace broken boundary");
525 advise (NULL
, "unable to create temporary file in %s",
530 free (part_boundary
);
540 get_multipart_boundary (CT ct
, char **part_boundary
) {
542 char *end_boundary
= NULL
;
543 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
544 ? (off_t
) (ct
->c_end
- sizeof buffer
)
545 : (off_t
) ct
->c_begin
;
549 /* This will fail if the boundary spans fread() calls. BUFSIZ should
550 be big enough, even if it's just 1024, to make that unlikely. */
552 /* free_content() will close ct->c_fp. */
553 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
554 advise (ct
->c_file
, "unable to open for reading");
558 /* Get boundary at end of multipart. */
559 while (begin
>= (off_t
) ct
->c_begin
) {
560 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
561 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
562 char *cp
= rfind_str (buffer
, bytes_read
, "--");
567 /* Trim off trailing "--" and anything beyond. */
569 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
570 if (strlen (end
) > 3 && *end
++ == '\n' &&
571 *end
++ == '-' && *end
++ == '-') {
572 end_boundary
= add (end
, NULL
);
579 if (! end_boundary
&& begin
> (off_t
) (ct
->c_begin
+ sizeof buffer
)) {
580 begin
-= sizeof buffer
;
586 /* Get boundary at beginning of multipart. */
588 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
589 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
590 if (bytes_read
>= strlen (end_boundary
)) {
591 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
593 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
594 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
599 /* The start and end boundaries didn't match, or the
600 start boundary doesn't begin with "\n--" (or "--"
601 if at the beginning of buffer). Keep trying. */
610 *part_boundary
= end_boundary
;
612 *part_boundary
= NULL
;
620 /* Open and copy ct->c_file to file, replacing the multipart boundary. */
622 replace_boundary (CT ct
, char *file
, char *boundary
) {
625 char buf
[BUFSIZ
], name
[NAMESZ
];
627 m_getfld_state_t gstate
= 0;
630 if (ct
->c_file
== NULL
) {
631 advise (NULL
, "missing input filename");
635 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
636 advise (ct
->c_file
, "unable to open for reading");
640 if ((fpout
= fopen (file
, "w")) == NULL
) {
642 advise (file
, "unable to open for writing");
646 for (compnum
= 1;;) {
647 int bufsz
= (int) sizeof buf
;
649 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
)) {
654 /* get copies of the buffers */
655 np
= add (name
, NULL
);
656 vp
= add (buf
, NULL
);
658 /* if necessary, get rest of field */
659 while (state
== FLDPLUS
) {
661 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
);
662 vp
= add (buf
, vp
); /* add to previous value */
665 if (strcasecmp (TYPE_FIELD
, np
)) {
666 fprintf (fpout
, "%s:%s", np
, vp
);
668 char *new_ctline
, *new_params
;
670 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
671 &ct
->c_ctinfo
.ci_last_pm
, "boundary",
674 new_ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
675 ct
->c_ctinfo
.ci_subtype
, NULL
);
676 new_params
= output_params(strlen(TYPE_FIELD
) +
677 strlen(new_ctline
) + 1,
678 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
679 fprintf (fpout
, "%s:%s%s\n", np
, new_ctline
,
680 new_params
? new_params
: "");
693 /* buf will have a terminating NULL, skip it. */
694 fwrite (buf
, 1, bufsz
-1, fpout
);
702 advise (NULL
, "message format error in component #%d", compnum
);
707 advise (NULL
, "getfld() returned %d", state
);
715 m_getfld_state_destroy (&gstate
);
724 fix_multipart_cte (CT ct
, int *message_mods
) {
727 if (ct
->c_type
== CT_MULTIPART
) {
731 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
732 ct
->c_encoding
!= CE_BINARY
) {
735 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
736 char *name
= hf
->name
;
737 for (; *name
&& isspace ((unsigned char) *name
); ++name
) {
741 if (! strncasecmp (name
, ENCODING_FIELD
,
742 strlen (ENCODING_FIELD
))) {
743 char *prefix
= "Nmh-REPLACED-INVALID-";
744 HF h
= mh_xmalloc (sizeof *h
);
746 h
->name
= add (hf
->name
, NULL
);
747 h
->hf_encoding
= hf
->hf_encoding
;
751 /* Retain old header but prefix its name. */
753 hf
->name
= concat (prefix
, h
->name
, NULL
);
757 char *encoding
= cpytrim (hf
->value
);
758 report (NULL
, ct
->c_partno
, ct
->c_file
,
759 "replace Content-Transfer-Encoding of %s "
760 "with 8 bit", encoding
);
764 h
->value
= add (" 8bit\n", NULL
);
766 /* Don't need to warn for multiple C-T-E header
767 fields, parse_mime() already does that. But
768 if there are any, fix them all as necessary. */
773 set_ce (ct
, CE_8BIT
);
776 m
= (struct multipart
*) ct
->c_ctparams
;
777 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
778 if (fix_multipart_cte (part
->mp_part
, message_mods
) != OK
) {
790 set_ce (CT ct
, int encoding
) {
791 const char *ce
= ce_str (encoding
);
792 const struct str2init
*ctinit
= get_ce_method (ce
);
795 char *cte
= concat (" ", ce
, "\n", NULL
);
798 /* Decoded contents might be in ct->c_cefile.ce_file, if the
799 caller is decode_text_parts (). Save because we'll
801 struct cefile decoded_content_info
= ct
->c_cefile
;
803 ct
->c_encoding
= encoding
;
805 ct
->c_ctinitfnx
= ctinit
->si_init
;
806 /* This will assign ct->c_cefile with an all-0 struct, which
808 (*ctinit
->si_init
) (ct
);
809 /* After returning, the caller should set
810 ct->c_cefile.ce_file to the name of the file containing
813 /* Restore the cefile. */
814 ct
->c_cefile
= decoded_content_info
;
816 /* Update/add Content-Transfer-Encoding header field. */
817 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
818 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
825 add_header (ct
, add (ENCODING_FIELD
, NULL
), cte
);
828 /* Update c_celine. It's used only by mhlist -debug. */
830 ct
->c_celine
= add (cte
, NULL
);
839 /* Make sure each text part has a corresponding text/plain part. */
841 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
, int replacetextplain
) {
844 switch ((*ct
)->c_type
) {
846 int has_text_plain
= 0;
848 /* Nothing to do for text/plain. */
849 if ((*ct
)->c_subtype
== TEXT_PLAIN
) return OK
;
851 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
852 parent
->c_subtype
== MULTI_ALTERNATE
) {
853 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
854 struct part
*part
, *prev
;
855 int new_subpart_number
= 1;
857 /* See if there is a sibling text/plain. */
858 for (prev
= part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
859 ++new_subpart_number
;
860 if (part
->mp_part
->c_type
== CT_TEXT
&&
861 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
862 if (replacetextplain
) {
863 struct part
*old_part
;
864 if (part
== mp
->mp_parts
) {
865 old_part
= mp
->mp_parts
;
866 mp
->mp_parts
= part
->mp_next
;
868 old_part
= prev
->mp_next
;
869 prev
->mp_next
= part
->mp_next
;
872 report (NULL
, parent
->c_partno
, parent
->c_file
,
873 "remove text/plain part %s",
874 old_part
->mp_part
->c_partno
);
876 free_content (old_part
->mp_part
);
886 if (! has_text_plain
) {
887 /* Parent is a multipart/alternative. Insert a new
888 text/plain subpart. */
889 struct part
*new_part
= mh_xmalloc (sizeof *new_part
);
891 if ((new_part
->mp_part
= build_text_plain_part (*ct
))) {
893 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
895 new_part
->mp_next
= mp
->mp_parts
;
896 mp
->mp_parts
= new_part
;
897 new_part
->mp_part
->c_partno
=
898 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
903 report (NULL
, parent
->c_partno
, parent
->c_file
,
904 "insert text/plain part");
907 free_content (new_part
->mp_part
);
913 /* Slip new text/plain part into a new multipart/alternative. */
914 CT tp_part
= build_text_plain_part (*ct
);
917 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
920 struct multipart
*mp
=
921 (struct multipart
*) mp_alt
->c_ctparams
;
923 if (mp
&& mp
->mp_parts
) {
924 mp
->mp_parts
->mp_part
= tp_part
;
925 /* Make the new multipart/alternative the parent. */
930 report (NULL
, (*ct
)->c_partno
, (*ct
)->c_file
,
931 "insert text/plain part");
934 free_content (tp_part
);
935 free_content (mp_alt
);
949 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
952 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
953 if ((*ct
)->c_type
== CT_MULTIPART
) {
954 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
,
962 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
965 e
= (struct exbody
*) (*ct
)->c_ctparams
;
966 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
,
977 build_text_plain_part (CT encoded_part
) {
978 CT tp_part
= divide_part (encoded_part
);
979 char *tmp_plain_file
= NULL
;
981 if (decode_part (tp_part
) == OK
) {
982 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
983 contains the decoded contents. And the decoding function, such
984 as openQuoted, will have set ...->ce_unlink to 1 so that it will
985 be unlinked by free_content (). */
988 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
989 advise (NULL
, "unable to create temporary file in %s",
992 tmp_plain_file
= add (tempfile
, NULL
);
993 if (reformat_part (tp_part
, tmp_plain_file
,
994 tp_part
->c_ctinfo
.ci_type
,
995 tp_part
->c_ctinfo
.ci_subtype
,
996 tp_part
->c_type
) == OK
) {
1001 free_content (tp_part
);
1002 (void) m_unlink (tmp_plain_file
);
1003 free (tmp_plain_file
);
1010 divide_part (CT ct
) {
1013 if ((new_part
= (CT
) calloc (1, sizeof *new_part
)) == NULL
)
1014 adios (NULL
, "out of memory");
1016 /* Just copy over what is needed for decoding. c_vrsn and
1017 c_celine aren't necessary. */
1018 new_part
->c_file
= add (ct
->c_file
, NULL
);
1019 new_part
->c_begin
= ct
->c_begin
;
1020 new_part
->c_end
= ct
->c_end
;
1021 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1022 new_part
->c_type
= ct
->c_type
;
1023 new_part
->c_cefile
= ct
->c_cefile
;
1024 new_part
->c_encoding
= ct
->c_encoding
;
1025 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1026 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1027 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1028 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1030 /* c_ctline is used by reformat__part(), so it can preserve
1031 anything after the type/subtype. */
1032 new_part
->c_ctline
= add (ct
->c_ctline
, NULL
);
1039 copy_ctinfo (CI dest
, CI src
) {
1042 dest
->ci_type
= src
->ci_type
? add (src
->ci_type
, NULL
) : NULL
;
1043 dest
->ci_subtype
= src
->ci_subtype
? add (src
->ci_subtype
, NULL
) : NULL
;
1045 for (s_pm
= src
->ci_first_pm
; s_pm
; s_pm
= s_pm
->pm_next
) {
1046 d_pm
= add_param(&dest
->ci_first_pm
, &dest
->ci_last_pm
, s_pm
->pm_name
,
1048 if (s_pm
->pm_charset
)
1049 d_pm
->pm_charset
= getcpy(s_pm
->pm_charset
);
1051 d_pm
->pm_lang
= getcpy(s_pm
->pm_lang
);
1054 dest
->ci_comment
= src
->ci_comment
? add (src
->ci_comment
, NULL
) : NULL
;
1055 dest
->ci_magic
= src
->ci_magic
? add (src
->ci_magic
, NULL
) : NULL
;
1060 decode_part (CT ct
) {
1065 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1066 adios (NULL
, "unable to create temporary file in %s", get_temp_dir());
1068 tmp_decoded
= add (tempfile
, NULL
);
1069 /* The following call will load ct->c_cefile.ce_file with the tmp
1070 filename of the decoded content. tmp_decoded will contain the
1071 encoded output, get rid of that. */
1072 status
= output_message (ct
, tmp_decoded
);
1073 (void) m_unlink (tmp_decoded
);
1080 /* Some of the arguments aren't really needed now, but maybe will
1081 be in the future for other than text types. */
1083 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
) {
1084 int output_subtype
, output_encoding
;
1088 /* Hacky: this redirects the output from whatever command is used
1089 to show the part to a file. So, the user can't have any output
1090 redirection in that command.
1091 Could show_multi() in mhshowsbr.c avoid this? */
1093 /* Check for invo_name-format-type/subtype. */
1094 if ((cf
= context_find_by_type ("format", type
, subtype
)) == NULL
) {
1096 advise (NULL
, "Don't know how to convert %s, there is no "
1097 "%s-format-%s/%s profile entry",
1098 ct
->c_file
, invo_name
, type
, subtype
);
1102 if (strchr (cf
, '>')) {
1103 advise (NULL
, "'>' prohibited in \"%s\",\nplease fix your "
1104 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1105 subtype
? subtype
: "");
1111 cp
= concat (cf
, " >", file
, NULL
);
1112 status
= show_content_aux (ct
, 0, cp
, NULL
, NULL
);
1115 /* Unlink decoded content tmp file and free its filename to avoid
1116 leaks. The file stream should already have been closed. */
1117 if (ct
->c_cefile
.ce_unlink
) {
1118 (void) m_unlink (ct
->c_cefile
.ce_file
);
1119 free (ct
->c_cefile
.ce_file
);
1120 ct
->c_cefile
.ce_file
= NULL
;
1121 ct
->c_cefile
.ce_unlink
= 0;
1124 if (c_type
== CT_TEXT
) {
1125 output_subtype
= TEXT_PLAIN
;
1127 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1130 output_encoding
= charset_encoding (ct
);
1132 if (set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1133 ct
->c_cefile
.ce_file
= file
;
1134 ct
->c_cefile
.ce_unlink
= 1;
1136 ct
->c_cefile
.ce_unlink
= 0;
1144 /* Identifies 7bit or 8bit content based on charset. */
1146 charset_encoding (CT ct
) {
1148 strcasecmp (content_charset (ct
), "US-ASCII") ? CE_8BIT
: CE_7BIT
;
1155 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
) {
1156 char *boundary_prefix
= "----=_nmh-multipart";
1157 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1158 char *boundary_indicator
= "; boundary=";
1159 char *typename
, *subtypename
, *name
;
1162 struct multipart
*m
;
1163 const struct str2init
*ctinit
;
1165 if ((ct
= (CT
) calloc (1, sizeof *ct
)) == NULL
)
1166 adios (NULL
, "out of memory");
1168 /* Set up the multipart/alternative part. These fields of *ct were
1169 initialized to 0 by calloc():
1170 c_fp, c_unlink, c_begin, c_end,
1171 c_vrsn, c_ctline, c_celine,
1172 c_id, c_descr, c_dispo, c_partno,
1173 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1174 c_cefile, c_encoding,
1175 c_digested, c_digest[16], c_ctexbody,
1176 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1178 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1181 ct
->c_file
= add (first_alt
->c_file
, NULL
);
1183 ct
->c_subtype
= subtype
;
1185 ctinit
= get_ct_init (ct
->c_type
);
1187 typename
= ct_type_str (type
);
1188 subtypename
= ct_subtype_str (type
, subtype
);
1192 int found_boundary
= 1;
1194 while (found_boundary
&& serial
< 1000000) {
1197 /* Ensure that the boundary doesn't appear in the decoded
1199 if (new_part
->c_cefile
.ce_file
) {
1200 if ((found_boundary
=
1201 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1202 new_part
->c_cefile
.ce_file
,
1209 /* Ensure that the boundary doesn't appear in the encoded
1211 if (! found_boundary
&& new_part
->c_file
) {
1212 if ((found_boundary
= boundary_in_content (&new_part
->c_fp
,
1220 if (found_boundary
) {
1221 /* Try a slightly different boundary. */
1226 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1228 concat (boundary_prefix
,
1229 first_alt
->c_partno
? first_alt
->c_partno
: "",
1230 "-", buffer2
, NULL
);
1234 if (found_boundary
) {
1235 advise (NULL
, "giving up trying to find a unique boundary");
1241 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1242 boundary
, "\"", NULL
);
1244 /* Load c_first_hf and c_last_hf. */
1245 transfer_noncontent_headers (first_alt
, ct
);
1246 add_header (ct
, add (TYPE_FIELD
, NULL
), concat (name
, "\n", NULL
));
1249 /* Load c_partno. */
1250 if (first_alt
->c_partno
) {
1251 ct
->c_partno
= add (first_alt
->c_partno
, NULL
);
1252 free (first_alt
->c_partno
);
1253 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1254 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1256 first_alt
->c_partno
= add ("1", NULL
);
1257 new_part
->c_partno
= add ("2", NULL
);
1261 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1262 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1265 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1266 "boundary", boundary
, 0);
1268 p
= (struct part
*) mh_xmalloc (sizeof *p
);
1269 p
->mp_next
= (struct part
*) mh_xmalloc (sizeof *p
->mp_next
);
1270 p
->mp_next
->mp_next
= NULL
;
1271 p
->mp_next
->mp_part
= first_alt
;
1273 if ((m
= (struct multipart
*) calloc (1, sizeof (struct multipart
))) ==
1275 adios (NULL
, "out of memory");
1276 m
->mp_start
= concat (boundary
, "\n", NULL
);
1277 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1279 ct
->c_ctparams
= (void *) m
;
1287 /* Check that the boundary does not appear in the content. */
1289 boundary_in_content (FILE **fp
, char *file
, const char *boundary
) {
1290 char buffer
[BUFSIZ
];
1292 int found_boundary
= 0;
1294 /* free_content() will close *fp if we fopen it here. */
1295 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1296 advise (file
, "unable to open %s for reading", file
);
1300 fseeko (*fp
, 0L, SEEK_SET
);
1301 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1302 if (find_str (buffer
, bytes_read
, boundary
)) {
1308 return found_boundary
;
1312 /* Remove all non-Content headers. */
1314 transfer_noncontent_headers (CT old
, CT
new) {
1317 hp_prev
= hp
= old
->c_first_hf
;
1321 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, strlen (XXX_FIELD_PRF
))) {
1322 if (hp
== old
->c_last_hf
) {
1323 if (hp
== old
->c_first_hf
) {
1324 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1326 hp_prev
->next
= NULL
;
1327 old
->c_last_hf
= hp_prev
;
1330 if (hp
== old
->c_first_hf
) {
1331 old
->c_first_hf
= next
;
1333 hp_prev
->next
= next
;
1337 /* Put node hp in the new CT. */
1338 if (new->c_first_hf
== NULL
) {
1339 new->c_first_hf
= hp
;
1341 new->c_last_hf
->next
= hp
;
1343 new->c_last_hf
= hp
;
1345 /* A Content- header, leave in old. */
1355 set_ct_type (CT ct
, int type
, int subtype
, int encoding
) {
1356 char *typename
= ct_type_str (type
);
1357 char *subtypename
= ct_subtype_str (type
, subtype
);
1358 /* E.g, " text/plain" */
1359 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1360 /* E.g, " text/plain\n" */
1361 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1362 int found_content_type
= 0;
1364 const char *cp
= NULL
;
1368 /* Update/add Content-Type header field. */
1369 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1370 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1371 found_content_type
= 1;
1373 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1374 ? concat (type_subtypename
, cp
, "\n", NULL
)
1375 : add (name_plus_nl
, NULL
);
1378 if (! found_content_type
) {
1379 add_header (ct
, add (TYPE_FIELD
, NULL
),
1380 (cp
= strchr (ct
->c_ctline
, ';'))
1381 ? concat (type_subtypename
, cp
, "\n", NULL
)
1382 : add (name_plus_nl
, NULL
));
1385 /* Some of these might not be used, but set them anyway. */
1387 ? concat (type_subtypename
, cp
, NULL
)
1388 : concat (type_subtypename
, NULL
);
1389 free (ct
->c_ctline
);
1390 ct
->c_ctline
= ctline
;
1391 /* Leave other ctinfo members as they were. */
1392 free (ct
->c_ctinfo
.ci_type
);
1393 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1394 free (ct
->c_ctinfo
.ci_subtype
);
1395 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1397 ct
->c_subtype
= subtype
;
1399 free (name_plus_nl
);
1400 free (type_subtypename
);
1402 status
= set_ce (ct
, encoding
);
1409 decode_text_parts (CT ct
, int encoding
, int *message_mods
) {
1412 switch (ct
->c_type
) {
1414 switch (ct
->c_encoding
) {
1419 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
1420 const char *reason
= NULL
;
1422 if ((ct_encoding
= content_encoding (ct
, &reason
)) == CE_BINARY
1423 && encoding
!= CE_BINARY
) {
1424 /* The decoding isn't acceptable so discard it.
1425 Leave status as OK to allow other transformations. */
1427 report (NULL
, ct
->c_partno
, ct
->c_file
,
1428 "will not decode%s because it is binary (%s)",
1430 : ct
->c_ctline
? ct
->c_ctline
1434 (void) m_unlink (ct
->c_cefile
.ce_file
);
1435 free (ct
->c_cefile
.ce_file
);
1436 ct
->c_cefile
.ce_file
= NULL
;
1437 } else if (ct
->c_encoding
== CE_QUOTED
&&
1438 ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
1439 /* The decoding isn't acceptable so discard it.
1440 Leave status as OK to allow other transformations. */
1442 report (NULL
, ct
->c_partno
, ct
->c_file
,
1443 "will not decode%s because it is 8bit",
1445 : ct
->c_ctline
? ct
->c_ctline
1448 (void) m_unlink (ct
->c_cefile
.ce_file
);
1449 free (ct
->c_cefile
.ce_file
);
1450 ct
->c_cefile
.ce_file
= NULL
;
1453 if (ct_encoding
== CE_BINARY
)
1455 else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
)
1458 enc
= charset_encoding (ct
);
1459 if (set_ce (ct
, enc
) == OK
) {
1462 report (NULL
, ct
->c_partno
, ct
->c_file
, "decode%s",
1463 ct
->c_ctline
? ct
->c_ctline
: "");
1465 strip_crs (ct
, message_mods
);
1477 strip_crs (ct
, message_mods
);
1485 case CT_MULTIPART
: {
1486 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1489 /* Should check to see if the body for this part is encoded?
1490 For now, it gets passed along as-is by InitMultiPart(). */
1491 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1492 status
= decode_text_parts (part
->mp_part
, encoding
, message_mods
);
1498 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1501 e
= (struct exbody
*) ct
->c_ctparams
;
1502 status
= decode_text_parts (e
->eb_content
, encoding
, message_mods
);
1514 /* See if the decoded content is 7bit, 8bit, or binary. It's binary
1515 if it has any NUL characters, a CR not followed by a LF, or lines
1516 greater than 998 characters in length. If binary, reason is set
1517 to a string explaining why. */
1519 content_encoding (CT ct
, const char **reason
) {
1520 CE ce
= &ct
->c_cefile
;
1521 int encoding
= CE_7BIT
;
1524 size_t line_len
= 0;
1525 char buffer
[BUFSIZ
];
1528 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1529 advise (ce
->ce_file
, "unable to open for reading");
1533 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
1534 while (encoding
!= CE_BINARY
&&
1535 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
1538 int last_char_was_cr
= 0;
1540 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
1541 if (*cp
== '\0' || ++line_len
> 998 ||
1542 (*cp
!= '\n' && last_char_was_cr
)) {
1543 encoding
= CE_BINARY
;
1545 *reason
= "null character";
1546 } else if (line_len
> 998) {
1547 *reason
= "line length > 998";
1548 } else if (*cp
!= '\n' && last_char_was_cr
) {
1549 *reason
= "CR not followed by LF";
1551 /* Should not reach this. */
1555 } else if (*cp
== '\n') {
1557 } else if (! isascii ((unsigned char) *cp
)) {
1561 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
1567 } /* else should never happen */
1574 strip_crs (CT ct
, int *message_mods
) {
1575 char *charset
= content_charset (ct
);
1578 /* Only strip carriage returns if content is ASCII or another
1579 charset that has the same readily recognizable CR followed by a
1580 LF. We can include UTF-8 here because if the high-order bit of
1581 a UTF-8 byte is 0, then it must be a single-byte ASCII
1583 if (! strcasecmp (charset
, "US-ASCII") ||
1584 ! strcasecmp (charset
, "UTF-8") ||
1585 ! strncasecmp (charset
, "ISO-8859-", 9) ||
1586 ! strncasecmp (charset
, "WINDOWS-12", 10)) {
1592 int opened_input_file
= 0;
1594 if (ct
->c_cefile
.ce_file
) {
1595 file
= &ct
->c_cefile
.ce_file
;
1596 fp
= &ct
->c_cefile
.ce_fp
;
1598 } else if (ct
->c_file
) {
1601 begin
= (size_t) ct
->c_begin
;
1602 end
= (size_t) ct
->c_end
;
1603 } /* else don't know where the content is */
1605 if (file
&& *file
&& fp
) {
1607 if ((*fp
= fopen (*file
, "r")) == NULL
) {
1608 advise (*file
, "unable to open for reading");
1611 opened_input_file
= 1;
1617 char buffer
[BUFSIZ
];
1619 size_t bytes_to_read
=
1620 end
> 0 && end
> begin
? end
- begin
: sizeof buffer
;
1622 fseeko (*fp
, begin
, SEEK_SET
);
1623 while ((bytes_read
= fread (buffer
, 1,
1624 min (bytes_to_read
, sizeof buffer
),
1626 /* Look for CR followed by a LF. This is supposed to
1627 be text so there should be LF's. If not, don't
1628 modify the content. */
1631 int last_char_was_cr
= 0;
1633 if (end
> 0) bytes_to_read
-= bytes_read
;
1635 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
1636 if (*cp
== '\n' && last_char_was_cr
) {
1641 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
1647 char *stripped_content_file
;
1648 char *tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
);
1650 if (tempfile
== NULL
) {
1651 adios (NULL
, "unable to create temporary file in %s",
1654 stripped_content_file
= add (tempfile
, NULL
);
1656 /* Strip each CR before a LF from the content. */
1657 fseeko (*fp
, begin
, SEEK_SET
);
1658 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) >
1662 int last_char_was_cr
= 0;
1664 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
1666 last_char_was_cr
= 1;
1667 } else if (last_char_was_cr
) {
1668 if (*cp
!= '\n') write (fd
, "\r", 1);
1670 last_char_was_cr
= 0;
1673 last_char_was_cr
= 0;
1679 admonish (NULL
, "unable to write temporary file %s",
1680 stripped_content_file
);
1681 (void) m_unlink (stripped_content_file
);
1684 /* Replace the decoded file with the converted one. */
1685 if (ct
->c_cefile
.ce_file
) {
1686 if (ct
->c_cefile
.ce_unlink
) {
1687 (void) m_unlink (ct
->c_cefile
.ce_file
);
1689 free (ct
->c_cefile
.ce_file
);
1691 ct
->c_cefile
.ce_file
= stripped_content_file
;
1692 ct
->c_cefile
.ce_unlink
= 1;
1696 report (NULL
, ct
->c_partno
,
1697 begin
== 0 && end
== 0 ? "" : *file
,
1703 if (opened_input_file
) {
1715 convert_charsets (CT ct
, char *dest_charset
, int *message_mods
) {
1718 switch (ct
->c_type
) {
1720 if (ct
->c_subtype
== TEXT_PLAIN
) {
1721 status
= convert_charset (ct
, dest_charset
, message_mods
);
1724 report (NULL
, ct
->c_partno
, ct
->c_file
,
1726 content_charset(ct
), dest_charset
);
1729 report ("iconv", ct
->c_partno
, ct
->c_file
,
1730 "failed to convert %s to %s",
1731 content_charset(ct
), dest_charset
);
1736 case CT_MULTIPART
: {
1737 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1740 /* Should check to see if the body for this part is encoded?
1741 For now, it gets passed along as-is by InitMultiPart(). */
1742 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1744 convert_charsets (part
->mp_part
, dest_charset
, message_mods
);
1750 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1753 e
= (struct exbody
*) ct
->c_ctparams
;
1755 convert_charsets (e
->eb_content
, dest_charset
, message_mods
);
1768 write_content (CT ct
, char *input_filename
, char *outfile
, int modify_inplace
,
1772 if (modify_inplace
) {
1773 if (message_mods
> 0) {
1774 if ((status
= output_message (ct
, outfile
)) == OK
) {
1775 char *infile
= input_filename
1776 ? add (input_filename
, NULL
)
1777 : add (ct
->c_file
? ct
->c_file
: "-", NULL
);
1779 if (remove_file (infile
) == OK
) {
1780 if (rename (outfile
, infile
)) {
1781 /* Rename didn't work, possibly because of an
1782 attempt to rename across filesystems. Try
1783 brute force copy. */
1784 int old
= open (outfile
, O_RDONLY
);
1786 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
1789 if (old
!= -1 && new != -1) {
1790 char buffer
[BUFSIZ
];
1792 while ((i
= read (old
, buffer
, sizeof buffer
)) >
1794 if (write (new, buffer
, i
) != i
) {
1800 if (new != -1) close (new);
1801 if (old
!= -1) close (old
);
1802 (void) m_unlink (outfile
);
1805 /* The -file argument processing used path() to
1806 expand filename to absolute path. */
1807 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
1809 admonish (NULL
, "unable to rename %s %s to %s",
1810 file
? "file" : "message", outfile
,
1816 admonish (NULL
, "unable to remove input file %s, "
1817 "not modifying it", infile
);
1818 (void) m_unlink (outfile
);
1827 /* No modifications and didn't need the tmp outfile. */
1828 (void) m_unlink (outfile
);
1831 /* Output is going to some file. Produce it whether or not
1832 there were modifications. */
1833 status
= output_message (ct
, outfile
);
1842 * If "rmmproc" is defined, call that to remove the file. Otherwise,
1843 * use the standard MH backup file.
1846 remove_file (char *file
) {
1848 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
1849 int status
= system (rmm_command
);
1852 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
1854 /* This is OK for a non-message file, it still uses the
1855 BACKUP_PREFIX form. The backup file will be in the same
1856 directory as file. */
1857 return rename (file
, m_backup (file
));
1863 report (char *what
, char *partno
, char *filename
, char *message
, ...) {
1868 va_start (args
, message
);
1869 fmt
= concat (filename
, partno
? " part " : ", ",
1870 partno
? partno
: "", partno
? ", " : "", message
, NULL
);
1872 advertise (what
, NULL
, fmt
, args
);
1885 fprintf (stderr
, "\n");