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");
295 if ((ct
= parse_mime (file
))) { *ctp
++ = ct
; }
298 * message(s) are coming from a folder
303 app_msgarg(&msgs
, "cur");
305 folder
= getfolder (1);
306 maildir
= m_maildir (folder
);
308 if (chdir (maildir
) == NOTOK
)
309 adios (maildir
, "unable to change directory to");
311 /* read folder and create message structure */
312 if (! (mp
= folder_read (folder
, 1)))
313 adios (NULL
, "unable to read folder %s", folder
);
315 /* check for empty folder */
317 adios (NULL
, "no messages in %s", folder
);
319 /* parse all the message ranges/sequences and set SELECTED */
320 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
321 if (! m_convert (mp
, msgs
.msgs
[msgnum
]))
323 seq_setprev (mp
); /* set the previous-sequence */
325 if (! (cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof *cts
))) {
326 adios (NULL
, "out of memory");
330 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
331 if (is_selected(mp
, msgnum
)) {
334 msgnam
= m_name (msgnum
);
335 if ((ct
= parse_mime (msgnam
))) { *ctp
++ = ct
; }
339 seq_setcur (mp
, mp
->hghsel
); /* update current message */
340 seq_save (mp
); /* synchronize sequences */
341 context_replace (pfolder
, folder
);/* update current folder */
342 context_save (); /* save the context file */
346 for (ctp
= cts
; *ctp
; ++ctp
) {
347 status
+= mhfixmsgsbr (ctp
, &fx
, outfile
);
350 (void) m_unlink (file
);
353 /* Just calling m_backup() unlinks the backup file. */
354 (void) m_backup (file
);
365 /* done is freects_done, which will clean up all of cts. */
372 mhfixmsgsbr (CT
*ctp
, const fix_transformations
*fx
, char *outfile
) {
373 /* Store input filename in case one of the transformations, i.e.,
374 fix_boundary(), rewrites to a tmp file. */
375 char *input_filename
= add ((*ctp
)->c_file
, NULL
);
376 int modify_inplace
= 0;
377 int message_mods
= 0;
380 if (outfile
== NULL
) {
383 if ((*ctp
)->c_file
) {
385 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
386 adios (NULL
, "unable to create temporary file in %s",
389 outfile
= add (tempfile
, NULL
);
391 adios (NULL
, "missing both input and output filenames\n");
395 reverse_alternative_parts (*ctp
);
396 if (status
== OK
&& fx
->fixboundary
) {
397 status
= fix_boundary (ctp
, &message_mods
);
399 if (status
== OK
&& fx
->fixcte
) {
400 status
= fix_multipart_cte (*ctp
, &message_mods
);
402 if (status
== OK
&& fx
->reformat
) {
404 ensure_text_plain (ctp
, NULL
, &message_mods
, fx
->replacetextplain
);
406 if (status
== OK
&& fx
->decodetext
) {
407 status
= decode_text_parts (*ctp
, fx
->decodetext
, &message_mods
);
409 if (status
== OK
&& fx
->textcharset
!= NULL
) {
410 status
= convert_charsets (*ctp
, fx
->textcharset
, &message_mods
);
413 if (! (*ctp
)->c_umask
) {
414 /* Set the umask for the contents file. This currently
415 isn't used but just in case it is in the future. */
418 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
419 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
421 (*ctp
)->c_umask
= ~m_gmprot();
426 * Write the content to a file
429 status
= write_content (*ctp
, input_filename
, outfile
, modify_inplace
,
431 } else if (! modify_inplace
) {
432 /* Something went wrong. Output might be expected, such
433 as if this were run as a filter. Just copy the input
435 int in
= open (input_filename
, O_RDONLY
);
436 int out
= strcmp (outfile
, "-")
437 ? open (outfile
, O_WRONLY
| O_CREAT
, m_gmprot ())
440 if (in
!= -1 && out
!= -1) {
441 cpydata (in
, out
, input_filename
, outfile
);
450 if (modify_inplace
) {
451 if (status
!= OK
) { (void) m_unlink (outfile
); }
456 free (input_filename
);
462 /* parse_mime() arranges alternates in reverse (priority) order, so
463 reverse them back. This will put a text/plain part at the front of
464 a multipart/alternative part, for example, where it belongs. */
466 reverse_alternative_parts (CT ct
) {
467 if (ct
->c_type
== CT_MULTIPART
) {
468 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
471 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
475 /* And call recursively on each part of a multipart. */
476 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
477 reverse_alternative_parts (part
->mp_part
);
484 fix_boundary (CT
*ct
, int *message_mods
) {
485 struct multipart
*mp
;
488 if (bogus_mp_content
) {
489 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
492 * 1) Get boundary at end of part.
493 * 2) Get boundary at beginning of part and compare to the end-of-part
495 * 3) Write out contents of ct to tmp file, replacing boundary in
496 * header with boundary from part. Set c_unlink to 1.
498 * 5) Call parse_mime() on the tmp file, replacing ct.
501 if (mp
&& mp
->mp_start
) {
504 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
507 if ((fixed
= m_mktemp2 (NULL
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
508 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
509 char *filename
= add ((*ct
)->c_file
, NULL
);
512 if ((*ct
= parse_mime (fixed
))) {
517 report (NULL
, NULL
, filename
,
518 "fix multipart boundary");
523 advise (NULL
, "unable to replace broken boundary");
527 advise (NULL
, "unable to create temporary file in %s",
532 free (part_boundary
);
542 get_multipart_boundary (CT ct
, char **part_boundary
) {
544 char *end_boundary
= NULL
;
545 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
546 ? (off_t
) (ct
->c_end
- sizeof buffer
)
547 : (off_t
) ct
->c_begin
;
551 /* This will fail if the boundary spans fread() calls. BUFSIZ should
552 be big enough, even if it's just 1024, to make that unlikely. */
554 /* free_content() will close ct->c_fp. */
555 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
556 advise (ct
->c_file
, "unable to open for reading");
560 /* Get boundary at end of multipart. */
561 while (begin
>= (off_t
) ct
->c_begin
) {
562 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
563 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
564 char *cp
= rfind_str (buffer
, bytes_read
, "--");
569 /* Trim off trailing "--" and anything beyond. */
571 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
572 if (strlen (end
) > 3 && *end
++ == '\n' &&
573 *end
++ == '-' && *end
++ == '-') {
574 end_boundary
= add (end
, NULL
);
581 if (! end_boundary
&& begin
> (off_t
) (ct
->c_begin
+ sizeof buffer
)) {
582 begin
-= sizeof buffer
;
588 /* Get boundary at beginning of multipart. */
590 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
591 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
592 if (bytes_read
>= strlen (end_boundary
)) {
593 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
595 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
596 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
601 /* The start and end boundaries didn't match, or the
602 start boundary doesn't begin with "\n--" (or "--"
603 if at the beginning of buffer). Keep trying. */
612 *part_boundary
= end_boundary
;
614 *part_boundary
= NULL
;
622 /* Open and copy ct->c_file to file, replacing the multipart boundary. */
624 replace_boundary (CT ct
, char *file
, char *boundary
) {
627 char buf
[BUFSIZ
], name
[NAMESZ
];
629 m_getfld_state_t gstate
= 0;
632 if (ct
->c_file
== NULL
) {
633 advise (NULL
, "missing input filename");
637 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
638 advise (ct
->c_file
, "unable to open for reading");
642 if ((fpout
= fopen (file
, "w")) == NULL
) {
644 advise (file
, "unable to open for writing");
648 for (compnum
= 1;;) {
649 int bufsz
= (int) sizeof buf
;
651 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
)) {
656 /* get copies of the buffers */
657 np
= add (name
, NULL
);
658 vp
= add (buf
, NULL
);
660 /* if necessary, get rest of field */
661 while (state
== FLDPLUS
) {
663 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
);
664 vp
= add (buf
, vp
); /* add to previous value */
667 if (strcasecmp (TYPE_FIELD
, np
)) {
668 fprintf (fpout
, "%s:%s", np
, vp
);
670 char *new_ctline
, *new_params
;
672 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
673 &ct
->c_ctinfo
.ci_last_pm
, "boundary",
676 new_ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
677 ct
->c_ctinfo
.ci_subtype
, NULL
);
678 new_params
= output_params(strlen(TYPE_FIELD
) +
679 strlen(new_ctline
) + 1,
680 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
681 fprintf (fpout
, "%s:%s%s\n", np
, new_ctline
,
682 new_params
? new_params
: "");
695 /* buf will have a terminating NULL, skip it. */
696 fwrite (buf
, 1, bufsz
-1, fpout
);
704 advise (NULL
, "message format error in component #%d", compnum
);
709 advise (NULL
, "getfld() returned %d", state
);
717 m_getfld_state_destroy (&gstate
);
726 fix_multipart_cte (CT ct
, int *message_mods
) {
729 if (ct
->c_type
== CT_MULTIPART
) {
733 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
734 ct
->c_encoding
!= CE_BINARY
) {
737 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
738 char *name
= hf
->name
;
739 for (; *name
&& isspace ((unsigned char) *name
); ++name
) {
743 if (! strncasecmp (name
, ENCODING_FIELD
,
744 strlen (ENCODING_FIELD
))) {
745 char *prefix
= "Nmh-REPLACED-INVALID-";
746 HF h
= mh_xmalloc (sizeof *h
);
748 h
->name
= add (hf
->name
, NULL
);
749 h
->hf_encoding
= hf
->hf_encoding
;
753 /* Retain old header but prefix its name. */
755 hf
->name
= concat (prefix
, h
->name
, NULL
);
759 char *encoding
= cpytrim (hf
->value
);
760 report (NULL
, ct
->c_partno
, ct
->c_file
,
761 "replace Content-Transfer-Encoding of %s "
762 "with 8 bit", encoding
);
766 h
->value
= add (" 8bit\n", NULL
);
768 /* Don't need to warn for multiple C-T-E header
769 fields, parse_mime() already does that. But
770 if there are any, fix them all as necessary. */
775 set_ce (ct
, CE_8BIT
);
778 m
= (struct multipart
*) ct
->c_ctparams
;
779 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
780 if (fix_multipart_cte (part
->mp_part
, message_mods
) != OK
) {
792 set_ce (CT ct
, int encoding
) {
793 const char *ce
= ce_str (encoding
);
794 const struct str2init
*ctinit
= get_ce_method (ce
);
797 char *cte
= concat (" ", ce
, "\n", NULL
);
800 /* Decoded contents might be in ct->c_cefile.ce_file, if the
801 caller is decode_text_parts (). Save because we'll
803 struct cefile decoded_content_info
= ct
->c_cefile
;
805 ct
->c_encoding
= encoding
;
807 ct
->c_ctinitfnx
= ctinit
->si_init
;
808 /* This will assign ct->c_cefile with an all-0 struct, which
810 (*ctinit
->si_init
) (ct
);
811 /* After returning, the caller should set
812 ct->c_cefile.ce_file to the name of the file containing
815 /* Restore the cefile. */
816 ct
->c_cefile
= decoded_content_info
;
818 /* Update/add Content-Transfer-Encoding header field. */
819 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
820 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
827 add_header (ct
, add (ENCODING_FIELD
, NULL
), cte
);
830 /* Update c_celine. It's used only by mhlist -debug. */
832 ct
->c_celine
= add (cte
, NULL
);
841 /* Make sure each text part has a corresponding text/plain part. */
843 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
, int replacetextplain
) {
846 switch ((*ct
)->c_type
) {
848 int has_text_plain
= 0;
850 /* Nothing to do for text/plain. */
851 if ((*ct
)->c_subtype
== TEXT_PLAIN
) { return OK
; }
853 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
854 parent
->c_subtype
== MULTI_ALTERNATE
) {
855 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
856 struct part
*part
, *prev
;
857 int new_subpart_number
= 1;
859 /* See if there is a sibling text/plain. */
860 for (prev
= part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
861 ++new_subpart_number
;
862 if (part
->mp_part
->c_type
== CT_TEXT
&&
863 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
864 if (replacetextplain
) {
865 struct part
*old_part
;
866 if (part
== mp
->mp_parts
) {
867 old_part
= mp
->mp_parts
;
868 mp
->mp_parts
= part
->mp_next
;
870 old_part
= prev
->mp_next
;
871 prev
->mp_next
= part
->mp_next
;
874 report (NULL
, parent
->c_partno
, parent
->c_file
,
875 "remove text/plain part %s",
876 old_part
->mp_part
->c_partno
);
878 free_content (old_part
->mp_part
);
888 if (! has_text_plain
) {
889 /* Parent is a multipart/alternative. Insert a new
890 text/plain subpart. */
891 struct part
*new_part
= mh_xmalloc (sizeof *new_part
);
893 if ((new_part
->mp_part
= build_text_plain_part (*ct
))) {
895 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
897 new_part
->mp_next
= mp
->mp_parts
;
898 mp
->mp_parts
= new_part
;
899 new_part
->mp_part
->c_partno
=
900 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
905 report (NULL
, parent
->c_partno
, parent
->c_file
,
906 "insert text/plain part");
909 free_content (new_part
->mp_part
);
915 /* Slip new text/plain part into a new multipart/alternative. */
916 CT tp_part
= build_text_plain_part (*ct
);
919 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
922 struct multipart
*mp
=
923 (struct multipart
*) mp_alt
->c_ctparams
;
925 if (mp
&& mp
->mp_parts
) {
926 mp
->mp_parts
->mp_part
= tp_part
;
927 /* Make the new multipart/alternative the parent. */
932 report (NULL
, (*ct
)->c_partno
, (*ct
)->c_file
,
933 "insert text/plain part");
936 free_content (tp_part
);
937 free_content (mp_alt
);
951 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
954 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
955 if ((*ct
)->c_type
== CT_MULTIPART
) {
956 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
,
964 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
967 e
= (struct exbody
*) (*ct
)->c_ctparams
;
968 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
,
979 build_text_plain_part (CT encoded_part
) {
980 CT tp_part
= divide_part (encoded_part
);
981 char *tmp_plain_file
= NULL
;
983 if (decode_part (tp_part
) == OK
) {
984 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
985 contains the decoded contents. And the decoding function, such
986 as openQuoted, will have set ...->ce_unlink to 1 so that it will
987 be unlinked by free_content (). */
990 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
991 advise (NULL
, "unable to create temporary file in %s",
994 tmp_plain_file
= add (tempfile
, NULL
);
995 if (reformat_part (tp_part
, tmp_plain_file
,
996 tp_part
->c_ctinfo
.ci_type
,
997 tp_part
->c_ctinfo
.ci_subtype
,
998 tp_part
->c_type
) == OK
) {
1003 free_content (tp_part
);
1004 (void) m_unlink (tmp_plain_file
);
1005 free (tmp_plain_file
);
1012 divide_part (CT ct
) {
1015 if ((new_part
= (CT
) calloc (1, sizeof *new_part
)) == NULL
)
1016 adios (NULL
, "out of memory");
1018 /* Just copy over what is needed for decoding. c_vrsn and
1019 c_celine aren't necessary. */
1020 new_part
->c_file
= add (ct
->c_file
, NULL
);
1021 new_part
->c_begin
= ct
->c_begin
;
1022 new_part
->c_end
= ct
->c_end
;
1023 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1024 new_part
->c_type
= ct
->c_type
;
1025 new_part
->c_cefile
= ct
->c_cefile
;
1026 new_part
->c_encoding
= ct
->c_encoding
;
1027 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1028 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1029 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1030 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1032 /* c_ctline is used by reformat__part(), so it can preserve
1033 anything after the type/subtype. */
1034 new_part
->c_ctline
= add (ct
->c_ctline
, NULL
);
1041 copy_ctinfo (CI dest
, CI src
) {
1044 dest
->ci_type
= src
->ci_type
? add (src
->ci_type
, NULL
) : NULL
;
1045 dest
->ci_subtype
= src
->ci_subtype
? add (src
->ci_subtype
, NULL
) : NULL
;
1047 for (s_pm
= src
->ci_first_pm
; s_pm
; s_pm
= s_pm
->pm_next
) {
1048 d_pm
= add_param(&dest
->ci_first_pm
, &dest
->ci_last_pm
, s_pm
->pm_name
,
1050 if (s_pm
->pm_charset
)
1051 d_pm
->pm_charset
= getcpy(s_pm
->pm_charset
);
1053 d_pm
->pm_lang
= getcpy(s_pm
->pm_lang
);
1056 dest
->ci_comment
= src
->ci_comment
? add (src
->ci_comment
, NULL
) : NULL
;
1057 dest
->ci_magic
= src
->ci_magic
? add (src
->ci_magic
, NULL
) : NULL
;
1062 decode_part (CT ct
) {
1067 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1068 adios (NULL
, "unable to create temporary file in %s", get_temp_dir());
1070 tmp_decoded
= add (tempfile
, NULL
);
1071 /* The following call will load ct->c_cefile.ce_file with the tmp
1072 filename of the decoded content. tmp_decoded will contain the
1073 encoded output, get rid of that. */
1074 status
= output_message (ct
, tmp_decoded
);
1075 (void) m_unlink (tmp_decoded
);
1082 /* Some of the arguments aren't really needed now, but maybe will
1083 be in the future for other than text types. */
1085 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
) {
1086 int output_subtype
, output_encoding
;
1090 /* Hacky: this redirects the output from whatever command is used
1091 to show the part to a file. So, the user can't have any output
1092 redirection in that command.
1093 Could show_multi() in mhshowsbr.c avoid this? */
1095 /* Check for invo_name-format-type/subtype. */
1096 if ((cf
= context_find_by_type ("format", type
, subtype
)) == NULL
) {
1098 advise (NULL
, "Don't know how to convert %s, there is no "
1099 "%s-format-%s/%s profile entry",
1100 ct
->c_file
, invo_name
, type
, subtype
);
1104 if (strchr (cf
, '>')) {
1105 advise (NULL
, "'>' prohibited in \"%s\",\nplease fix your "
1106 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1107 subtype
? subtype
: "");
1113 cp
= concat (cf
, " >", file
, NULL
);
1114 status
= show_content_aux (ct
, 0, cp
, NULL
, NULL
);
1117 /* Unlink decoded content tmp file and free its filename to avoid
1118 leaks. The file stream should already have been closed. */
1119 if (ct
->c_cefile
.ce_unlink
) {
1120 (void) m_unlink (ct
->c_cefile
.ce_file
);
1121 free (ct
->c_cefile
.ce_file
);
1122 ct
->c_cefile
.ce_file
= NULL
;
1123 ct
->c_cefile
.ce_unlink
= 0;
1126 if (c_type
== CT_TEXT
) {
1127 output_subtype
= TEXT_PLAIN
;
1129 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1132 output_encoding
= charset_encoding (ct
);
1134 if (set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1135 ct
->c_cefile
.ce_file
= file
;
1136 ct
->c_cefile
.ce_unlink
= 1;
1138 ct
->c_cefile
.ce_unlink
= 0;
1146 /* Identifies 7bit or 8bit content based on charset. */
1148 charset_encoding (CT ct
) {
1150 strcasecmp (content_charset (ct
), "US-ASCII") ? CE_8BIT
: CE_7BIT
;
1157 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
) {
1158 char *boundary_prefix
= "----=_nmh-multipart";
1159 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1160 char *boundary_indicator
= "; boundary=";
1161 char *typename
, *subtypename
, *name
;
1164 struct multipart
*m
;
1165 const struct str2init
*ctinit
;
1167 if ((ct
= (CT
) calloc (1, sizeof *ct
)) == NULL
)
1168 adios (NULL
, "out of memory");
1170 /* Set up the multipart/alternative part. These fields of *ct were
1171 initialized to 0 by calloc():
1172 c_fp, c_unlink, c_begin, c_end,
1173 c_vrsn, c_ctline, c_celine,
1174 c_id, c_descr, c_dispo, c_partno,
1175 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1176 c_cefile, c_encoding,
1177 c_digested, c_digest[16], c_ctexbody,
1178 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1180 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1183 ct
->c_file
= add (first_alt
->c_file
, NULL
);
1185 ct
->c_subtype
= subtype
;
1187 ctinit
= get_ct_init (ct
->c_type
);
1189 typename
= ct_type_str (type
);
1190 subtypename
= ct_subtype_str (type
, subtype
);
1194 int found_boundary
= 1;
1196 while (found_boundary
&& serial
< 1000000) {
1199 /* Ensure that the boundary doesn't appear in the decoded
1201 if (new_part
->c_cefile
.ce_file
) {
1202 if ((found_boundary
=
1203 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1204 new_part
->c_cefile
.ce_file
,
1211 /* Ensure that the boundary doesn't appear in the encoded
1213 if (! found_boundary
&& new_part
->c_file
) {
1214 if ((found_boundary
= boundary_in_content (&new_part
->c_fp
,
1222 if (found_boundary
) {
1223 /* Try a slightly different boundary. */
1228 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1230 concat (boundary_prefix
,
1231 first_alt
->c_partno
? first_alt
->c_partno
: "",
1232 "-", buffer2
, NULL
);
1236 if (found_boundary
) {
1237 advise (NULL
, "giving up trying to find a unique boundary");
1243 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1244 boundary
, "\"", NULL
);
1246 /* Load c_first_hf and c_last_hf. */
1247 transfer_noncontent_headers (first_alt
, ct
);
1248 add_header (ct
, add (TYPE_FIELD
, NULL
), concat (name
, "\n", NULL
));
1251 /* Load c_partno. */
1252 if (first_alt
->c_partno
) {
1253 ct
->c_partno
= add (first_alt
->c_partno
, NULL
);
1254 free (first_alt
->c_partno
);
1255 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1256 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1258 first_alt
->c_partno
= add ("1", NULL
);
1259 new_part
->c_partno
= add ("2", NULL
);
1263 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1264 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1267 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1268 "boundary", boundary
, 0);
1270 p
= (struct part
*) mh_xmalloc (sizeof *p
);
1271 p
->mp_next
= (struct part
*) mh_xmalloc (sizeof *p
->mp_next
);
1272 p
->mp_next
->mp_next
= NULL
;
1273 p
->mp_next
->mp_part
= first_alt
;
1275 if ((m
= (struct multipart
*) calloc (1, sizeof (struct multipart
))) ==
1277 adios (NULL
, "out of memory");
1278 m
->mp_start
= concat (boundary
, "\n", NULL
);
1279 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1281 ct
->c_ctparams
= (void *) m
;
1289 /* Check that the boundary does not appear in the content. */
1291 boundary_in_content (FILE **fp
, char *file
, const char *boundary
) {
1292 char buffer
[BUFSIZ
];
1294 int found_boundary
= 0;
1296 /* free_content() will close *fp if we fopen it here. */
1297 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1298 advise (file
, "unable to open %s for reading", file
);
1302 fseeko (*fp
, 0L, SEEK_SET
);
1303 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1304 if (find_str (buffer
, bytes_read
, boundary
)) {
1310 return found_boundary
;
1314 /* Remove all non-Content headers. */
1316 transfer_noncontent_headers (CT old
, CT
new) {
1319 hp_prev
= hp
= old
->c_first_hf
;
1323 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, strlen (XXX_FIELD_PRF
))) {
1324 if (hp
== old
->c_last_hf
) {
1325 if (hp
== old
->c_first_hf
) {
1326 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1328 hp_prev
->next
= NULL
;
1329 old
->c_last_hf
= hp_prev
;
1332 if (hp
== old
->c_first_hf
) {
1333 old
->c_first_hf
= next
;
1335 hp_prev
->next
= next
;
1339 /* Put node hp in the new CT. */
1340 if (new->c_first_hf
== NULL
) {
1341 new->c_first_hf
= hp
;
1343 new->c_last_hf
->next
= hp
;
1345 new->c_last_hf
= hp
;
1347 /* A Content- header, leave in old. */
1357 set_ct_type (CT ct
, int type
, int subtype
, int encoding
) {
1358 char *typename
= ct_type_str (type
);
1359 char *subtypename
= ct_subtype_str (type
, subtype
);
1360 /* E.g, " text/plain" */
1361 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1362 /* E.g, " text/plain\n" */
1363 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1364 int found_content_type
= 0;
1366 const char *cp
= NULL
;
1370 /* Update/add Content-Type header field. */
1371 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1372 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1373 found_content_type
= 1;
1375 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1376 ? concat (type_subtypename
, cp
, "\n", NULL
)
1377 : add (name_plus_nl
, NULL
);
1380 if (! found_content_type
) {
1381 add_header (ct
, add (TYPE_FIELD
, NULL
),
1382 (cp
= strchr (ct
->c_ctline
, ';'))
1383 ? concat (type_subtypename
, cp
, "\n", NULL
)
1384 : add (name_plus_nl
, NULL
));
1387 /* Some of these might not be used, but set them anyway. */
1389 ? concat (type_subtypename
, cp
, NULL
)
1390 : concat (type_subtypename
, NULL
);
1391 free (ct
->c_ctline
);
1392 ct
->c_ctline
= ctline
;
1393 /* Leave other ctinfo members as they were. */
1394 free (ct
->c_ctinfo
.ci_type
);
1395 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1396 free (ct
->c_ctinfo
.ci_subtype
);
1397 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1399 ct
->c_subtype
= subtype
;
1401 free (name_plus_nl
);
1402 free (type_subtypename
);
1404 status
= set_ce (ct
, encoding
);
1411 decode_text_parts (CT ct
, int encoding
, int *message_mods
) {
1414 switch (ct
->c_type
) {
1416 switch (ct
->c_encoding
) {
1421 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
1422 const char *reason
= NULL
;
1424 if ((ct_encoding
= content_encoding (ct
, &reason
)) == CE_BINARY
1425 && encoding
!= CE_BINARY
) {
1426 /* The decoding isn't acceptable so discard it.
1427 Leave status as OK to allow other transformations. */
1429 report (NULL
, ct
->c_partno
, ct
->c_file
,
1430 "will not decode%s because it is binary (%s)",
1432 : ct
->c_ctline
? ct
->c_ctline
1436 (void) m_unlink (ct
->c_cefile
.ce_file
);
1437 free (ct
->c_cefile
.ce_file
);
1438 ct
->c_cefile
.ce_file
= NULL
;
1439 } else if (ct
->c_encoding
== CE_QUOTED
&&
1440 ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
1441 /* The decoding isn't acceptable so discard it.
1442 Leave status as OK to allow other transformations. */
1444 report (NULL
, ct
->c_partno
, ct
->c_file
,
1445 "will not decode%s because it is 8bit",
1447 : ct
->c_ctline
? ct
->c_ctline
1450 (void) m_unlink (ct
->c_cefile
.ce_file
);
1451 free (ct
->c_cefile
.ce_file
);
1452 ct
->c_cefile
.ce_file
= NULL
;
1455 if (ct_encoding
== CE_BINARY
)
1457 else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
)
1460 enc
= charset_encoding (ct
);
1461 if (set_ce (ct
, enc
) == OK
) {
1464 report (NULL
, ct
->c_partno
, ct
->c_file
, "decode%s",
1465 ct
->c_ctline
? ct
->c_ctline
: "");
1467 strip_crs (ct
, message_mods
);
1479 strip_crs (ct
, message_mods
);
1487 case CT_MULTIPART
: {
1488 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1491 /* Should check to see if the body for this part is encoded?
1492 For now, it gets passed along as-is by InitMultiPart(). */
1493 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1494 status
= decode_text_parts (part
->mp_part
, encoding
, message_mods
);
1500 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1503 e
= (struct exbody
*) ct
->c_ctparams
;
1504 status
= decode_text_parts (e
->eb_content
, encoding
, message_mods
);
1516 /* See if the decoded content is 7bit, 8bit, or binary. It's binary
1517 if it has any NUL characters, a CR not followed by a LF, or lines
1518 greater than 998 characters in length. If binary, reason is set
1519 to a string explaining why. */
1521 content_encoding (CT ct
, const char **reason
) {
1522 CE ce
= &ct
->c_cefile
;
1523 int encoding
= CE_7BIT
;
1526 size_t line_len
= 0;
1527 char buffer
[BUFSIZ
];
1530 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1531 advise (ce
->ce_file
, "unable to open for reading");
1535 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
1536 while (encoding
!= CE_BINARY
&&
1537 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
1540 int last_char_was_cr
= 0;
1542 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
1543 if (*cp
== '\0' || ++line_len
> 998 ||
1544 (*cp
!= '\n' && last_char_was_cr
)) {
1545 encoding
= CE_BINARY
;
1547 *reason
= "null character";
1548 } else if (line_len
> 998) {
1549 *reason
= "line length > 998";
1550 } else if (*cp
!= '\n' && last_char_was_cr
) {
1551 *reason
= "CR not followed by LF";
1553 /* Should not reach this. */
1557 } else if (*cp
== '\n') {
1559 } else if (! isascii ((unsigned char) *cp
)) {
1563 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
1569 } /* else should never happen */
1576 strip_crs (CT ct
, int *message_mods
) {
1577 char *charset
= content_charset (ct
);
1580 /* Only strip carriage returns if content is ASCII or another
1581 charset that has the same readily recognizable CR followed by a
1582 LF. We can include UTF-8 here because if the high-order bit of
1583 a UTF-8 byte is 0, then it must be a single-byte ASCII
1585 if (! strcasecmp (charset
, "US-ASCII") ||
1586 ! strcasecmp (charset
, "UTF-8") ||
1587 ! strncasecmp (charset
, "ISO-8859-", 9) ||
1588 ! strncasecmp (charset
, "WINDOWS-12", 10)) {
1594 int opened_input_file
= 0;
1596 if (ct
->c_cefile
.ce_file
) {
1597 file
= &ct
->c_cefile
.ce_file
;
1598 fp
= &ct
->c_cefile
.ce_fp
;
1600 } else if (ct
->c_file
) {
1603 begin
= (size_t) ct
->c_begin
;
1604 end
= (size_t) ct
->c_end
;
1605 } /* else don't know where the content is */
1607 if (file
&& *file
&& fp
) {
1609 if ((*fp
= fopen (*file
, "r")) == NULL
) {
1610 advise (*file
, "unable to open for reading");
1613 opened_input_file
= 1;
1619 char buffer
[BUFSIZ
];
1621 size_t bytes_to_read
=
1622 end
> 0 && end
> begin
? end
- begin
: sizeof buffer
;
1624 fseeko (*fp
, begin
, SEEK_SET
);
1625 while ((bytes_read
= fread (buffer
, 1,
1626 min (bytes_to_read
, sizeof buffer
),
1628 /* Look for CR followed by a LF. This is supposed to
1629 be text so there should be LF's. If not, don't
1630 modify the content. */
1633 int last_char_was_cr
= 0;
1635 if (end
> 0) { bytes_to_read
-= bytes_read
; }
1637 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
1638 if (*cp
== '\n' && last_char_was_cr
) {
1643 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
1649 char *stripped_content_file
;
1650 char *tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
);
1652 if (tempfile
== NULL
) {
1653 adios (NULL
, "unable to create temporary file in %s",
1656 stripped_content_file
= add (tempfile
, NULL
);
1658 /* Strip each CR before a LF from the content. */
1659 fseeko (*fp
, begin
, SEEK_SET
);
1660 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) >
1664 int last_char_was_cr
= 0;
1666 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
1668 last_char_was_cr
= 1;
1669 } else if (last_char_was_cr
) {
1671 if (write (fd
, "\r", 1) < 0) {
1672 advise (tempfile
, "write of CR failed");
1675 if (write (fd
, cp
, 1) < 0) {
1676 advise (tempfile
, "write failed");
1678 last_char_was_cr
= 0;
1680 if (write (fd
, cp
, 1) < 0) {
1681 advise (tempfile
, "write failed");
1683 last_char_was_cr
= 0;
1689 admonish (NULL
, "unable to write temporary file %s",
1690 stripped_content_file
);
1691 (void) m_unlink (stripped_content_file
);
1694 /* Replace the decoded file with the converted one. */
1695 if (ct
->c_cefile
.ce_file
) {
1696 if (ct
->c_cefile
.ce_unlink
) {
1697 (void) m_unlink (ct
->c_cefile
.ce_file
);
1699 free (ct
->c_cefile
.ce_file
);
1701 ct
->c_cefile
.ce_file
= stripped_content_file
;
1702 ct
->c_cefile
.ce_unlink
= 1;
1706 report (NULL
, ct
->c_partno
,
1707 begin
== 0 && end
== 0 ? "" : *file
,
1713 if (opened_input_file
) {
1725 convert_charsets (CT ct
, char *dest_charset
, int *message_mods
) {
1728 switch (ct
->c_type
) {
1730 if (ct
->c_subtype
== TEXT_PLAIN
) {
1731 status
= convert_charset (ct
, dest_charset
, message_mods
);
1734 report (NULL
, ct
->c_partno
, ct
->c_file
,
1736 content_charset(ct
), dest_charset
);
1739 report ("iconv", ct
->c_partno
, ct
->c_file
,
1740 "failed to convert %s to %s",
1741 content_charset(ct
), dest_charset
);
1746 case CT_MULTIPART
: {
1747 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1750 /* Should check to see if the body for this part is encoded?
1751 For now, it gets passed along as-is by InitMultiPart(). */
1752 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1754 convert_charsets (part
->mp_part
, dest_charset
, message_mods
);
1760 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1763 e
= (struct exbody
*) ct
->c_ctparams
;
1765 convert_charsets (e
->eb_content
, dest_charset
, message_mods
);
1778 write_content (CT ct
, char *input_filename
, char *outfile
, int modify_inplace
,
1782 if (modify_inplace
) {
1783 if (message_mods
> 0) {
1784 if ((status
= output_message (ct
, outfile
)) == OK
) {
1785 char *infile
= input_filename
1786 ? add (input_filename
, NULL
)
1787 : add (ct
->c_file
? ct
->c_file
: "-", NULL
);
1789 if (remove_file (infile
) == OK
) {
1790 if (rename (outfile
, infile
)) {
1791 /* Rename didn't work, possibly because of an
1792 attempt to rename across filesystems. Try
1793 brute force copy. */
1794 int old
= open (outfile
, O_RDONLY
);
1796 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
1799 if (old
!= -1 && new != -1) {
1800 char buffer
[BUFSIZ
];
1802 while ((i
= read (old
, buffer
, sizeof buffer
)) >
1804 if (write (new, buffer
, i
) != i
) {
1810 if (new != -1) { close (new); }
1811 if (old
!= -1) { close (old
); }
1812 (void) m_unlink (outfile
);
1815 /* The -file argument processing used path() to
1816 expand filename to absolute path. */
1817 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
1819 admonish (NULL
, "unable to rename %s %s to %s",
1820 file
? "file" : "message", outfile
,
1826 admonish (NULL
, "unable to remove input file %s, "
1827 "not modifying it", infile
);
1828 (void) m_unlink (outfile
);
1837 /* No modifications and didn't need the tmp outfile. */
1838 (void) m_unlink (outfile
);
1841 /* Output is going to some file. Produce it whether or not
1842 there were modifications. */
1843 status
= output_message (ct
, outfile
);
1852 * If "rmmproc" is defined, call that to remove the file. Otherwise,
1853 * use the standard MH backup file.
1856 remove_file (char *file
) {
1858 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
1859 int status
= system (rmm_command
);
1862 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
1864 /* This is OK for a non-message file, it still uses the
1865 BACKUP_PREFIX form. The backup file will be in the same
1866 directory as file. */
1867 return rename (file
, m_backup (file
));
1873 report (char *what
, char *partno
, char *filename
, char *message
, ...) {
1878 va_start (args
, message
);
1879 fmt
= concat (filename
, partno
? " part " : ", ",
1880 partno
? partno
: "", partno
? ", " : "", message
, NULL
);
1882 advertise (what
, NULL
, fmt
, args
);
1895 fprintf (stderr
, "\n");