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.
12 #include <h/signals.h>
19 #include <h/mhparse.h>
24 * The list of top-level contents to display
31 * Cache of current directory. This must be
32 * set before these routines are called.
37 * The directory in which to store the contents.
42 * Type for a compare function for qsort. This keeps
45 typedef int (*qsort_comp
) (const void *, const void *);
49 int part_ok (CT
, int);
50 int type_ok (CT
, int);
51 void flush_errors (void);
54 int show_content_aux (CT
, int, int, char *, char *);
59 void store_all_messages (CT
*);
64 static void store_single_message (CT
);
65 static int store_switch (CT
);
66 static int store_generic (CT
);
67 static int store_application (CT
);
68 static int store_multi (CT
);
69 static int store_partial (CT
);
70 static int store_external (CT
);
71 static int ct_compar (CT
*, CT
*);
72 static int store_content (CT
, CT
);
73 static int output_content_file (CT
, int);
74 static int output_content_folder (char *, char *);
75 static int parse_format_string (CT
, char *, char *, int, char *);
76 static void get_storeproc (CT
);
77 static int copy_some_headers (FILE *, CT
);
78 static char *clobber_check (char *);
81 * Main entry point to store content
82 * from a collection of messages.
86 store_all_messages (CT
*cts
)
92 * Check for the directory in which to
95 if ((cp
= context_find (nmhstorage
)) && *cp
)
100 for (ctp
= cts
; *ctp
; ctp
++) {
102 store_single_message (ct
);
110 * Entry point to store the content
111 * in a (single) message
115 store_single_message (CT ct
)
117 if (type_ok (ct
, 1)) {
124 if (ct
->c_ceclosefnx
)
125 (*ct
->c_ceclosefnx
) (ct
);
131 * Switching routine to store different content types
137 switch (ct
->c_type
) {
139 return store_multi (ct
);
143 switch (ct
->c_subtype
) {
144 case MESSAGE_PARTIAL
:
145 return store_partial (ct
);
148 case MESSAGE_EXTERNAL
:
149 return store_external (ct
);
153 return store_generic (ct
);
159 return store_application (ct
);
166 return store_generic (ct
);
170 adios (NULL
, "unknown content type %d", ct
->c_type
);
174 return OK
; /* NOT REACHED */
179 * Generic routine to store a MIME content.
180 * (audio, video, image, text, message/rfc822)
184 store_generic (CT ct
)
187 * Check if the content specifies a filename.
188 * Don't bother with this for type "message"
189 * (only "message/rfc822" will use store_generic).
191 if (autosw
&& ct
->c_type
!= CT_MESSAGE
)
194 return store_content (ct
, NULL
);
199 * Store content of type "application"
203 store_application (CT ct
)
206 CI ci
= &ct
->c_ctinfo
;
208 /* Check if the content specifies a filename */
213 * If storeproc is not defined, and the content is type
214 * "application/octet-stream", we also check for various
215 * attribute/value pairs which specify if this a tar file.
217 if (!ct
->c_storeproc
&& ct
->c_subtype
== APPLICATION_OCTETS
) {
218 int tarP
= 0, zP
= 0, gzP
= 0;
220 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
221 /* check for "type=tar" attribute */
222 if (!mh_strcasecmp (*ap
, "type")) {
223 if (mh_strcasecmp (*ep
, "tar"))
230 /* check for "conversions=compress" attribute */
231 if ((!mh_strcasecmp (*ap
, "conversions") || !mh_strcasecmp (*ap
, "x-conversions"))
232 && (!mh_strcasecmp (*ep
, "compress") || !mh_strcasecmp (*ep
, "x-compress"))) {
236 /* check for "conversions=gzip" attribute */
237 if ((!mh_strcasecmp (*ap
, "conversions") || !mh_strcasecmp (*ap
, "x-conversions"))
238 && (!mh_strcasecmp (*ep
, "gzip") || !mh_strcasecmp (*ep
, "x-gzip"))) {
245 ct
->c_showproc
= add (zP
? "%euncompress | tar tvf -"
246 : (gzP
? "%egzip -dc | tar tvf -"
247 : "%etar tvf -"), NULL
);
248 if (!ct
->c_storeproc
) {
250 ct
->c_storeproc
= add (zP
? "| uncompress | tar xvpf -"
251 : (gzP
? "| gzip -dc | tar xvpf -"
252 : "| tar xvpf -"), NULL
);
255 ct
->c_storeproc
= add (zP
? "%m%P.tar.Z"
256 : (gzP
? "%m%P.tar.gz"
257 : "%m%P.tar"), NULL
);
263 return store_content (ct
, NULL
);
268 * Store the content of a multipart message
275 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
279 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
280 CT p
= part
->mp_part
;
282 if (part_ok (p
, 1) && type_ok (p
, 1)) {
283 result
= store_switch (p
);
284 if (result
== OK
&& ct
->c_subtype
== MULTI_ALTERNATE
)
294 * Reassemble and store the contents of a collection
295 * of messages of type "message/partial".
299 store_partial (CT ct
)
304 struct partial
*pm
, *qm
;
306 qm
= (struct partial
*) ct
->c_ctparams
;
311 for (ctp
= cts
; *ctp
; ctp
++) {
313 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
314 pm
= (struct partial
*) p
->c_ctparams
;
316 && strcmp (qm
->pm_partid
, pm
->pm_partid
) == 0) {
317 pm
->pm_marked
= pm
->pm_partno
;
329 advise (NULL
, "missing (at least) last part of multipart message");
333 if ((base
= (CT
*) calloc ((size_t) (i
+ 1), sizeof(*base
))) == NULL
)
334 adios (NULL
, "out of memory");
337 for (ctp
= cts
; *ctp
; ctp
++) {
339 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
340 pm
= (struct partial
*) p
->c_ctparams
;
348 qsort ((char *) base
, i
, sizeof(*base
), (qsort_comp
) ct_compar
);
351 for (ctq
= base
; *ctq
; ctq
++) {
353 pm
= (struct partial
*) p
->c_ctparams
;
354 if (pm
->pm_marked
!= cur
) {
355 if (pm
->pm_marked
== cur
- 1) {
357 "duplicate part %d of %d part multipart message",
364 "missing %spart %d of %d part multipart message",
365 cur
!= hi
? "(at least) " : "", cur
, hi
);
377 * Now cycle through the sorted list of messages of type
378 * "message/partial" and save/append them to a file.
383 if (store_content (ct
, NULL
) == NOTOK
) {
385 free ((char *) base
);
389 for (; *ctq
; ctq
++) {
391 if (store_content (p
, ct
) == NOTOK
)
395 free ((char *) base
);
401 * Store content from a message of type "message/external".
405 store_external (CT ct
)
408 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
409 CT p
= e
->eb_content
;
415 * Check if the parameters for the external body
416 * specified a filename.
421 if ((cp
= e
->eb_name
)
426 && !strchr (cp
, '%')) {
427 if (!ct
->c_storeproc
)
428 ct
->c_storeproc
= add (cp
, NULL
);
430 p
->c_storeproc
= add (cp
, NULL
);
435 * Since we will let the Content structure for the
436 * external body substitute for the current content,
437 * we temporarily change its partno (number inside
438 * multipart), so everything looks right.
440 p
->c_partno
= ct
->c_partno
;
442 /* we probably need to check if content is really there */
443 result
= store_switch (p
);
451 * Compare the numbering from two different
452 * message/partials (needed for sorting).
456 ct_compar (CT
*a
, CT
*b
)
458 struct partial
*am
= (struct partial
*) ((*a
)->c_ctparams
);
459 struct partial
*bm
= (struct partial
*) ((*b
)->c_ctparams
);
461 return (am
->pm_marked
- bm
->pm_marked
);
466 * Store contents of a message or message part to
467 * a folder, a file, the standard output, or pass
468 * the contents to a command.
470 * If the current content to be saved is a followup part
471 * to a collection of messages of type "message/partial",
472 * then field "p" is a pointer to the Content structure
473 * to the first message/partial in the group.
477 store_content (CT ct
, CT p
)
479 int appending
= 0, msgnum
= 0;
480 int is_partial
= 0, first_partial
= 0;
481 int last_partial
= 0;
482 char *cp
, buffer
[BUFSIZ
];
485 * Do special processing for messages of
486 * type "message/partial".
488 * We first check if this content is of type
489 * "message/partial". If it is, then we need to check
490 * whether it is the first and/or last in the group.
492 * Then if "p" is a valid pointer, it points to the Content
493 * structure of the first partial in the group. So we copy
494 * the file name and/or folder name from that message. In
495 * this case, we also note that we will be appending.
497 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
498 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
500 /* Yep, it's a message/partial */
503 /* But is it the first and/or last in the collection? */
504 if (pm
->pm_partno
== 1)
506 if (pm
->pm_maxno
&& pm
->pm_partno
== pm
->pm_maxno
)
510 * If "p" is a valid pointer, then it points to the
511 * Content structure for the first message in the group.
512 * So we just copy the filename or foldername information
513 * from the previous iteration of this function.
517 ct
->c_storage
= add (p
->c_storage
, NULL
);
519 /* record the folder name */
521 ct
->c_folder
= add (p
->c_folder
, NULL
);
528 * Get storage formatting string.
530 * 1) If we have storeproc defined, then use that
531 * 2) Else check for a mhn-store-<type>/<subtype> entry
532 * 3) Else check for a mhn-store-<type> entry
533 * 4) Else if content is "message", use "+" (current folder)
534 * 5) Else use string "%m%P.%s".
536 if ((cp
= ct
->c_storeproc
) == NULL
|| *cp
== '\0') {
537 CI ci
= &ct
->c_ctinfo
;
539 snprintf (buffer
, sizeof(buffer
), "%s-store-%s/%s",
540 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
541 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
542 snprintf (buffer
, sizeof(buffer
), "%s-store-%s", invo_name
, ci
->ci_type
);
543 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
544 cp
= ct
->c_type
== CT_MESSAGE
? "+" : "%m%P.%s";
550 * Check the beginning of storage formatting string
551 * to see if we are saving content to a folder.
553 if (*cp
== '+' || *cp
== '@') {
554 char *tmpfilenam
, *folder
;
556 /* Store content in temporary file for now */
557 tmpfilenam
= m_mktemp(invo_name
, NULL
, NULL
);
558 ct
->c_storage
= add (tmpfilenam
, NULL
);
560 /* Get the folder name */
562 folder
= pluspath (cp
);
564 folder
= getfolder (1);
566 /* Check if folder exists */
567 create_folder(m_mailpath(folder
), 0, exit
);
569 /* Record the folder name */
570 ct
->c_folder
= add (folder
, NULL
);
579 * Parse and expand the storage formatting string
580 * in `cp' into `buffer'.
582 parse_format_string (ct
, cp
, buffer
, sizeof(buffer
), dir
);
585 * If formatting begins with '|' or '!', then pass
586 * content to standard input of a command and return.
588 if (buffer
[0] == '|' || buffer
[0] == '!')
589 return show_content_aux (ct
, 1, 0, buffer
+ 1, dir
);
591 /* record the filename */
592 if ((ct
->c_storage
= clobber_check (add (buffer
, NULL
))) == NULL
) {
597 /* flush the output stream */
600 /* Now save or append the content to a file */
601 if (output_content_file (ct
, appending
) == NOTOK
)
605 * If necessary, link the file into a folder and remove
606 * the temporary file. If this message is a partial,
607 * then only do this if it is the last one in the group.
609 if (ct
->c_folder
&& (!is_partial
|| last_partial
)) {
610 msgnum
= output_content_folder (ct
->c_folder
, ct
->c_storage
);
611 unlink (ct
->c_storage
);
617 * Now print out the name/number of the message
618 * that we are storing.
622 fprintf (stderr
, "reassembling partials ");
624 fprintf (stderr
, "%s", ct
->c_file
);
626 fprintf (stderr
, "%s,", ct
->c_file
);
628 fprintf (stderr
, "storing message %s", ct
->c_file
);
630 fprintf (stderr
, " part %s", ct
->c_partno
);
634 * Unless we are in the "middle" of group of message/partials,
635 * we now print the name of the file, folder, and/or message
636 * to which we are storing the content.
638 if (!is_partial
|| last_partial
) {
640 fprintf (stderr
, " to folder %s as message %d\n", ct
->c_folder
, msgnum
);
641 } else if (!strcmp(ct
->c_storage
, "-")) {
642 fprintf (stderr
, " to stdout\n");
646 cwdlen
= strlen (cwd
);
647 fprintf (stderr
, " as file %s\n",
648 strncmp (ct
->c_storage
, cwd
, cwdlen
)
649 || ct
->c_storage
[cwdlen
] != '/'
650 ? ct
->c_storage
: ct
->c_storage
+ cwdlen
+ 1);
659 * Output content to a file
663 output_content_file (CT ct
, int appending
)
666 char *file
, buffer
[BUFSIZ
];
671 * If the pathname is absolute, make sure
672 * all the relevant directories exist.
674 if (strchr(ct
->c_storage
, '/')
675 && make_intermediates (ct
->c_storage
) == NOTOK
)
678 if (ct
->c_encoding
!= CE_7BIT
) {
681 if (!ct
->c_ceopenfnx
) {
682 advise (NULL
, "don't know how to decode part %s of message %s",
683 ct
->c_partno
, ct
->c_file
);
687 file
= appending
|| !strcmp (ct
->c_storage
, "-") ? NULL
689 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
691 if (!strcmp (file
, ct
->c_storage
)) {
692 (*ct
->c_ceclosefnx
) (ct
);
697 * Send to standard output
699 if (!strcmp (ct
->c_storage
, "-")) {
702 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
703 advise ("stdout", "unable to dup");
705 (*ct
->c_ceclosefnx
) (ct
);
708 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
709 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
710 appending
? "a" : "w");
718 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
719 advise (ct
->c_storage
, "unable to fopen for %s",
720 appending
? "appending" : "writing");
726 * Filter the header fields of the initial enclosing
727 * message/partial into the file.
729 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
730 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
732 if (pm
->pm_partno
== 1)
733 copy_some_headers (fp
, ct
);
737 switch (cc
= read (fd
, buffer
, sizeof(buffer
))) {
739 advise (file
, "error reading content from");
746 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
752 (*ct
->c_ceclosefnx
) (ct
);
754 if (cc
!= NOTOK
&& fflush (fp
))
755 advise (ct
->c_storage
, "error writing to");
759 return (cc
!= NOTOK
? OK
: NOTOK
);
762 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
763 advise (ct
->c_file
, "unable to open for reading");
769 fseek (ct
->c_fp
, pos
, SEEK_SET
);
771 if (!strcmp (ct
->c_storage
, "-")) {
774 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
775 advise ("stdout", "unable to dup");
778 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
779 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
780 appending
? "a" : "w");
785 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
786 advise (ct
->c_storage
, "unable to fopen for %s",
787 appending
? "appending" : "writing");
793 * Copy a few of the header fields of the initial
794 * enclosing message/partial into the file.
797 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
798 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
800 if (pm
->pm_partno
== 1) {
801 copy_some_headers (fp
, ct
);
806 while (fgets (buffer
, sizeof(buffer
) - 1, ct
->c_fp
)) {
807 if ((pos
+= strlen (buffer
)) > last
) {
810 diff
= strlen (buffer
) - (pos
- last
);
815 * If this is the first content of a group of
816 * message/partial contents, then we only copy a few
817 * of the header fields of the enclosed message.
832 if (!uprf (buffer
, XXX_FIELD_PRF
)
833 && !uprf (buffer
, VRSN_FIELD
)
834 && !uprf (buffer
, "Subject:")
835 && !uprf (buffer
, "Encrypted:")
836 && !uprf (buffer
, "Message-ID:")) {
851 advise (ct
->c_storage
, "error writing to");
861 * Add a file to a folder.
863 * Return the new message number of the file
864 * when added to the folder. Return -1, if
869 output_content_folder (char *folder
, char *filename
)
874 /* Read the folder. */
875 if ((mp
= folder_read (folder
))) {
876 /* Link file into folder */
877 msgnum
= folder_addmsg (&mp
, filename
, 0, 0, 0, 0, (char *)0);
879 advise (NULL
, "unable to read folder %s", folder
);
883 /* free folder structure */
887 * Return msgnum. We are relying on the fact that
888 * msgnum will be -1, if folder_addmsg() had an error.
895 * Parse and expand the storage formatting string
896 * pointed to by "cp" into "buffer".
900 parse_format_string (CT ct
, char *cp
, char *buffer
, int buflen
, char *dir
)
904 CI ci
= &ct
->c_ctinfo
;
907 * If storage string is "-", just copy it, and
908 * return (send content to standard output).
910 if (cp
[0] == '-' && cp
[1] == '\0') {
911 strncpy (buffer
, cp
, buflen
);
919 * If formatting string is a pathname that doesn't
920 * begin with '/', then preface the path with the
921 * appropriate directory.
923 if (*cp
!= '/' && *cp
!= '|' && *cp
!= '!') {
924 snprintf (bp
, buflen
, "%s/", dir
[1] ? dir
: "");
932 /* We are processing a storage escape */
937 * Insert parameters from Content-Type.
938 * This is only valid for '|' commands.
940 if (buffer
[0] != '|' && buffer
[0] != '!') {
949 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
;
951 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
961 /* insert message number */
962 snprintf (bp
, buflen
, "%s", r1bindex (ct
->c_file
, '/'));
966 /* insert part number with leading dot */
968 snprintf (bp
, buflen
, ".%s", ct
->c_partno
);
972 /* insert part number withouth leading dot */
974 strncpy (bp
, ct
->c_partno
, buflen
);
978 /* insert content type */
979 strncpy (bp
, ci
->ci_type
, buflen
);
983 /* insert content subtype */
984 strncpy (bp
, ci
->ci_subtype
, buflen
);
988 /* insert the character % */
998 /* Advance bp and decrement buflen */
1016 * Check if the content specifies a filename
1017 * in its MIME parameters.
1021 get_storeproc (CT ct
)
1023 char **ap
, **ep
, *cp
;
1024 CI ci
= &ct
->c_ctinfo
;
1027 * If the storeproc has already been defined,
1028 * we just return (for instance, if this content
1029 * is part of a "message/external".
1031 if (ct
->c_storeproc
)
1035 * Check the attribute/value pairs, for the attribute "name".
1036 * If found, do a few sanity checks and copy the value into
1039 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1040 if (!mh_strcasecmp (*ap
, "name")
1041 && *(cp
= *ep
) != '/'
1045 && !strchr (cp
, '%')) {
1046 ct
->c_storeproc
= add (cp
, NULL
);
1054 * Copy some of the header fields of the initial message/partial
1055 * message into the header of the reassembled message.
1059 copy_some_headers (FILE *out
, CT ct
)
1063 hp
= ct
->c_first_hf
; /* start at first header field */
1067 * A few of the header fields of the enclosing
1068 * messages are not copied.
1070 if (!uprf (hp
->name
, XXX_FIELD_PRF
)
1071 && mh_strcasecmp (hp
->name
, VRSN_FIELD
)
1072 && mh_strcasecmp (hp
->name
, "Subject")
1073 && mh_strcasecmp (hp
->name
, "Encrypted")
1074 && mh_strcasecmp (hp
->name
, "Message-ID"))
1075 fprintf (out
, "%s:%s", hp
->name
, hp
->value
);
1076 hp
= hp
->next
; /* next header field */
1082 /******************************************************************************/
1083 /* -clobber support */
1085 enum clobber_policy_t
{
1093 static enum clobber_policy_t clobber_policy
= NMH_CLOBBER_ALWAYS
;
1095 int files_not_clobbered
= 0;
1098 save_clobber_policy (const char *value
) {
1099 if (! mh_strcasecmp (value
, "always")) {
1100 clobber_policy
= NMH_CLOBBER_ALWAYS
;
1101 } else if (! mh_strcasecmp (value
, "auto")) {
1102 clobber_policy
= NMH_CLOBBER_AUTO
;
1103 } else if (! mh_strcasecmp (value
, "suffix")) {
1104 clobber_policy
= NMH_CLOBBER_SUFFIX
;
1105 } else if (! mh_strcasecmp (value
, "ask")) {
1106 clobber_policy
= NMH_CLOBBER_ASK
;
1107 } else if (! mh_strcasecmp (value
, "never")) {
1108 clobber_policy
= NMH_CLOBBER_NEVER
;
1118 next_version (char *file
, enum clobber_policy_t clobber_policy
) {
1119 const size_t max_versions
= 1000000;
1120 /* 8 = log max_versions + one for - or . + one for null terminator */
1121 const size_t buflen
= strlen (file
) + 8;
1122 char *buffer
= mh_xmalloc (buflen
);
1125 char *extension
= NULL
;
1126 if (clobber_policy
== NMH_CLOBBER_AUTO
&&
1127 ((extension
= strrchr (file
, '.')) != NULL
)) {
1128 *extension
++ = '\0';
1131 for (version
= 1; version
< max_versions
; ++version
) {
1134 switch (clobber_policy
) {
1135 case NMH_CLOBBER_AUTO
: {
1136 snprintf (buffer
, buflen
, "%s-%ld%s%s", file
, (long) version
,
1137 extension
== NULL
? "" : ".",
1138 extension
== NULL
? "" : extension
);
1142 case NMH_CLOBBER_SUFFIX
:
1143 snprintf (buffer
, buflen
, "%s.%ld", file
, (long) version
);
1147 /* Should never get here. */
1148 advise (NULL
, "will not overwrite %s, invalid clobber policy", buffer
);
1150 ++files_not_clobbered
;
1154 /* Actually (try to) create the file here to avoid a race
1155 condition on file naming + creation. This won't solve the
1156 problem with old NFS that doesn't support O_EXCL, though.
1157 Let the umask strip off permissions from 0666 as desired.
1158 That's what fopen () would do if it was creating the file. */
1159 if ((fd
= open (buffer
, O_CREAT
| O_EXCL
,
1160 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
|
1161 S_IROTH
| S_IWOTH
)) >= 0) {
1169 if (version
>= max_versions
) {
1170 advise (NULL
, "will not overwrite %s, too many versions", buffer
);
1173 ++files_not_clobbered
;
1181 clobber_check (char *original_file
) {
1182 /* clobber policy return value
1183 * -------------- ------------
1185 * -auto file-<digits>.extension
1186 * -suffix file.<digits>
1187 * -ask file, 0, or another filename/path
1195 if (clobber_policy
== NMH_CLOBBER_ASK
) {
1196 /* Save cwd for possible use in loop below. */
1199 cwd
= add (original_file
, NULL
);
1200 slash
= strrchr (cwd
, '/');
1205 /* original_file wasn't a full path, which shouldn't happen. */
1213 file
= original_file
;
1216 switch (clobber_policy
) {
1217 case NMH_CLOBBER_ALWAYS
:
1220 case NMH_CLOBBER_SUFFIX
:
1221 case NMH_CLOBBER_AUTO
:
1222 if (stat (file
, &st
) == OK
) {
1223 file
= next_version (original_file
, clobber_policy
);
1227 case NMH_CLOBBER_ASK
:
1228 if (stat (file
, &st
) == OK
) {
1229 enum answers
{ NMH_YES
, NMH_NO
, NMH_RENAME
};
1230 static struct swit answer
[4] = {
1231 { "yes", 0 }, { "no", 0 }, { "rename", 0 }, { NULL
, 0 } };
1234 if (isatty (fileno (stdin
))) {
1236 concat ("Overwrite \"", file
, "\" [y/n/rename]? ", NULL
);
1237 ans
= getans (prompt
, answer
);
1240 /* Overwrite, that's what nmh used to do. And warn. */
1241 advise (NULL
, "-clobber ask but no tty, so overwrite %s", file
);
1245 switch ((enum answers
) smatch (*ans
, answer
)) {
1251 ++files_not_clobbered
;
1255 printf ("Enter filename or full path of the new file: ");
1256 if (fgets (buf
, sizeof buf
, stdin
) == NULL
||
1259 ++files_not_clobbered
;
1261 char *newline
= strchr (buf
, '\n');
1269 if (buf
[0] == '/') {
1270 /* Full path, use it. */
1271 file
= add (buf
, NULL
);
1273 /* Relative path. */
1274 file
= cwd
? concat (cwd
, "/", buf
, NULL
) : add (buf
, NULL
);
1284 case NMH_CLOBBER_NEVER
:
1285 if (stat (file
, &st
) == OK
) {
1286 /* Keep count of files that would have been clobbered,
1287 and return that as process exit status. */
1288 advise (NULL
, "will not overwrite %s with -clobber never", file
);
1291 ++files_not_clobbered
;
1296 original_file
= file
;
1297 } while (check_again
);
1306 /* -clobber support */
1307 /******************************************************************************/