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
);
141 switch (ct
->c_subtype
) {
142 case MESSAGE_PARTIAL
:
143 return store_partial (ct
);
146 case MESSAGE_EXTERNAL
:
147 return store_external (ct
);
151 return store_generic (ct
);
157 return store_application (ct
);
164 return store_generic (ct
);
168 adios (NULL
, "unknown content type %d", ct
->c_type
);
172 return OK
; /* NOT REACHED */
177 * Generic routine to store a MIME content.
178 * (audio, video, image, text, message/rfc822)
182 store_generic (CT ct
)
185 * Check if the content specifies a filename.
186 * Don't bother with this for type "message"
187 * (only "message/rfc822" will use store_generic).
189 if (autosw
&& ct
->c_type
!= CT_MESSAGE
)
192 return store_content (ct
, NULL
);
197 * Store content of type "application"
201 store_application (CT ct
)
204 CI ci
= &ct
->c_ctinfo
;
206 /* Check if the content specifies a filename */
211 * If storeproc is not defined, and the content is type
212 * "application/octet-stream", we also check for various
213 * attribute/value pairs which specify if this a tar file.
215 if (!ct
->c_storeproc
&& ct
->c_subtype
== APPLICATION_OCTETS
) {
216 int tarP
= 0, zP
= 0, gzP
= 0;
218 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
219 /* check for "type=tar" attribute */
220 if (!strcasecmp (*ap
, "type")) {
221 if (strcasecmp (*ep
, "tar"))
228 /* check for "conversions=compress" attribute */
229 if ((!strcasecmp (*ap
, "conversions") || !strcasecmp (*ap
, "x-conversions"))
230 && (!strcasecmp (*ep
, "compress") || !strcasecmp (*ep
, "x-compress"))) {
234 /* check for "conversions=gzip" attribute */
235 if ((!strcasecmp (*ap
, "conversions") || !strcasecmp (*ap
, "x-conversions"))
236 && (!strcasecmp (*ep
, "gzip") || !strcasecmp (*ep
, "x-gzip"))) {
243 ct
->c_showproc
= add (zP
? "%euncompress | tar tvf -"
244 : (gzP
? "%egzip -dc | tar tvf -"
245 : "%etar tvf -"), NULL
);
246 if (!ct
->c_storeproc
) {
248 ct
->c_storeproc
= add (zP
? "| uncompress | tar xvpf -"
249 : (gzP
? "| gzip -dc | tar xvpf -"
250 : "| tar xvpf -"), NULL
);
253 ct
->c_storeproc
= add (zP
? "%m%P.tar.Z"
254 : (gzP
? "%m%P.tar.gz"
255 : "%m%P.tar"), NULL
);
261 return store_content (ct
, NULL
);
266 * Store the content of a multipart message
273 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
277 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
278 CT p
= part
->mp_part
;
280 if (part_ok (p
, 1) && type_ok (p
, 1)) {
282 /* Support mhstore -outfile. The MIME parser doesn't
283 load c_storage, so we know that p->c_storage is
285 p
->c_storage
= ct
->c_storage
;
287 result
= store_switch (p
);
290 if (result
== OK
&& ct
->c_subtype
== MULTI_ALTERNATE
)
300 * Reassemble and store the contents of a collection
301 * of messages of type "message/partial".
305 store_partial (CT ct
)
310 struct partial
*pm
, *qm
;
312 qm
= (struct partial
*) ct
->c_ctparams
;
317 for (ctp
= cts
; *ctp
; ctp
++) {
319 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
320 pm
= (struct partial
*) p
->c_ctparams
;
322 && strcmp (qm
->pm_partid
, pm
->pm_partid
) == 0) {
323 pm
->pm_marked
= pm
->pm_partno
;
335 advise (NULL
, "missing (at least) last part of multipart message");
339 if ((base
= (CT
*) calloc ((size_t) (i
+ 1), sizeof(*base
))) == NULL
)
340 adios (NULL
, "out of memory");
343 for (ctp
= cts
; *ctp
; ctp
++) {
345 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
346 pm
= (struct partial
*) p
->c_ctparams
;
354 qsort ((char *) base
, i
, sizeof(*base
), (qsort_comp
) ct_compar
);
357 for (ctq
= base
; *ctq
; ctq
++) {
359 pm
= (struct partial
*) p
->c_ctparams
;
360 if (pm
->pm_marked
!= cur
) {
361 if (pm
->pm_marked
== cur
- 1) {
363 "duplicate part %d of %d part multipart message",
370 "missing %spart %d of %d part multipart message",
371 cur
!= hi
? "(at least) " : "", cur
, hi
);
383 * Now cycle through the sorted list of messages of type
384 * "message/partial" and save/append them to a file.
389 if (store_content (ct
, NULL
) == NOTOK
) {
391 free ((char *) base
);
395 for (; *ctq
; ctq
++) {
397 if (store_content (p
, ct
) == NOTOK
)
401 free ((char *) base
);
407 * Store content from a message of type "message/external".
411 store_external (CT ct
)
414 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
415 CT p
= e
->eb_content
;
421 * Check if the parameters for the external body
422 * specified a filename.
427 if ((cp
= e
->eb_name
)
432 && !strchr (cp
, '%')) {
433 if (!ct
->c_storeproc
)
434 ct
->c_storeproc
= add (cp
, NULL
);
436 p
->c_storeproc
= add (cp
, NULL
);
441 * Since we will let the Content structure for the
442 * external body substitute for the current content,
443 * we temporarily change its partno (number inside
444 * multipart), so everything looks right.
446 p
->c_partno
= ct
->c_partno
;
448 /* we probably need to check if content is really there */
450 /* Support mhstore -outfile. The MIME parser doesn't load
451 c_storage, so we know that p->c_storage is NULL here. */
452 p
->c_storage
= ct
->c_storage
;
454 result
= store_switch (p
);
463 * Compare the numbering from two different
464 * message/partials (needed for sorting).
468 ct_compar (CT
*a
, CT
*b
)
470 struct partial
*am
= (struct partial
*) ((*a
)->c_ctparams
);
471 struct partial
*bm
= (struct partial
*) ((*b
)->c_ctparams
);
473 return (am
->pm_marked
- bm
->pm_marked
);
478 * Store contents of a message or message part to
479 * a folder, a file, the standard output, or pass
480 * the contents to a command.
482 * If the current content to be saved is a followup part
483 * to a collection of messages of type "message/partial",
484 * then field "p" is a pointer to the Content structure
485 * to the first message/partial in the group.
489 store_content (CT ct
, CT p
)
491 int appending
= 0, msgnum
= 0;
492 int is_partial
= 0, first_partial
= 0;
493 int last_partial
= 0;
494 char *cp
, buffer
[BUFSIZ
];
497 * Do special processing for messages of
498 * type "message/partial".
500 * We first check if this content is of type
501 * "message/partial". If it is, then we need to check
502 * whether it is the first and/or last in the group.
504 * Then if "p" is a valid pointer, it points to the Content
505 * structure of the first partial in the group. So we copy
506 * the file name and/or folder name from that message. In
507 * this case, we also note that we will be appending.
509 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
510 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
512 /* Yep, it's a message/partial */
515 /* But is it the first and/or last in the collection? */
516 if (pm
->pm_partno
== 1)
518 if (pm
->pm_maxno
&& pm
->pm_partno
== pm
->pm_maxno
)
522 * If "p" is a valid pointer, then it points to the
523 * Content structure for the first message in the group.
524 * So we just copy the filename or foldername information
525 * from the previous iteration of this function.
529 if (! ct
->c_storage
) {
530 ct
->c_storage
= add (p
->c_storage
, NULL
);
532 /* record the folder name */
534 ct
->c_folder
= add (p
->c_folder
, NULL
);
542 * Get storage formatting string.
544 * 1) If we have storeproc defined, then use that
545 * 2) Else check for a mhn-store-<type>/<subtype> entry
546 * 3) Else check for a mhn-store-<type> entry
547 * 4) Else if content is "message", use "+" (current folder)
548 * 5) Else use string "%m%P.%s".
550 if ((cp
= ct
->c_storeproc
) == NULL
|| *cp
== '\0') {
551 CI ci
= &ct
->c_ctinfo
;
553 snprintf (buffer
, sizeof(buffer
), "%s-store-%s/%s",
554 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
555 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
556 snprintf (buffer
, sizeof(buffer
), "%s-store-%s", invo_name
, ci
->ci_type
);
557 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
558 cp
= ct
->c_type
== CT_MESSAGE
? "+" : "%m%P.%s";
563 if (! ct
->c_storage
) {
565 * Check the beginning of storage formatting string
566 * to see if we are saving content to a folder.
568 if (*cp
== '+' || *cp
== '@') {
569 char *tmpfilenam
, *folder
;
571 /* Store content in temporary file for now */
572 tmpfilenam
= m_mktemp(invo_name
, NULL
, NULL
);
573 ct
->c_storage
= add (tmpfilenam
, NULL
);
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
= add (folder
, NULL
);
594 * Parse and expand the storage formatting string
595 * in `cp' into `buffer'.
597 parse_format_string (ct
, cp
, buffer
, sizeof(buffer
), 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
, 1, 0, buffer
+ 1, dir
);
606 /* record the filename */
607 if ((ct
->c_storage
= clobber_check (add (buffer
, NULL
))) == NULL
) {
611 /* The output filename was explicitly specified, so use it. */
612 if ((ct
->c_storage
= clobber_check (add (ct
->c_storage
, NULL
))) ==
619 /* flush the output stream */
622 /* Now save or append the content to a file */
623 if (output_content_file (ct
, appending
) == NOTOK
)
627 * If necessary, link the file into a folder and remove
628 * the temporary file. If this message is a partial,
629 * then only do this if it is the last one in the group.
631 if (ct
->c_folder
&& (!is_partial
|| last_partial
)) {
632 msgnum
= output_content_folder (ct
->c_folder
, ct
->c_storage
);
633 unlink (ct
->c_storage
);
639 * Now print out the name/number of the message
640 * that we are storing.
644 fprintf (stderr
, "reassembling partials ");
646 fprintf (stderr
, "%s", ct
->c_file
);
648 fprintf (stderr
, "%s,", ct
->c_file
);
650 fprintf (stderr
, "storing message %s", ct
->c_file
);
652 fprintf (stderr
, " part %s", ct
->c_partno
);
656 * Unless we are in the "middle" of group of message/partials,
657 * we now print the name of the file, folder, and/or message
658 * to which we are storing the content.
660 if (!is_partial
|| last_partial
) {
662 fprintf (stderr
, " to folder %s as message %d\n", ct
->c_folder
, msgnum
);
663 } else if (!strcmp(ct
->c_storage
, "-")) {
664 fprintf (stderr
, " to stdout\n");
668 cwdlen
= strlen (cwd
);
669 fprintf (stderr
, " as file %s\n",
670 strncmp (ct
->c_storage
, cwd
, cwdlen
)
671 || ct
->c_storage
[cwdlen
] != '/'
672 ? ct
->c_storage
: ct
->c_storage
+ cwdlen
+ 1);
681 * Output content to a file
685 output_content_file (CT ct
, int appending
)
688 char *file
, buffer
[BUFSIZ
];
693 * If the pathname is absolute, make sure
694 * all the relevant directories exist.
696 if (strchr(ct
->c_storage
, '/')
697 && make_intermediates (ct
->c_storage
) == NOTOK
)
700 if (ct
->c_encoding
!= CE_7BIT
) {
703 if (!ct
->c_ceopenfnx
) {
704 advise (NULL
, "don't know how to decode part %s of message %s",
705 ct
->c_partno
, ct
->c_file
);
709 file
= appending
|| !strcmp (ct
->c_storage
, "-") ? NULL
711 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
713 if (!strcmp (file
, ct
->c_storage
)) {
714 (*ct
->c_ceclosefnx
) (ct
);
719 * Send to standard output
721 if (!strcmp (ct
->c_storage
, "-")) {
724 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
725 advise ("stdout", "unable to dup");
727 (*ct
->c_ceclosefnx
) (ct
);
730 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
731 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
732 appending
? "a" : "w");
740 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
741 advise (ct
->c_storage
, "unable to fopen for %s",
742 appending
? "appending" : "writing");
748 * Filter the header fields of the initial enclosing
749 * message/partial into the file.
751 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
752 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
754 if (pm
->pm_partno
== 1)
755 copy_some_headers (fp
, ct
);
759 switch (cc
= read (fd
, buffer
, sizeof(buffer
))) {
761 advise (file
, "error reading content from");
768 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
774 (*ct
->c_ceclosefnx
) (ct
);
776 if (cc
!= NOTOK
&& fflush (fp
))
777 advise (ct
->c_storage
, "error writing to");
781 return (cc
!= NOTOK
? OK
: NOTOK
);
784 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
785 advise (ct
->c_file
, "unable to open for reading");
791 fseek (ct
->c_fp
, pos
, SEEK_SET
);
793 if (!strcmp (ct
->c_storage
, "-")) {
796 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
797 advise ("stdout", "unable to dup");
800 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
801 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
802 appending
? "a" : "w");
807 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
808 advise (ct
->c_storage
, "unable to fopen for %s",
809 appending
? "appending" : "writing");
815 * Copy a few of the header fields of the initial
816 * enclosing message/partial into the file.
819 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
820 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
822 if (pm
->pm_partno
== 1) {
823 copy_some_headers (fp
, ct
);
828 while (fgets (buffer
, sizeof(buffer
) - 1, ct
->c_fp
)) {
829 if ((pos
+= strlen (buffer
)) > last
) {
832 diff
= strlen (buffer
) - (pos
- last
);
837 * If this is the first content of a group of
838 * message/partial contents, then we only copy a few
839 * of the header fields of the enclosed message.
854 if (!uprf (buffer
, XXX_FIELD_PRF
)
855 && !uprf (buffer
, VRSN_FIELD
)
856 && !uprf (buffer
, "Subject:")
857 && !uprf (buffer
, "Encrypted:")
858 && !uprf (buffer
, "Message-ID:")) {
873 advise (ct
->c_storage
, "error writing to");
883 * Add a file to a folder.
885 * Return the new message number of the file
886 * when added to the folder. Return -1, if
891 output_content_folder (char *folder
, char *filename
)
896 /* Read the folder. */
897 if ((mp
= folder_read (folder
, 0))) {
898 /* Link file into folder */
899 msgnum
= folder_addmsg (&mp
, filename
, 0, 0, 0, 0, (char *)0);
901 advise (NULL
, "unable to read folder %s", folder
);
905 /* free folder structure */
909 * Return msgnum. We are relying on the fact that
910 * msgnum will be -1, if folder_addmsg() had an error.
917 * Parse and expand the storage formatting string
918 * pointed to by "cp" into "buffer".
922 parse_format_string (CT ct
, char *cp
, char *buffer
, int buflen
, char *dir
)
926 CI ci
= &ct
->c_ctinfo
;
929 * If storage string is "-", just copy it, and
930 * return (send content to standard output).
932 if (cp
[0] == '-' && cp
[1] == '\0') {
933 strncpy (buffer
, cp
, buflen
);
941 * If formatting string is a pathname that doesn't
942 * begin with '/', then preface the path with the
943 * appropriate directory.
945 if (*cp
!= '/' && *cp
!= '|' && *cp
!= '!') {
946 snprintf (bp
, buflen
, "%s/", dir
[1] ? dir
: "");
954 /* We are processing a storage escape */
959 * Insert parameters from Content-Type.
960 * This is only valid for '|' commands.
962 if (buffer
[0] != '|' && buffer
[0] != '!') {
971 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
;
973 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
983 /* insert message number */
984 snprintf (bp
, buflen
, "%s", r1bindex (ct
->c_file
, '/'));
988 /* insert part number with leading dot */
990 snprintf (bp
, buflen
, ".%s", ct
->c_partno
);
994 /* insert part number withouth leading dot */
996 strncpy (bp
, ct
->c_partno
, buflen
);
1000 /* insert content type */
1001 strncpy (bp
, ci
->ci_type
, buflen
);
1005 /* insert content subtype */
1006 strncpy (bp
, ci
->ci_subtype
, buflen
);
1010 /* insert the character % */
1020 /* Advance bp and decrement buflen */
1038 * Check if the content specifies a filename
1039 * in its MIME parameters.
1043 get_storeproc (CT ct
)
1045 char **ap
, **ep
, *cp
;
1049 * If the storeproc has already been defined,
1050 * we just return (for instance, if this content
1051 * is part of a "message/external".
1053 if (ct
->c_storeproc
)
1057 * If there's a Content-Disposition header and it has a filename,
1058 * use that (RFC-2183).
1061 char *cp
= strchr (ct
->c_dispo
, ';');
1062 CI ci
= calloc (1, sizeof *ci
);
1064 int found_filename
= 0;
1066 if (cp
&& parse_header_attrs (ct
->c_file
, strlen (invo_name
) + 2, &cp
,
1067 ci
, &status
) == OK
) {
1068 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1069 if (! strcasecmp (*ap
, "filename")
1070 && *(cp
= *ep
) != '/'
1074 && !strchr (cp
, '%')) {
1075 ct
->c_storeproc
= add (cp
, NULL
);
1083 if (found_filename
) return;
1087 * Check the attribute/value pairs, for the attribute "name".
1088 * If found, do a few sanity checks and copy the value into
1092 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1093 if (! strcasecmp (*ap
, "name")
1094 && *(cp
= *ep
) != '/'
1098 && !strchr (cp
, '%')) {
1099 ct
->c_storeproc
= add (cp
, NULL
);
1107 * Copy some of the header fields of the initial message/partial
1108 * message into the header of the reassembled message.
1112 copy_some_headers (FILE *out
, CT ct
)
1116 hp
= ct
->c_first_hf
; /* start at first header field */
1120 * A few of the header fields of the enclosing
1121 * messages are not copied.
1123 if (!uprf (hp
->name
, XXX_FIELD_PRF
)
1124 && strcasecmp (hp
->name
, VRSN_FIELD
)
1125 && strcasecmp (hp
->name
, "Subject")
1126 && strcasecmp (hp
->name
, "Encrypted")
1127 && strcasecmp (hp
->name
, "Message-ID"))
1128 fprintf (out
, "%s:%s", hp
->name
, hp
->value
);
1129 hp
= hp
->next
; /* next header field */
1135 /******************************************************************************/
1136 /* -clobber support */
1138 enum clobber_policy_t
{
1146 static enum clobber_policy_t clobber_policy
= NMH_CLOBBER_ALWAYS
;
1148 int files_not_clobbered
= 0;
1151 save_clobber_policy (const char *value
) {
1152 if (! strcasecmp (value
, "always")) {
1153 clobber_policy
= NMH_CLOBBER_ALWAYS
;
1154 } else if (! strcasecmp (value
, "auto")) {
1155 clobber_policy
= NMH_CLOBBER_AUTO
;
1156 } else if (! strcasecmp (value
, "suffix")) {
1157 clobber_policy
= NMH_CLOBBER_SUFFIX
;
1158 } else if (! strcasecmp (value
, "ask")) {
1159 clobber_policy
= NMH_CLOBBER_ASK
;
1160 } else if (! strcasecmp (value
, "never")) {
1161 clobber_policy
= NMH_CLOBBER_NEVER
;
1171 next_version (char *file
, enum clobber_policy_t clobber_policy
) {
1172 const size_t max_versions
= 1000000;
1173 /* 8 = log max_versions + one for - or . + one for null terminator */
1174 const size_t buflen
= strlen (file
) + 8;
1175 char *buffer
= mh_xmalloc (buflen
);
1178 char *extension
= NULL
;
1179 if (clobber_policy
== NMH_CLOBBER_AUTO
&&
1180 ((extension
= strrchr (file
, '.')) != NULL
)) {
1181 *extension
++ = '\0';
1184 for (version
= 1; version
< max_versions
; ++version
) {
1187 switch (clobber_policy
) {
1188 case NMH_CLOBBER_AUTO
: {
1189 snprintf (buffer
, buflen
, "%s-%ld%s%s", file
, (long) version
,
1190 extension
== NULL
? "" : ".",
1191 extension
== NULL
? "" : extension
);
1195 case NMH_CLOBBER_SUFFIX
:
1196 snprintf (buffer
, buflen
, "%s.%ld", file
, (long) version
);
1200 /* Should never get here. */
1201 advise (NULL
, "will not overwrite %s, invalid clobber policy", buffer
);
1203 ++files_not_clobbered
;
1207 /* Actually (try to) create the file here to avoid a race
1208 condition on file naming + creation. This won't solve the
1209 problem with old NFS that doesn't support O_EXCL, though.
1210 Let the umask strip off permissions from 0666 as desired.
1211 That's what fopen () would do if it was creating the file. */
1212 if ((fd
= open (buffer
, O_CREAT
| O_EXCL
,
1213 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
|
1214 S_IROTH
| S_IWOTH
)) >= 0) {
1222 if (version
>= max_versions
) {
1223 advise (NULL
, "will not overwrite %s, too many versions", buffer
);
1226 ++files_not_clobbered
;
1234 clobber_check (char *original_file
) {
1235 /* clobber policy return value
1236 * -------------- ------------
1237 * -always original_file
1238 * -auto original_file-<digits>.extension
1239 * -suffix original_file.<digits>
1240 * -ask original_file, 0, or another filename/path
1248 if (clobber_policy
== NMH_CLOBBER_ASK
) {
1249 /* Save cwd for possible use in loop below. */
1252 cwd
= add (original_file
, NULL
);
1253 slash
= strrchr (cwd
, '/');
1258 /* original_file wasn't a full path, which shouldn't happen. */
1266 file
= original_file
;
1269 switch (clobber_policy
) {
1270 case NMH_CLOBBER_ALWAYS
:
1273 case NMH_CLOBBER_SUFFIX
:
1274 case NMH_CLOBBER_AUTO
:
1275 if (stat (file
, &st
) == OK
) {
1276 file
= next_version (original_file
, clobber_policy
);
1280 case NMH_CLOBBER_ASK
:
1281 if (stat (file
, &st
) == OK
) {
1282 enum answers
{ NMH_YES
, NMH_NO
, NMH_RENAME
};
1283 static struct swit answer
[4] = {
1284 { "yes", 0, NMH_YES
}, { "no", 0, NMH_NO
}, { "rename", 0, NMH_RENAME
}, { NULL
, 0, 0 } };
1287 if (isatty (fileno (stdin
))) {
1289 concat ("Overwrite \"", file
, "\" [y/n/rename]? ", NULL
);
1290 ans
= getans (prompt
, answer
);
1293 /* Overwrite, that's what nmh used to do. And warn. */
1294 advise (NULL
, "-clobber ask but no tty, so overwrite %s", file
);
1298 switch ((enum answers
) smatch (*ans
, answer
)) {
1304 ++files_not_clobbered
;
1308 printf ("Enter filename or full path of the new file: ");
1309 if (fgets (buf
, sizeof buf
, stdin
) == NULL
||
1312 ++files_not_clobbered
;
1314 char *newline
= strchr (buf
, '\n');
1322 if (buf
[0] == '/') {
1323 /* Full path, use it. */
1324 file
= add (buf
, NULL
);
1326 /* Relative path. */
1327 file
= cwd
? concat (cwd
, "/", buf
, NULL
) : add (buf
, NULL
);
1337 case NMH_CLOBBER_NEVER
:
1338 if (stat (file
, &st
) == OK
) {
1339 /* Keep count of files that would have been clobbered,
1340 and return that as process exit status. */
1341 advise (NULL
, "will not overwrite %s with -clobber never", file
);
1344 ++files_not_clobbered
;
1349 original_file
= file
;
1350 } while (check_again
);
1359 /* -clobber support */
1360 /******************************************************************************/