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>
20 #define MHFIXMSG_SWITCHES \
21 X("decodetext 8bit|7bit", 0, DECODETEXTSW) \
22 X("nodecodetext", 0, NDECODETEXTSW) \
23 X("textcodeset", 0, TEXTCODESETSW) \
24 X("notextcodeset", 0, NTEXTCODESETSW) \
25 X("reformat", 0, REFORMATSW) \
26 X("noreformat", 0, NREFORMATSW) \
27 X("fixboundary", 0, FIXBOUNDARYSW) \
28 X("nofixboundary", 0, NFIXBOUNDARYSW) \
29 X("fixcte", 0, FIXCTESW) \
30 X("nofixcte", 0, NFIXCTESW) \
31 X("file file", 0, FILESW) \
32 X("outfile file", 0, OUTFILESW) \
33 X("rmmproc program", 0, RPROCSW) \
34 X("normmproc", 0, NRPRCSW) \
35 X("verbose", 0, VERBSW) \
36 X("noverbose", 0, NVERBSW) \
37 X("version", 0, VERSIONSW) \
38 X("help", 0, HELPSW) \
40 #define X(sw, minchars, id) id,
41 DEFINE_SWITCH_ENUM(MHFIXMSG
);
44 #define X(sw, minchars, id) { sw, minchars, id },
45 DEFINE_SWITCH_ARRAY(MHFIXMSG
, switches
);
50 int debugsw
; /* Needed by mhparse.c. */
52 #define quitser pipeser
55 extern char *tmp
; /* directory to place tmp files */
56 extern int skip_mp_cte_check
; /* flag to InitMultiPart */
57 extern int suppress_bogus_mp_content_warning
; /* flag to InitMultiPart */
58 extern int bogus_mp_content
; /* flag from InitMultiPart */
59 CT
parse_mime (char *);
60 void reverse_parts (CT
);
63 int output_message (CT
, char *);
66 int show_content_aux (CT
, int, int, char *, char *);
69 void flush_errors (void);
73 void freects_done (int) NORETURN
;
78 typedef struct fix_transformations
{
84 } fix_transformations
;
86 int mhfixmsgsbr (CT
*, const fix_transformations
*, char *);
87 static void reverse_alternative_parts (CT
);
88 static int fix_boundary (CT
*, int *);
89 static int get_multipart_boundary (CT
, char **);
90 static int replace_boundary (CT
, char *, const char *);
91 static char *update_attr (char *, const char *, const char *e
);
92 static int fix_multipart_cte (CT
, int *);
93 static int set_ce (CT
, int);
94 static int ensure_text_plain (CT
*, CT
, int *);
95 static CT
build_text_plain_part (CT
);
96 static CT
divide_part (CT
);
97 static void copy_ctinfo (CI
, CI
);
98 static int decode_part (CT
);
99 static int reformat_part (CT
, char *, char *, char *, int);
100 static int charset_encoding (CT
);
101 static CT
build_multipart_alt (CT
, CT
, int, int);
102 static int boundary_in_content (FILE **, char *, const char *);
103 static void transfer_noncontent_headers (CT
, CT
);
104 static int set_ct_type (CT
, int type
, int subtype
, int encoding
);
105 static int decode_text_parts (CT
, int, int *);
106 static int content_encoding (CT
);
107 static int convert_codesets (CT
, char *, int *);
108 static int convert_codeset (CT
, char *, int *);
109 static int write_content (CT
, char *, char *, int, int);
110 static int remove_file (char *);
111 static void report (char *, char *, char *, ...);
112 static char *upcase (char *);
113 static void pipeser (int);
117 main (int argc
, char **argv
) {
119 char *cp
, *file
= NULL
, *folder
= NULL
;
120 char *maildir
, buf
[100], *outfile
= NULL
;
121 char **argp
, **arguments
;
122 struct msgs_array msgs
= { 0, 0, NULL
};
123 struct msgs
*mp
= NULL
;
128 fix_transformations fx
;
129 fx
.reformat
= fx
.fixcte
= fx
.fixboundary
= 1;
130 fx
.decodetext
= CE_8BIT
;
131 fx
.textcodeset
= NULL
;
136 setlocale(LC_ALL
, "");
138 invo_name
= r1bindex (argv
[0], '/');
140 /* read user profile/context */
143 arguments
= getarguments (invo_name
, argc
, argv
, 1);
149 while ((cp
= *argp
++)) {
151 switch (smatch (++cp
, switches
)) {
153 ambigsw (cp
, switches
);
156 adios (NULL
, "-%s unknown", cp
);
159 snprintf (buf
, sizeof buf
, "%s [+folder] [msgs] [switches]",
161 print_help (buf
, switches
, 1);
164 print_version(invo_name
);
168 if (! (cp
= *argp
++) || *cp
== '-')
169 adios (NULL
, "missing argument to %s", argp
[-2]);
170 if (! strcasecmp (cp
, "8bit")) {
171 fx
.decodetext
= CE_8BIT
;
172 } else if (! strcasecmp (cp
, "7bit")) {
173 fx
.decodetext
= CE_7BIT
;
175 adios (NULL
, "invalid argument to %s", argp
[-2]);
182 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
183 adios (NULL
, "missing argument to %s", argp
[-2]);
209 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
210 adios (NULL
, "missing argument to %s", argp
[-2]);
211 file
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
215 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
216 adios (NULL
, "missing argument to %s", argp
[-2]);
217 outfile
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
221 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
222 adios (NULL
, "missing argument to %s", argp
[-2]);
236 if (*cp
== '+' || *cp
== '@') {
238 adios (NULL
, "only one folder at a time!");
240 folder
= pluspath (cp
);
242 app_msgarg(&msgs
, cp
);
245 SIGNAL (SIGQUIT
, quitser
);
246 SIGNAL (SIGPIPE
, pipeser
);
249 * Read the standard profile setup
251 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
252 readconfig ((struct node
**) 0, fp
, cp
, 0);
257 * Check for storage directory. If specified,
258 * then store temporary files there. Else we
259 * store them in standard nmh directory.
261 if ((cp
= context_find (nmhstorage
)) && *cp
)
262 tmp
= concat (cp
, "/", invo_name
, NULL
);
264 tmp
= add (m_maildir (invo_name
), NULL
);
266 suppress_bogus_mp_content_warning
= skip_mp_cte_check
= 1;
268 if (! context_find ("path"))
269 free (path ("./", TFOLDER
));
271 if (file
&& msgs
.size
)
272 adios (NULL
, "cannot specify msg and file at same time!");
275 * check if message is coming from file
278 /* If file is stdin, create a tmp file name before parse_mime()
279 has a chance, because it might put in on a different
280 filesystem than the output file. Instead, put it in the
281 user's preferred tmp directory. */
284 if (! strcmp ("-", file
)) {
290 if ((cp
= m_mktemp2 (tmp
, invo_name
, &fd
, NULL
)) == NULL
) {
291 adios (NULL
, "unable to create temporary file");
294 file
= add (cp
, NULL
);
296 cpydata (STDIN_FILENO
, fd
, "-", file
);
301 adios (NULL
, "failed to write temporary file");
305 if (! (cts
= (CT
*) calloc ((size_t) 2, sizeof *cts
)))
306 adios (NULL
, "out of memory");
309 if ((ct
= parse_mime (file
))) *ctp
++ = ct
;
312 * message(s) are coming from a folder
317 app_msgarg(&msgs
, "cur");
319 folder
= getfolder (1);
320 maildir
= m_maildir (folder
);
322 if (chdir (maildir
) == NOTOK
)
323 adios (maildir
, "unable to change directory to");
325 /* read folder and create message structure */
326 if (! (mp
= folder_read (folder
, 1)))
327 adios (NULL
, "unable to read folder %s", folder
);
329 /* check for empty folder */
331 adios (NULL
, "no messages in %s", folder
);
333 /* parse all the message ranges/sequences and set SELECTED */
334 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
335 if (! m_convert (mp
, msgs
.msgs
[msgnum
]))
337 seq_setprev (mp
); /* set the previous-sequence */
339 if (! (cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof *cts
)))
340 adios (NULL
, "out of memory");
343 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
344 if (is_selected(mp
, msgnum
)) {
347 msgnam
= m_name (msgnum
);
348 if ((ct
= parse_mime (msgnam
))) *ctp
++ = ct
;
352 seq_setcur (mp
, mp
->hghsel
); /* update current message */
353 seq_save (mp
); /* synchronize sequences */
354 context_replace (pfolder
, folder
);/* update current folder */
355 context_save (); /* save the context file */
359 for (ctp
= cts
; *ctp
; ++ctp
) {
360 status
+= mhfixmsgsbr (ctp
, &fx
, outfile
);
366 /* Just calling m_backup() unlinks the backup file. */
367 (void) m_backup (file
);
379 /* done is freects_done, which will clean up all of cts. */
386 mhfixmsgsbr (CT
*ctp
, const fix_transformations
*fx
, char *outfile
) {
387 /* Store input filename in case one of the transformations, i.e.,
388 fix_boundary(), rewrites to a tmp file. */
389 char *input_filename
= add ((*ctp
)->c_file
, NULL
);
390 int modify_inplace
= 0;
391 int message_mods
= 0;
394 if (outfile
== NULL
) {
397 if ((*ctp
)->c_file
) {
398 outfile
= add (m_mktemp2 (tmp
, invo_name
, NULL
, NULL
), NULL
);
400 adios (NULL
, "missing both input and output filenames\n");
404 reverse_alternative_parts (*ctp
);
405 if (status
== OK
&& fx
->fixboundary
) {
406 status
= fix_boundary (ctp
, &message_mods
);
408 if (status
== OK
&& fx
->fixcte
) {
409 status
= fix_multipart_cte (*ctp
, &message_mods
);
411 if (status
== OK
&& fx
->reformat
) {
412 status
= ensure_text_plain (ctp
, NULL
, &message_mods
);
414 if (status
== OK
&& fx
->decodetext
) {
415 status
= decode_text_parts (*ctp
, fx
->decodetext
, &message_mods
);
417 if (status
== OK
&& fx
->textcodeset
!= NULL
) {
418 status
= convert_codesets (*ctp
, fx
->textcodeset
, &message_mods
);
421 if (! (*ctp
)->c_umask
) {
422 /* Set the umask for the contents file. This currently
423 isn't used but just in case it is in the future. */
426 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
427 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
429 (*ctp
)->c_umask
= ~m_gmprot();
434 * Write the content to a file
437 status
= write_content (*ctp
, input_filename
, outfile
, modify_inplace
,
439 } else if (! modify_inplace
) {
440 /* Something went wrong. Output might be expected, such
441 as if this were run as a filter. Just copy the input
443 int in
= open (input_filename
, O_RDONLY
);
444 int out
= strcmp (outfile
, "-")
445 ? open (outfile
, O_WRONLY
| O_CREAT
, m_gmprot ())
448 if (in
!= -1 && out
!= -1) {
449 cpydata (in
, out
, input_filename
, outfile
);
458 if (modify_inplace
) {
463 free (input_filename
);
469 /* parse_mime() arranges alternates in reverse (priority) order, so
470 reverse them back. This will put a text/plain part at the front of
471 a multipart/alternative part, for example, where it belongs. */
473 reverse_alternative_parts (CT ct
) {
474 if (ct
->c_type
== CT_MULTIPART
) {
475 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
478 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
482 /* And call recursively on each part of a multipart. */
483 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
484 reverse_alternative_parts (part
->mp_part
);
491 fix_boundary (CT
*ct
, int *message_mods
) {
492 struct multipart
*mp
;
495 if (bogus_mp_content
) {
496 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
499 * 1) Get boundary at end of part.
500 * 2) Get boundary at beginning of part and compare to the end-of-part
502 * 3) Write out contents of ct to tmp file, replacing boundary in
503 * header with boundary from part. Set c_unlink to 1.
505 * 5) Call parse_mime() on the tmp file, replacing ct.
508 if (mp
&& mp
->mp_start
) {
511 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
514 if ((fixed
= m_mktemp2 (tmp
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
515 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
516 char *filename
= add ((*ct
)->c_file
, NULL
);
519 if ((*ct
= parse_mime (fixed
))) {
524 report (NULL
, filename
,
525 "fix multipart boundary");
530 advise (NULL
, "unable to replace broken boundary");
534 advise (NULL
, "unable to create temporary file");
538 free (part_boundary
);
548 get_multipart_boundary (CT ct
, char **part_boundary
) {
550 char *end_boundary
= NULL
;
551 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
552 ? (off_t
) (ct
->c_end
- sizeof buffer
)
553 : (off_t
) ct
->c_begin
;
557 /* This will fail if the boundary spans fread() calls. BUFSIZ should
558 be big enough, even if it's just 1024, to make that unlikely. */
560 /* free_content() will close ct->c_fp. */
561 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
562 advise (ct
->c_file
, "unable to open for reading");
566 /* Get boundary at end of multipart. */
567 while (begin
>= (off_t
) ct
->c_begin
) {
568 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
569 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
570 char *end
= buffer
+ bytes_read
- 1;
573 if ((cp
= rfind_str (buffer
, bytes_read
, "--"))) {
574 /* Trim off trailing "--" and anything beyond. */
576 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
577 if (strlen (end
) > 3 && *end
++ == '\n' &&
578 *end
++ == '-' && *end
++ == '-') {
579 end_boundary
= add (end
, NULL
);
586 if (! end_boundary
&& begin
> (off_t
) (ct
->c_begin
+ sizeof buffer
)) {
587 begin
-= sizeof buffer
;
593 /* Get boundary at beginning of multipart. */
595 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
596 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
597 if (bytes_read
>= strlen (end_boundary
)) {
598 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
600 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
601 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
606 /* The start and end boundaries didn't match, or the
607 start boundary doesn't begin with "\n--" (or "--"
608 if at the beginning of buffer). Keep trying. */
617 *part_boundary
= end_boundary
;
619 *part_boundary
= NULL
;
627 /* Open and copy ct->c_file to file, replacing the multipart boundary. */
629 replace_boundary (CT ct
, char *file
, const char *boundary
) {
632 char buf
[BUFSIZ
], name
[NAMESZ
];
634 m_getfld_state_t gstate
= 0;
637 if (ct
->c_file
== NULL
) {
638 advise (NULL
, "missing input filename");
642 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
643 advise (ct
->c_file
, "unable to open for reading");
647 if ((fpout
= fopen (file
, "w")) == NULL
) {
649 advise (file
, "unable to open for writing");
653 for (compnum
= 1;;) {
654 int bufsz
= (int) sizeof buf
;
656 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
)) {
661 /* get copies of the buffers */
662 np
= add (name
, NULL
);
663 vp
= add (buf
, NULL
);
665 /* if necessary, get rest of field */
666 while (state
== FLDPLUS
) {
668 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
);
669 vp
= add (buf
, vp
); /* add to previous value */
672 if (strcasecmp (TYPE_FIELD
, np
)) {
673 fprintf (fpout
, "%s:%s", np
, vp
);
675 char *new_boundary
= update_attr (vp
, "boundary=", boundary
);
677 fprintf (fpout
, "%s:%s\n", np
, new_boundary
);
688 /* buf will have a terminating NULL, skip it. */
689 fwrite (buf
, 1, bufsz
-1, fpout
);
697 advise (NULL
, "message format error in component #%d", compnum
);
702 advise (NULL
, "getfld() returned %d", state
);
710 m_getfld_state_destroy (&gstate
);
718 /* Change the value of a name=value pair in a header field body.
719 If the name isn't there, append them. In any case, a new
720 string will be allocated and must be free'd by the caller.
721 Trims any trailing newlines. */
723 update_attr (char *body
, const char *name
, const char *value
) {
724 char *bp
= nmh_strcasestr (body
, name
);
728 char *other_attrs
= strchr (bp
, ';');
730 *(bp
+ strlen (name
)) = '\0';
731 new_body
= concat (body
, "\"", value
, "\"", NULL
);
736 /* Trim any trailing newlines. */
737 for (cp
= &other_attrs
[strlen (other_attrs
) - 1];
738 cp
> other_attrs
&& *cp
== '\n';
739 *cp
-- = '\0') continue;
740 new_body
= add (other_attrs
, new_body
);
745 /* Append name/value pair, after first removing a final newline
746 and (extraneous) semicolon. */
747 if (*(cp
= &body
[strlen (body
) - 1]) == '\n') *cp
= '\0';
748 if (*(cp
= &body
[strlen (body
) - 1]) == ';') *cp
= '\0';
749 new_body
= concat (body
, "; ", name
, "\"", value
, "\"", NULL
);
757 fix_multipart_cte (CT ct
, int *message_mods
) {
760 if (ct
->c_type
== CT_MULTIPART
) {
764 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
765 ct
->c_encoding
!= CE_BINARY
) {
768 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
769 char *name
= hf
->name
;
770 for (; *name
&& isspace ((unsigned char) *name
); ++name
) {
774 if (! strncasecmp (name
, ENCODING_FIELD
,
775 strlen (ENCODING_FIELD
))) {
776 char *prefix
= "Nmh-REPLACED-INVALID-";
777 HF h
= mh_xmalloc (sizeof *h
);
779 h
->name
= add (hf
->name
, NULL
);
780 h
->hf_encoding
= hf
->hf_encoding
;
784 /* Retain old header but prefix its name. */
786 hf
->name
= concat (prefix
, h
->name
, NULL
);
790 char *encoding
= cpytrim (hf
->value
);
791 report (ct
->c_partno
, ct
->c_file
,
792 "replace Content-Transfer-Encoding of %s "
793 "with 8 bit", encoding
);
797 h
->value
= add (" 8bit\n", NULL
);
799 /* Don't need to warn for multiple C-T-E header
800 fields, parse_mime() already does that. But
801 if there are any, fix them all as necessary. */
806 set_ce (ct
, CE_8BIT
);
809 m
= (struct multipart
*) ct
->c_ctparams
;
810 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
811 if (fix_multipart_cte (part
->mp_part
, message_mods
) != OK
) {
823 set_ce (CT ct
, int encoding
) {
824 const char *ce
= ce_str (encoding
);
825 const struct str2init
*ctinit
= get_ce_method (ce
);
828 char *cte
= concat (" ", ce
, "\n", NULL
);
831 /* Decoded contents might be in ct->c_cefile.ce_file, if the
832 caller is decode_text_parts (). Save because we'll
834 struct cefile decoded_content_info
= ct
->c_cefile
;
836 ct
->c_encoding
= encoding
;
838 ct
->c_ctinitfnx
= ctinit
->si_init
;
839 /* This will assign ct->c_cefile with an all-0 struct, which
841 (*ctinit
->si_init
) (ct
);
842 /* After returning, the caller should set
843 ct->c_cefile.ce_file to the name of the file containing
846 /* Restore the cefile. */
847 ct
->c_cefile
= decoded_content_info
;
849 /* Update/add Content-Transfer-Encoding header field. */
850 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
851 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
858 add_header (ct
, add (ENCODING_FIELD
, NULL
), cte
);
861 /* Update c_celine. It's used only by mhlist -debug. */
863 ct
->c_celine
= add (cte
, NULL
);
872 /* Make sure each text part has a corresponding text/plain part. */
874 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
) {
877 switch ((*ct
)->c_type
) {
879 int has_text_plain
= 0;
881 /* Nothing to do for text/plain. */
882 if ((*ct
)->c_subtype
== TEXT_PLAIN
) return OK
;
884 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
885 parent
->c_subtype
== MULTI_ALTERNATE
) {
886 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
888 int new_subpart_number
= 1;
890 /* See if there is a sibling text/plain. */
891 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
892 ++new_subpart_number
;
893 if (part
->mp_part
->c_type
== CT_TEXT
&&
894 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
900 if (! has_text_plain
) {
901 /* Parent is a multipart/alternative. Insert a new
902 text/plain subpart. */
903 struct part
*new_part
= mh_xmalloc (sizeof *new_part
);
905 if ((new_part
->mp_part
= build_text_plain_part (*ct
))) {
907 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
909 new_part
->mp_next
= mp
->mp_parts
;
910 mp
->mp_parts
= new_part
;
911 new_part
->mp_part
->c_partno
=
912 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
917 report (parent
->c_partno
, parent
->c_file
,
918 "insert text/plain part");
921 free_content (new_part
->mp_part
);
927 /* Slip new text/plain part into a new multipart/alternative. */
928 CT tp_part
= build_text_plain_part (*ct
);
931 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
934 struct multipart
*mp
=
935 (struct multipart
*) mp_alt
->c_ctparams
;
937 if (mp
&& mp
->mp_parts
) {
938 mp
->mp_parts
->mp_part
= tp_part
;
939 /* Make the new multipart/alternative the parent. */
944 report ((*ct
)->c_partno
, (*ct
)->c_file
,
945 "insert text/plain part");
948 free_content (tp_part
);
949 free_content (mp_alt
);
959 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
962 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
963 if ((*ct
)->c_type
== CT_MULTIPART
) {
964 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
);
971 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
974 e
= (struct exbody
*) (*ct
)->c_ctparams
;
975 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
);
985 build_text_plain_part (CT encoded_part
) {
986 CT tp_part
= divide_part (encoded_part
);
987 char *tmp_plain_file
= NULL
;
989 if (decode_part (tp_part
) == OK
) {
990 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
991 contains the decoded contents. And the decoding function, such
992 as openQuoted, will have set ...->ce_unlink to 1 so that it will
993 be unlinked by free_content (). */
994 tmp_plain_file
= add (m_mktemp2 (tmp
, invo_name
, NULL
, NULL
), 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 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
) {
1042 char **s_ap
, **d_ap
, **s_vp
, **d_vp
;
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_ap
= src
->ci_attrs
, d_ap
= dest
->ci_attrs
,
1048 s_vp
= src
->ci_values
, d_vp
= dest
->ci_values
;
1050 ++s_ap
, ++d_ap
, ++s_vp
, ++d_vp
) {
1051 *d_ap
= add (*s_ap
, NULL
);
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
) {
1066 tmp_decoded
= add (m_mktemp2 (tmp
, invo_name
, NULL
, NULL
), NULL
);
1067 /* The following call will load ct->c_cefile.ce_file with the tmp
1068 filename of the decoded content. tmp_decoded will contain the
1069 encoded output, get rid of that. */
1070 status
= output_message (ct
, tmp_decoded
);
1071 unlink (tmp_decoded
);
1078 /* Some of the arguments aren't really needed now, but maybe will
1079 be in the future for other than text types. */
1081 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
) {
1082 int output_subtype
, output_encoding
;
1086 /* Hacky: this redirects the output from whatever command is used
1087 to show the part to a file. So, the user can't have any output
1088 redirection in that command.
1089 Could show_multi() in mhshowsbr.c avoid this? */
1091 /* Check for invo_name-format-type/subtype. */
1092 cp
= concat (invo_name
, "-format-", type
, "/", subtype
, NULL
);
1093 if ((cf
= context_find (cp
)) && *cf
!= '\0') {
1094 if (strchr (cf
, '>')) {
1096 advise (NULL
, "'>' prohibited in \"%s\",\nplease fix your "
1097 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1104 /* Check for invo_name-format-type. */
1105 cp
= concat (invo_name
, "-format-", type
, NULL
);
1106 if (! (cf
= context_find (cp
)) || *cf
== '\0') {
1109 advise (NULL
, "Don't know how to convert %s, there is no "
1110 "%s-format-%s/%s profile entry",
1111 ct
->c_file
, invo_name
, type
, subtype
);
1116 if (strchr (cf
, '>')) {
1118 advise (NULL
, "'>' prohibited in \"%s\"", cf
);
1124 cp
= concat (cf
, " >", file
, NULL
);
1125 status
= show_content_aux (ct
, 1, 0, cp
, NULL
);
1128 /* Unlink decoded content tmp file and free its filename to avoid
1129 leaks. The file stream should already have been closed. */
1130 if (ct
->c_cefile
.ce_unlink
) {
1131 unlink (ct
->c_cefile
.ce_file
);
1132 free (ct
->c_cefile
.ce_file
);
1133 ct
->c_cefile
.ce_file
= NULL
;
1134 ct
->c_cefile
.ce_unlink
= 0;
1137 if (c_type
== CT_TEXT
) {
1138 output_subtype
= TEXT_PLAIN
;
1140 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1143 output_encoding
= charset_encoding (ct
);
1145 if (set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1146 ct
->c_cefile
.ce_file
= file
;
1147 ct
->c_cefile
.ce_unlink
= 1;
1149 ct
->c_cefile
.ce_unlink
= 0;
1157 /* Identifies 7bit or 8bit content based on charset, if specified. */
1159 charset_encoding (CT ct
) {
1160 int encoding
= CE_8BIT
;
1161 CI ctinfo
= &ct
->c_ctinfo
;
1164 for (ap
= ctinfo
->ci_attrs
, vp
= ctinfo
->ci_values
; *ap
; ++ap
, ++vp
) {
1165 if (! strcasecmp (*ap
, "charset")) {
1166 /* norm_charmap() is case sensitive. */
1167 char *ch
= upcase (*vp
);
1169 if (! strcmp (norm_charmap (ch
), "US-ASCII")) encoding
= CE_7BIT
;
1180 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
) {
1181 char *boundary_prefix
= "----=_nmh-multipart";
1182 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1183 char *boundary_indicator
= "; boundary=";
1184 char *typename
, *subtypename
, *name
;
1187 struct multipart
*m
;
1189 const struct str2init
*ctinit
;
1191 if ((ct
= (CT
) calloc (1, sizeof *ct
)) == NULL
)
1192 adios (NULL
, "out of memory");
1194 /* Set up the multipart/alternative part. These fields of *ct were
1195 initialized to 0 by calloc():
1196 c_fp, c_unlink, c_begin, c_end,
1197 c_vrsn, c_ctline, c_celine,
1198 c_id, c_descr, c_dispo, c_partno,
1199 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1200 c_cefile, c_encoding,
1201 c_digested, c_digest[16], c_ctexbody,
1202 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1203 c_umask, c_pid, c_rfc934,
1204 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1207 ct
->c_file
= add (first_alt
->c_file
, NULL
);
1209 ct
->c_subtype
= subtype
;
1211 ctinit
= get_ct_init (ct
->c_type
);
1213 typename
= ct_type_str (type
);
1214 subtypename
= ct_subtype_str (type
, subtype
);
1218 int found_boundary
= 1;
1220 while (found_boundary
&& serial
< 1000000) {
1223 /* Ensure that the boundary doesn't appear in the decoded
1225 if (new_part
->c_cefile
.ce_file
) {
1226 if ((found_boundary
=
1227 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1228 new_part
->c_cefile
.ce_file
,
1234 /* Ensure that the boundary doesn't appear in the encoded
1236 if (! found_boundary
&& new_part
->c_file
) {
1237 if ((found_boundary
= boundary_in_content (&new_part
->c_fp
,
1244 if (found_boundary
) {
1245 /* Try a slightly different boundary. */
1250 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1252 concat (boundary_prefix
,
1253 first_alt
->c_partno
? first_alt
->c_partno
: "",
1254 "-", buffer2
, NULL
);
1258 if (found_boundary
) {
1259 advise (NULL
, "giving up trying to find a unique boundary");
1264 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1265 boundary
, "\"", NULL
);
1267 /* Load c_first_hf and c_last_hf. */
1268 transfer_noncontent_headers (first_alt
, ct
);
1269 add_header (ct
, add (TYPE_FIELD
, NULL
), concat (name
, "\n", NULL
));
1272 /* Load c_partno. */
1273 if (first_alt
->c_partno
) {
1274 ct
->c_partno
= add (first_alt
->c_partno
, NULL
);
1275 free (first_alt
->c_partno
);
1276 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1277 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1279 first_alt
->c_partno
= add ("1", NULL
);
1280 new_part
->c_partno
= add ("2", NULL
);
1284 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1285 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1288 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
,
1290 if ((cp
= strstr (name
, boundary_indicator
))) {
1291 ct
->c_ctinfo
.ci_attrs
[0] = name
;
1292 ct
->c_ctinfo
.ci_attrs
[1] = NULL
;
1293 /* ci_values don't get free'd, so point into ci_attrs. */
1294 ct
->c_ctinfo
.ci_values
[0] = cp
+ strlen (boundary_indicator
);
1297 p
= (struct part
*) mh_xmalloc (sizeof *p
);
1298 p
->mp_next
= (struct part
*) mh_xmalloc (sizeof *p
->mp_next
);
1299 p
->mp_next
->mp_next
= NULL
;
1300 p
->mp_next
->mp_part
= first_alt
;
1302 if ((m
= (struct multipart
*) calloc (1, sizeof (struct multipart
))) ==
1304 adios (NULL
, "out of memory");
1305 m
->mp_start
= concat (boundary
, "\n", NULL
);
1306 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1308 ct
->c_ctparams
= (void *) m
;
1316 /* Check that the boundary does not appear in the content. */
1318 boundary_in_content (FILE **fp
, char *file
, const char *boundary
) {
1319 char buffer
[BUFSIZ
];
1321 int found_boundary
= 0;
1323 /* free_content() will close *fp if we fopen it here. */
1324 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1325 advise (file
, "unable to open %s for reading", file
);
1329 fseeko (*fp
, 0L, SEEK_SET
);
1330 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1331 if (find_str (buffer
, bytes_read
, boundary
)) {
1337 return found_boundary
;
1341 /* Remove all non-Content headers. */
1343 transfer_noncontent_headers (CT old
, CT
new) {
1346 hp_prev
= hp
= old
->c_first_hf
;
1350 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, strlen (XXX_FIELD_PRF
))) {
1351 if (hp
== old
->c_last_hf
) {
1352 if (hp
== old
->c_first_hf
) {
1353 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1355 hp_prev
->next
= NULL
;
1356 old
->c_last_hf
= hp_prev
;
1359 if (hp
== old
->c_first_hf
) {
1360 old
->c_first_hf
= next
;
1362 hp_prev
->next
= next
;
1366 /* Put node hp in the new CT. */
1367 if (new->c_first_hf
== NULL
) {
1368 new->c_first_hf
= hp
;
1370 new->c_last_hf
->next
= hp
;
1372 new->c_last_hf
= hp
;
1374 /* A Content- header, leave in old. */
1384 set_ct_type (CT ct
, int type
, int subtype
, int encoding
) {
1385 char *typename
= ct_type_str (type
);
1386 char *subtypename
= ct_subtype_str (type
, subtype
);
1387 /* E.g, " text/plain" */
1388 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1389 /* E.g, " text/plain\n" */
1390 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1391 int found_content_type
= 0;
1393 const char *cp
= NULL
;
1397 /* Update/add Content-Type header field. */
1398 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1399 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1400 found_content_type
= 1;
1402 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1403 ? concat (type_subtypename
, cp
, "\n", NULL
)
1404 : add (name_plus_nl
, NULL
);
1407 if (! found_content_type
) {
1408 add_header (ct
, add (TYPE_FIELD
, NULL
),
1409 (cp
= strchr (ct
->c_ctline
, ';'))
1410 ? concat (type_subtypename
, cp
, "\n", NULL
)
1411 : add (name_plus_nl
, NULL
));
1414 /* Some of these might not be used, but set them anyway. */
1416 ? concat (type_subtypename
, cp
, NULL
)
1417 : concat (type_subtypename
, NULL
);
1418 free (ct
->c_ctline
);
1419 ct
->c_ctline
= ctline
;
1420 /* Leave other ctinfo members as they were. */
1421 free (ct
->c_ctinfo
.ci_type
);
1422 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1423 free (ct
->c_ctinfo
.ci_subtype
);
1424 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1426 ct
->c_subtype
= subtype
;
1428 free (name_plus_nl
);
1429 free (type_subtypename
);
1431 status
= set_ce (ct
, encoding
);
1438 decode_text_parts (CT ct
, int encoding
, int *message_mods
) {
1441 switch (ct
->c_type
) {
1443 switch (ct
->c_encoding
) {
1448 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
1449 if ((ct_encoding
= content_encoding (ct
)) == CE_BINARY
&&
1450 encoding
!= CE_BINARY
) {
1452 report (ct
->c_partno
, ct
->c_file
,
1453 "will not decode%s because it is binary",
1455 : ct
->c_ctline
? ct
->c_ctline
1458 unlink (ct
->c_cefile
.ce_file
);
1459 free (ct
->c_cefile
.ce_file
);
1460 ct
->c_cefile
.ce_file
= NULL
;
1461 } else if (ct
->c_encoding
== CE_QUOTED
&&
1462 ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
1464 report (ct
->c_partno
, ct
->c_file
,
1465 "will not decode%s because it is 8bit",
1467 : ct
->c_ctline
? ct
->c_ctline
1470 unlink (ct
->c_cefile
.ce_file
);
1471 free (ct
->c_cefile
.ce_file
);
1472 ct
->c_cefile
.ce_file
= NULL
;
1475 if (ct_encoding
== CE_BINARY
)
1477 else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
)
1480 enc
= charset_encoding (ct
);
1481 if (set_ce (ct
, enc
) == OK
) {
1484 report (ct
->c_partno
, ct
->c_file
, "decode%s",
1485 ct
->c_ctline
? ct
->c_ctline
: "");
1501 case CT_MULTIPART
: {
1502 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1505 /* Should check to see if the body for this part is encoded?
1506 For now, it gets passed along as-is by InitMultiPart(). */
1507 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1508 status
= decode_text_parts (part
->mp_part
, encoding
, message_mods
);
1514 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1517 e
= (struct exbody
*) ct
->c_ctparams
;
1518 status
= decode_text_parts (e
->eb_content
, encoding
, message_mods
);
1530 /* See if the decoded content is 7bit, 8bit, or binary. It's binary
1531 if it has any NUL characters, a CR not followed by a LF, or lines
1532 greater than 998 characters in length. */
1534 content_encoding (CT ct
) {
1535 CE ce
= &ct
->c_cefile
;
1536 int encoding
= CE_7BIT
;
1539 char buffer
[BUFSIZ
];
1542 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1543 advise (ce
->ce_file
, "unable to open for reading");
1547 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
1548 while (encoding
!= CE_BINARY
&&
1549 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
1552 size_t line_len
= 0;
1553 int last_char_was_cr
= 0;
1555 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
1556 if (*cp
== '\0' || ++line_len
> 998 ||
1557 (*cp
!= '\n' && last_char_was_cr
)) {
1558 encoding
= CE_BINARY
;
1560 } else if (*cp
== '\n') {
1562 } else if (! isascii ((unsigned char) *cp
)) {
1566 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
1572 } /* else should never happen */
1579 convert_codesets (CT ct
, char *dest_codeset
, int *message_mods
) {
1582 switch (ct
->c_type
) {
1584 if (ct
->c_subtype
== TEXT_PLAIN
) {
1585 status
= convert_codeset (ct
, dest_codeset
, message_mods
);
1589 case CT_MULTIPART
: {
1590 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1593 /* Should check to see if the body for this part is encoded?
1594 For now, it gets passed along as-is by InitMultiPart(). */
1595 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1597 convert_codesets (part
->mp_part
, dest_codeset
, message_mods
);
1603 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1606 e
= (struct exbody
*) ct
->c_ctparams
;
1608 convert_codesets (e
->eb_content
, dest_codeset
, message_mods
);
1621 convert_codeset (CT ct
, char *dest_codeset
, int *message_mods
) {
1622 const char *const charset
= "charset";
1623 char **src_codeset
= NULL
;
1624 char *default_codeset
= NULL
;
1625 CI ctinfo
= &ct
->c_ctinfo
;
1629 for (ap
= ctinfo
->ci_attrs
, vp
= ctinfo
->ci_values
; *ap
; ++ap
, ++vp
) {
1630 if (! strcasecmp (*ap
, charset
)) {
1635 /* RFC 2045, Sec. 5.2: default to us-ascii. */
1636 if (src_codeset
== NULL
) src_codeset
= &default_codeset
;
1637 if (*src_codeset
== NULL
) *src_codeset
= "US-ASCII";
1639 if (strcmp (norm_charmap (*src_codeset
), norm_charmap (dest_codeset
))) {
1641 iconv_t conv_desc
= NULL
;
1648 int opened_input_file
= 0;
1649 char src_buffer
[BUFSIZ
];
1652 if ((conv_desc
= iconv_open (dest_codeset
, *src_codeset
)) ==
1654 advise (NULL
, "Can't convert %s to %s", *src_codeset
, dest_codeset
);
1658 dest
= add (m_mktemp2 (tmp
, invo_name
, &fd
, NULL
), NULL
);
1660 if (ct
->c_cefile
.ce_file
) {
1661 file
= &ct
->c_cefile
.ce_file
;
1662 fp
= &ct
->c_cefile
.ce_fp
;
1665 } else if (ct
->c_file
) {
1668 begin
= ct
->c_begin
;
1670 } /* else no input file: shouldn't happen */
1672 if (file
&& *file
&& fp
) {
1674 if ((*fp
= fopen (*file
, "r")) == NULL
) {
1675 advise (*file
, "unable to open for reading");
1678 opened_input_file
= 1;
1685 size_t max
= end
> 0 ? (size_t) (end
-begin
) : sizeof src_buffer
;
1687 fseeko (*fp
, begin
, SEEK_SET
);
1688 while (status
== OK
&& max
> 0 &&
1689 (inbytes
= fread (src_buffer
, 1, max
, *fp
)) > 0) {
1690 char dest_buffer
[BUFSIZ
];
1691 char *ib
= src_buffer
, *ob
= dest_buffer
;
1692 size_t outbytes
= sizeof dest_buffer
;
1693 size_t outbytes_before
= outbytes
;
1695 if (end
> 0) max
-= inbytes
;
1697 if (iconv (conv_desc
, &ib
, &inbytes
, &ob
, &outbytes
) ==
1702 write (fd
, dest_buffer
, outbytes_before
- outbytes
);
1706 if (opened_input_file
) {
1712 iconv_close (conv_desc
);
1716 /* Replace the decoded file with the converted one. */
1717 if (ct
->c_cefile
.ce_file
) {
1718 if (ct
->c_cefile
.ce_unlink
) {
1719 unlink (ct
->c_cefile
.ce_file
);
1721 free (ct
->c_cefile
.ce_file
);
1723 ct
->c_cefile
.ce_file
= dest
;
1724 ct
->c_cefile
.ce_unlink
= 1;
1728 report (ct
->c_partno
, ct
->c_file
, "convert %s to %s",
1729 *src_codeset
, dest_codeset
);
1732 /* Update ci_attrs. */
1733 *src_codeset
= dest_codeset
;
1735 /* Update ct->c_ctline. */
1738 update_attr (ct
->c_ctline
, "charset=", dest_codeset
);
1740 free (ct
->c_ctline
);
1741 ct
->c_ctline
= ctline
;
1742 } /* else no CT line, which is odd */
1744 /* Update Content-Type header field. */
1745 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1746 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1747 char *ctline_less_newline
=
1748 update_attr (hf
->value
, "charset=", dest_codeset
);
1749 char *ctline
= concat (ctline_less_newline
, "\n", NULL
);
1750 free (ctline_less_newline
);
1760 #else /* ! HAVE_ICONV */
1761 NMH_UNUSED (message_mods
);
1763 advise (NULL
, "Can't convert %s to %s without iconv", *src_codeset
,
1766 #endif /* ! HAVE_ICONV */
1774 write_content (CT ct
, char *input_filename
, char *outfile
, int modify_inplace
,
1778 if (modify_inplace
) {
1779 if (message_mods
> 0) {
1780 if ((status
= output_message (ct
, outfile
)) == OK
) {
1781 char *infile
= input_filename
1782 ? add (input_filename
, NULL
)
1783 : add (ct
->c_file
? ct
->c_file
: "-", NULL
);
1785 if (remove_file (infile
) == OK
) {
1786 if (rename (outfile
, infile
)) {
1787 /* Rename didn't work, possibly because of an
1788 attempt to rename across filesystems. Try
1789 brute force copy. */
1790 int old
= open (outfile
, O_RDONLY
);
1792 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
1795 if (old
!= -1 && new != -1) {
1796 char buffer
[BUFSIZ
];
1798 while ((i
= read (old
, buffer
, sizeof buffer
)) >
1800 if (write (new, buffer
, i
) != i
) {
1806 if (new != -1) close (new);
1807 if (old
!= -1) close (old
);
1811 /* The -file argument processing used path() to
1812 expand filename to absolute path. */
1813 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
1815 admonish (NULL
, "unable to rename %s %s to %s",
1816 file
? "file" : "message", outfile
,
1822 admonish (NULL
, "unable to remove input file %s, "
1823 "not modifying it", infile
);
1831 /* No modifications and didn't need the tmp outfile. */
1835 /* Output is going to some file. Produce it whether or not
1836 there were modifications. */
1837 status
= output_message (ct
, outfile
);
1846 * If "rmmproc" is defined, call that to remove the file. Otherwise,
1847 * use the standard MH backup file.
1850 remove_file (char *file
) {
1852 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
1853 int status
= system (rmm_command
);
1856 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
1858 /* This is OK for a non-message file, it still uses the
1859 BACKUP_PREFIX form. The backup file will be in the same
1860 directory as file. */
1861 return rename (file
, m_backup (file
));
1867 report (char *partno
, char *filename
, char *message
, ...) {
1872 va_start (args
, message
);
1873 fmt
= concat (filename
, partno
? " part " : ", ",
1874 partno
? partno
: "", partno
? ", " : "", message
, NULL
);
1876 advertise (NULL
, NULL
, fmt
, args
);
1885 upcase (char *str
) {
1886 char *up
= cpytrim (str
);
1889 for (cp
= up
; *cp
; ++cp
) *cp
= toupper ((unsigned char) *cp
);
1900 fprintf (stderr
, "\n");