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
)))
356 seq_setcur (mp
, mp
->hghsel
); /* update current message */
357 seq_save (mp
); /* synchronize sequences */
358 context_replace (pfolder
, folder
);/* update current folder */
359 context_save (); /* save the context file */
363 for (ctp
= cts
; *ctp
; ++ctp
) {
364 status
+= mhfixmsgsbr (ctp
, &fx
, outfile
);
370 /* Just calling m_backup() unlinks the backup file. */
371 (void) m_backup (file
);
383 /* done is freects_done, which will clean up all of cts. */
390 mhfixmsgsbr (CT
*ctp
, const fix_transformations
*fx
, char *outfile
) {
391 /* Store input filename in case one of the transformations, i.e.,
392 fix_boundary(), rewrites to a tmp file. */
393 char *input_filename
= add ((*ctp
)->c_file
, NULL
);
394 int modify_inplace
= 0;
395 int message_mods
= 0;
398 if (outfile
== NULL
) {
401 if ((*ctp
)->c_file
) {
402 outfile
= add (m_mktemp2 (tmp
, invo_name
, NULL
, NULL
), NULL
);
404 adios (NULL
, "missing both input and output filenames\n");
408 reverse_alternative_parts (*ctp
);
409 if (status
== OK
&& fx
->fixboundary
) {
410 status
= fix_boundary (ctp
, &message_mods
);
412 if (status
== OK
&& fx
->fixcte
) {
413 status
= fix_multipart_cte (*ctp
, &message_mods
);
415 if (status
== OK
&& fx
->reformat
) {
416 status
= ensure_text_plain (ctp
, NULL
, &message_mods
);
418 if (status
== OK
&& fx
->decodetext
) {
419 status
= decode_text_parts (*ctp
, fx
->decodetext
, &message_mods
);
421 if (status
== OK
&& fx
->textcodeset
!= NULL
) {
422 status
= convert_codesets (*ctp
, fx
->textcodeset
, &message_mods
);
425 if (! (*ctp
)->c_umask
) {
426 /* Set the umask for the contents file. This currently
427 isn't used but just in case it is in the future. */
430 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
431 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
433 (*ctp
)->c_umask
= ~m_gmprot();
438 * Write the content to a file
441 status
= write_content (*ctp
, input_filename
, outfile
, modify_inplace
,
443 } else if (! modify_inplace
) {
444 /* Something went wrong. Output might be expected, such
445 as if this were run as a filter. Just copy the input
447 int in
= open (input_filename
, O_RDONLY
);
448 int out
= strcmp (outfile
, "-")
449 ? open (outfile
, O_WRONLY
| O_CREAT
, m_gmprot ())
452 if (in
!= -1 && out
!= -1) {
453 cpydata (in
, out
, input_filename
, outfile
);
462 if (modify_inplace
) {
467 free (input_filename
);
473 /* parse_mime() arranges alternates in reverse (priority) order, so
474 reverse them back. This will put a text/plain part at the front of
475 a multipart/alternative part, for example, where it belongs. */
477 reverse_alternative_parts (CT ct
) {
478 if (ct
->c_type
== CT_MULTIPART
) {
479 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
482 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
486 /* And call recursively on each part of a multipart. */
487 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
488 reverse_alternative_parts (part
->mp_part
);
495 fix_boundary (CT
*ct
, int *message_mods
) {
496 struct multipart
*mp
;
499 if (bogus_mp_content
) {
500 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
503 * 1) Get boundary at end of part.
504 * 2) Get boundary at beginning of part and compare to the end-of-part
506 * 3) Write out contents of ct to tmp file, replacing boundary in
507 * header with boundary from part. Set c_unlink to 1.
509 * 5) Call parse_mime() on the tmp file, replacing ct.
512 if (mp
&& mp
->mp_start
) {
515 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
518 if ((fixed
= m_mktemp2 (tmp
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
519 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
520 char *filename
= add ((*ct
)->c_file
, NULL
);
523 *ct
= parse_mime (fixed
);
528 report (NULL
, filename
, "fix multipart boundary");
532 advise (NULL
, "unable to replace broken boundary");
536 advise (NULL
, "unable to create temporary file");
540 free (part_boundary
);
550 get_multipart_boundary (CT ct
, char **part_boundary
) {
552 char *end_boundary
= NULL
;
553 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
554 ? (off_t
) (ct
->c_end
- sizeof buffer
)
555 : (off_t
) ct
->c_begin
;
559 /* This will fail if the boundary spans fread() calls. BUFSIZ should
560 be big enough, even if it's just 1024, to make that unlikely. */
562 /* free_content() will close ct->c_fp. */
563 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
564 advise (ct
->c_file
, "unable to open for reading");
568 /* Get boundary at end of multipart. */
569 while (begin
>= (off_t
) ct
->c_begin
) {
570 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
571 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
572 char *end
= buffer
+ bytes_read
- 1;
575 if ((cp
= rfind_str (buffer
, bytes_read
, "--"))) {
576 /* Trim off trailing "--" and anything beyond. */
578 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
579 if (strlen (end
) > 3 && *end
++ == '\n' &&
580 *end
++ == '-' && *end
++ == '-') {
581 end_boundary
= add (end
, NULL
);
588 if (! end_boundary
&& begin
> (off_t
) (ct
->c_begin
+ sizeof buffer
)) {
589 begin
-= sizeof buffer
;
595 /* Get boundary at beginning of multipart. */
597 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
598 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
599 if (bytes_read
>= strlen (end_boundary
)) {
600 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
602 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
603 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
606 /* Else the start and end boundaries didn't match, or
607 the start boundary doesn't begin with "\n--" (or
608 "--" if at the beginning of buffer). Keep trying. */
616 *part_boundary
= end_boundary
;
618 *part_boundary
= NULL
;
626 /* Open and copy ct->c_file to file, replacing the multipart boundary. */
628 replace_boundary (CT ct
, char *file
, const char *boundary
) {
631 char buf
[BUFSIZ
], name
[NAMESZ
];
633 m_getfld_state_t gstate
= 0;
636 if (ct
->c_file
== NULL
) {
637 advise (NULL
, "missing input filename");
641 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
642 advise (ct
->c_file
, "unable to open for reading");
646 if ((fpout
= fopen (file
, "w")) == NULL
) {
648 advise (file
, "unable to open for writing");
652 for (compnum
= 1;;) {
653 int bufsz
= (int) sizeof buf
;
655 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
)) {
660 /* get copies of the buffers */
661 np
= add (name
, NULL
);
662 vp
= add (buf
, NULL
);
664 /* if necessary, get rest of field */
665 while (state
== FLDPLUS
) {
667 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
);
668 vp
= add (buf
, vp
); /* add to previous value */
671 if (strcasecmp (TYPE_FIELD
, np
)) {
672 fprintf (fpout
, "%s:%s", np
, vp
);
674 char *new_boundary
= update_attr (vp
, "boundary=", boundary
);
676 fprintf (fpout
, "%s:%s\n", np
, new_boundary
);
687 /* buf will have a terminating NULL, skip it. */
688 fwrite (buf
, 1, bufsz
-1, fpout
);
696 advise (NULL
, "message format error in component #%d", compnum
);
701 advise (NULL
, "getfld() returned %d", state
);
709 m_getfld_state_destroy (&gstate
);
717 /* Change the value of a name=value pair in a header field body.
718 If the name isn't there, append them. In any case, a new
719 string will be allocated and must be free'd by the caller.
720 Trims any trailing newlines. */
722 update_attr (char *body
, const char *name
, const char *value
) {
723 char *bp
= nmh_strcasestr (body
, name
);
727 char *other_attrs
= strchr (bp
, ';');
729 *(bp
+ strlen (name
)) = '\0';
730 new_body
= concat (body
, "\"", value
, "\"", NULL
);
735 /* Trim any trailing newlines. */
736 for (cp
= &other_attrs
[strlen (other_attrs
) - 1];
737 cp
> other_attrs
&& *cp
== '\n';
738 *cp
-- = '\0') continue;
739 new_body
= add (other_attrs
, new_body
);
744 /* Append name/value pair, after first removing a final newline
745 and (extraneous) semicolon. */
746 if (*(cp
= &body
[strlen (body
) - 1]) == '\n') *cp
= '\0';
747 if (*(cp
= &body
[strlen (body
) - 1]) == ';') *cp
= '\0';
748 new_body
= concat (body
, "; ", name
, "\"", value
, "\"", NULL
);
756 fix_multipart_cte (CT ct
, int *message_mods
) {
759 if (ct
->c_type
== CT_MULTIPART
) {
763 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
764 ct
->c_encoding
!= CE_BINARY
) {
767 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
768 char *name
= hf
->name
;
769 for (; *name
&& isspace ((unsigned char) *name
); ++name
) {
773 if (! strncasecmp (name
, ENCODING_FIELD
,
774 strlen (ENCODING_FIELD
))) {
775 char *prefix
= "Nmh-REPLACED-INVALID-";
776 HF h
= mh_xmalloc (sizeof *h
);
778 h
->name
= add (hf
->name
, NULL
);
779 h
->hf_encoding
= hf
->hf_encoding
;
783 /* Retain old header but prefix its name. */
785 hf
->name
= concat (prefix
, h
->name
, NULL
);
789 char *encoding
= cpytrim (hf
->value
);
790 report (ct
->c_partno
, ct
->c_file
,
791 "replace Content-Transfer-Encoding of %s "
792 "with 8 bit", encoding
);
796 h
->value
= add (" 8bit\n", NULL
);
798 /* Don't need to warn for multiple C-T-E header
799 fields, parse_mime() already does that. But
800 if there are any, fix them all as necessary. */
805 set_ce (ct
, CE_8BIT
);
808 m
= (struct multipart
*) ct
->c_ctparams
;
809 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
810 if (fix_multipart_cte (part
->mp_part
, message_mods
) != OK
) {
822 set_ce (CT ct
, int encoding
) {
823 const char *ce
= ce_str (encoding
);
824 const struct str2init
*ctinit
= get_ce_method (ce
);
827 char *cte
= concat (" ", ce
, "\n", NULL
);
830 /* Decoded contents might be in ct->c_cefile.ce_file, if the
831 caller is decode_text_parts (). Save because we'll
833 struct cefile decoded_content_info
= ct
->c_cefile
;
835 ct
->c_encoding
= encoding
;
837 ct
->c_ctinitfnx
= ctinit
->si_init
;
838 /* This will assign ct->c_cefile with an all-0 struct, which
840 (*ctinit
->si_init
) (ct
);
841 /* After returning, the caller should set
842 ct->c_cefile.ce_file to the name of the file containing
845 /* Restore the cefile. */
846 ct
->c_cefile
= decoded_content_info
;
848 /* Update/add Content-Transfer-Encoding header field. */
849 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
850 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
857 add_header (ct
, add (ENCODING_FIELD
, NULL
), cte
);
860 /* Update c_celine. It's used only by mhlist -debug. */
862 ct
->c_celine
= add (cte
, NULL
);
871 /* Make sure each text part has a corresponding text/plain part. */
873 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
) {
876 switch ((*ct
)->c_type
) {
878 int has_text_plain
= 0;
880 /* Nothing to do for text/plain. */
881 if ((*ct
)->c_subtype
== TEXT_PLAIN
) return OK
;
883 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
884 parent
->c_subtype
== MULTI_ALTERNATE
) {
885 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
887 int new_subpart_number
= 1;
889 /* See if there is a sibling text/plain. */
890 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
891 ++new_subpart_number
;
892 if (part
->mp_part
->c_type
== CT_TEXT
&&
893 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
899 if (! has_text_plain
) {
900 /* Parent is a multipart/alternative. Insert a new
901 text/plain subpart. */
902 struct part
*new_part
= mh_xmalloc (sizeof *new_part
);
904 if ((new_part
->mp_part
= build_text_plain_part (*ct
))) {
906 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
908 new_part
->mp_next
= mp
->mp_parts
;
909 mp
->mp_parts
= new_part
;
910 new_part
->mp_part
->c_partno
=
911 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
916 report (parent
->c_partno
, parent
->c_file
,
917 "insert text/plain part");
920 free_content (new_part
->mp_part
);
926 /* Slip new text/plain part into a new multipart/alternative. */
927 CT tp_part
= build_text_plain_part (*ct
);
930 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
933 struct multipart
*mp
=
934 (struct multipart
*) mp_alt
->c_ctparams
;
936 if (mp
&& mp
->mp_parts
) {
937 mp
->mp_parts
->mp_part
= tp_part
;
938 /* Make the new multipart/alternative the parent. */
943 report ((*ct
)->c_partno
, (*ct
)->c_file
,
944 "insert text/plain part");
947 free_content (tp_part
);
948 free_content (mp_alt
);
958 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
961 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
962 if ((*ct
)->c_type
== CT_MULTIPART
) {
963 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
);
970 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
973 e
= (struct exbody
*) (*ct
)->c_ctparams
;
974 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
);
984 build_text_plain_part (CT encoded_part
) {
985 CT tp_part
= divide_part (encoded_part
);
986 char *tmp_plain_file
= NULL
;
988 if (decode_part (tp_part
) == OK
) {
989 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
990 contains the decoded contents. And the decoding function, such
991 as openQuoted, will have set ...->ce_unlink to 1 so that it will
992 be unlinked by free_content (). */
993 tmp_plain_file
= add (m_mktemp2 (tmp
, invo_name
, NULL
, NULL
), NULL
);
994 if (reformat_part (tp_part
, tmp_plain_file
,
995 tp_part
->c_ctinfo
.ci_type
,
996 tp_part
->c_ctinfo
.ci_subtype
,
997 tp_part
->c_type
) == OK
) {
1002 free_content (tp_part
);
1003 unlink (tmp_plain_file
);
1004 free (tmp_plain_file
);
1011 divide_part (CT ct
) {
1014 if ((new_part
= (CT
) calloc (1, sizeof *new_part
)) == NULL
)
1015 adios (NULL
, "out of memory");
1017 /* Just copy over what is needed for decoding. c_vrsn and
1018 c_celine aren't necessary. */
1019 new_part
->c_file
= add (ct
->c_file
, NULL
);
1020 new_part
->c_begin
= ct
->c_begin
;
1021 new_part
->c_end
= ct
->c_end
;
1022 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1023 new_part
->c_type
= ct
->c_type
;
1024 new_part
->c_cefile
= ct
->c_cefile
;
1025 new_part
->c_encoding
= ct
->c_encoding
;
1026 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1027 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1028 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1029 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1031 /* c_ctline is used by reformat__part(), so it can preserve
1032 anything after the type/subtype. */
1033 new_part
->c_ctline
= add (ct
->c_ctline
, NULL
);
1040 copy_ctinfo (CI dest
, CI src
) {
1041 char **s_ap
, **d_ap
, **s_vp
, **d_vp
;
1043 dest
->ci_type
= src
->ci_type
? add (src
->ci_type
, NULL
) : NULL
;
1044 dest
->ci_subtype
= src
->ci_subtype
? add (src
->ci_subtype
, NULL
) : NULL
;
1046 for (s_ap
= src
->ci_attrs
, d_ap
= dest
->ci_attrs
,
1047 s_vp
= src
->ci_values
, d_vp
= dest
->ci_values
;
1049 ++s_ap
, ++d_ap
, ++s_vp
, ++d_vp
) {
1050 *d_ap
= add (*s_ap
, NULL
);
1055 dest
->ci_comment
= src
->ci_comment
? add (src
->ci_comment
, NULL
) : NULL
;
1056 dest
->ci_magic
= src
->ci_magic
? add (src
->ci_magic
, NULL
) : NULL
;
1061 decode_part (CT ct
) {
1065 tmp_decoded
= add (m_mktemp2 (tmp
, invo_name
, NULL
, NULL
), NULL
);
1066 /* The following call will load ct->c_cefile.ce_file with the tmp
1067 filename of the decoded content. tmp_decoded will contain the
1068 encoded output, get rid of that. */
1069 status
= output_message (ct
, tmp_decoded
);
1070 unlink (tmp_decoded
);
1077 /* Some of the arguments aren't really needed now, but maybe will
1078 be in the future for other than text types. */
1080 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
) {
1081 int output_subtype
, output_encoding
;
1085 /* Hacky: this redirects the output from whatever command is used
1086 to show the part to a file. So, the user can't have any output
1087 redirection in that command.
1088 Could show_multi() in mhshowsbr.c avoid this? */
1090 /* Check for invo_name-format-type/subtype. */
1091 cp
= concat (invo_name
, "-format-", type
, "/", subtype
, NULL
);
1092 if ((cf
= context_find (cp
)) && *cf
!= '\0') {
1093 if (strchr (cf
, '>')) {
1095 advise (NULL
, "'>' prohibited in \"%s\",\nplease fix your "
1096 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1103 /* Check for invo_name-format-type. */
1104 cp
= concat (invo_name
, "-format-", type
, NULL
);
1105 if (! (cf
= context_find (cp
)) || *cf
== '\0') {
1108 advise (NULL
, "Don't know how to convert %s, there is no "
1109 "%s-format-%s/%s profile entry",
1110 ct
->c_file
, invo_name
, type
, subtype
);
1115 if (strchr (cf
, '>')) {
1117 advise (NULL
, "'>' prohibited in \"%s\"", cf
);
1123 cp
= concat (cf
, " >", file
, NULL
);
1124 status
= show_content_aux (ct
, 1, 0, cp
, NULL
);
1127 /* Unlink decoded content tmp file and free its filename to avoid
1128 leaks. The file stream should already have been closed. */
1129 if (ct
->c_cefile
.ce_unlink
) {
1130 unlink (ct
->c_cefile
.ce_file
);
1131 free (ct
->c_cefile
.ce_file
);
1132 ct
->c_cefile
.ce_file
= NULL
;
1133 ct
->c_cefile
.ce_unlink
= 0;
1136 if (c_type
== CT_TEXT
) {
1137 output_subtype
= TEXT_PLAIN
;
1139 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1142 output_encoding
= charset_encoding (ct
);
1144 if (set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1145 ct
->c_cefile
.ce_file
= file
;
1146 ct
->c_cefile
.ce_unlink
= 1;
1148 ct
->c_cefile
.ce_unlink
= 0;
1156 /* Identifies 7bit or 8bit content based on charset, if specified. */
1158 charset_encoding (CT ct
) {
1159 int encoding
= CE_8BIT
;
1160 CI ctinfo
= &ct
->c_ctinfo
;
1163 for (ap
= ctinfo
->ci_attrs
, vp
= ctinfo
->ci_values
; *ap
; ++ap
, ++vp
) {
1164 if (! strcasecmp (*ap
, "charset")) {
1165 /* norm_charmap() is case sensitive. */
1166 char *ch
= upcase (*vp
);
1168 if (! strcmp (norm_charmap (ch
), "US-ASCII")) encoding
= CE_7BIT
;
1179 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
) {
1180 char *boundary_prefix
= "----=_nmh-multipart";
1181 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1182 char *boundary_indicator
= "; boundary=";
1183 char *typename
, *subtypename
, *name
;
1186 struct multipart
*m
;
1188 const struct str2init
*ctinit
;
1190 if ((ct
= (CT
) calloc (1, sizeof *ct
)) == NULL
)
1191 adios (NULL
, "out of memory");
1193 /* Set up the multipart/alternative part. These fields of *ct were
1194 initialized to 0 by calloc():
1195 c_fp, c_unlink, c_begin, c_end,
1196 c_vrsn, c_ctline, c_celine,
1197 c_id, c_descr, c_dispo, c_partno,
1198 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1199 c_cefile, c_encoding,
1200 c_digested, c_digest[16], c_ctexbody,
1201 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1202 c_umask, c_pid, c_rfc934,
1203 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1206 ct
->c_file
= add (first_alt
->c_file
, NULL
);
1208 ct
->c_subtype
= subtype
;
1210 ctinit
= get_ct_init (ct
->c_type
);
1212 typename
= ct_type_str (type
);
1213 subtypename
= ct_subtype_str (type
, subtype
);
1217 int found_boundary
= 1;
1219 while (found_boundary
&& serial
< 1000000) {
1222 /* Ensure that the boundary doesn't appear in the decoded
1224 if (new_part
->c_cefile
.ce_file
) {
1225 if ((found_boundary
=
1226 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1227 new_part
->c_cefile
.ce_file
,
1233 /* Ensure that the boundary doesn't appear in the encoded
1235 if (! found_boundary
&& new_part
->c_file
) {
1236 if ((found_boundary
= boundary_in_content (&new_part
->c_fp
,
1243 if (found_boundary
) {
1244 /* Try a slightly different boundary. */
1249 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1251 concat (boundary_prefix
,
1252 first_alt
->c_partno
? first_alt
->c_partno
: "",
1253 "-", buffer2
, NULL
);
1257 if (found_boundary
) {
1258 advise (NULL
, "giving up trying to find a unique boundary");
1263 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1264 boundary
, "\"", NULL
);
1266 /* Load c_first_hf and c_last_hf. */
1267 transfer_noncontent_headers (first_alt
, ct
);
1268 add_header (ct
, add (TYPE_FIELD
, NULL
), concat (name
, "\n", NULL
));
1271 /* Load c_partno. */
1272 if (first_alt
->c_partno
) {
1273 ct
->c_partno
= add (first_alt
->c_partno
, NULL
);
1274 free (first_alt
->c_partno
);
1275 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1276 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1278 first_alt
->c_partno
= add ("1", NULL
);
1279 new_part
->c_partno
= add ("2", NULL
);
1283 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1284 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1287 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
,
1289 if ((cp
= strstr (name
, boundary_indicator
))) {
1290 ct
->c_ctinfo
.ci_attrs
[0] = name
;
1291 ct
->c_ctinfo
.ci_attrs
[1] = NULL
;
1292 /* ci_values don't get free'd, so point into ci_attrs. */
1293 ct
->c_ctinfo
.ci_values
[0] = cp
+ strlen (boundary_indicator
);
1296 p
= (struct part
*) mh_xmalloc (sizeof *p
);
1297 p
->mp_next
= (struct part
*) mh_xmalloc (sizeof *p
->mp_next
);
1298 p
->mp_next
->mp_next
= NULL
;
1299 p
->mp_next
->mp_part
= first_alt
;
1301 if ((m
= (struct multipart
*) calloc (1, sizeof (struct multipart
))) ==
1303 adios (NULL
, "out of memory");
1304 m
->mp_start
= concat (boundary
, "\n", NULL
);
1305 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1307 ct
->c_ctparams
= (void *) m
;
1315 /* Check that the boundary does not appear in the content. */
1317 boundary_in_content (FILE **fp
, char *file
, const char *boundary
) {
1318 char buffer
[BUFSIZ
];
1320 int found_boundary
= 0;
1322 /* free_content() will close *fp if we fopen it here. */
1323 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1324 advise (file
, "unable to open %s for reading", file
);
1328 fseeko (*fp
, 0L, SEEK_SET
);
1329 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1330 if (find_str (buffer
, bytes_read
, boundary
)) {
1336 return found_boundary
;
1340 /* Remove all non-Content headers. */
1342 transfer_noncontent_headers (CT old
, CT
new) {
1345 hp_prev
= hp
= old
->c_first_hf
;
1349 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, strlen (XXX_FIELD_PRF
))) {
1350 if (hp
== old
->c_last_hf
) {
1351 if (hp
== old
->c_first_hf
) {
1352 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1354 hp_prev
->next
= NULL
;
1355 old
->c_last_hf
= hp_prev
;
1358 if (hp
== old
->c_first_hf
) {
1359 old
->c_first_hf
= next
;
1361 hp_prev
->next
= next
;
1365 /* Put node hp in the new CT. */
1366 if (new->c_first_hf
== NULL
) {
1367 new->c_first_hf
= hp
;
1369 new->c_last_hf
->next
= hp
;
1371 new->c_last_hf
= hp
;
1373 /* A Content- header, leave in old. */
1383 set_ct_type (CT ct
, int type
, int subtype
, int encoding
) {
1384 char *typename
= ct_type_str (type
);
1385 char *subtypename
= ct_subtype_str (type
, subtype
);
1386 /* E.g, " text/plain" */
1387 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1388 /* E.g, " text/plain\n" */
1389 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1390 int found_content_type
= 0;
1392 const char *cp
= NULL
;
1396 /* Update/add Content-Type header field. */
1397 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1398 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1399 found_content_type
= 1;
1401 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1402 ? concat (type_subtypename
, cp
, "\n", NULL
)
1403 : add (name_plus_nl
, NULL
);
1406 if (! found_content_type
) {
1407 add_header (ct
, add (TYPE_FIELD
, NULL
),
1408 (cp
= strchr (ct
->c_ctline
, ';'))
1409 ? concat (type_subtypename
, cp
, "\n", NULL
)
1410 : add (name_plus_nl
, NULL
));
1413 /* Some of these might not be used, but set them anyway. */
1415 ? concat (type_subtypename
, cp
, NULL
)
1416 : concat (type_subtypename
, NULL
);
1417 free (ct
->c_ctline
);
1418 ct
->c_ctline
= ctline
;
1419 /* Leave other ctinfo members as they were. */
1420 free (ct
->c_ctinfo
.ci_type
);
1421 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1422 free (ct
->c_ctinfo
.ci_subtype
);
1423 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1425 ct
->c_subtype
= subtype
;
1427 free (name_plus_nl
);
1428 free (type_subtypename
);
1430 status
= set_ce (ct
, encoding
);
1437 decode_text_parts (CT ct
, int encoding
, int *message_mods
) {
1440 switch (ct
->c_type
) {
1442 switch (ct
->c_encoding
) {
1447 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
1448 if ((ct_encoding
= content_encoding (ct
)) == CE_BINARY
&&
1449 encoding
!= CE_BINARY
) {
1451 report (ct
->c_partno
, ct
->c_file
,
1452 "will not decode%s because it is binary",
1454 : ct
->c_ctline
? ct
->c_ctline
1457 unlink (ct
->c_cefile
.ce_file
);
1458 free (ct
->c_cefile
.ce_file
);
1459 ct
->c_cefile
.ce_file
= NULL
;
1460 } else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
1462 report (ct
->c_partno
, ct
->c_file
,
1463 "will not decode%s because it is 8bit",
1465 : ct
->c_ctline
? ct
->c_ctline
1468 unlink (ct
->c_cefile
.ce_file
);
1469 free (ct
->c_cefile
.ce_file
);
1470 ct
->c_cefile
.ce_file
= NULL
;
1472 int enc
= ct_encoding
== CE_BINARY
1474 : charset_encoding (ct
);
1475 if (set_ce (ct
, enc
) == OK
) {
1478 report (ct
->c_partno
, ct
->c_file
, "decode%s",
1479 ct
->c_ctline
? ct
->c_ctline
: "");
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 char buffer
[BUFSIZ
];
1536 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1537 advise (ce
->ce_file
, "unable to open for reading");
1541 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
1542 while (encoding
!= CE_BINARY
&&
1543 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
1546 size_t line_len
= 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 convert_codesets (CT ct
, char *dest_codeset
, int *message_mods
) {
1576 switch (ct
->c_type
) {
1578 if (ct
->c_subtype
== TEXT_PLAIN
) {
1579 status
= convert_codeset (ct
, dest_codeset
, message_mods
);
1583 case CT_MULTIPART
: {
1584 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1587 /* Should check to see if the body for this part is encoded?
1588 For now, it gets passed along as-is by InitMultiPart(). */
1589 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1591 convert_codesets (part
->mp_part
, dest_codeset
, message_mods
);
1597 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1600 e
= (struct exbody
*) ct
->c_ctparams
;
1602 convert_codesets (e
->eb_content
, dest_codeset
, message_mods
);
1615 convert_codeset (CT ct
, char *dest_codeset
, int *message_mods
) {
1616 const char *const charset
= "charset";
1617 char **src_codeset
= NULL
;
1618 char *default_codeset
= NULL
;
1619 CI ctinfo
= &ct
->c_ctinfo
;
1623 for (ap
= ctinfo
->ci_attrs
, vp
= ctinfo
->ci_values
; *ap
; ++ap
, ++vp
) {
1624 if (! strcasecmp (*ap
, charset
)) {
1629 /* RFC 2045, Sec. 5.2: default to us-ascii. */
1630 if (src_codeset
== NULL
) src_codeset
= &default_codeset
;
1631 if (*src_codeset
== NULL
) *src_codeset
= "US-ASCII";
1633 if (strcmp (norm_charmap (*src_codeset
), norm_charmap (dest_codeset
))) {
1635 iconv_t conv_desc
= NULL
;
1642 int opened_input_file
= 0;
1643 char src_buffer
[BUFSIZ
];
1646 if ((conv_desc
= iconv_open (dest_codeset
, *src_codeset
)) ==
1648 advise (NULL
, "Can't convert %s to %s", *src_codeset
, dest_codeset
);
1652 dest
= add (m_mktemp2 (tmp
, invo_name
, &fd
, NULL
), NULL
);
1654 if (ct
->c_cefile
.ce_file
) {
1655 file
= &ct
->c_cefile
.ce_file
;
1656 fp
= &ct
->c_cefile
.ce_fp
;
1659 } else if (ct
->c_file
) {
1662 begin
= ct
->c_begin
;
1664 } /* else no input file: shouldn't happen */
1666 if (file
&& *file
&& fp
) {
1668 if ((*fp
= fopen (*file
, "r")) == NULL
) {
1669 advise (*file
, "unable to open for reading");
1672 opened_input_file
= 1;
1679 size_t max
= end
> 0 ? (size_t) (end
-begin
) : sizeof src_buffer
;
1681 fseeko (*fp
, begin
, SEEK_SET
);
1682 while (status
== OK
&& max
> 0 &&
1683 (inbytes
= fread (src_buffer
, 1, max
, *fp
)) > 0) {
1684 char dest_buffer
[BUFSIZ
];
1685 char *ib
= src_buffer
, *ob
= dest_buffer
;
1686 size_t outbytes
= sizeof dest_buffer
;
1687 size_t outbytes_before
= outbytes
;
1689 if (end
> 0) max
-= inbytes
;
1691 if (iconv (conv_desc
, &ib
, &inbytes
, &ob
, &outbytes
) ==
1696 write (fd
, dest_buffer
, outbytes_before
- outbytes
);
1700 if (opened_input_file
) {
1706 iconv_close (conv_desc
);
1710 /* Replace the decoded file with the converted one. */
1711 if (ct
->c_cefile
.ce_file
) {
1712 if (ct
->c_cefile
.ce_unlink
) {
1713 unlink (ct
->c_cefile
.ce_file
);
1715 free (ct
->c_cefile
.ce_file
);
1717 ct
->c_cefile
.ce_file
= dest
;
1718 ct
->c_cefile
.ce_unlink
= 1;
1722 report (ct
->c_partno
, ct
->c_file
, "convert %s to %s",
1723 *src_codeset
, dest_codeset
);
1726 /* Update ci_attrs. */
1727 *src_codeset
= dest_codeset
;
1729 /* Update ct->c_ctline. */
1732 update_attr (ct
->c_ctline
, "charset=", dest_codeset
);
1734 free (ct
->c_ctline
);
1735 ct
->c_ctline
= ctline
;
1736 } /* else no CT line, which is odd */
1738 /* Update Content-Type header field. */
1739 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1740 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1741 char *ctline_less_newline
=
1742 update_attr (hf
->value
, "charset=", dest_codeset
);
1743 char *ctline
= concat (ctline_less_newline
, "\n", NULL
);
1744 free (ctline_less_newline
);
1754 #else /* ! HAVE_ICONV */
1755 NMH_UNUSED (message_mods
);
1757 advise (NULL
, "Can't convert %s to %s without iconv", *src_codeset
,
1760 #endif /* ! HAVE_ICONV */
1768 write_content (CT ct
, char *input_filename
, char *outfile
, int modify_inplace
,
1772 if (modify_inplace
) {
1773 if (message_mods
> 0) {
1774 if ((status
= output_message (ct
, outfile
)) == OK
) {
1775 char *infile
= input_filename
1776 ? add (input_filename
, NULL
)
1777 : add (ct
->c_file
? ct
->c_file
: "-", NULL
);
1779 if (remove_file (infile
) == OK
) {
1780 if (rename (outfile
, infile
)) {
1781 /* Rename didn't work, possibly because of an
1782 attempt to rename across filesystems. Try
1783 brute force copy. */
1784 int old
= open (outfile
, O_RDONLY
);
1786 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
1789 if (old
!= -1 && new != -1) {
1790 char buffer
[BUFSIZ
];
1792 while ((i
= read (old
, buffer
, sizeof buffer
)) >
1794 if (write (new, buffer
, i
) != i
) {
1800 if (new != -1) close (new);
1801 if (old
!= -1) close (old
);
1805 /* The -file argument processing used path() to
1806 expand filename to absolute path. */
1807 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
1809 admonish (NULL
, "unable to rename %s %s to %s",
1810 file
? "file" : "message", outfile
,
1816 admonish (NULL
, "unable to remove input file %s, "
1817 "not modifying it", infile
);
1825 /* No modifications and didn't need the tmp outfile. */
1829 /* Output is going to some file. Produce it whether or not
1830 there were modifications. */
1831 status
= output_message (ct
, outfile
);
1840 * If "rmmproc" is defined, call that to remove the file. Otherwise,
1841 * use the standard MH backup file.
1844 remove_file (char *file
) {
1846 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
1847 int status
= system (rmm_command
);
1850 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
1852 /* This is OK for a non-message file, it still uses the
1853 BACKUP_PREFIX form. The backup file will be in the same
1854 directory as file. */
1855 return rename (file
, m_backup (file
));
1861 report (char *partno
, char *filename
, char *message
, ...) {
1866 va_start (args
, message
);
1867 fmt
= concat (filename
, partno
? " part " : ", ",
1868 partno
? partno
: "", partno
? ", " : "", message
, NULL
);
1870 advertise (NULL
, NULL
, fmt
, args
);
1879 upcase (char *str
) {
1880 char *up
= cpytrim (str
);
1883 for (cp
= up
; *cp
; ++cp
) *cp
= toupper ((unsigned char) *cp
);
1894 fprintf (stderr
, "\n");