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 CT
parse_mime (char *);
57 void reverse_parts (CT
);
60 int output_message (CT
, char *);
63 int show_content_aux (CT
, int, int, char *, char *);
66 void flush_errors (void);
70 void freects_done (int) NORETURN
;
75 typedef struct fix_transformations
{
82 } fix_transformations
;
84 int mhfixmsgsbr (CT
*, const fix_transformations
*, char *);
85 static void reverse_alternative_parts (CT
);
86 static int fix_boundary (CT
*, int *);
87 static int get_multipart_boundary (CT
, char **);
88 static int replace_boundary (CT
, char *, const char *);
89 static int fix_multipart_cte (CT
, int *);
90 static int set_ce (CT
, int);
91 static int ensure_text_plain (CT
*, CT
, int *, int);
92 static CT
build_text_plain_part (CT
);
93 static CT
divide_part (CT
);
94 static void copy_ctinfo (CI
, CI
);
95 static int decode_part (CT
);
96 static int reformat_part (CT
, char *, char *, char *, int);
97 static int charset_encoding (CT
);
98 static CT
build_multipart_alt (CT
, CT
, int, int);
99 static int boundary_in_content (FILE **, char *, const char *);
100 static void transfer_noncontent_headers (CT
, CT
);
101 static int set_ct_type (CT
, int type
, int subtype
, int encoding
);
102 static int decode_text_parts (CT
, int, int *);
103 static int content_encoding (CT
);
104 static int strip_crs (CT
, int *);
105 static int convert_charsets (CT
, char *, int *);
106 static int write_content (CT
, char *, char *, int, int);
107 static int remove_file (char *);
108 static void report (char *, char *, char *, ...);
109 static void pipeser (int);
113 main (int argc
, char **argv
) {
115 char *cp
, *file
= NULL
, *folder
= NULL
;
116 char *maildir
, buf
[100], *outfile
= NULL
;
117 char **argp
, **arguments
;
118 struct msgs_array msgs
= { 0, 0, NULL
};
119 struct msgs
*mp
= NULL
;
124 fix_transformations fx
;
125 fx
.reformat
= fx
.fixcte
= fx
.fixboundary
= 1;
126 fx
.replacetextplain
= 0;
127 fx
.decodetext
= CE_8BIT
;
128 fx
.textcharset
= NULL
;
130 if (nmh_init(argv
[0], 1)) { return 1; }
134 arguments
= getarguments (invo_name
, argc
, argv
, 1);
140 while ((cp
= *argp
++)) {
142 switch (smatch (++cp
, switches
)) {
144 ambigsw (cp
, switches
);
147 adios (NULL
, "-%s unknown", cp
);
150 snprintf (buf
, sizeof buf
, "%s [+folder] [msgs] [switches]",
152 print_help (buf
, switches
, 1);
155 print_version(invo_name
);
159 if (! (cp
= *argp
++) || *cp
== '-')
160 adios (NULL
, "missing argument to %s", argp
[-2]);
161 if (! strcasecmp (cp
, "8bit")) {
162 fx
.decodetext
= CE_8BIT
;
163 } else if (! strcasecmp (cp
, "7bit")) {
164 fx
.decodetext
= CE_7BIT
;
166 adios (NULL
, "invalid argument to %s", argp
[-2]);
173 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
174 adios (NULL
, "missing argument to %s", argp
[-2]);
198 case REPLACETEXTPLAINSW
:
199 fx
.replacetextplain
= 1;
201 case NREPLACETEXTPLAINSW
:
202 fx
.replacetextplain
= 0;
205 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
206 adios (NULL
, "missing argument to %s", argp
[-2]);
207 file
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
210 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
211 adios (NULL
, "missing argument to %s", argp
[-2]);
212 outfile
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
215 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
216 adios (NULL
, "missing argument to %s", argp
[-2]);
229 if (*cp
== '+' || *cp
== '@') {
231 adios (NULL
, "only one folder at a time!");
233 folder
= pluspath (cp
);
236 /* Interpret a full path as a filename, not a message. */
237 file
= add (cp
, NULL
);
239 app_msgarg (&msgs
, cp
);
244 SIGNAL (SIGQUIT
, quitser
);
245 SIGNAL (SIGPIPE
, pipeser
);
248 * Read the standard profile setup
250 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
251 readconfig ((struct node
**) 0, fp
, cp
, 0);
255 suppress_bogus_mp_content_warning
= skip_mp_cte_check
= 1;
257 if (! context_find ("path"))
258 free (path ("./", TFOLDER
));
260 if (file
&& msgs
.size
)
261 adios (NULL
, "cannot specify msg and file at same time!");
264 * check if message is coming from file
267 /* If file is stdin, create a tmp file name before parse_mime()
268 has a chance, because it might put in on a different
269 filesystem than the output file. Instead, put it in the
270 user's preferred tmp directory. */
273 if (! strcmp ("-", file
)) {
279 if ((cp
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
280 adios (NULL
, "unable to create temporary file in %s",
284 file
= add (cp
, NULL
);
285 cpydata (STDIN_FILENO
, fd
, "-", file
);
289 (void) m_unlink (file
);
290 adios (NULL
, "failed to write temporary file");
294 if (! (cts
= (CT
*) calloc ((size_t) 2, sizeof *cts
)))
295 adios (NULL
, "out of memory");
298 if ((ct
= parse_mime (file
))) *ctp
++ = ct
;
301 * message(s) are coming from a folder
306 app_msgarg(&msgs
, "cur");
308 folder
= getfolder (1);
309 maildir
= m_maildir (folder
);
311 if (chdir (maildir
) == NOTOK
)
312 adios (maildir
, "unable to change directory to");
314 /* read folder and create message structure */
315 if (! (mp
= folder_read (folder
, 1)))
316 adios (NULL
, "unable to read folder %s", folder
);
318 /* check for empty folder */
320 adios (NULL
, "no messages in %s", folder
);
322 /* parse all the message ranges/sequences and set SELECTED */
323 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
324 if (! m_convert (mp
, msgs
.msgs
[msgnum
]))
326 seq_setprev (mp
); /* set the previous-sequence */
328 if (! (cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof *cts
)))
329 adios (NULL
, "out of memory");
332 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
333 if (is_selected(mp
, msgnum
)) {
336 msgnam
= m_name (msgnum
);
337 if ((ct
= parse_mime (msgnam
))) *ctp
++ = ct
;
341 seq_setcur (mp
, mp
->hghsel
); /* update current message */
342 seq_save (mp
); /* synchronize sequences */
343 context_replace (pfolder
, folder
);/* update current folder */
344 context_save (); /* save the context file */
348 for (ctp
= cts
; *ctp
; ++ctp
) {
349 status
+= mhfixmsgsbr (ctp
, &fx
, outfile
);
352 (void) m_unlink (file
);
355 /* Just calling m_backup() unlinks the backup file. */
356 (void) m_backup (file
);
367 /* done is freects_done, which will clean up all of cts. */
374 mhfixmsgsbr (CT
*ctp
, const fix_transformations
*fx
, char *outfile
) {
375 /* Store input filename in case one of the transformations, i.e.,
376 fix_boundary(), rewrites to a tmp file. */
377 char *input_filename
= add ((*ctp
)->c_file
, NULL
);
378 int modify_inplace
= 0;
379 int message_mods
= 0;
382 if (outfile
== NULL
) {
385 if ((*ctp
)->c_file
) {
387 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
388 adios (NULL
, "unable to create temporary file in %s",
391 outfile
= add (tempfile
, NULL
);
393 adios (NULL
, "missing both input and output filenames\n");
397 reverse_alternative_parts (*ctp
);
398 if (status
== OK
&& fx
->fixboundary
) {
399 status
= fix_boundary (ctp
, &message_mods
);
401 if (status
== OK
&& fx
->fixcte
) {
402 status
= fix_multipart_cte (*ctp
, &message_mods
);
404 if (status
== OK
&& fx
->reformat
) {
406 ensure_text_plain (ctp
, NULL
, &message_mods
, fx
->replacetextplain
);
408 if (status
== OK
&& fx
->decodetext
) {
409 status
= decode_text_parts (*ctp
, fx
->decodetext
, &message_mods
);
411 if (status
== OK
&& fx
->textcharset
!= NULL
) {
412 status
= convert_charsets (*ctp
, fx
->textcharset
, &message_mods
);
415 if (! (*ctp
)->c_umask
) {
416 /* Set the umask for the contents file. This currently
417 isn't used but just in case it is in the future. */
420 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
421 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
423 (*ctp
)->c_umask
= ~m_gmprot();
428 * Write the content to a file
431 status
= write_content (*ctp
, input_filename
, outfile
, modify_inplace
,
433 } else if (! modify_inplace
) {
434 /* Something went wrong. Output might be expected, such
435 as if this were run as a filter. Just copy the input
437 int in
= open (input_filename
, O_RDONLY
);
438 int out
= strcmp (outfile
, "-")
439 ? open (outfile
, O_WRONLY
| O_CREAT
, m_gmprot ())
442 if (in
!= -1 && out
!= -1) {
443 cpydata (in
, out
, input_filename
, outfile
);
452 if (modify_inplace
) {
453 if (status
!= OK
) (void) m_unlink (outfile
);
458 free (input_filename
);
464 /* parse_mime() arranges alternates in reverse (priority) order, so
465 reverse them back. This will put a text/plain part at the front of
466 a multipart/alternative part, for example, where it belongs. */
468 reverse_alternative_parts (CT ct
) {
469 if (ct
->c_type
== CT_MULTIPART
) {
470 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
473 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
477 /* And call recursively on each part of a multipart. */
478 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
479 reverse_alternative_parts (part
->mp_part
);
486 fix_boundary (CT
*ct
, int *message_mods
) {
487 struct multipart
*mp
;
490 if (bogus_mp_content
) {
491 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
494 * 1) Get boundary at end of part.
495 * 2) Get boundary at beginning of part and compare to the end-of-part
497 * 3) Write out contents of ct to tmp file, replacing boundary in
498 * header with boundary from part. Set c_unlink to 1.
500 * 5) Call parse_mime() on the tmp file, replacing ct.
503 if (mp
&& mp
->mp_start
) {
506 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
509 if ((fixed
= m_mktemp2 (NULL
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
510 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
511 char *filename
= add ((*ct
)->c_file
, NULL
);
514 if ((*ct
= parse_mime (fixed
))) {
519 report (NULL
, filename
,
520 "fix multipart boundary");
525 advise (NULL
, "unable to replace broken boundary");
529 advise (NULL
, "unable to create temporary file in %s",
534 free (part_boundary
);
544 get_multipart_boundary (CT ct
, char **part_boundary
) {
546 char *end_boundary
= NULL
;
547 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
548 ? (off_t
) (ct
->c_end
- sizeof buffer
)
549 : (off_t
) ct
->c_begin
;
553 /* This will fail if the boundary spans fread() calls. BUFSIZ should
554 be big enough, even if it's just 1024, to make that unlikely. */
556 /* free_content() will close ct->c_fp. */
557 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
558 advise (ct
->c_file
, "unable to open for reading");
562 /* Get boundary at end of multipart. */
563 while (begin
>= (off_t
) ct
->c_begin
) {
564 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
565 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
566 char *end
= buffer
+ bytes_read
- 1;
569 if ((cp
= rfind_str (buffer
, bytes_read
, "--"))) {
570 /* Trim off trailing "--" and anything beyond. */
572 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
573 if (strlen (end
) > 3 && *end
++ == '\n' &&
574 *end
++ == '-' && *end
++ == '-') {
575 end_boundary
= add (end
, NULL
);
582 if (! end_boundary
&& begin
> (off_t
) (ct
->c_begin
+ sizeof buffer
)) {
583 begin
-= sizeof buffer
;
589 /* Get boundary at beginning of multipart. */
591 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
592 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
593 if (bytes_read
>= strlen (end_boundary
)) {
594 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
596 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
597 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
602 /* The start and end boundaries didn't match, or the
603 start boundary doesn't begin with "\n--" (or "--"
604 if at the beginning of buffer). Keep trying. */
613 *part_boundary
= end_boundary
;
615 *part_boundary
= NULL
;
623 /* Open and copy ct->c_file to file, replacing the multipart boundary. */
625 replace_boundary (CT ct
, char *file
, const char *boundary
) {
628 char buf
[BUFSIZ
], name
[NAMESZ
];
630 m_getfld_state_t gstate
= 0;
633 if (ct
->c_file
== NULL
) {
634 advise (NULL
, "missing input filename");
638 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
639 advise (ct
->c_file
, "unable to open for reading");
643 if ((fpout
= fopen (file
, "w")) == NULL
) {
645 advise (file
, "unable to open for writing");
649 for (compnum
= 1;;) {
650 int bufsz
= (int) sizeof buf
;
652 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
)) {
657 /* get copies of the buffers */
658 np
= add (name
, NULL
);
659 vp
= add (buf
, NULL
);
661 /* if necessary, get rest of field */
662 while (state
== FLDPLUS
) {
664 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
);
665 vp
= add (buf
, vp
); /* add to previous value */
668 if (strcasecmp (TYPE_FIELD
, np
)) {
669 fprintf (fpout
, "%s:%s", np
, vp
);
671 char *new_boundary
= update_attr (vp
, "boundary=", boundary
);
673 fprintf (fpout
, "%s:%s\n", np
, new_boundary
);
684 /* buf will have a terminating NULL, skip it. */
685 fwrite (buf
, 1, bufsz
-1, fpout
);
693 advise (NULL
, "message format error in component #%d", compnum
);
698 advise (NULL
, "getfld() returned %d", state
);
706 m_getfld_state_destroy (&gstate
);
715 fix_multipart_cte (CT ct
, int *message_mods
) {
718 if (ct
->c_type
== CT_MULTIPART
) {
722 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
723 ct
->c_encoding
!= CE_BINARY
) {
726 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
727 char *name
= hf
->name
;
728 for (; *name
&& isspace ((unsigned char) *name
); ++name
) {
732 if (! strncasecmp (name
, ENCODING_FIELD
,
733 strlen (ENCODING_FIELD
))) {
734 char *prefix
= "Nmh-REPLACED-INVALID-";
735 HF h
= mh_xmalloc (sizeof *h
);
737 h
->name
= add (hf
->name
, NULL
);
738 h
->hf_encoding
= hf
->hf_encoding
;
742 /* Retain old header but prefix its name. */
744 hf
->name
= concat (prefix
, h
->name
, NULL
);
748 char *encoding
= cpytrim (hf
->value
);
749 report (ct
->c_partno
, ct
->c_file
,
750 "replace Content-Transfer-Encoding of %s "
751 "with 8 bit", encoding
);
755 h
->value
= add (" 8bit\n", NULL
);
757 /* Don't need to warn for multiple C-T-E header
758 fields, parse_mime() already does that. But
759 if there are any, fix them all as necessary. */
764 set_ce (ct
, CE_8BIT
);
767 m
= (struct multipart
*) ct
->c_ctparams
;
768 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
769 if (fix_multipart_cte (part
->mp_part
, message_mods
) != OK
) {
781 set_ce (CT ct
, int encoding
) {
782 const char *ce
= ce_str (encoding
);
783 const struct str2init
*ctinit
= get_ce_method (ce
);
786 char *cte
= concat (" ", ce
, "\n", NULL
);
789 /* Decoded contents might be in ct->c_cefile.ce_file, if the
790 caller is decode_text_parts (). Save because we'll
792 struct cefile decoded_content_info
= ct
->c_cefile
;
794 ct
->c_encoding
= encoding
;
796 ct
->c_ctinitfnx
= ctinit
->si_init
;
797 /* This will assign ct->c_cefile with an all-0 struct, which
799 (*ctinit
->si_init
) (ct
);
800 /* After returning, the caller should set
801 ct->c_cefile.ce_file to the name of the file containing
804 /* Restore the cefile. */
805 ct
->c_cefile
= decoded_content_info
;
807 /* Update/add Content-Transfer-Encoding header field. */
808 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
809 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
816 add_header (ct
, add (ENCODING_FIELD
, NULL
), cte
);
819 /* Update c_celine. It's used only by mhlist -debug. */
821 ct
->c_celine
= add (cte
, NULL
);
830 /* Make sure each text part has a corresponding text/plain part. */
832 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
, int replacetextplain
) {
835 switch ((*ct
)->c_type
) {
837 int has_text_plain
= 0;
839 /* Nothing to do for text/plain. */
840 if ((*ct
)->c_subtype
== TEXT_PLAIN
) return OK
;
842 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
843 parent
->c_subtype
== MULTI_ALTERNATE
) {
844 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
845 struct part
*part
, *prev
;
846 int new_subpart_number
= 1;
848 /* See if there is a sibling text/plain. */
849 for (prev
= part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
850 ++new_subpart_number
;
851 if (part
->mp_part
->c_type
== CT_TEXT
&&
852 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
853 if (replacetextplain
) {
854 struct part
*old_part
;
855 if (part
== mp
->mp_parts
) {
856 old_part
= mp
->mp_parts
;
857 mp
->mp_parts
= part
->mp_next
;
859 old_part
= prev
->mp_next
;
860 prev
->mp_next
= part
->mp_next
;
863 report (parent
->c_partno
, parent
->c_file
,
864 "remove text/plain part %s",
865 old_part
->mp_part
->c_partno
);
867 free_content (old_part
->mp_part
);
877 if (! has_text_plain
) {
878 /* Parent is a multipart/alternative. Insert a new
879 text/plain subpart. */
880 struct part
*new_part
= mh_xmalloc (sizeof *new_part
);
882 if ((new_part
->mp_part
= build_text_plain_part (*ct
))) {
884 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
886 new_part
->mp_next
= mp
->mp_parts
;
887 mp
->mp_parts
= new_part
;
888 new_part
->mp_part
->c_partno
=
889 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
894 report (parent
->c_partno
, parent
->c_file
,
895 "insert text/plain part");
898 free_content (new_part
->mp_part
);
904 /* Slip new text/plain part into a new multipart/alternative. */
905 CT tp_part
= build_text_plain_part (*ct
);
908 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
911 struct multipart
*mp
=
912 (struct multipart
*) mp_alt
->c_ctparams
;
914 if (mp
&& mp
->mp_parts
) {
915 mp
->mp_parts
->mp_part
= tp_part
;
916 /* Make the new multipart/alternative the parent. */
921 report ((*ct
)->c_partno
, (*ct
)->c_file
,
922 "insert text/plain part");
925 free_content (tp_part
);
926 free_content (mp_alt
);
940 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
943 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
944 if ((*ct
)->c_type
== CT_MULTIPART
) {
945 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
,
953 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
956 e
= (struct exbody
*) (*ct
)->c_ctparams
;
957 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
,
968 build_text_plain_part (CT encoded_part
) {
969 CT tp_part
= divide_part (encoded_part
);
970 char *tmp_plain_file
= NULL
;
972 if (decode_part (tp_part
) == OK
) {
973 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
974 contains the decoded contents. And the decoding function, such
975 as openQuoted, will have set ...->ce_unlink to 1 so that it will
976 be unlinked by free_content (). */
979 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
980 advise (NULL
, "unable to create temporary file in %s",
983 tmp_plain_file
= add (tempfile
, NULL
);
984 if (reformat_part (tp_part
, tmp_plain_file
,
985 tp_part
->c_ctinfo
.ci_type
,
986 tp_part
->c_ctinfo
.ci_subtype
,
987 tp_part
->c_type
) == OK
) {
992 free_content (tp_part
);
993 (void) m_unlink (tmp_plain_file
);
994 free (tmp_plain_file
);
1001 divide_part (CT ct
) {
1004 if ((new_part
= (CT
) calloc (1, sizeof *new_part
)) == NULL
)
1005 adios (NULL
, "out of memory");
1007 /* Just copy over what is needed for decoding. c_vrsn and
1008 c_celine aren't necessary. */
1009 new_part
->c_file
= add (ct
->c_file
, NULL
);
1010 new_part
->c_begin
= ct
->c_begin
;
1011 new_part
->c_end
= ct
->c_end
;
1012 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1013 new_part
->c_type
= ct
->c_type
;
1014 new_part
->c_cefile
= ct
->c_cefile
;
1015 new_part
->c_encoding
= ct
->c_encoding
;
1016 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1017 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1018 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1019 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1021 /* c_ctline is used by reformat__part(), so it can preserve
1022 anything after the type/subtype. */
1023 new_part
->c_ctline
= add (ct
->c_ctline
, NULL
);
1030 copy_ctinfo (CI dest
, CI src
) {
1031 char **s_ap
, **d_ap
, **s_vp
, **d_vp
;
1033 dest
->ci_type
= src
->ci_type
? add (src
->ci_type
, NULL
) : NULL
;
1034 dest
->ci_subtype
= src
->ci_subtype
? add (src
->ci_subtype
, NULL
) : NULL
;
1036 for (s_ap
= src
->ci_attrs
, d_ap
= dest
->ci_attrs
,
1037 s_vp
= src
->ci_values
, d_vp
= dest
->ci_values
;
1039 ++s_ap
, ++d_ap
, ++s_vp
, ++d_vp
) {
1040 *d_ap
= add (*s_ap
, NULL
);
1045 dest
->ci_comment
= src
->ci_comment
? add (src
->ci_comment
, NULL
) : NULL
;
1046 dest
->ci_magic
= src
->ci_magic
? add (src
->ci_magic
, NULL
) : NULL
;
1051 decode_part (CT ct
) {
1056 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1057 adios (NULL
, "unable to create temporary file in %s", get_temp_dir());
1059 tmp_decoded
= add (tempfile
, NULL
);
1060 /* The following call will load ct->c_cefile.ce_file with the tmp
1061 filename of the decoded content. tmp_decoded will contain the
1062 encoded output, get rid of that. */
1063 status
= output_message (ct
, tmp_decoded
);
1064 (void) m_unlink (tmp_decoded
);
1071 /* Some of the arguments aren't really needed now, but maybe will
1072 be in the future for other than text types. */
1074 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
) {
1075 int output_subtype
, output_encoding
;
1079 /* Hacky: this redirects the output from whatever command is used
1080 to show the part to a file. So, the user can't have any output
1081 redirection in that command.
1082 Could show_multi() in mhshowsbr.c avoid this? */
1084 /* Check for invo_name-format-type/subtype. */
1085 cp
= concat (invo_name
, "-format-", type
, "/", subtype
, NULL
);
1086 if ((cf
= context_find (cp
)) && *cf
!= '\0') {
1087 if (strchr (cf
, '>')) {
1089 advise (NULL
, "'>' prohibited in \"%s\",\nplease fix your "
1090 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1097 /* Check for invo_name-format-type. */
1098 cp
= concat (invo_name
, "-format-", type
, NULL
);
1099 if (! (cf
= context_find (cp
)) || *cf
== '\0') {
1102 advise (NULL
, "Don't know how to convert %s, there is no "
1103 "%s-format-%s/%s profile entry",
1104 ct
->c_file
, invo_name
, type
, subtype
);
1109 if (strchr (cf
, '>')) {
1111 advise (NULL
, "'>' prohibited in \"%s\"", cf
);
1117 cp
= concat (cf
, " >", file
, NULL
);
1118 status
= show_content_aux (ct
, 1, 0, cp
, NULL
);
1121 /* Unlink decoded content tmp file and free its filename to avoid
1122 leaks. The file stream should already have been closed. */
1123 if (ct
->c_cefile
.ce_unlink
) {
1124 (void) m_unlink (ct
->c_cefile
.ce_file
);
1125 free (ct
->c_cefile
.ce_file
);
1126 ct
->c_cefile
.ce_file
= NULL
;
1127 ct
->c_cefile
.ce_unlink
= 0;
1130 if (c_type
== CT_TEXT
) {
1131 output_subtype
= TEXT_PLAIN
;
1133 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1136 output_encoding
= charset_encoding (ct
);
1138 if (set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1139 ct
->c_cefile
.ce_file
= file
;
1140 ct
->c_cefile
.ce_unlink
= 1;
1142 ct
->c_cefile
.ce_unlink
= 0;
1150 /* Identifies 7bit or 8bit content based on charset. */
1152 charset_encoding (CT ct
) {
1153 /* norm_charmap() is case sensitive. */
1154 char *charset
= upcase (content_charset (ct
));
1156 strcmp (norm_charmap (charset
), "US-ASCII") ? CE_8BIT
: CE_7BIT
;
1164 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
) {
1165 char *boundary_prefix
= "----=_nmh-multipart";
1166 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1167 char *boundary_indicator
= "; boundary=";
1168 char *typename
, *subtypename
, *name
;
1171 struct multipart
*m
;
1173 const struct str2init
*ctinit
;
1175 if ((ct
= (CT
) calloc (1, sizeof *ct
)) == NULL
)
1176 adios (NULL
, "out of memory");
1178 /* Set up the multipart/alternative part. These fields of *ct were
1179 initialized to 0 by calloc():
1180 c_fp, c_unlink, c_begin, c_end,
1181 c_vrsn, c_ctline, c_celine,
1182 c_id, c_descr, c_dispo, c_partno,
1183 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1184 c_cefile, c_encoding,
1185 c_digested, c_digest[16], c_ctexbody,
1186 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1187 c_umask, c_pid, c_rfc934,
1188 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1191 ct
->c_file
= add (first_alt
->c_file
, NULL
);
1193 ct
->c_subtype
= subtype
;
1195 ctinit
= get_ct_init (ct
->c_type
);
1197 typename
= ct_type_str (type
);
1198 subtypename
= ct_subtype_str (type
, subtype
);
1202 int found_boundary
= 1;
1204 while (found_boundary
&& serial
< 1000000) {
1207 /* Ensure that the boundary doesn't appear in the decoded
1209 if (new_part
->c_cefile
.ce_file
) {
1210 if ((found_boundary
=
1211 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1212 new_part
->c_cefile
.ce_file
,
1218 /* Ensure that the boundary doesn't appear in the encoded
1220 if (! found_boundary
&& new_part
->c_file
) {
1221 if ((found_boundary
= boundary_in_content (&new_part
->c_fp
,
1228 if (found_boundary
) {
1229 /* Try a slightly different boundary. */
1234 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1236 concat (boundary_prefix
,
1237 first_alt
->c_partno
? first_alt
->c_partno
: "",
1238 "-", buffer2
, NULL
);
1242 if (found_boundary
) {
1243 advise (NULL
, "giving up trying to find a unique boundary");
1248 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1249 boundary
, "\"", NULL
);
1251 /* Load c_first_hf and c_last_hf. */
1252 transfer_noncontent_headers (first_alt
, ct
);
1253 add_header (ct
, add (TYPE_FIELD
, NULL
), concat (name
, "\n", NULL
));
1256 /* Load c_partno. */
1257 if (first_alt
->c_partno
) {
1258 ct
->c_partno
= add (first_alt
->c_partno
, NULL
);
1259 free (first_alt
->c_partno
);
1260 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1261 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1263 first_alt
->c_partno
= add ("1", NULL
);
1264 new_part
->c_partno
= add ("2", NULL
);
1268 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1269 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1272 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
,
1274 if ((cp
= strstr (name
, boundary_indicator
))) {
1275 ct
->c_ctinfo
.ci_attrs
[0] = name
;
1276 ct
->c_ctinfo
.ci_attrs
[1] = NULL
;
1277 /* ci_values don't get free'd, so point into ci_attrs. */
1278 ct
->c_ctinfo
.ci_values
[0] = cp
+ strlen (boundary_indicator
);
1281 p
= (struct part
*) mh_xmalloc (sizeof *p
);
1282 p
->mp_next
= (struct part
*) mh_xmalloc (sizeof *p
->mp_next
);
1283 p
->mp_next
->mp_next
= NULL
;
1284 p
->mp_next
->mp_part
= first_alt
;
1286 if ((m
= (struct multipart
*) calloc (1, sizeof (struct multipart
))) ==
1288 adios (NULL
, "out of memory");
1289 m
->mp_start
= concat (boundary
, "\n", NULL
);
1290 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1292 ct
->c_ctparams
= (void *) m
;
1300 /* Check that the boundary does not appear in the content. */
1302 boundary_in_content (FILE **fp
, char *file
, const char *boundary
) {
1303 char buffer
[BUFSIZ
];
1305 int found_boundary
= 0;
1307 /* free_content() will close *fp if we fopen it here. */
1308 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1309 advise (file
, "unable to open %s for reading", file
);
1313 fseeko (*fp
, 0L, SEEK_SET
);
1314 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1315 if (find_str (buffer
, bytes_read
, boundary
)) {
1321 return found_boundary
;
1325 /* Remove all non-Content headers. */
1327 transfer_noncontent_headers (CT old
, CT
new) {
1330 hp_prev
= hp
= old
->c_first_hf
;
1334 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, strlen (XXX_FIELD_PRF
))) {
1335 if (hp
== old
->c_last_hf
) {
1336 if (hp
== old
->c_first_hf
) {
1337 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1339 hp_prev
->next
= NULL
;
1340 old
->c_last_hf
= hp_prev
;
1343 if (hp
== old
->c_first_hf
) {
1344 old
->c_first_hf
= next
;
1346 hp_prev
->next
= next
;
1350 /* Put node hp in the new CT. */
1351 if (new->c_first_hf
== NULL
) {
1352 new->c_first_hf
= hp
;
1354 new->c_last_hf
->next
= hp
;
1356 new->c_last_hf
= hp
;
1358 /* A Content- header, leave in old. */
1368 set_ct_type (CT ct
, int type
, int subtype
, int encoding
) {
1369 char *typename
= ct_type_str (type
);
1370 char *subtypename
= ct_subtype_str (type
, subtype
);
1371 /* E.g, " text/plain" */
1372 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1373 /* E.g, " text/plain\n" */
1374 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1375 int found_content_type
= 0;
1377 const char *cp
= NULL
;
1381 /* Update/add Content-Type header field. */
1382 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1383 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1384 found_content_type
= 1;
1386 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1387 ? concat (type_subtypename
, cp
, "\n", NULL
)
1388 : add (name_plus_nl
, NULL
);
1391 if (! found_content_type
) {
1392 add_header (ct
, add (TYPE_FIELD
, NULL
),
1393 (cp
= strchr (ct
->c_ctline
, ';'))
1394 ? concat (type_subtypename
, cp
, "\n", NULL
)
1395 : add (name_plus_nl
, NULL
));
1398 /* Some of these might not be used, but set them anyway. */
1400 ? concat (type_subtypename
, cp
, NULL
)
1401 : concat (type_subtypename
, NULL
);
1402 free (ct
->c_ctline
);
1403 ct
->c_ctline
= ctline
;
1404 /* Leave other ctinfo members as they were. */
1405 free (ct
->c_ctinfo
.ci_type
);
1406 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1407 free (ct
->c_ctinfo
.ci_subtype
);
1408 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1410 ct
->c_subtype
= subtype
;
1412 free (name_plus_nl
);
1413 free (type_subtypename
);
1415 status
= set_ce (ct
, encoding
);
1422 decode_text_parts (CT ct
, int encoding
, int *message_mods
) {
1425 switch (ct
->c_type
) {
1427 switch (ct
->c_encoding
) {
1432 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
1433 if ((ct_encoding
= content_encoding (ct
)) == CE_BINARY
&&
1434 encoding
!= CE_BINARY
) {
1435 /* The decoding isn't acceptable so discard it.
1436 Leave status as OK to allow other transformations. */
1438 report (ct
->c_partno
, ct
->c_file
,
1439 "will not decode%s because it is binary",
1441 : ct
->c_ctline
? ct
->c_ctline
1444 (void) m_unlink (ct
->c_cefile
.ce_file
);
1445 free (ct
->c_cefile
.ce_file
);
1446 ct
->c_cefile
.ce_file
= NULL
;
1447 } else if (ct
->c_encoding
== CE_QUOTED
&&
1448 ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
1449 /* The decoding isn't acceptable so discard it.
1450 Leave status as OK to allow other transformations. */
1452 report (ct
->c_partno
, ct
->c_file
,
1453 "will not decode%s because it is 8bit",
1455 : ct
->c_ctline
? ct
->c_ctline
1458 (void) m_unlink (ct
->c_cefile
.ce_file
);
1459 free (ct
->c_cefile
.ce_file
);
1460 ct
->c_cefile
.ce_file
= NULL
;
1463 if (ct_encoding
== CE_BINARY
)
1465 else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
)
1468 enc
= charset_encoding (ct
);
1469 if (set_ce (ct
, enc
) == OK
) {
1472 report (ct
->c_partno
, ct
->c_file
, "decode%s",
1473 ct
->c_ctline
? ct
->c_ctline
: "");
1475 strip_crs (ct
, message_mods
);
1487 strip_crs (ct
, message_mods
);
1495 case CT_MULTIPART
: {
1496 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1499 /* Should check to see if the body for this part is encoded?
1500 For now, it gets passed along as-is by InitMultiPart(). */
1501 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1502 status
= decode_text_parts (part
->mp_part
, encoding
, message_mods
);
1508 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1511 e
= (struct exbody
*) ct
->c_ctparams
;
1512 status
= decode_text_parts (e
->eb_content
, encoding
, message_mods
);
1524 /* See if the decoded content is 7bit, 8bit, or binary. It's binary
1525 if it has any NUL characters, a CR not followed by a LF, or lines
1526 greater than 998 characters in length. */
1528 content_encoding (CT ct
) {
1529 CE ce
= &ct
->c_cefile
;
1530 int encoding
= CE_7BIT
;
1533 size_t line_len
= 0;
1534 char buffer
[BUFSIZ
];
1537 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1538 advise (ce
->ce_file
, "unable to open for reading");
1542 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
1543 while (encoding
!= CE_BINARY
&&
1544 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
1547 int last_char_was_cr
= 0;
1549 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
1550 if (*cp
== '\0' || ++line_len
> 998 ||
1551 (*cp
!= '\n' && last_char_was_cr
)) {
1552 encoding
= CE_BINARY
;
1554 } else if (*cp
== '\n') {
1556 } else if (! isascii ((unsigned char) *cp
)) {
1560 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
1566 } /* else should never happen */
1573 strip_crs (CT ct
, int *message_mods
) {
1574 /* norm_charmap() is case sensitive. */
1575 char *charset
= upcase (content_charset (ct
));
1578 /* Only strip carriage returns if content is ASCII or another
1579 charset that has the same readily recognizable CR followed by a
1580 LF. We can include UTF-8 here because if the high-order bit of
1581 a UTF-8 byte is 0, then it must be a single-byte ASCII
1583 if (! strcmp (norm_charmap (charset
), "US-ASCII") ||
1584 ! strncmp (norm_charmap (charset
), "ISO-8859-", 9) ||
1585 ! strncmp (norm_charmap (charset
), "UTF-8", 5) ||
1586 ! strncmp (norm_charmap (charset
), "WINDOWS-12", 10)) {
1592 int opened_input_file
= 0;
1594 if (ct
->c_cefile
.ce_file
) {
1595 file
= &ct
->c_cefile
.ce_file
;
1596 fp
= &ct
->c_cefile
.ce_fp
;
1598 } else if (ct
->c_file
) {
1601 begin
= (size_t) ct
->c_begin
;
1602 end
= (size_t) ct
->c_end
;
1603 } /* else don't know where the content is */
1605 if (file
&& *file
&& fp
) {
1607 if ((*fp
= fopen (*file
, "r")) == NULL
) {
1608 advise (*file
, "unable to open for reading");
1611 opened_input_file
= 1;
1617 char buffer
[BUFSIZ
];
1619 size_t bytes_to_read
=
1620 end
> 0 && end
> begin
? end
- begin
: sizeof buffer
;
1622 fseeko (*fp
, begin
, SEEK_SET
);
1623 while ((bytes_read
= fread (buffer
, 1,
1624 min (bytes_to_read
, sizeof buffer
),
1626 /* Look for CR followed by a LF. This is supposed to
1627 be text so there should be LF's. If not, don't
1628 modify the content. */
1631 int last_char_was_cr
= 0;
1633 if (end
> 0) bytes_to_read
-= bytes_read
;
1635 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
1636 if (*cp
== '\n' && last_char_was_cr
) {
1641 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
1647 char *stripped_content_file
;
1648 char *tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
);
1650 if (tempfile
== NULL
) {
1651 adios (NULL
, "unable to create temporary file in %s",
1654 stripped_content_file
= add (tempfile
, NULL
);
1656 /* Strip each CR before a LF from the content. */
1657 fseeko (*fp
, begin
, SEEK_SET
);
1658 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) >
1662 int last_char_was_cr
= 0;
1664 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
1666 last_char_was_cr
= 1;
1667 } else if (last_char_was_cr
) {
1668 if (*cp
!= '\n') write (fd
, "\r", 1);
1670 last_char_was_cr
= 0;
1673 last_char_was_cr
= 0;
1679 admonish (NULL
, "unable to write temporary file %s",
1680 stripped_content_file
);
1681 (void) m_unlink (stripped_content_file
);
1684 /* Replace the decoded file with the converted one. */
1685 if (ct
->c_cefile
.ce_file
) {
1686 if (ct
->c_cefile
.ce_unlink
) {
1687 (void) m_unlink (ct
->c_cefile
.ce_file
);
1689 free (ct
->c_cefile
.ce_file
);
1691 ct
->c_cefile
.ce_file
= stripped_content_file
;
1692 ct
->c_cefile
.ce_unlink
= 1;
1696 report (ct
->c_partno
,
1697 begin
== 0 && end
== 0 ? "" : *file
,
1703 if (opened_input_file
) {
1716 convert_charsets (CT ct
, char *dest_charset
, int *message_mods
) {
1719 switch (ct
->c_type
) {
1721 if (ct
->c_subtype
== TEXT_PLAIN
) {
1722 status
= convert_charset (ct
, dest_charset
, message_mods
);
1723 if (verbosw
&& status
== OK
) {
1724 report (ct
->c_partno
, ct
->c_file
, "convert %s to %s",
1725 content_charset(ct
), dest_charset
);
1730 case CT_MULTIPART
: {
1731 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1734 /* Should check to see if the body for this part is encoded?
1735 For now, it gets passed along as-is by InitMultiPart(). */
1736 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1738 convert_charsets (part
->mp_part
, dest_charset
, message_mods
);
1744 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1747 e
= (struct exbody
*) ct
->c_ctparams
;
1749 convert_charsets (e
->eb_content
, dest_charset
, message_mods
);
1762 write_content (CT ct
, char *input_filename
, char *outfile
, int modify_inplace
,
1766 if (modify_inplace
) {
1767 if (message_mods
> 0) {
1768 if ((status
= output_message (ct
, outfile
)) == OK
) {
1769 char *infile
= input_filename
1770 ? add (input_filename
, NULL
)
1771 : add (ct
->c_file
? ct
->c_file
: "-", NULL
);
1773 if (remove_file (infile
) == OK
) {
1774 if (rename (outfile
, infile
)) {
1775 /* Rename didn't work, possibly because of an
1776 attempt to rename across filesystems. Try
1777 brute force copy. */
1778 int old
= open (outfile
, O_RDONLY
);
1780 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
1783 if (old
!= -1 && new != -1) {
1784 char buffer
[BUFSIZ
];
1786 while ((i
= read (old
, buffer
, sizeof buffer
)) >
1788 if (write (new, buffer
, i
) != i
) {
1794 if (new != -1) close (new);
1795 if (old
!= -1) close (old
);
1796 (void) m_unlink (outfile
);
1799 /* The -file argument processing used path() to
1800 expand filename to absolute path. */
1801 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
1803 admonish (NULL
, "unable to rename %s %s to %s",
1804 file
? "file" : "message", outfile
,
1810 admonish (NULL
, "unable to remove input file %s, "
1811 "not modifying it", infile
);
1812 (void) m_unlink (outfile
);
1821 /* No modifications and didn't need the tmp outfile. */
1822 (void) m_unlink (outfile
);
1825 /* Output is going to some file. Produce it whether or not
1826 there were modifications. */
1827 status
= output_message (ct
, outfile
);
1836 * If "rmmproc" is defined, call that to remove the file. Otherwise,
1837 * use the standard MH backup file.
1840 remove_file (char *file
) {
1842 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
1843 int status
= system (rmm_command
);
1846 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
1848 /* This is OK for a non-message file, it still uses the
1849 BACKUP_PREFIX form. The backup file will be in the same
1850 directory as file. */
1851 return rename (file
, m_backup (file
));
1857 report (char *partno
, char *filename
, char *message
, ...) {
1862 va_start (args
, message
);
1863 fmt
= concat (filename
, partno
? " part " : ", ",
1864 partno
? partno
: "", partno
? ", " : "", message
, NULL
);
1866 advertise (NULL
, NULL
, fmt
, args
);
1879 fprintf (stderr
, "\n");