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>
22 #define MHFIXMSG_SWITCHES \
23 X("decodetext 8bit|7bit", 0, DECODETEXTSW) \
24 X("nodecodetext", 0, NDECODETEXTSW) \
25 X("textcodeset", 0, TEXTCODESETSW) \
26 X("notextcodeset", 0, NTEXTCODESETSW) \
27 X("reformat", 0, REFORMATSW) \
28 X("noreformat", 0, NREFORMATSW) \
29 X("fixboundary", 0, FIXBOUNDARYSW) \
30 X("nofixboundary", 0, NFIXBOUNDARYSW) \
31 X("fixcte", 0, FIXCTESW) \
32 X("nofixcte", 0, NFIXCTESW) \
33 X("file file", 0, FILESW) \
34 X("outfile file", 0, OUTFILESW) \
35 X("rmmproc program", 0, RPROCSW) \
36 X("normmproc", 0, NRPRCSW) \
37 X("verbose", 0, VERBSW) \
38 X("noverbose", 0, NVERBSW) \
39 X("version", 0, VERSIONSW) \
40 X("help", 0, HELPSW) \
42 #define X(sw, minchars, id) id,
43 DEFINE_SWITCH_ENUM(MHFIXMSG
);
46 #define X(sw, minchars, id) { sw, minchars, id },
47 DEFINE_SWITCH_ARRAY(MHFIXMSG
, switches
);
52 int debugsw
; /* Needed by mhparse.c. */
54 #define quitser pipeser
57 extern char *tmp
; /* directory to place tmp files */
58 extern int skip_mp_cte_check
; /* flag to InitMultiPart */
59 extern int suppress_bogus_mp_content_warning
; /* flag to InitMultiPart */
60 extern int bogus_mp_content
; /* flag from InitMultiPart */
61 CT
parse_mime (char *);
62 void reverse_parts (CT
);
65 int output_message (CT
, char *);
68 int show_content_aux (CT
, int, int, char *, char *);
71 void flush_errors (void);
75 void freects_done (int) NORETURN
;
80 typedef struct fix_transformations
{
86 } fix_transformations
;
88 int mhfixmsgsbr (CT
*, const fix_transformations
*, char *);
89 static void reverse_alternative_parts (CT
);
90 static int fix_boundary (CT
*, int *);
91 static int get_multipart_boundary (CT
, char **);
92 static int replace_boundary (CT
, char *, const char *);
93 static char *update_attr (char *, const char *, const char *e
);
94 static int fix_multipart_cte (CT
, int *);
95 static int set_ce (CT
, int);
96 static int ensure_text_plain (CT
*, CT
, int *);
97 static CT
build_text_plain_part (CT
);
98 static CT
divide_part (CT
);
99 static void copy_ctinfo (CI
, CI
);
100 static int decode_part (CT
);
101 static int reformat_part (CT
, char *, char *, char *, int);
102 static int charset_encoding (CT
);
103 static CT
build_multipart_alt (CT
, CT
, int, int);
104 static int boundary_in_content (FILE **, char *, const char *);
105 static void transfer_noncontent_headers (CT
, CT
);
106 static int set_ct_type (CT
, int type
, int subtype
, int encoding
);
107 static int decode_text_parts (CT
, int, int *);
108 static int content_encoding (CT
);
109 static int convert_codesets (CT
, char *, int *);
110 static int convert_codeset (CT
, char *, int *);
111 static int write_content (CT
, char *, char *, int, int);
112 static int remove_file (char *);
113 static void report (char *, char *, char *, ...);
114 static char *upcase (char *);
115 static void pipeser (int);
119 main (int argc
, char **argv
) {
121 char *cp
, *file
= NULL
, *folder
= NULL
;
122 char *maildir
, buf
[100], *outfile
= NULL
;
123 char **argp
, **arguments
;
124 struct msgs_array msgs
= { 0, 0, NULL
};
125 struct msgs
*mp
= NULL
;
130 fix_transformations fx
;
131 fx
.reformat
= fx
.fixcte
= fx
.fixboundary
= 1;
132 fx
.decodetext
= CE_8BIT
;
133 fx
.textcodeset
= NULL
;
138 setlocale(LC_ALL
, "");
140 invo_name
= r1bindex (argv
[0], '/');
142 /* read user profile/context */
145 arguments
= getarguments (invo_name
, argc
, argv
, 1);
151 while ((cp
= *argp
++)) {
153 switch (smatch (++cp
, switches
)) {
155 ambigsw (cp
, switches
);
158 adios (NULL
, "-%s unknown", cp
);
161 snprintf (buf
, sizeof buf
, "%s [+folder] [msgs] [switches]",
163 print_help (buf
, switches
, 1);
166 print_version(invo_name
);
170 if (! (cp
= *argp
++) || *cp
== '-')
171 adios (NULL
, "missing argument to %s", argp
[-2]);
172 if (! strcasecmp (cp
, "8bit")) {
173 fx
.decodetext
= CE_8BIT
;
174 } else if (! strcasecmp (cp
, "7bit")) {
175 fx
.decodetext
= CE_7BIT
;
177 adios (NULL
, "invalid argument to %s", argp
[-2]);
184 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
185 adios (NULL
, "missing argument to %s", argp
[-2]);
211 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
212 adios (NULL
, "missing argument to %s", argp
[-2]);
213 file
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
217 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
218 adios (NULL
, "missing argument to %s", argp
[-2]);
219 outfile
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
223 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
224 adios (NULL
, "missing argument to %s", argp
[-2]);
238 if (*cp
== '+' || *cp
== '@') {
240 adios (NULL
, "only one folder at a time!");
242 folder
= pluspath (cp
);
244 app_msgarg(&msgs
, cp
);
247 SIGNAL (SIGQUIT
, quitser
);
248 SIGNAL (SIGPIPE
, pipeser
);
251 * Read the standard profile setup
253 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
254 readconfig ((struct node
**) 0, fp
, cp
, 0);
259 * Check for storage directory. If specified,
260 * then store temporary files there. Else we
261 * store them in standard nmh directory.
263 if ((cp
= context_find (nmhstorage
)) && *cp
)
264 tmp
= concat (cp
, "/", invo_name
, NULL
);
266 tmp
= add (m_maildir (invo_name
), NULL
);
268 suppress_bogus_mp_content_warning
= skip_mp_cte_check
= 1;
270 if (! context_find ("path"))
271 free (path ("./", TFOLDER
));
273 if (file
&& msgs
.size
)
274 adios (NULL
, "cannot specify msg and file at same time!");
277 * check if message is coming from file
280 /* If file is stdin, create a tmp file name before parse_mime()
281 has a chance, because it might put in on a different
282 filesystem than the output file. Instead, put it in the
283 user's preferred tmp directory. */
286 if (! strcmp ("-", file
)) {
292 if ((cp
= m_mktemp2 (tmp
, invo_name
, &fd
, NULL
)) == NULL
) {
293 adios (NULL
, "unable to create temporary file");
296 file
= add (cp
, NULL
);
298 cpydata (STDIN_FILENO
, fd
, "-", file
);
303 adios (NULL
, "failed to write temporary file");
307 if (! (cts
= (CT
*) calloc ((size_t) 2, sizeof *cts
)))
308 adios (NULL
, "out of memory");
311 if ((ct
= parse_mime (file
)))
315 * message(s) are coming from a folder
320 app_msgarg(&msgs
, "cur");
322 folder
= getfolder (1);
323 maildir
= m_maildir (folder
);
325 if (chdir (maildir
) == NOTOK
)
326 adios (maildir
, "unable to change directory to");
328 /* read folder and create message structure */
329 if (! (mp
= folder_read (folder
, 1)))
330 adios (NULL
, "unable to read folder %s", folder
);
332 /* check for empty folder */
334 adios (NULL
, "no messages in %s", folder
);
336 /* parse all the message ranges/sequences and set SELECTED */
337 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
338 if (! m_convert (mp
, msgs
.msgs
[msgnum
]))
340 seq_setprev (mp
); /* set the previous-sequence */
342 if (! (cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof *cts
)))
343 adios (NULL
, "out of memory");
346 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
347 if (is_selected(mp
, msgnum
)) {
350 msgnam
= m_name (msgnum
);
351 if ((ct
= parse_mime (msgnam
)))
357 * This is a hack. If we are using an external rmmproc,
358 * then save the current folder to the context file,
359 * so the external rmmproc will remove files from the correct
360 * directory. This should be moved to folder_delmsgs().
363 context_replace (pfolder
, folder
);/* update current folder */
364 seq_setcur (mp
, mp
->hghsel
); /* update current message */
365 seq_save (mp
); /* synchronize sequences */
366 context_save (); /* save the context file */
372 for (ctp
= cts
; *ctp
; ++ctp
) {
373 status
+= mhfixmsgsbr (ctp
, &fx
, outfile
);
379 /* Just calling m_backup() unlinks the backup file. */
380 (void) m_backup (file
);
392 /* done is freects_done, which will clean up all of cts. */
399 mhfixmsgsbr (CT
*ctp
, const fix_transformations
*fx
, char *outfile
) {
400 /* Store input filename in case one of the transformations, i.e.,
401 fix_boundary(), rewrites to a tmp file. */
402 char *input_filename
= add ((*ctp
)->c_file
, NULL
);
403 int modify_inplace
= 0;
404 int message_mods
= 0;
407 if (outfile
== NULL
) {
410 if ((*ctp
)->c_file
) {
411 outfile
= add (m_mktemp2 (tmp
, invo_name
, NULL
, NULL
), NULL
);
413 adios (NULL
, "missing both input and output filenames\n");
417 reverse_alternative_parts (*ctp
);
418 if (status
== OK
&& fx
->fixboundary
) {
419 status
= fix_boundary (ctp
, &message_mods
);
421 if (status
== OK
&& fx
->fixcte
) {
422 status
= fix_multipart_cte (*ctp
, &message_mods
);
424 if (status
== OK
&& fx
->reformat
) {
425 status
= ensure_text_plain (ctp
, NULL
, &message_mods
);
427 if (status
== OK
&& fx
->decodetext
) {
428 status
= decode_text_parts (*ctp
, fx
->decodetext
, &message_mods
);
430 if (status
== OK
&& fx
->textcodeset
!= NULL
) {
431 status
= convert_codesets (*ctp
, fx
->textcodeset
, &message_mods
);
434 if (! (*ctp
)->c_umask
) {
435 /* Set the umask for the contents file. This currently
436 isn't used but just in case it is in the future. */
439 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
440 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
442 (*ctp
)->c_umask
= ~m_gmprot();
447 * Write the content to a file
450 status
= write_content (*ctp
, input_filename
, outfile
, modify_inplace
,
452 } else if (! modify_inplace
) {
453 /* Something went wrong. Output might be expected, such
454 as if this were run as a filter. Just copy the input
456 int in
= open (input_filename
, O_RDONLY
);
457 int out
= strcmp (outfile
, "-")
458 ? open (outfile
, O_WRONLY
| O_CREAT
, m_gmprot ())
461 if (in
!= -1 && out
!= -1) {
462 cpydata (in
, out
, input_filename
, outfile
);
471 if (modify_inplace
) {
476 free (input_filename
);
482 /* parse_mime() arranges alternates in reverse (priority) order, so
483 reverse them back. This will put a text/plain part at the front of
484 a multipart/alternative part, for example, where it belongs. */
486 reverse_alternative_parts (CT ct
) {
487 if (ct
->c_type
== CT_MULTIPART
) {
488 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
491 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
495 /* And call recursively on each part of a multipart. */
496 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
497 reverse_alternative_parts (part
->mp_part
);
504 fix_boundary (CT
*ct
, int *message_mods
) {
505 struct multipart
*mp
;
508 if (bogus_mp_content
) {
509 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
512 * 1) Get boundary at end of part.
513 * 2) Get boundary at beginning of part and compare to the end-of-part
515 * 3) Write out contents of ct to tmp file, replacing boundary in
516 * header with boundary from part. Set c_unlink to 1.
518 * 5) Call parse_mime() on the tmp file, replacing ct.
521 if (mp
&& mp
->mp_start
) {
524 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
527 if ((fixed
= m_mktemp2 (tmp
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
528 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
529 char *filename
= add ((*ct
)->c_file
, NULL
);
532 *ct
= parse_mime (fixed
);
537 report (NULL
, filename
, "fix multipart boundary");
541 advise (NULL
, "unable to replace broken boundary");
545 advise (NULL
, "unable to create temporary file");
549 free (part_boundary
);
559 get_multipart_boundary (CT ct
, char **part_boundary
) {
561 char *end_boundary
= NULL
;
562 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
563 ? (off_t
) (ct
->c_end
- sizeof buffer
)
564 : (off_t
) ct
->c_begin
;
568 /* This will fail if the boundary spans fread() calls. BUFSIZ should
569 be big enough, even if it's just 1024, to make that unlikely. */
571 /* free_content() will close ct->c_fp. */
572 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
573 advise (ct
->c_file
, "unable to open for reading");
577 /* Get boundary at end of multipart. */
578 while (begin
>= (off_t
) ct
->c_begin
) {
579 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
580 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
581 char *end
= buffer
+ bytes_read
- 1;
584 if ((cp
= rfind_str (buffer
, bytes_read
, "--"))) {
585 /* Trim off trailing "--" and anything beyond. */
587 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
588 if (strlen (end
) > 3 && *end
++ == '\n' &&
589 *end
++ == '-' && *end
++ == '-') {
590 end_boundary
= add (end
, NULL
);
597 if (! end_boundary
&& begin
> (off_t
) (ct
->c_begin
+ sizeof buffer
)) {
598 begin
-= sizeof buffer
;
604 /* Get boundary at beginning of multipart. */
606 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
607 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
608 if (bytes_read
>= strlen (end_boundary
)) {
609 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
611 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
612 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
615 /* Else the start and end boundaries didn't match, or
616 the start boundary doesn't begin with "\n--" (or
617 "--" if at the beginning of buffer). Keep trying. */
625 *part_boundary
= end_boundary
;
627 *part_boundary
= NULL
;
635 /* Open and copy ct->c_file to file, replacing the multipart boundary. */
637 replace_boundary (CT ct
, char *file
, const char *boundary
) {
640 char buf
[BUFSIZ
], name
[NAMESZ
];
642 m_getfld_state_t gstate
= 0;
645 if (ct
->c_file
== NULL
) {
646 advise (NULL
, "missing input filename");
650 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
651 advise (ct
->c_file
, "unable to open for reading");
655 if ((fpout
= fopen (file
, "w")) == NULL
) {
657 advise (file
, "unable to open for writing");
661 for (compnum
= 1;;) {
662 int bufsz
= (int) sizeof buf
;
664 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
)) {
669 /* get copies of the buffers */
670 np
= add (name
, NULL
);
671 vp
= add (buf
, NULL
);
673 /* if necessary, get rest of field */
674 while (state
== FLDPLUS
) {
676 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
);
677 vp
= add (buf
, vp
); /* add to previous value */
680 if (strcasecmp (TYPE_FIELD
, np
)) {
681 fprintf (fpout
, "%s:%s", np
, vp
);
683 char *new_boundary
= update_attr (vp
, "boundary=", boundary
);
685 fprintf (fpout
, "%s:%s\n", np
, new_boundary
);
696 /* buf will have a terminating NULL, skip it. */
697 fwrite (buf
, 1, bufsz
-1, fpout
);
705 advise (NULL
, "message format error in component #%d", compnum
);
710 advise (NULL
, "getfld() returned %d", state
);
718 m_getfld_state_destroy (&gstate
);
726 /* Change the value of a name=value pair in a header field body.
727 If the name isn't there, append them. In any case, a new
728 string will be allocated and must be free'd by the caller.
729 Trims any trailing newlines. */
731 update_attr (char *body
, const char *name
, const char *value
) {
732 char *bp
= nmh_strcasestr (body
, name
);
736 char *other_attrs
= strchr (bp
, ';');
738 *(bp
+ strlen (name
)) = '\0';
739 new_body
= concat (body
, "\"", value
, "\"", NULL
);
744 /* Trim any trailing newlines. */
745 for (cp
= &other_attrs
[strlen (other_attrs
) - 1];
746 cp
> other_attrs
&& *cp
== '\n';
747 *cp
-- = '\0') continue;
748 new_body
= add (other_attrs
, new_body
);
753 /* Append name/value pair, after first removing a final newline
754 and (extraneous) semicolon. */
755 if (*(cp
= &body
[strlen (body
) - 1]) == '\n') *cp
= '\0';
756 if (*(cp
= &body
[strlen (body
) - 1]) == ';') *cp
= '\0';
757 new_body
= concat (body
, "; ", name
, "\"", value
, "\"", NULL
);
765 fix_multipart_cte (CT ct
, int *message_mods
) {
768 if (ct
->c_type
== CT_MULTIPART
) {
772 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
773 ct
->c_encoding
!= CE_BINARY
) {
776 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
777 char *name
= hf
->name
;
778 for (; *name
&& isspace ((unsigned char) *name
); ++name
) {
782 if (! strncasecmp (name
, ENCODING_FIELD
,
783 strlen (ENCODING_FIELD
))) {
784 char *prefix
= "Nmh-REPLACED-INVALID-";
785 HF h
= mh_xmalloc (sizeof *h
);
787 h
->name
= add (hf
->name
, NULL
);
788 h
->hf_encoding
= hf
->hf_encoding
;
792 /* Retain old header but prefix its name. */
794 hf
->name
= concat (prefix
, h
->name
, NULL
);
798 char *encoding
= cpytrim (hf
->value
);
799 report (ct
->c_partno
, ct
->c_file
,
800 "replace Content-Transfer-Encoding of %s "
801 "with 8 bit", encoding
);
805 h
->value
= add (" 8bit\n", NULL
);
807 /* Don't need to warn for multiple C-T-E header
808 fields, parse_mime() already does that. But
809 if there are any, fix them all as necessary. */
814 set_ce (ct
, CE_8BIT
);
817 m
= (struct multipart
*) ct
->c_ctparams
;
818 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
819 if (fix_multipart_cte (part
->mp_part
, message_mods
) != OK
) {
831 set_ce (CT ct
, int encoding
) {
832 const char *ce
= ce_str (encoding
);
833 const struct str2init
*ctinit
= get_ce_method (ce
);
836 char *cte
= concat (" ", ce
, "\n", NULL
);
839 /* Decoded contents might be in ct->c_cefile.ce_file, if the
840 caller is decode_text_parts (). Save because we'll
842 struct cefile decoded_content_info
= ct
->c_cefile
;
844 ct
->c_encoding
= encoding
;
846 ct
->c_ctinitfnx
= ctinit
->si_init
;
847 /* This will assign ct->c_cefile with an all-0 struct, which
849 (*ctinit
->si_init
) (ct
);
850 /* After returning, the caller should set
851 ct->c_cefile.ce_file to the name of the file containing
854 /* Restore the cefile. */
855 ct
->c_cefile
= decoded_content_info
;
857 /* Update/add Content-Transfer-Encoding header field. */
858 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
859 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
866 add_header (ct
, add (ENCODING_FIELD
, NULL
), cte
);
869 /* Update c_celine. It's used only by mhlist -debug. */
871 ct
->c_celine
= add (cte
, NULL
);
880 /* Make sure each text part has a corresponding text/plain part. */
882 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
) {
885 switch ((*ct
)->c_type
) {
887 int has_text_plain
= 0;
889 /* Nothing to do for text/plain. */
890 if ((*ct
)->c_subtype
== TEXT_PLAIN
) return OK
;
892 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
893 parent
->c_subtype
== MULTI_ALTERNATE
) {
894 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
896 int new_subpart_number
= 1;
898 /* See if there is a sibling text/plain. */
899 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
900 ++new_subpart_number
;
901 if (part
->mp_part
->c_type
== CT_TEXT
&&
902 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
908 if (! has_text_plain
) {
909 /* Parent is a multipart/alternative. Insert a new
910 text/plain subpart. */
911 struct part
*new_part
= mh_xmalloc (sizeof *new_part
);
913 if ((new_part
->mp_part
= build_text_plain_part (*ct
))) {
915 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
917 new_part
->mp_next
= mp
->mp_parts
;
918 mp
->mp_parts
= new_part
;
919 new_part
->mp_part
->c_partno
=
920 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
925 report (parent
->c_partno
, parent
->c_file
,
926 "insert text/plain part");
929 free_content (new_part
->mp_part
);
935 /* Slip new text/plain part into a new multipart/alternative. */
936 CT tp_part
= build_text_plain_part (*ct
);
939 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
942 struct multipart
*mp
=
943 (struct multipart
*) mp_alt
->c_ctparams
;
945 if (mp
&& mp
->mp_parts
) {
946 mp
->mp_parts
->mp_part
= tp_part
;
947 /* Make the new multipart/alternative the parent. */
952 report ((*ct
)->c_partno
, (*ct
)->c_file
,
953 "insert text/plain part");
956 free_content (tp_part
);
957 free_content (mp_alt
);
967 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
970 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
971 if ((*ct
)->c_type
== CT_MULTIPART
) {
972 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
);
979 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
982 e
= (struct exbody
*) (*ct
)->c_ctparams
;
983 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
);
993 build_text_plain_part (CT encoded_part
) {
994 CT tp_part
= divide_part (encoded_part
);
995 char *tmp_plain_file
= NULL
;
997 if (decode_part (tp_part
) == OK
) {
998 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
999 contains the decoded contents. And the decoding function, such
1000 as openQuoted, will have set ...->ce_unlink to 1 so that it will
1001 be unlinked by free_content (). */
1002 tmp_plain_file
= add (m_mktemp2 (tmp
, invo_name
, NULL
, NULL
), NULL
);
1003 if (reformat_part (tp_part
, tmp_plain_file
,
1004 tp_part
->c_ctinfo
.ci_type
,
1005 tp_part
->c_ctinfo
.ci_subtype
,
1006 tp_part
->c_type
) == OK
) {
1011 free_content (tp_part
);
1012 unlink (tmp_plain_file
);
1013 free (tmp_plain_file
);
1020 divide_part (CT ct
) {
1023 if ((new_part
= (CT
) calloc (1, sizeof *new_part
)) == NULL
)
1024 adios (NULL
, "out of memory");
1026 /* Just copy over what is needed for decoding. c_vrsn and
1027 c_celine aren't necessary. */
1028 new_part
->c_file
= add (ct
->c_file
, NULL
);
1029 new_part
->c_begin
= ct
->c_begin
;
1030 new_part
->c_end
= ct
->c_end
;
1031 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1032 new_part
->c_type
= ct
->c_type
;
1033 new_part
->c_cefile
= ct
->c_cefile
;
1034 new_part
->c_encoding
= ct
->c_encoding
;
1035 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1036 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1037 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1038 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1040 /* c_ctline is used by reformat__part(), so it can preserve
1041 anything after the type/subtype. */
1042 new_part
->c_ctline
= add (ct
->c_ctline
, NULL
);
1049 copy_ctinfo (CI dest
, CI src
) {
1050 char **s_ap
, **d_ap
, **s_vp
, **d_vp
;
1052 dest
->ci_type
= src
->ci_type
? add (src
->ci_type
, NULL
) : NULL
;
1053 dest
->ci_subtype
= src
->ci_subtype
? add (src
->ci_subtype
, NULL
) : NULL
;
1055 for (s_ap
= src
->ci_attrs
, d_ap
= dest
->ci_attrs
,
1056 s_vp
= src
->ci_values
, d_vp
= dest
->ci_values
;
1058 ++s_ap
, ++d_ap
, ++s_vp
, ++d_vp
) {
1059 *d_ap
= add (*s_ap
, NULL
);
1064 dest
->ci_comment
= src
->ci_comment
? add (src
->ci_comment
, NULL
) : NULL
;
1065 dest
->ci_magic
= src
->ci_magic
? add (src
->ci_magic
, NULL
) : NULL
;
1070 decode_part (CT ct
) {
1074 tmp_decoded
= add (m_mktemp2 (tmp
, invo_name
, NULL
, NULL
), NULL
);
1075 /* The following call will load ct->c_cefile.ce_file with the tmp
1076 filename of the decoded content. tmp_decoded will contain the
1077 encoded output, get rid of that. */
1078 status
= output_message (ct
, tmp_decoded
);
1079 unlink (tmp_decoded
);
1086 /* Some of the arguments aren't really needed now, but maybe will
1087 be in the future for other than text types. */
1089 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
) {
1090 int output_subtype
, output_encoding
;
1094 /* Hacky: this redirects the output from whatever command is used
1095 to show the part to a file. So, the user can't have any output
1096 redirection in that command.
1097 Could show_multi() in mhshowsbr.c avoid this? */
1099 /* Check for invo_name-format-type/subtype. */
1100 cp
= concat (invo_name
, "-format-", type
, "/", subtype
, NULL
);
1101 if ((cf
= context_find (cp
)) && *cf
!= '\0') {
1102 if (strchr (cf
, '>')) {
1104 advise (NULL
, "'>' prohibited in \"%s\",\nplease fix your "
1105 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1112 /* Check for invo_name-format-type. */
1113 cp
= concat (invo_name
, "-format-", type
, NULL
);
1114 if (! (cf
= context_find (cp
)) || *cf
== '\0') {
1117 advise (NULL
, "Don't know how to convert %s, there is no "
1118 "%s-format-%s/%s profile entry",
1119 ct
->c_file
, invo_name
, type
, subtype
);
1124 if (strchr (cf
, '>')) {
1126 advise (NULL
, "'>' prohibited in \"%s\"", cf
);
1132 cp
= concat (cf
, " >", file
, NULL
);
1133 status
= show_content_aux (ct
, 1, 0, cp
, NULL
);
1136 /* Unlink decoded content tmp file and free its filename to avoid
1137 leaks. The file stream should already have been closed. */
1138 if (ct
->c_cefile
.ce_unlink
) {
1139 unlink (ct
->c_cefile
.ce_file
);
1140 free (ct
->c_cefile
.ce_file
);
1141 ct
->c_cefile
.ce_file
= NULL
;
1142 ct
->c_cefile
.ce_unlink
= 0;
1145 if (c_type
== CT_TEXT
) {
1146 output_subtype
= TEXT_PLAIN
;
1148 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1151 output_encoding
= charset_encoding (ct
);
1153 if (set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1154 ct
->c_cefile
.ce_file
= file
;
1155 ct
->c_cefile
.ce_unlink
= 1;
1157 ct
->c_cefile
.ce_unlink
= 0;
1165 /* Identifies 7bit or 8bit content based on charset, if specified. */
1167 charset_encoding (CT ct
) {
1168 int encoding
= CE_8BIT
;
1169 CI ctinfo
= &ct
->c_ctinfo
;
1172 for (ap
= ctinfo
->ci_attrs
, vp
= ctinfo
->ci_values
; *ap
; ++ap
, ++vp
) {
1173 if (! strcasecmp (*ap
, "charset")) {
1174 /* norm_charmap() is case sensitive. */
1175 char *ch
= upcase (*vp
);
1177 if (! strcmp (norm_charmap (ch
), "US-ASCII")) encoding
= CE_7BIT
;
1188 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
) {
1189 char *boundary_prefix
= "----=_nmh-multipart";
1190 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1191 char *boundary_indicator
= "; boundary=";
1192 char *typename
, *subtypename
, *name
;
1195 struct multipart
*m
;
1197 const struct str2init
*ctinit
;
1199 if ((ct
= (CT
) calloc (1, sizeof *ct
)) == NULL
)
1200 adios (NULL
, "out of memory");
1202 /* Set up the multipart/alternative part. These fields of *ct were
1203 initialized to 0 by calloc():
1204 c_fp, c_unlink, c_begin, c_end,
1205 c_vrsn, c_ctline, c_celine,
1206 c_id, c_descr, c_dispo, c_partno,
1207 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1208 c_cefile, c_encoding,
1209 c_digested, c_digest[16], c_ctexbody,
1210 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1211 c_umask, c_pid, c_rfc934,
1212 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1215 ct
->c_file
= add (first_alt
->c_file
, NULL
);
1217 ct
->c_subtype
= subtype
;
1219 ctinit
= get_ct_init (ct
->c_type
);
1221 typename
= ct_type_str (type
);
1222 subtypename
= ct_subtype_str (type
, subtype
);
1226 int found_boundary
= 1;
1228 while (found_boundary
&& serial
< 1000000) {
1231 /* Ensure that the boundary doesn't appear in the decoded
1233 if (new_part
->c_cefile
.ce_file
) {
1234 if ((found_boundary
=
1235 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1236 new_part
->c_cefile
.ce_file
,
1242 /* Ensure that the boundary doesn't appear in the encoded
1244 if (! found_boundary
&& new_part
->c_file
) {
1245 if ((found_boundary
= boundary_in_content (&new_part
->c_fp
,
1252 if (found_boundary
) {
1253 /* Try a slightly different boundary. */
1258 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1260 concat (boundary_prefix
,
1261 first_alt
->c_partno
? first_alt
->c_partno
: "",
1262 "-", buffer2
, NULL
);
1266 if (found_boundary
) {
1267 advise (NULL
, "giving up trying to find a unique boundary");
1272 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1273 boundary
, "\"", NULL
);
1275 /* Load c_first_hf and c_last_hf. */
1276 transfer_noncontent_headers (first_alt
, ct
);
1277 add_header (ct
, add (TYPE_FIELD
, NULL
), concat (name
, "\n", NULL
));
1280 /* Load c_partno. */
1281 if (first_alt
->c_partno
) {
1282 ct
->c_partno
= add (first_alt
->c_partno
, NULL
);
1283 free (first_alt
->c_partno
);
1284 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1285 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1287 first_alt
->c_partno
= add ("1", NULL
);
1288 new_part
->c_partno
= add ("2", NULL
);
1292 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1293 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1296 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
,
1298 if ((cp
= strstr (name
, boundary_indicator
))) {
1299 ct
->c_ctinfo
.ci_attrs
[0] = name
;
1300 ct
->c_ctinfo
.ci_attrs
[1] = NULL
;
1301 /* ci_values don't get free'd, so point into ci_attrs. */
1302 ct
->c_ctinfo
.ci_values
[0] = cp
+ strlen (boundary_indicator
);
1305 p
= (struct part
*) mh_xmalloc (sizeof *p
);
1306 p
->mp_next
= (struct part
*) mh_xmalloc (sizeof *p
->mp_next
);
1307 p
->mp_next
->mp_next
= NULL
;
1308 p
->mp_next
->mp_part
= first_alt
;
1310 if ((m
= (struct multipart
*) calloc (1, sizeof (struct multipart
))) ==
1312 adios (NULL
, "out of memory");
1313 m
->mp_start
= concat (boundary
, "\n", NULL
);
1314 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1316 ct
->c_ctparams
= (void *) m
;
1324 /* Check that the boundary does not appear in the content. */
1326 boundary_in_content (FILE **fp
, char *file
, const char *boundary
) {
1327 char buffer
[BUFSIZ
];
1329 int found_boundary
= 0;
1331 /* free_content() will close *fp if we fopen it here. */
1332 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1333 advise (file
, "unable to open %s for reading", file
);
1337 fseeko (*fp
, 0L, SEEK_SET
);
1338 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1339 if (find_str (buffer
, bytes_read
, boundary
)) {
1345 return found_boundary
;
1349 /* Remove all non-Content headers. */
1351 transfer_noncontent_headers (CT old
, CT
new) {
1354 hp_prev
= hp
= old
->c_first_hf
;
1358 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, strlen (XXX_FIELD_PRF
))) {
1359 if (hp
== old
->c_last_hf
) {
1360 if (hp
== old
->c_first_hf
) {
1361 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1363 hp_prev
->next
= NULL
;
1364 old
->c_last_hf
= hp_prev
;
1367 if (hp
== old
->c_first_hf
) {
1368 old
->c_first_hf
= next
;
1370 hp_prev
->next
= next
;
1374 /* Put node hp in the new CT. */
1375 if (new->c_first_hf
== NULL
) {
1376 new->c_first_hf
= hp
;
1378 new->c_last_hf
->next
= hp
;
1380 new->c_last_hf
= hp
;
1382 /* A Content- header, leave in old. */
1392 set_ct_type (CT ct
, int type
, int subtype
, int encoding
) {
1393 char *typename
= ct_type_str (type
);
1394 char *subtypename
= ct_subtype_str (type
, subtype
);
1395 /* E.g, " text/plain" */
1396 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1397 /* E.g, " text/plain\n" */
1398 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1399 int found_content_type
= 0;
1401 const char *cp
= NULL
;
1405 /* Update/add Content-Type header field. */
1406 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1407 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1408 found_content_type
= 1;
1410 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1411 ? concat (type_subtypename
, cp
, "\n", NULL
)
1412 : add (name_plus_nl
, NULL
);
1415 if (! found_content_type
) {
1416 add_header (ct
, add (TYPE_FIELD
, NULL
),
1417 (cp
= strchr (ct
->c_ctline
, ';'))
1418 ? concat (type_subtypename
, cp
, "\n", NULL
)
1419 : add (name_plus_nl
, NULL
));
1422 /* Some of these might not be used, but set them anyway. */
1424 ? concat (type_subtypename
, cp
, NULL
)
1425 : concat (type_subtypename
, NULL
);
1426 free (ct
->c_ctline
);
1427 ct
->c_ctline
= ctline
;
1428 /* Leave other ctinfo members as they were. */
1429 free (ct
->c_ctinfo
.ci_type
);
1430 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1431 free (ct
->c_ctinfo
.ci_subtype
);
1432 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1434 ct
->c_subtype
= subtype
;
1436 free (name_plus_nl
);
1437 free (type_subtypename
);
1439 status
= set_ce (ct
, encoding
);
1446 decode_text_parts (CT ct
, int encoding
, int *message_mods
) {
1449 switch (ct
->c_type
) {
1451 switch (ct
->c_encoding
) {
1456 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
1457 if ((ct_encoding
= content_encoding (ct
)) == CE_BINARY
&&
1458 encoding
!= CE_BINARY
) {
1460 report (ct
->c_partno
, ct
->c_file
,
1461 "will not decode%s because it is binary",
1463 : ct
->c_ctline
? ct
->c_ctline
1466 unlink (ct
->c_cefile
.ce_file
);
1467 free (ct
->c_cefile
.ce_file
);
1468 ct
->c_cefile
.ce_file
= NULL
;
1469 } else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
1471 report (ct
->c_partno
, ct
->c_file
,
1472 "will not decode%s because it is 8bit",
1474 : ct
->c_ctline
? ct
->c_ctline
1477 unlink (ct
->c_cefile
.ce_file
);
1478 free (ct
->c_cefile
.ce_file
);
1479 ct
->c_cefile
.ce_file
= NULL
;
1481 int enc
= ct_encoding
== CE_BINARY
1483 : charset_encoding (ct
);
1484 if (set_ce (ct
, enc
) == OK
) {
1487 report (ct
->c_partno
, ct
->c_file
, "decode%s",
1488 ct
->c_ctline
? ct
->c_ctline
: "");
1504 case CT_MULTIPART
: {
1505 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1508 /* Should check to see if the body for this part is encoded?
1509 For now, it gets passed along as-is by InitMultiPart(). */
1510 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1511 status
= decode_text_parts (part
->mp_part
, encoding
, message_mods
);
1517 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1520 e
= (struct exbody
*) ct
->c_ctparams
;
1521 status
= decode_text_parts (e
->eb_content
, encoding
, message_mods
);
1533 /* See if the decoded content is 7bit, 8bit, or binary. It's binary
1534 if it has any NUL characters, a CR not followed by a LF, or lines
1535 greater than 998 characters in length. */
1537 content_encoding (CT ct
) {
1538 CE ce
= &ct
->c_cefile
;
1539 int encoding
= CE_7BIT
;
1542 char buffer
[BUFSIZ
];
1545 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1546 advise (ce
->ce_file
, "unable to open for reading");
1550 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
1551 while (encoding
!= CE_BINARY
&&
1552 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
1555 size_t line_len
= 0;
1556 int last_char_was_cr
= 0;
1558 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
1559 if (*cp
== '\0' || ++line_len
> 998 ||
1560 (*cp
!= '\n' && last_char_was_cr
)) {
1561 encoding
= CE_BINARY
;
1563 } else if (*cp
== '\n') {
1565 } else if (! isascii ((unsigned char) *cp
)) {
1569 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
1575 } /* else should never happen */
1582 convert_codesets (CT ct
, char *dest_codeset
, int *message_mods
) {
1585 switch (ct
->c_type
) {
1587 if (ct
->c_subtype
== TEXT_PLAIN
) {
1588 status
= convert_codeset (ct
, dest_codeset
, message_mods
);
1592 case CT_MULTIPART
: {
1593 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1596 /* Should check to see if the body for this part is encoded?
1597 For now, it gets passed along as-is by InitMultiPart(). */
1598 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1600 convert_codesets (part
->mp_part
, dest_codeset
, message_mods
);
1606 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1609 e
= (struct exbody
*) ct
->c_ctparams
;
1611 convert_codesets (e
->eb_content
, dest_codeset
, message_mods
);
1624 convert_codeset (CT ct
, char *dest_codeset
, int *message_mods
) {
1625 const char *const charset
= "charset";
1626 char **src_codeset
= NULL
;
1627 char *default_codeset
= NULL
;
1628 CI ctinfo
= &ct
->c_ctinfo
;
1632 for (ap
= ctinfo
->ci_attrs
, vp
= ctinfo
->ci_values
; *ap
; ++ap
, ++vp
) {
1633 if (! strcasecmp (*ap
, charset
)) {
1638 /* RFC 2045, Sec. 5.2: default to us-ascii. */
1639 if (src_codeset
== NULL
) src_codeset
= &default_codeset
;
1640 if (*src_codeset
== NULL
) *src_codeset
= "US-ASCII";
1642 if (strcmp (norm_charmap (*src_codeset
), norm_charmap (dest_codeset
))) {
1644 iconv_t conv_desc
= NULL
;
1651 int opened_input_file
= 0;
1652 char src_buffer
[BUFSIZ
];
1655 if ((conv_desc
= iconv_open (dest_codeset
, *src_codeset
)) ==
1657 advise (NULL
, "Can't convert %s to %s", *src_codeset
, dest_codeset
);
1661 dest
= add (m_mktemp2 (tmp
, invo_name
, &fd
, NULL
), NULL
);
1663 if (ct
->c_cefile
.ce_file
) {
1664 file
= &ct
->c_cefile
.ce_file
;
1665 fp
= &ct
->c_cefile
.ce_fp
;
1668 } else if (ct
->c_file
) {
1671 begin
= ct
->c_begin
;
1673 } /* else no input file: shouldn't happen */
1675 if (file
&& *file
&& fp
) {
1677 if ((*fp
= fopen (*file
, "r")) == NULL
) {
1678 advise (*file
, "unable to open for reading");
1681 opened_input_file
= 1;
1688 size_t max
= end
> 0 ? (size_t) (end
-begin
) : sizeof src_buffer
;
1690 fseeko (*fp
, begin
, SEEK_SET
);
1691 while (status
== OK
&& max
> 0 &&
1692 (inbytes
= fread (src_buffer
, 1, max
, *fp
)) > 0) {
1693 char dest_buffer
[BUFSIZ
];
1694 char *ib
= src_buffer
, *ob
= dest_buffer
;
1695 size_t outbytes
= sizeof dest_buffer
;
1696 size_t outbytes_before
= outbytes
;
1698 if (end
> 0) max
-= inbytes
;
1700 if (iconv (conv_desc
, &ib
, &inbytes
, &ob
, &outbytes
) ==
1705 write (fd
, dest_buffer
, outbytes_before
- outbytes
);
1709 if (opened_input_file
) {
1715 iconv_close (conv_desc
);
1719 /* Replace the decoded file with the converted one. */
1720 if (ct
->c_cefile
.ce_file
) {
1721 if (ct
->c_cefile
.ce_unlink
) {
1722 unlink (ct
->c_cefile
.ce_file
);
1724 free (ct
->c_cefile
.ce_file
);
1726 ct
->c_cefile
.ce_file
= dest
;
1727 ct
->c_cefile
.ce_unlink
= 1;
1731 report (ct
->c_partno
, ct
->c_file
, "convert %s to %s",
1732 *src_codeset
, dest_codeset
);
1735 /* Update ci_attrs. */
1736 *src_codeset
= dest_codeset
;
1738 /* Update ct->c_ctline. */
1741 update_attr (ct
->c_ctline
, "charset=", dest_codeset
);
1743 free (ct
->c_ctline
);
1744 ct
->c_ctline
= ctline
;
1745 } /* else no CT line, which is odd */
1747 /* Update Content-Type header field. */
1748 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1749 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1750 char *ctline_less_newline
=
1751 update_attr (hf
->value
, "charset=", dest_codeset
);
1752 char *ctline
= concat (ctline_less_newline
, "\n", NULL
);
1753 free (ctline_less_newline
);
1763 #else /* ! HAVE_ICONV */
1764 NMH_UNUSED (message_mods
);
1766 advise (NULL
, "Can't convert %s to %s without iconv", *src_codeset
,
1769 #endif /* ! HAVE_ICONV */
1777 write_content (CT ct
, char *input_filename
, char *outfile
, int modify_inplace
,
1781 if (modify_inplace
) {
1782 if (message_mods
> 0) {
1783 if ((status
= output_message (ct
, outfile
)) == OK
) {
1784 char *infile
= input_filename
1785 ? add (input_filename
, NULL
)
1786 : add (ct
->c_file
? ct
->c_file
: "-", NULL
);
1788 if (remove_file (infile
) == OK
) {
1789 if (rename (outfile
, infile
)) {
1790 /* Rename didn't work, possibly because of an
1791 attempt to rename across filesystems. Try
1792 brute force copy. */
1793 int old
= open (outfile
, O_RDONLY
);
1795 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
1798 if (old
!= -1 && new != -1) {
1799 char buffer
[BUFSIZ
];
1801 while ((i
= read (old
, buffer
, sizeof buffer
)) >
1803 if (write (new, buffer
, i
) != i
) {
1809 if (new != -1) close (new);
1810 if (old
!= -1) close (old
);
1814 /* The -file argument processing used path() to
1815 expand filename to absolute path. */
1816 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
1818 admonish (NULL
, "unable to rename %s %s to %s",
1819 file
? "file" : "message", outfile
,
1825 admonish (NULL
, "unable to remove input file %s, "
1826 "not modifying it", infile
);
1834 /* No modifications and didn't need the tmp outfile. */
1838 /* Output is going to some file. Produce it whether or not
1839 there were modifications. */
1840 status
= output_message (ct
, outfile
);
1849 * If "rmmproc" is defined, call that to remove the file. Otherwise,
1850 * use the standard MH backup file.
1853 remove_file (char *file
) {
1855 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
1856 int status
= system (rmm_command
);
1859 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
1861 /* This is OK for a non-message file, it still uses the
1862 BACKUP_PREFIX form. The backup file will be in the same
1863 directory as file. */
1864 return rename (file
, m_backup (file
));
1870 report (char *partno
, char *filename
, char *message
, ...) {
1875 va_start (args
, message
);
1876 fmt
= concat (filename
, partno
? " part " : ", ",
1877 partno
? partno
: "", partno
? ", " : "", message
, NULL
);
1879 advertise (NULL
, NULL
, fmt
, args
);
1888 upcase (char *str
) {
1889 char *up
= cpytrim (str
);
1892 for (cp
= up
; *cp
; ++cp
) *cp
= toupper ((unsigned char) *cp
);
1903 fprintf (stderr
, "\n");