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>
17 #include <h/mhparse.h>
22 * The list of top-level contents to display
29 * Cache of current directory. This must be
30 * set before these routines are called.
35 * The directory in which to store the contents.
40 * Type for a compare function for qsort. This keeps
43 typedef int (*qsort_comp
) (const void *, const void *);
47 int part_ok (CT
, int);
48 int type_ok (CT
, int);
49 void flush_errors (void);
52 int show_content_aux (CT
, int, int, char *, char *);
57 void store_all_messages (CT
*);
62 static void store_single_message (CT
);
63 static int store_switch (CT
);
64 static int store_generic (CT
);
65 static int store_application (CT
);
66 static int store_multi (CT
);
67 static int store_partial (CT
);
68 static int store_external (CT
);
69 static int ct_compar (CT
*, CT
*);
70 static int store_content (CT
, CT
);
71 static int output_content_file (CT
, int);
72 static int output_content_folder (char *, char *);
73 static int parse_format_string (CT
, char *, char *, int, char *);
74 static void get_storeproc (CT
);
75 static int copy_some_headers (FILE *, CT
);
76 static char *clobber_check (char *);
79 * Main entry point to store content
80 * from a collection of messages.
84 store_all_messages (CT
*cts
)
90 * Check for the directory in which to
93 if ((cp
= context_find (nmhstorage
)) && *cp
)
98 for (ctp
= cts
; *ctp
; ctp
++) {
100 store_single_message (ct
);
108 * Entry point to store the content
109 * in a (single) message
113 store_single_message (CT ct
)
115 if (type_ok (ct
, 1)) {
122 if (ct
->c_ceclosefnx
)
123 (*ct
->c_ceclosefnx
) (ct
);
129 * Switching routine to store different content types
135 switch (ct
->c_type
) {
137 return store_multi (ct
);
140 switch (ct
->c_subtype
) {
141 case MESSAGE_PARTIAL
:
142 return store_partial (ct
);
144 case MESSAGE_EXTERNAL
:
145 return store_external (ct
);
149 return store_generic (ct
);
153 return store_application (ct
);
159 return store_generic (ct
);
162 adios (NULL
, "unknown content type %d", ct
->c_type
);
165 return OK
; /* NOT REACHED */
170 * Generic routine to store a MIME content.
171 * (audio, video, image, text, message/rfc822)
175 store_generic (CT ct
)
178 * Check if the content specifies a filename.
179 * Don't bother with this for type "message"
180 * (only "message/rfc822" will use store_generic).
182 if (autosw
&& ct
->c_type
!= CT_MESSAGE
)
185 return store_content (ct
, NULL
);
190 * Store content of type "application"
194 store_application (CT ct
)
197 CI ci
= &ct
->c_ctinfo
;
199 /* Check if the content specifies a filename */
204 * If storeproc is not defined, and the content is type
205 * "application/octet-stream", we also check for various
206 * attribute/value pairs which specify if this a tar file.
208 if (!ct
->c_storeproc
&& ct
->c_subtype
== APPLICATION_OCTETS
) {
209 int tarP
= 0, zP
= 0, gzP
= 0;
211 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
212 /* check for "type=tar" attribute */
213 if (!strcasecmp (*ap
, "type")) {
214 if (strcasecmp (*ep
, "tar"))
221 /* check for "conversions=compress" attribute */
222 if ((!strcasecmp (*ap
, "conversions") || !strcasecmp (*ap
, "x-conversions"))
223 && (!strcasecmp (*ep
, "compress") || !strcasecmp (*ep
, "x-compress"))) {
227 /* check for "conversions=gzip" attribute */
228 if ((!strcasecmp (*ap
, "conversions") || !strcasecmp (*ap
, "x-conversions"))
229 && (!strcasecmp (*ep
, "gzip") || !strcasecmp (*ep
, "x-gzip"))) {
236 ct
->c_showproc
= add (zP
? "%euncompress | tar tvf -"
237 : (gzP
? "%egzip -dc | tar tvf -"
238 : "%etar tvf -"), NULL
);
239 if (!ct
->c_storeproc
) {
241 ct
->c_storeproc
= add (zP
? "| uncompress | tar xvpf -"
242 : (gzP
? "| gzip -dc | tar xvpf -"
243 : "| tar xvpf -"), NULL
);
246 ct
->c_storeproc
= add (zP
? "%m%P.tar.Z"
247 : (gzP
? "%m%P.tar.gz"
248 : "%m%P.tar"), NULL
);
254 return store_content (ct
, NULL
);
259 * Store the content of a multipart message
266 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
270 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
271 CT p
= part
->mp_part
;
273 if (part_ok (p
, 1) && type_ok (p
, 1)) {
275 /* Support mhstore -outfile. The MIME parser doesn't
276 load c_storage, so we know that p->c_storage is
278 p
->c_storage
= ct
->c_storage
;
280 result
= store_switch (p
);
283 if (result
== OK
&& ct
->c_subtype
== MULTI_ALTERNATE
)
293 * Reassemble and store the contents of a collection
294 * of messages of type "message/partial".
298 store_partial (CT ct
)
303 struct partial
*pm
, *qm
;
305 qm
= (struct partial
*) ct
->c_ctparams
;
310 for (ctp
= cts
; *ctp
; ctp
++) {
312 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
313 pm
= (struct partial
*) p
->c_ctparams
;
315 && strcmp (qm
->pm_partid
, pm
->pm_partid
) == 0) {
316 pm
->pm_marked
= pm
->pm_partno
;
328 advise (NULL
, "missing (at least) last part of multipart message");
332 if ((base
= (CT
*) calloc ((size_t) (i
+ 1), sizeof(*base
))) == NULL
)
333 adios (NULL
, "out of memory");
336 for (ctp
= cts
; *ctp
; ctp
++) {
338 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
339 pm
= (struct partial
*) p
->c_ctparams
;
347 qsort ((char *) base
, i
, sizeof(*base
), (qsort_comp
) ct_compar
);
350 for (ctq
= base
; *ctq
; ctq
++) {
352 pm
= (struct partial
*) p
->c_ctparams
;
353 if (pm
->pm_marked
!= cur
) {
354 if (pm
->pm_marked
== cur
- 1) {
356 "duplicate part %d of %d part multipart message",
363 "missing %spart %d of %d part multipart message",
364 cur
!= hi
? "(at least) " : "", cur
, hi
);
376 * Now cycle through the sorted list of messages of type
377 * "message/partial" and save/append them to a file.
382 if (store_content (ct
, NULL
) == NOTOK
) {
384 free ((char *) base
);
388 for (; *ctq
; ctq
++) {
390 if (store_content (p
, ct
) == NOTOK
)
394 free ((char *) base
);
400 * Store content from a message of type "message/external".
404 store_external (CT ct
)
407 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
408 CT p
= e
->eb_content
;
414 * Check if the parameters for the external body
415 * specified a filename.
420 if ((cp
= e
->eb_name
)
425 && !strchr (cp
, '%')) {
426 if (!ct
->c_storeproc
)
427 ct
->c_storeproc
= add (cp
, NULL
);
429 p
->c_storeproc
= add (cp
, NULL
);
434 * Since we will let the Content structure for the
435 * external body substitute for the current content,
436 * we temporarily change its partno (number inside
437 * multipart), so everything looks right.
439 p
->c_partno
= ct
->c_partno
;
441 /* we probably need to check if content is really there */
443 /* Support mhstore -outfile. The MIME parser doesn't load
444 c_storage, so we know that p->c_storage is NULL here. */
445 p
->c_storage
= ct
->c_storage
;
447 result
= store_switch (p
);
456 * Compare the numbering from two different
457 * message/partials (needed for sorting).
461 ct_compar (CT
*a
, CT
*b
)
463 struct partial
*am
= (struct partial
*) ((*a
)->c_ctparams
);
464 struct partial
*bm
= (struct partial
*) ((*b
)->c_ctparams
);
466 return (am
->pm_marked
- bm
->pm_marked
);
471 * Store contents of a message or message part to
472 * a folder, a file, the standard output, or pass
473 * the contents to a command.
475 * If the current content to be saved is a followup part
476 * to a collection of messages of type "message/partial",
477 * then field "p" is a pointer to the Content structure
478 * to the first message/partial in the group.
482 store_content (CT ct
, CT p
)
484 int appending
= 0, msgnum
= 0;
485 int is_partial
= 0, first_partial
= 0;
486 int last_partial
= 0;
487 char *cp
, buffer
[BUFSIZ
];
490 * Do special processing for messages of
491 * type "message/partial".
493 * We first check if this content is of type
494 * "message/partial". If it is, then we need to check
495 * whether it is the first and/or last in the group.
497 * Then if "p" is a valid pointer, it points to the Content
498 * structure of the first partial in the group. So we copy
499 * the file name and/or folder name from that message. In
500 * this case, we also note that we will be appending.
502 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
503 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
505 /* Yep, it's a message/partial */
508 /* But is it the first and/or last in the collection? */
509 if (pm
->pm_partno
== 1)
511 if (pm
->pm_maxno
&& pm
->pm_partno
== pm
->pm_maxno
)
515 * If "p" is a valid pointer, then it points to the
516 * Content structure for the first message in the group.
517 * So we just copy the filename or foldername information
518 * from the previous iteration of this function.
522 if (! ct
->c_storage
) {
523 ct
->c_storage
= add (p
->c_storage
, NULL
);
525 /* record the folder name */
527 ct
->c_folder
= add (p
->c_folder
, NULL
);
535 * Get storage formatting string.
537 * 1) If we have storeproc defined, then use that
538 * 2) Else check for a mhn-store-<type>/<subtype> entry
539 * 3) Else check for a mhn-store-<type> entry
540 * 4) Else if content is "message", use "+" (current folder)
541 * 5) Else use string "%m%P.%s".
543 if ((cp
= ct
->c_storeproc
) == NULL
|| *cp
== '\0') {
544 CI ci
= &ct
->c_ctinfo
;
546 snprintf (buffer
, sizeof(buffer
), "%s-store-%s/%s",
547 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
548 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
549 snprintf (buffer
, sizeof(buffer
), "%s-store-%s", invo_name
, ci
->ci_type
);
550 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
551 cp
= ct
->c_type
== CT_MESSAGE
? "+" : "%m%P.%s";
556 if (! ct
->c_storage
) {
558 * Check the beginning of storage formatting string
559 * to see if we are saving content to a folder.
561 if (*cp
== '+' || *cp
== '@') {
562 char *tmpfilenam
, *folder
;
564 /* Store content in temporary file for now */
565 tmpfilenam
= m_mktemp(invo_name
, NULL
, NULL
);
566 ct
->c_storage
= add (tmpfilenam
, NULL
);
568 /* Get the folder name */
570 folder
= pluspath (cp
);
572 folder
= getfolder (1);
574 /* Check if folder exists */
575 create_folder(m_mailpath(folder
), 0, exit
);
577 /* Record the folder name */
578 ct
->c_folder
= add (folder
, NULL
);
587 * Parse and expand the storage formatting string
588 * in `cp' into `buffer'.
590 parse_format_string (ct
, cp
, buffer
, sizeof(buffer
), dir
);
593 * If formatting begins with '|' or '!', then pass
594 * content to standard input of a command and return.
596 if (buffer
[0] == '|' || buffer
[0] == '!')
597 return show_content_aux (ct
, 1, 0, buffer
+ 1, dir
);
599 /* record the filename */
600 if ((ct
->c_storage
= clobber_check (add (buffer
, NULL
))) == NULL
) {
604 /* The output filename was explicitly specified, so use it. */
605 if ((ct
->c_storage
= clobber_check (add (ct
->c_storage
, NULL
))) ==
612 /* flush the output stream */
615 /* Now save or append the content to a file */
616 if (output_content_file (ct
, appending
) == NOTOK
)
620 * If necessary, link the file into a folder and remove
621 * the temporary file. If this message is a partial,
622 * then only do this if it is the last one in the group.
624 if (ct
->c_folder
&& (!is_partial
|| last_partial
)) {
625 msgnum
= output_content_folder (ct
->c_folder
, ct
->c_storage
);
626 unlink (ct
->c_storage
);
632 * Now print out the name/number of the message
633 * that we are storing.
637 fprintf (stderr
, "reassembling partials ");
639 fprintf (stderr
, "%s", ct
->c_file
);
641 fprintf (stderr
, "%s,", ct
->c_file
);
643 fprintf (stderr
, "storing message %s", ct
->c_file
);
645 fprintf (stderr
, " part %s", ct
->c_partno
);
649 * Unless we are in the "middle" of group of message/partials,
650 * we now print the name of the file, folder, and/or message
651 * to which we are storing the content.
653 if (!is_partial
|| last_partial
) {
655 fprintf (stderr
, " to folder %s as message %d\n", ct
->c_folder
, msgnum
);
656 } else if (!strcmp(ct
->c_storage
, "-")) {
657 fprintf (stderr
, " to stdout\n");
661 cwdlen
= strlen (cwd
);
662 fprintf (stderr
, " as file %s\n",
663 strncmp (ct
->c_storage
, cwd
, cwdlen
)
664 || ct
->c_storage
[cwdlen
] != '/'
665 ? ct
->c_storage
: ct
->c_storage
+ cwdlen
+ 1);
674 * Output content to a file
678 output_content_file (CT ct
, int appending
)
681 char *file
, buffer
[BUFSIZ
];
686 * If the pathname is absolute, make sure
687 * all the relevant directories exist.
689 if (strchr(ct
->c_storage
, '/')
690 && make_intermediates (ct
->c_storage
) == NOTOK
)
693 if (ct
->c_encoding
!= CE_7BIT
) {
696 if (!ct
->c_ceopenfnx
) {
697 advise (NULL
, "don't know how to decode part %s of message %s",
698 ct
->c_partno
, ct
->c_file
);
702 file
= appending
|| !strcmp (ct
->c_storage
, "-") ? NULL
704 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
706 if (!strcmp (file
, ct
->c_storage
)) {
707 (*ct
->c_ceclosefnx
) (ct
);
712 * Send to standard output
714 if (!strcmp (ct
->c_storage
, "-")) {
717 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
718 advise ("stdout", "unable to dup");
720 (*ct
->c_ceclosefnx
) (ct
);
723 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
724 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
725 appending
? "a" : "w");
733 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
734 advise (ct
->c_storage
, "unable to fopen for %s",
735 appending
? "appending" : "writing");
741 * Filter the header fields of the initial enclosing
742 * message/partial into the file.
744 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
745 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
747 if (pm
->pm_partno
== 1)
748 copy_some_headers (fp
, ct
);
752 switch (cc
= read (fd
, buffer
, sizeof(buffer
))) {
754 advise (file
, "error reading content from");
761 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
767 (*ct
->c_ceclosefnx
) (ct
);
769 if (cc
!= NOTOK
&& fflush (fp
))
770 advise (ct
->c_storage
, "error writing to");
774 return (cc
!= NOTOK
? OK
: NOTOK
);
777 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
778 advise (ct
->c_file
, "unable to open for reading");
784 fseek (ct
->c_fp
, pos
, SEEK_SET
);
786 if (!strcmp (ct
->c_storage
, "-")) {
789 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
790 advise ("stdout", "unable to dup");
793 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
794 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
795 appending
? "a" : "w");
800 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
801 advise (ct
->c_storage
, "unable to fopen for %s",
802 appending
? "appending" : "writing");
808 * Copy a few of the header fields of the initial
809 * enclosing message/partial into the file.
812 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
813 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
815 if (pm
->pm_partno
== 1) {
816 copy_some_headers (fp
, ct
);
821 while (fgets (buffer
, sizeof(buffer
) - 1, ct
->c_fp
)) {
822 if ((pos
+= strlen (buffer
)) > last
) {
825 diff
= strlen (buffer
) - (pos
- last
);
830 * If this is the first content of a group of
831 * message/partial contents, then we only copy a few
832 * of the header fields of the enclosed message.
847 if (!uprf (buffer
, XXX_FIELD_PRF
)
848 && !uprf (buffer
, VRSN_FIELD
)
849 && !uprf (buffer
, "Subject:")
850 && !uprf (buffer
, "Encrypted:")
851 && !uprf (buffer
, "Message-ID:")) {
866 advise (ct
->c_storage
, "error writing to");
876 * Add a file to a folder.
878 * Return the new message number of the file
879 * when added to the folder. Return -1, if
884 output_content_folder (char *folder
, char *filename
)
889 /* Read the folder. */
890 if ((mp
= folder_read (folder
, 0))) {
891 /* Link file into folder */
892 msgnum
= folder_addmsg (&mp
, filename
, 0, 0, 0, 0, (char *)0);
894 advise (NULL
, "unable to read folder %s", folder
);
898 /* free folder structure */
902 * Return msgnum. We are relying on the fact that
903 * msgnum will be -1, if folder_addmsg() had an error.
910 * Parse and expand the storage formatting string
911 * pointed to by "cp" into "buffer".
915 parse_format_string (CT ct
, char *cp
, char *buffer
, int buflen
, char *dir
)
919 CI ci
= &ct
->c_ctinfo
;
922 * If storage string is "-", just copy it, and
923 * return (send content to standard output).
925 if (cp
[0] == '-' && cp
[1] == '\0') {
926 strncpy (buffer
, cp
, buflen
);
934 * If formatting string is a pathname that doesn't
935 * begin with '/', then preface the path with the
936 * appropriate directory.
938 if (*cp
!= '/' && *cp
!= '|' && *cp
!= '!') {
939 snprintf (bp
, buflen
, "%s/", dir
[1] ? dir
: "");
947 /* We are processing a storage escape */
952 * Insert parameters from Content-Type.
953 * This is only valid for '|' commands.
955 if (buffer
[0] != '|' && buffer
[0] != '!') {
964 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
;
966 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
976 /* insert message number */
977 snprintf (bp
, buflen
, "%s", r1bindex (ct
->c_file
, '/'));
981 /* insert part number with leading dot */
983 snprintf (bp
, buflen
, ".%s", ct
->c_partno
);
987 /* insert part number withouth leading dot */
989 strncpy (bp
, ct
->c_partno
, buflen
);
993 /* insert content type */
994 strncpy (bp
, ci
->ci_type
, buflen
);
998 /* insert content subtype */
999 strncpy (bp
, ci
->ci_subtype
, buflen
);
1003 /* insert the character % */
1013 /* Advance bp and decrement buflen */
1031 * Check if the content specifies a filename
1032 * in its MIME parameters.
1036 get_storeproc (CT ct
)
1038 char **ap
, **ep
, *cp
;
1042 * If the storeproc has already been defined,
1043 * we just return (for instance, if this content
1044 * is part of a "message/external".
1046 if (ct
->c_storeproc
)
1050 * If there's a Content-Disposition header and it has a filename,
1051 * use that (RFC-2183).
1054 char *cp
= strchr (ct
->c_dispo
, ';');
1055 CI ci
= calloc (1, sizeof *ci
);
1057 int found_filename
= 0;
1059 if (cp
&& parse_header_attrs (ct
->c_file
, strlen (invo_name
) + 2, &cp
,
1060 ci
, &status
) == OK
) {
1061 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1062 if (! strcasecmp (*ap
, "filename")
1063 && *(cp
= *ep
) != '/'
1067 && !strchr (cp
, '%')) {
1068 ct
->c_storeproc
= add (cp
, NULL
);
1076 if (found_filename
) return;
1080 * Check the attribute/value pairs, for the attribute "name".
1081 * If found, do a few sanity checks and copy the value into
1085 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1086 if (! strcasecmp (*ap
, "name")
1087 && *(cp
= *ep
) != '/'
1091 && !strchr (cp
, '%')) {
1092 ct
->c_storeproc
= add (cp
, NULL
);
1100 * Copy some of the header fields of the initial message/partial
1101 * message into the header of the reassembled message.
1105 copy_some_headers (FILE *out
, CT ct
)
1109 hp
= ct
->c_first_hf
; /* start at first header field */
1113 * A few of the header fields of the enclosing
1114 * messages are not copied.
1116 if (!uprf (hp
->name
, XXX_FIELD_PRF
)
1117 && strcasecmp (hp
->name
, VRSN_FIELD
)
1118 && strcasecmp (hp
->name
, "Subject")
1119 && strcasecmp (hp
->name
, "Encrypted")
1120 && strcasecmp (hp
->name
, "Message-ID"))
1121 fprintf (out
, "%s:%s", hp
->name
, hp
->value
);
1122 hp
= hp
->next
; /* next header field */
1128 /******************************************************************************/
1129 /* -clobber support */
1131 enum clobber_policy_t
{
1139 static enum clobber_policy_t clobber_policy
= NMH_CLOBBER_ALWAYS
;
1141 int files_not_clobbered
= 0;
1144 save_clobber_policy (const char *value
) {
1145 if (! strcasecmp (value
, "always")) {
1146 clobber_policy
= NMH_CLOBBER_ALWAYS
;
1147 } else if (! strcasecmp (value
, "auto")) {
1148 clobber_policy
= NMH_CLOBBER_AUTO
;
1149 } else if (! strcasecmp (value
, "suffix")) {
1150 clobber_policy
= NMH_CLOBBER_SUFFIX
;
1151 } else if (! strcasecmp (value
, "ask")) {
1152 clobber_policy
= NMH_CLOBBER_ASK
;
1153 } else if (! strcasecmp (value
, "never")) {
1154 clobber_policy
= NMH_CLOBBER_NEVER
;
1164 next_version (char *file
, enum clobber_policy_t clobber_policy
) {
1165 const size_t max_versions
= 1000000;
1166 /* 8 = log max_versions + one for - or . + one for null terminator */
1167 const size_t buflen
= strlen (file
) + 8;
1168 char *buffer
= mh_xmalloc (buflen
);
1171 char *extension
= NULL
;
1172 if (clobber_policy
== NMH_CLOBBER_AUTO
&&
1173 ((extension
= strrchr (file
, '.')) != NULL
)) {
1174 *extension
++ = '\0';
1177 for (version
= 1; version
< max_versions
; ++version
) {
1180 switch (clobber_policy
) {
1181 case NMH_CLOBBER_AUTO
: {
1182 snprintf (buffer
, buflen
, "%s-%ld%s%s", file
, (long) version
,
1183 extension
== NULL
? "" : ".",
1184 extension
== NULL
? "" : extension
);
1188 case NMH_CLOBBER_SUFFIX
:
1189 snprintf (buffer
, buflen
, "%s.%ld", file
, (long) version
);
1193 /* Should never get here. */
1194 advise (NULL
, "will not overwrite %s, invalid clobber policy", buffer
);
1196 ++files_not_clobbered
;
1200 /* Actually (try to) create the file here to avoid a race
1201 condition on file naming + creation. This won't solve the
1202 problem with old NFS that doesn't support O_EXCL, though.
1203 Let the umask strip off permissions from 0666 as desired.
1204 That's what fopen () would do if it was creating the file. */
1205 if ((fd
= open (buffer
, O_CREAT
| O_EXCL
,
1206 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
|
1207 S_IROTH
| S_IWOTH
)) >= 0) {
1215 if (version
>= max_versions
) {
1216 advise (NULL
, "will not overwrite %s, too many versions", buffer
);
1219 ++files_not_clobbered
;
1227 clobber_check (char *original_file
) {
1228 /* clobber policy return value
1229 * -------------- ------------
1230 * -always original_file
1231 * -auto original_file-<digits>.extension
1232 * -suffix original_file.<digits>
1233 * -ask original_file, 0, or another filename/path
1241 if (clobber_policy
== NMH_CLOBBER_ASK
) {
1242 /* Save cwd for possible use in loop below. */
1245 cwd
= add (original_file
, NULL
);
1246 slash
= strrchr (cwd
, '/');
1251 /* original_file wasn't a full path, which shouldn't happen. */
1259 file
= original_file
;
1262 switch (clobber_policy
) {
1263 case NMH_CLOBBER_ALWAYS
:
1266 case NMH_CLOBBER_SUFFIX
:
1267 case NMH_CLOBBER_AUTO
:
1268 if (stat (file
, &st
) == OK
) {
1269 file
= next_version (original_file
, clobber_policy
);
1273 case NMH_CLOBBER_ASK
:
1274 if (stat (file
, &st
) == OK
) {
1275 enum answers
{ NMH_YES
, NMH_NO
, NMH_RENAME
};
1276 static struct swit answer
[4] = {
1277 { "yes", 0, NMH_YES
}, { "no", 0, NMH_NO
}, { "rename", 0, NMH_RENAME
}, { NULL
, 0, 0 } };
1280 if (isatty (fileno (stdin
))) {
1282 concat ("Overwrite \"", file
, "\" [y/n/rename]? ", NULL
);
1283 ans
= getans (prompt
, answer
);
1286 /* Overwrite, that's what nmh used to do. And warn. */
1287 advise (NULL
, "-clobber ask but no tty, so overwrite %s", file
);
1291 switch ((enum answers
) smatch (*ans
, answer
)) {
1297 ++files_not_clobbered
;
1301 printf ("Enter filename or full path of the new file: ");
1302 if (fgets (buf
, sizeof buf
, stdin
) == NULL
||
1305 ++files_not_clobbered
;
1307 char *newline
= strchr (buf
, '\n');
1315 if (buf
[0] == '/') {
1316 /* Full path, use it. */
1317 file
= add (buf
, NULL
);
1319 /* Relative path. */
1320 file
= cwd
? concat (cwd
, "/", buf
, NULL
) : add (buf
, NULL
);
1330 case NMH_CLOBBER_NEVER
:
1331 if (stat (file
, &st
) == OK
) {
1332 /* Keep count of files that would have been clobbered,
1333 and return that as process exit status. */
1334 advise (NULL
, "will not overwrite %s with -clobber never", file
);
1337 ++files_not_clobbered
;
1342 original_file
= file
;
1343 } while (check_again
);
1352 /* -clobber support */
1353 /******************************************************************************/