3 * mhstoresbr.c -- routines to save/store the contents of MIME messages
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
16 #include <h/mhparse.h>
19 enum clobber_policy_t
{
20 NMH_CLOBBER_ALWAYS
= 0,
27 static enum clobber_policy_t
clobber_policy (const char *);
30 CT
*cts
; /* Top-level list of contents to store. */
31 char *cwd
; /* cached current directory */
32 int autosw
; /* -auto enabled */
33 int verbosw
; /* -verbose enabled */
34 int files_not_clobbered
; /* output flag indicating that store failed
35 in order to not clobber an existing file */
37 /* The following must never be touched by a caller: they are for
38 internal use by the mhstoresbr functions. */
39 char *dir
; /* directory in which to store contents */
40 enum clobber_policy_t clobber_policy
; /* -clobber selection */
43 static bool use_param_as_filename(const char *p
);
46 mhstoreinfo_create (CT
*ct
, char *pwd
, const char *csw
, int asw
, int vsw
) {
54 info
->files_not_clobbered
= 0;
56 info
->clobber_policy
= clobber_policy (csw
);
62 mhstoreinfo_free (mhstoreinfo_t info
) {
69 mhstoreinfo_files_not_clobbered (const mhstoreinfo_t info
) {
70 return info
->files_not_clobbered
;
75 * Type for a compare function for qsort. This keeps
78 typedef int (*qsort_comp
) (const void *, const void *);
83 int type_ok (CT
, int);
84 void flush_errors (void);
89 static void store_single_message (CT
, mhstoreinfo_t
);
90 static int store_switch (CT
, mhstoreinfo_t
);
91 static int store_generic (CT
, mhstoreinfo_t
);
92 static int store_application (CT
, mhstoreinfo_t
);
93 static int store_multi (CT
, mhstoreinfo_t
);
94 static int store_partial (CT
, mhstoreinfo_t
);
95 static int store_external (CT
, mhstoreinfo_t
);
96 static int ct_compar (CT
*, CT
*);
97 static int store_content (CT
, CT
, mhstoreinfo_t
);
98 static int output_content_file (CT
, int);
99 static int output_content_folder (char *, char *);
100 static int parse_format_string (CT
, char *, char *, int, char *);
101 static void get_storeproc (CT
);
102 static int copy_some_headers (FILE *, CT
);
103 static char *clobber_check (char *, mhstoreinfo_t
);
106 * Main entry point to store content
107 * from a collection of messages.
111 store_all_messages (mhstoreinfo_t info
)
117 * Check for the directory in which to
118 * store any contents.
120 if ((cp
= context_find (nmhstorage
)) && *cp
)
121 info
->dir
= mh_xstrdup(cp
);
123 info
->dir
= getcpy (info
->cwd
);
125 for (ctp
= info
->cts
; *ctp
; ctp
++) {
127 store_single_message (ct
, info
);
135 * Entry point to store the content
136 * in a (single) message
140 store_single_message (CT ct
, mhstoreinfo_t info
)
142 if (type_ok (ct
, 1)) {
144 store_switch (ct
, info
);
149 if (ct
->c_ceclosefnx
)
150 (*ct
->c_ceclosefnx
) (ct
);
156 * Switching routine to store different content types
160 store_switch (CT ct
, mhstoreinfo_t info
)
162 switch (ct
->c_type
) {
164 return store_multi (ct
, info
);
167 switch (ct
->c_subtype
) {
168 case MESSAGE_PARTIAL
:
169 return store_partial (ct
, info
);
171 case MESSAGE_EXTERNAL
:
172 return store_external (ct
, info
);
176 return store_generic (ct
, info
);
181 return store_application (ct
, info
);
187 return store_generic (ct
, info
);
190 return OK
; /* NOT REACHED */
195 * Generic routine to store a MIME content.
196 * (audio, video, image, text, message/rfc822)
200 store_generic (CT ct
, mhstoreinfo_t info
)
203 * Check if the content specifies a filename.
204 * Don't bother with this for type "message"
205 * (only "message/rfc822" will use store_generic).
207 if (info
->autosw
&& ct
->c_type
!= CT_MESSAGE
)
210 return store_content (ct
, NULL
, info
);
215 * Store content of type "application"
219 store_application (CT ct
, mhstoreinfo_t info
)
221 CI ci
= &ct
->c_ctinfo
;
223 /* Check if the content specifies a filename */
228 * If storeproc is not defined, and the content is type
229 * "application/octet-stream", we also check for various
230 * attribute/value pairs which specify if this a tar file.
232 if (!ct
->c_storeproc
&& ct
->c_subtype
== APPLICATION_OCTETS
) {
233 int tarP
= 0, zP
= 0, gzP
= 0;
236 if ((cp
= get_param(ci
->ci_first_pm
, "type", ' ', 1))) {
237 if (strcasecmp (cp
, "tar") == 0)
241 /* check for "conversions=compress" attribute */
242 if ((cp
= get_param(ci
->ci_first_pm
, "conversions", ' ', 1)) ||
243 (cp
= get_param(ci
->ci_first_pm
, "x-conversions", ' ', 1))) {
244 if (strcasecmp (cp
, "compress") == 0 ||
245 strcasecmp (cp
, "x-compress") == 0) {
248 if (strcasecmp (cp
, "gzip") == 0 ||
249 strcasecmp (cp
, "x-gzip") == 0) {
255 ct
->c_showproc
= add (zP
? "%euncompress | tar tvf -"
256 : (gzP
? "%egzip -dc | tar tvf -"
257 : "%etar tvf -"), NULL
);
258 if (!ct
->c_storeproc
) {
260 ct
->c_storeproc
= add (zP
? "| uncompress | tar xvpf -"
261 : (gzP
? "| gzip -dc | tar xvpf -"
262 : "| tar xvpf -"), NULL
);
265 ct
->c_storeproc
= add (zP
? "%m%P.tar.Z"
266 : (gzP
? "%m%P.tar.gz"
267 : "%m%P.tar"), NULL
);
273 return store_content (ct
, NULL
, info
);
278 * Store the content of a multipart message
282 store_multi (CT ct
, mhstoreinfo_t info
)
285 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
289 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
290 CT p
= part
->mp_part
;
292 if (part_ok (p
) && type_ok (p
, 1)) {
294 /* Support mhstore -outfile. The MIME parser doesn't
295 load c_storage, so we know that p->c_storage is
297 p
->c_storage
= mh_xstrdup(ct
->c_storage
);
299 result
= store_switch (p
, info
);
301 if (result
== OK
&& ct
->c_subtype
== MULTI_ALTERNATE
)
311 * Reassemble and store the contents of a collection
312 * of messages of type "message/partial".
316 store_partial (CT ct
, mhstoreinfo_t info
)
321 struct partial
*pm
, *qm
;
323 qm
= (struct partial
*) ct
->c_ctparams
;
328 for (ctp
= info
->cts
; *ctp
; ctp
++) {
330 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
331 pm
= (struct partial
*) p
->c_ctparams
;
333 && strcmp (qm
->pm_partid
, pm
->pm_partid
) == 0) {
334 pm
->pm_marked
= pm
->pm_partno
;
346 inform("missing (at least) last part of multipart message");
350 base
= mh_xcalloc(i
+ 1, sizeof *base
);
352 for (ctp
= info
->cts
; *ctp
; ctp
++) {
354 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
355 pm
= (struct partial
*) p
->c_ctparams
;
363 qsort ((char *) base
, i
, sizeof(*base
), (qsort_comp
) ct_compar
);
366 for (ctq
= base
; *ctq
; ctq
++) {
368 pm
= (struct partial
*) p
->c_ctparams
;
369 if (pm
->pm_marked
!= cur
) {
370 if (pm
->pm_marked
== cur
- 1) {
371 inform("duplicate part %d of %d part multipart message, continuing...",
377 inform("missing %spart %d of %d part multipart message",
378 cur
!= hi
? "(at least) " : "", cur
, hi
);
390 * Now cycle through the sorted list of messages of type
391 * "message/partial" and save/append them to a file.
396 if (store_content (ct
, NULL
, info
) == NOTOK
) {
402 for (; *ctq
; ctq
++) {
404 if (store_content (p
, ct
, info
) == NOTOK
)
414 * Store content from a message of type "message/external".
418 store_external (CT ct
, mhstoreinfo_t info
)
421 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
422 CT p
= e
->eb_content
;
428 * Check if the parameters for the external body
429 * specified a filename.
434 if ((cp
= e
->eb_name
) && use_param_as_filename(cp
)) {
435 if (!ct
->c_storeproc
)
436 ct
->c_storeproc
= mh_xstrdup(cp
);
438 p
->c_storeproc
= mh_xstrdup(cp
);
443 * Since we will let the Content structure for the
444 * external body substitute for the current content,
445 * we temporarily change its partno (number inside
446 * multipart), so everything looks right.
448 p
->c_partno
= ct
->c_partno
;
450 /* we probably need to check if content is really there */
452 /* Support mhstore -outfile. The MIME parser doesn't load
453 c_storage, so we know that p->c_storage is NULL here. */
454 p
->c_storage
= mh_xstrdup(ct
->c_storage
);
456 result
= store_switch (p
, info
);
464 * Compare the numbering from two different
465 * message/partials (needed for sorting).
469 ct_compar (CT
*a
, CT
*b
)
471 struct partial
*am
= (struct partial
*) ((*a
)->c_ctparams
);
472 struct partial
*bm
= (struct partial
*) ((*b
)->c_ctparams
);
474 return (am
->pm_marked
- bm
->pm_marked
);
479 * Store contents of a message or message part to
480 * a folder, a file, the standard output, or pass
481 * the contents to a command.
483 * If the current content to be saved is a followup part
484 * to a collection of messages of type "message/partial",
485 * then field "p" is a pointer to the Content structure
486 * to the first message/partial in the group.
490 store_content (CT ct
, CT p
, mhstoreinfo_t info
)
492 int appending
= 0, msgnum
= 0;
493 int is_partial
= 0, first_partial
= 0;
494 int last_partial
= 0;
495 char *cp
, buffer
[BUFSIZ
];
498 * Do special processing for messages of
499 * type "message/partial".
501 * We first check if this content is of type
502 * "message/partial". If it is, then we need to check
503 * whether it is the first and/or last in the group.
505 * Then if "p" is a valid pointer, it points to the Content
506 * structure of the first partial in the group. So we copy
507 * the file name and/or folder name from that message. In
508 * this case, we also note that we will be appending.
510 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
511 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
513 /* Yep, it's a message/partial */
516 /* But is it the first and/or last in the collection? */
517 if (pm
->pm_partno
== 1)
519 if (pm
->pm_maxno
&& pm
->pm_partno
== pm
->pm_maxno
)
523 * If "p" is a valid pointer, then it points to the
524 * Content structure for the first message in the group.
525 * So we just copy the filename or foldername information
526 * from the previous iteration of this function.
530 if (! ct
->c_storage
) {
531 ct
->c_storage
= add (p
->c_storage
, NULL
);
533 /* record the folder name */
535 ct
->c_folder
= mh_xstrdup(p
->c_folder
);
543 * Get storage formatting string.
545 * 1) If we have storeproc defined, then use that
546 * 2) Else check for a mhn-store-<type>/<subtype> entry
547 * 3) Else check for a mhn-store-<type> entry
548 * 4) Else if content is "message", use "+" (current folder)
549 * 5) Else use string "%m%P.%s".
551 if ((cp
= ct
->c_storeproc
) == NULL
|| *cp
== '\0') {
552 CI ci
= &ct
->c_ctinfo
;
554 cp
= context_find_by_type ("store", ci
->ci_type
, ci
->ci_subtype
);
556 cp
= ct
->c_type
== CT_MESSAGE
? "+" : "%m%P.%s";
560 if (! ct
->c_storage
) {
562 * Check the beginning of storage formatting string
563 * to see if we are saving content to a folder.
565 if (*cp
== '+' || *cp
== '@') {
566 char *tmpfilenam
, *folder
;
568 /* Store content in temporary file for now */
569 if ((tmpfilenam
= m_mktemp(invo_name
, NULL
, NULL
)) == NULL
) {
570 adios(NULL
, "unable to create temporary file in %s",
573 ct
->c_storage
= mh_xstrdup(tmpfilenam
);
575 /* Get the folder name */
577 folder
= pluspath (cp
);
579 folder
= getfolder (1);
581 /* Check if folder exists */
582 create_folder(m_mailpath(folder
), 0, exit
);
584 /* Record the folder name */
585 ct
->c_folder
= mh_xstrdup(folder
);
594 * Parse and expand the storage formatting string
595 * in `cp' into `buffer'.
597 parse_format_string (ct
, cp
, buffer
, sizeof(buffer
), info
->dir
);
600 * If formatting begins with '|' or '!', then pass
601 * content to standard input of a command and return.
603 if (buffer
[0] == '|' || buffer
[0] == '!')
604 return show_content_aux (ct
, 0, buffer
+ 1, info
->dir
, NULL
);
606 /* record the filename */
607 if ((ct
->c_storage
= clobber_check (mh_xstrdup(buffer
), info
)) ==
612 /* The output filename was explicitly specified, so use it. */
613 if ((ct
->c_storage
= clobber_check (ct
->c_storage
, info
)) ==
620 /* flush the output stream */
623 /* Now save or append the content to a file */
624 if (output_content_file (ct
, appending
) == NOTOK
)
628 * If necessary, link the file into a folder and remove
629 * the temporary file. If this message is a partial,
630 * then only do this if it is the last one in the group.
632 if (ct
->c_folder
&& (!is_partial
|| last_partial
)) {
633 msgnum
= output_content_folder (ct
->c_folder
, ct
->c_storage
);
634 (void) m_unlink (ct
->c_storage
);
641 * Now print out the name/number of the message
642 * that we are storing.
646 fprintf (stderr
, "reassembling partials ");
648 fputs(ct
->c_file
, stderr
);
650 fprintf (stderr
, "%s,", ct
->c_file
);
652 fprintf (stderr
, "storing message %s", ct
->c_file
);
654 fprintf (stderr
, " part %s", ct
->c_partno
);
658 * Unless we are in the "middle" of group of message/partials,
659 * we now print the name of the file, folder, and/or message
660 * to which we are storing the content.
662 if (!is_partial
|| last_partial
) {
664 fprintf (stderr
, " to folder %s as message %d\n", ct
->c_folder
,
666 } else if (!strcmp(ct
->c_storage
, "-")) {
667 fprintf (stderr
, " to stdout\n");
669 int cwdlen
= strlen (info
->cwd
);
671 fprintf (stderr
, " as file %s\n",
672 !has_prefix(ct
->c_storage
, info
->cwd
)
673 || ct
->c_storage
[cwdlen
] != '/'
674 ? ct
->c_storage
: ct
->c_storage
+ cwdlen
+ 1);
684 * Output content to a file
688 output_content_file (CT ct
, int appending
)
691 char *file
, buffer
[BUFSIZ
];
696 * If the pathname is absolute, make sure
697 * all the relevant directories exist.
699 if (strchr(ct
->c_storage
, '/')
700 && make_intermediates (ct
->c_storage
) == NOTOK
)
703 if (ct
->c_encoding
!= CE_7BIT
) {
706 if (!ct
->c_ceopenfnx
) {
707 inform("don't know how to decode part %s of message %s",
708 ct
->c_partno
, ct
->c_file
);
712 file
= appending
|| !strcmp (ct
->c_storage
, "-") ? NULL
714 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
716 if (!strcmp (file
, ct
->c_storage
)) {
717 (*ct
->c_ceclosefnx
) (ct
);
722 * Send to standard output
724 if (!strcmp (ct
->c_storage
, "-")) {
727 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
728 advise ("stdout", "unable to dup");
730 (*ct
->c_ceclosefnx
) (ct
);
733 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
734 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
735 appending
? "a" : "w");
743 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
744 advise (ct
->c_storage
, "unable to fopen for %s",
745 appending
? "appending" : "writing");
751 * Filter the header fields of the initial enclosing
752 * message/partial into the file.
754 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
755 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
757 if (pm
->pm_partno
== 1)
758 copy_some_headers (fp
, ct
);
762 switch (cc
= read (fd
, buffer
, sizeof(buffer
))) {
764 advise (file
, "error reading content from");
771 if ((int) fwrite (buffer
, sizeof(*buffer
), cc
, fp
) < cc
) {
772 advise ("output_content_file", "fwrite");
779 (*ct
->c_ceclosefnx
) (ct
);
781 if (cc
!= NOTOK
&& fflush (fp
))
782 advise (ct
->c_storage
, "error writing to");
786 return (cc
!= NOTOK
? OK
: NOTOK
);
789 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
790 advise (ct
->c_file
, "unable to open for reading");
796 fseek (ct
->c_fp
, pos
, SEEK_SET
);
798 if (!strcmp (ct
->c_storage
, "-")) {
801 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
802 advise ("stdout", "unable to dup");
805 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
806 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
807 appending
? "a" : "w");
812 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
813 advise (ct
->c_storage
, "unable to fopen for %s",
814 appending
? "appending" : "writing");
820 * Copy a few of the header fields of the initial
821 * enclosing message/partial into the file.
824 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
825 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
827 if (pm
->pm_partno
== 1) {
828 copy_some_headers (fp
, ct
);
833 while (fgets (buffer
, sizeof buffer
, ct
->c_fp
)) {
834 if ((pos
+= strlen (buffer
)) > last
) {
837 diff
= strlen (buffer
) - (pos
- last
);
842 * If this is the first content of a group of
843 * message/partial contents, then we only copy a few
844 * of the header fields of the enclosed message.
859 if (!uprf (buffer
, XXX_FIELD_PRF
)
860 && !uprf (buffer
, VRSN_FIELD
)
861 && !uprf (buffer
, "Subject:")
862 && !uprf (buffer
, "Encrypted:")
863 && !uprf (buffer
, "Message-ID:")) {
878 advise (ct
->c_storage
, "error writing to");
888 * Add a file to a folder.
890 * Return the new message number of the file
891 * when added to the folder. Return -1, if
896 output_content_folder (char *folder
, char *filename
)
901 /* Read the folder. */
902 if ((mp
= folder_read (folder
, 0))) {
903 /* Link file into folder */
904 msgnum
= folder_addmsg (&mp
, filename
, 0, 0, 0, 0, NULL
);
906 inform("unable to read folder %s", folder
);
910 /* free folder structure */
914 * Return msgnum. We are relying on the fact that
915 * msgnum will be -1, if folder_addmsg() had an error.
922 * Parse and expand the storage formatting string
923 * pointed to by "cp" into "buffer".
927 parse_format_string (CT ct
, char *cp
, char *buffer
, int buflen
, char *dir
)
931 CI ci
= &ct
->c_ctinfo
;
934 * If storage string is "-", just copy it, and
935 * return (send content to standard output).
937 if (cp
[0] == '-' && cp
[1] == '\0') {
938 strncpy (buffer
, cp
, buflen
);
946 * If formatting string is a pathname that doesn't
947 * begin with '/', then preface the path with the
948 * appropriate directory.
950 if (*cp
!= '/' && *cp
!= '|' && *cp
!= '!') {
951 snprintf (bp
, buflen
, "%s/", dir
[1] ? dir
: "");
959 /* We are processing a storage escape */
964 * Insert parameters from Content-Type.
965 * This is only valid for '|' commands.
967 if (buffer
[0] != '|' && buffer
[0] != '!') {
976 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
977 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
,
978 pm
->pm_name
, get_param_value(pm
, '?'));
988 /* insert message number */
989 snprintf (bp
, buflen
, "%s", r1bindex (ct
->c_file
, '/'));
993 /* insert part number with leading dot */
995 snprintf (bp
, buflen
, ".%s", ct
->c_partno
);
999 /* insert part number withouth leading dot */
1001 strncpy (bp
, ct
->c_partno
, buflen
);
1005 /* insert content type */
1006 strncpy (bp
, ci
->ci_type
, buflen
);
1010 /* insert content subtype */
1011 strncpy (bp
, ci
->ci_subtype
, buflen
);
1015 /* insert the character % */
1025 /* Advance bp and decrement buflen */
1043 * Check if the content specifies a filename
1044 * in its MIME parameters.
1048 get_storeproc (CT ct
)
1054 * If the storeproc has already been defined,
1055 * we just return (for instance, if this content
1056 * is part of a "message/external".
1058 if (ct
->c_storeproc
)
1062 * If there's a Content-Disposition header and it has a filename,
1063 * use that (RFC-2183).
1066 if ((cp
= get_param(ct
->c_dispo_first
, "filename", '_', 0)) &&
1067 use_param_as_filename(cp
)) {
1068 ct
->c_storeproc
= mh_xstrdup(cp
);
1076 * Check the attribute/value pairs, for the attribute "name".
1077 * If found, do a few sanity checks and copy the value into
1081 if ((cp
= get_param(ci
->ci_first_pm
, "name", '_', 0)) &&
1082 use_param_as_filename(cp
)) {
1083 ct
->c_storeproc
= mh_xstrdup(cp
);
1091 * Copy some of the header fields of the initial message/partial
1092 * message into the header of the reassembled message.
1096 copy_some_headers (FILE *out
, CT ct
)
1100 hp
= ct
->c_first_hf
; /* start at first header field */
1104 * A few of the header fields of the enclosing
1105 * messages are not copied.
1107 if (!uprf (hp
->name
, XXX_FIELD_PRF
)
1108 && strcasecmp (hp
->name
, VRSN_FIELD
)
1109 && strcasecmp (hp
->name
, "Subject")
1110 && strcasecmp (hp
->name
, "Encrypted")
1111 && strcasecmp (hp
->name
, "Message-ID"))
1112 fprintf (out
, "%s:%s", hp
->name
, hp
->value
);
1113 hp
= hp
->next
; /* next header field */
1119 /******************************************************************************/
1120 /* -clobber support */
1123 enum clobber_policy_t
1124 clobber_policy (const char *value
) {
1125 if (value
== NULL
|| ! strcasecmp (value
, "always")) {
1126 return NMH_CLOBBER_ALWAYS
;
1128 if (! strcasecmp (value
, "auto")) {
1129 return NMH_CLOBBER_AUTO
;
1131 if (! strcasecmp (value
, "suffix")) {
1132 return NMH_CLOBBER_SUFFIX
;
1134 if (! strcasecmp (value
, "ask")) {
1135 return NMH_CLOBBER_ASK
;
1137 if (! strcasecmp (value
, "never")) {
1138 return NMH_CLOBBER_NEVER
;
1141 adios (NULL
, "invalid argument, %s, to clobber", value
);
1146 next_version (char *file
, enum clobber_policy_t clobber_policy
) {
1147 const size_t max_versions
= 1000000;
1148 /* 8 = log max_versions + one for - or . + one for null terminator */
1149 const size_t buflen
= strlen (file
) + 8;
1150 char *buffer
= mh_xmalloc (buflen
);
1153 char *extension
= NULL
;
1154 if (clobber_policy
== NMH_CLOBBER_AUTO
&&
1155 ((extension
= strrchr (file
, '.')) != NULL
)) {
1156 *extension
++ = '\0';
1159 for (version
= 1; version
< max_versions
; ++version
) {
1162 switch (clobber_policy
) {
1163 case NMH_CLOBBER_AUTO
: {
1164 snprintf (buffer
, buflen
, "%s-%ld%s%s", file
, (long) version
,
1165 extension
== NULL
? "" : ".",
1166 extension
== NULL
? "" : extension
);
1170 case NMH_CLOBBER_SUFFIX
:
1171 snprintf (buffer
, buflen
, "%s.%ld", file
, (long) version
);
1175 /* Should never get here. */
1176 inform("will not overwrite %s, invalid clobber policy", buffer
);
1181 /* Actually (try to) create the file here to avoid a race
1182 condition on file naming + creation. This won't solve the
1183 problem with old NFS that doesn't support O_EXCL, though.
1184 Let the umask strip off permissions from 0666 as desired.
1185 That's what fopen () would do if it was creating the file. */
1186 if ((fd
= open (buffer
, O_CREAT
| O_EXCL
,
1187 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
|
1188 S_IROTH
| S_IWOTH
)) >= 0) {
1196 if (version
>= max_versions
) {
1197 inform("will not overwrite %s, too many versions", buffer
);
1207 clobber_check (char *original_file
, mhstoreinfo_t info
) {
1208 /* clobber policy return value
1209 * -------------- ------------
1210 * -always original_file
1211 * -auto original_file-<digits>.extension
1212 * -suffix original_file.<digits>
1213 * -ask original_file, 0, or another filename/path
1221 if (! strcmp (original_file
, "-")) {
1222 return original_file
;
1225 if (info
->clobber_policy
== NMH_CLOBBER_ASK
) {
1226 /* Save cwd for possible use in loop below. */
1229 cwd
= mh_xstrdup(original_file
);
1230 slash
= strrchr (cwd
, '/');
1235 /* original_file isn't a full path, which should only happen if
1245 file
= original_file
;
1248 switch (info
->clobber_policy
) {
1249 case NMH_CLOBBER_ALWAYS
:
1252 case NMH_CLOBBER_SUFFIX
:
1253 case NMH_CLOBBER_AUTO
:
1254 if (stat (file
, &st
) == OK
) {
1255 if ((file
= next_version (original_file
, info
->clobber_policy
)) ==
1257 ++info
->files_not_clobbered
;
1262 case NMH_CLOBBER_ASK
:
1263 if (stat (file
, &st
) == OK
) {
1264 enum answers
{ NMH_YES
, NMH_NO
, NMH_RENAME
};
1265 static struct swit answer
[4] = {
1266 { "yes", 0, NMH_YES
},
1267 { "no", 0, NMH_NO
},
1268 { "rename", 0, NMH_RENAME
},
1272 if (isatty (fileno (stdin
))) {
1274 concat ("Overwrite \"", file
, "\" [y/n/rename]? ", NULL
);
1275 ans
= read_switch_multiword (prompt
, answer
);
1278 /* Overwrite, that's what nmh used to do. And warn. */
1279 inform("-clobber ask but no tty, so overwrite %s", file
);
1283 switch ((enum answers
) smatch (*ans
, answer
)) {
1289 ++info
->files_not_clobbered
;
1293 printf ("Enter filename or full path of the new file: ");
1294 if (fgets (buf
, sizeof buf
, stdin
) == NULL
||
1297 ++info
->files_not_clobbered
;
1299 trim_suffix_c(buf
, '\n');
1304 if (buf
[0] == '/') {
1305 /* Full path, use it. */
1306 file
= mh_xstrdup(buf
);
1308 /* Relative path. */
1309 file
= cwd
? concat (cwd
, "/", buf
, NULL
) : mh_xstrdup(buf
);
1319 case NMH_CLOBBER_NEVER
:
1320 if (stat (file
, &st
) == OK
) {
1321 /* Keep count of files that would have been clobbered,
1322 and return that as process exit status. */
1323 inform("will not overwrite %s with -clobber never", file
);
1326 ++info
->files_not_clobbered
;
1331 original_file
= file
;
1332 } while (check_again
);
1339 static bool use_param_as_filename(const char *p
)
1341 /* Preserve result of original test that considered an empty string
1343 return !*p
|| (!strchr("/.|!", *p
) && !strchr(p
, '%'));
1346 /* -clobber support */
1347 /******************************************************************************/