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)) {
281 result
= store_switch (p
);
282 if (result
== OK
&& ct
->c_subtype
== MULTI_ALTERNATE
)
292 * Reassemble and store the contents of a collection
293 * of messages of type "message/partial".
297 store_partial (CT ct
)
302 struct partial
*pm
, *qm
;
304 qm
= (struct partial
*) ct
->c_ctparams
;
309 for (ctp
= cts
; *ctp
; ctp
++) {
311 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
312 pm
= (struct partial
*) p
->c_ctparams
;
314 && strcmp (qm
->pm_partid
, pm
->pm_partid
) == 0) {
315 pm
->pm_marked
= pm
->pm_partno
;
327 advise (NULL
, "missing (at least) last part of multipart message");
331 if ((base
= (CT
*) calloc ((size_t) (i
+ 1), sizeof(*base
))) == NULL
)
332 adios (NULL
, "out of memory");
335 for (ctp
= cts
; *ctp
; ctp
++) {
337 if (p
->c_type
== CT_MESSAGE
&& p
->c_subtype
== ct
->c_subtype
) {
338 pm
= (struct partial
*) p
->c_ctparams
;
346 qsort ((char *) base
, i
, sizeof(*base
), (qsort_comp
) ct_compar
);
349 for (ctq
= base
; *ctq
; ctq
++) {
351 pm
= (struct partial
*) p
->c_ctparams
;
352 if (pm
->pm_marked
!= cur
) {
353 if (pm
->pm_marked
== cur
- 1) {
355 "duplicate part %d of %d part multipart message",
362 "missing %spart %d of %d part multipart message",
363 cur
!= hi
? "(at least) " : "", cur
, hi
);
375 * Now cycle through the sorted list of messages of type
376 * "message/partial" and save/append them to a file.
381 if (store_content (ct
, NULL
) == NOTOK
) {
383 free ((char *) base
);
387 for (; *ctq
; ctq
++) {
389 if (store_content (p
, ct
) == NOTOK
)
393 free ((char *) base
);
399 * Store content from a message of type "message/external".
403 store_external (CT ct
)
406 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
407 CT p
= e
->eb_content
;
413 * Check if the parameters for the external body
414 * specified a filename.
419 if ((cp
= e
->eb_name
)
424 && !strchr (cp
, '%')) {
425 if (!ct
->c_storeproc
)
426 ct
->c_storeproc
= add (cp
, NULL
);
428 p
->c_storeproc
= add (cp
, NULL
);
433 * Since we will let the Content structure for the
434 * external body substitute for the current content,
435 * we temporarily change its partno (number inside
436 * multipart), so everything looks right.
438 p
->c_partno
= ct
->c_partno
;
440 /* we probably need to check if content is really there */
441 result
= store_switch (p
);
449 * Compare the numbering from two different
450 * message/partials (needed for sorting).
454 ct_compar (CT
*a
, CT
*b
)
456 struct partial
*am
= (struct partial
*) ((*a
)->c_ctparams
);
457 struct partial
*bm
= (struct partial
*) ((*b
)->c_ctparams
);
459 return (am
->pm_marked
- bm
->pm_marked
);
464 * Store contents of a message or message part to
465 * a folder, a file, the standard output, or pass
466 * the contents to a command.
468 * If the current content to be saved is a followup part
469 * to a collection of messages of type "message/partial",
470 * then field "p" is a pointer to the Content structure
471 * to the first message/partial in the group.
475 store_content (CT ct
, CT p
)
477 int appending
= 0, msgnum
= 0;
478 int is_partial
= 0, first_partial
= 0;
479 int last_partial
= 0;
480 char *cp
, buffer
[BUFSIZ
];
483 * Do special processing for messages of
484 * type "message/partial".
486 * We first check if this content is of type
487 * "message/partial". If it is, then we need to check
488 * whether it is the first and/or last in the group.
490 * Then if "p" is a valid pointer, it points to the Content
491 * structure of the first partial in the group. So we copy
492 * the file name and/or folder name from that message. In
493 * this case, we also note that we will be appending.
495 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
496 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
498 /* Yep, it's a message/partial */
501 /* But is it the first and/or last in the collection? */
502 if (pm
->pm_partno
== 1)
504 if (pm
->pm_maxno
&& pm
->pm_partno
== pm
->pm_maxno
)
508 * If "p" is a valid pointer, then it points to the
509 * Content structure for the first message in the group.
510 * So we just copy the filename or foldername information
511 * from the previous iteration of this function.
515 ct
->c_storage
= add (p
->c_storage
, NULL
);
517 /* record the folder name */
519 ct
->c_folder
= add (p
->c_folder
, NULL
);
526 * Get storage formatting string.
528 * 1) If we have storeproc defined, then use that
529 * 2) Else check for a mhn-store-<type>/<subtype> entry
530 * 3) Else check for a mhn-store-<type> entry
531 * 4) Else if content is "message", use "+" (current folder)
532 * 5) Else use string "%m%P.%s".
534 if ((cp
= ct
->c_storeproc
) == NULL
|| *cp
== '\0') {
535 CI ci
= &ct
->c_ctinfo
;
537 snprintf (buffer
, sizeof(buffer
), "%s-store-%s/%s",
538 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
539 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
540 snprintf (buffer
, sizeof(buffer
), "%s-store-%s", invo_name
, ci
->ci_type
);
541 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
542 cp
= ct
->c_type
== CT_MESSAGE
? "+" : "%m%P.%s";
548 * Check the beginning of storage formatting string
549 * to see if we are saving content to a folder.
551 if (*cp
== '+' || *cp
== '@') {
552 char *tmpfilenam
, *folder
;
554 /* Store content in temporary file for now */
555 tmpfilenam
= m_mktemp(invo_name
, NULL
, NULL
);
556 ct
->c_storage
= add (tmpfilenam
, NULL
);
558 /* Get the folder name */
560 folder
= pluspath (cp
);
562 folder
= getfolder (1);
564 /* Check if folder exists */
565 create_folder(m_mailpath(folder
), 0, exit
);
567 /* Record the folder name */
568 ct
->c_folder
= add (folder
, NULL
);
577 * Parse and expand the storage formatting string
578 * in `cp' into `buffer'.
580 parse_format_string (ct
, cp
, buffer
, sizeof(buffer
), dir
);
583 * If formatting begins with '|' or '!', then pass
584 * content to standard input of a command and return.
586 if (buffer
[0] == '|' || buffer
[0] == '!')
587 return show_content_aux (ct
, 1, 0, buffer
+ 1, dir
);
589 /* record the filename */
590 if ((ct
->c_storage
= clobber_check (add (buffer
, NULL
))) == NULL
) {
595 /* flush the output stream */
598 /* Now save or append the content to a file */
599 if (output_content_file (ct
, appending
) == NOTOK
)
603 * If necessary, link the file into a folder and remove
604 * the temporary file. If this message is a partial,
605 * then only do this if it is the last one in the group.
607 if (ct
->c_folder
&& (!is_partial
|| last_partial
)) {
608 msgnum
= output_content_folder (ct
->c_folder
, ct
->c_storage
);
609 unlink (ct
->c_storage
);
615 * Now print out the name/number of the message
616 * that we are storing.
620 fprintf (stderr
, "reassembling partials ");
622 fprintf (stderr
, "%s", ct
->c_file
);
624 fprintf (stderr
, "%s,", ct
->c_file
);
626 fprintf (stderr
, "storing message %s", ct
->c_file
);
628 fprintf (stderr
, " part %s", ct
->c_partno
);
632 * Unless we are in the "middle" of group of message/partials,
633 * we now print the name of the file, folder, and/or message
634 * to which we are storing the content.
636 if (!is_partial
|| last_partial
) {
638 fprintf (stderr
, " to folder %s as message %d\n", ct
->c_folder
, msgnum
);
639 } else if (!strcmp(ct
->c_storage
, "-")) {
640 fprintf (stderr
, " to stdout\n");
644 cwdlen
= strlen (cwd
);
645 fprintf (stderr
, " as file %s\n",
646 strncmp (ct
->c_storage
, cwd
, cwdlen
)
647 || ct
->c_storage
[cwdlen
] != '/'
648 ? ct
->c_storage
: ct
->c_storage
+ cwdlen
+ 1);
657 * Output content to a file
661 output_content_file (CT ct
, int appending
)
664 char *file
, buffer
[BUFSIZ
];
669 * If the pathname is absolute, make sure
670 * all the relevant directories exist.
672 if (strchr(ct
->c_storage
, '/')
673 && make_intermediates (ct
->c_storage
) == NOTOK
)
676 if (ct
->c_encoding
!= CE_7BIT
) {
679 if (!ct
->c_ceopenfnx
) {
680 advise (NULL
, "don't know how to decode part %s of message %s",
681 ct
->c_partno
, ct
->c_file
);
685 file
= appending
|| !strcmp (ct
->c_storage
, "-") ? NULL
687 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
689 if (!strcmp (file
, ct
->c_storage
)) {
690 (*ct
->c_ceclosefnx
) (ct
);
695 * Send to standard output
697 if (!strcmp (ct
->c_storage
, "-")) {
700 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
701 advise ("stdout", "unable to dup");
703 (*ct
->c_ceclosefnx
) (ct
);
706 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
707 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
708 appending
? "a" : "w");
716 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
717 advise (ct
->c_storage
, "unable to fopen for %s",
718 appending
? "appending" : "writing");
724 * Filter the header fields of the initial enclosing
725 * message/partial into the file.
727 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
728 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
730 if (pm
->pm_partno
== 1)
731 copy_some_headers (fp
, ct
);
735 switch (cc
= read (fd
, buffer
, sizeof(buffer
))) {
737 advise (file
, "error reading content from");
744 fwrite (buffer
, sizeof(*buffer
), cc
, fp
);
750 (*ct
->c_ceclosefnx
) (ct
);
752 if (cc
!= NOTOK
&& fflush (fp
))
753 advise (ct
->c_storage
, "error writing to");
757 return (cc
!= NOTOK
? OK
: NOTOK
);
760 if (!ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
761 advise (ct
->c_file
, "unable to open for reading");
767 fseek (ct
->c_fp
, pos
, SEEK_SET
);
769 if (!strcmp (ct
->c_storage
, "-")) {
772 if ((gd
= dup (fileno (stdout
))) == NOTOK
) {
773 advise ("stdout", "unable to dup");
776 if ((fp
= fdopen (gd
, appending
? "a" : "w")) == NULL
) {
777 advise ("stdout", "unable to fdopen (%d, \"%s\") from", gd
,
778 appending
? "a" : "w");
783 if ((fp
= fopen (ct
->c_storage
, appending
? "a" : "w")) == NULL
) {
784 advise (ct
->c_storage
, "unable to fopen for %s",
785 appending
? "appending" : "writing");
791 * Copy a few of the header fields of the initial
792 * enclosing message/partial into the file.
795 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_PARTIAL
) {
796 struct partial
*pm
= (struct partial
*) ct
->c_ctparams
;
798 if (pm
->pm_partno
== 1) {
799 copy_some_headers (fp
, ct
);
804 while (fgets (buffer
, sizeof(buffer
) - 1, ct
->c_fp
)) {
805 if ((pos
+= strlen (buffer
)) > last
) {
808 diff
= strlen (buffer
) - (pos
- last
);
813 * If this is the first content of a group of
814 * message/partial contents, then we only copy a few
815 * of the header fields of the enclosed message.
830 if (!uprf (buffer
, XXX_FIELD_PRF
)
831 && !uprf (buffer
, VRSN_FIELD
)
832 && !uprf (buffer
, "Subject:")
833 && !uprf (buffer
, "Encrypted:")
834 && !uprf (buffer
, "Message-ID:")) {
849 advise (ct
->c_storage
, "error writing to");
859 * Add a file to a folder.
861 * Return the new message number of the file
862 * when added to the folder. Return -1, if
867 output_content_folder (char *folder
, char *filename
)
872 /* Read the folder. */
873 if ((mp
= folder_read (folder
, 0))) {
874 /* Link file into folder */
875 msgnum
= folder_addmsg (&mp
, filename
, 0, 0, 0, 0, (char *)0);
877 advise (NULL
, "unable to read folder %s", folder
);
881 /* free folder structure */
885 * Return msgnum. We are relying on the fact that
886 * msgnum will be -1, if folder_addmsg() had an error.
893 * Parse and expand the storage formatting string
894 * pointed to by "cp" into "buffer".
898 parse_format_string (CT ct
, char *cp
, char *buffer
, int buflen
, char *dir
)
902 CI ci
= &ct
->c_ctinfo
;
905 * If storage string is "-", just copy it, and
906 * return (send content to standard output).
908 if (cp
[0] == '-' && cp
[1] == '\0') {
909 strncpy (buffer
, cp
, buflen
);
917 * If formatting string is a pathname that doesn't
918 * begin with '/', then preface the path with the
919 * appropriate directory.
921 if (*cp
!= '/' && *cp
!= '|' && *cp
!= '!') {
922 snprintf (bp
, buflen
, "%s/", dir
[1] ? dir
: "");
930 /* We are processing a storage escape */
935 * Insert parameters from Content-Type.
936 * This is only valid for '|' commands.
938 if (buffer
[0] != '|' && buffer
[0] != '!') {
947 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
;
949 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
959 /* insert message number */
960 snprintf (bp
, buflen
, "%s", r1bindex (ct
->c_file
, '/'));
964 /* insert part number with leading dot */
966 snprintf (bp
, buflen
, ".%s", ct
->c_partno
);
970 /* insert part number withouth leading dot */
972 strncpy (bp
, ct
->c_partno
, buflen
);
976 /* insert content type */
977 strncpy (bp
, ci
->ci_type
, buflen
);
981 /* insert content subtype */
982 strncpy (bp
, ci
->ci_subtype
, buflen
);
986 /* insert the character % */
996 /* Advance bp and decrement buflen */
1014 * Check if the content specifies a filename
1015 * in its MIME parameters.
1019 get_storeproc (CT ct
)
1021 char **ap
, **ep
, *cp
;
1025 * If the storeproc has already been defined,
1026 * we just return (for instance, if this content
1027 * is part of a "message/external".
1029 if (ct
->c_storeproc
)
1033 * If there's a Content-Disposition header and it has a filename,
1034 * use that (RFC-2183).
1037 char *cp
= strchr (ct
->c_dispo
, ';');
1038 CI ci
= calloc (1, sizeof *ci
);
1040 int found_filename
= 0;
1042 if (cp
&& parse_header_attrs (ct
->c_file
, strlen (invo_name
) + 2, &cp
,
1043 ci
, &status
) == OK
) {
1044 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1045 if (! strcasecmp (*ap
, "filename")
1046 && *(cp
= *ep
) != '/'
1050 && !strchr (cp
, '%')) {
1051 ct
->c_storeproc
= add (cp
, NULL
);
1059 if (found_filename
) return;
1063 * Check the attribute/value pairs, for the attribute "name".
1064 * If found, do a few sanity checks and copy the value into
1068 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1069 if (! strcasecmp (*ap
, "name")
1070 && *(cp
= *ep
) != '/'
1074 && !strchr (cp
, '%')) {
1075 ct
->c_storeproc
= add (cp
, NULL
);
1083 * Copy some of the header fields of the initial message/partial
1084 * message into the header of the reassembled message.
1088 copy_some_headers (FILE *out
, CT ct
)
1092 hp
= ct
->c_first_hf
; /* start at first header field */
1096 * A few of the header fields of the enclosing
1097 * messages are not copied.
1099 if (!uprf (hp
->name
, XXX_FIELD_PRF
)
1100 && strcasecmp (hp
->name
, VRSN_FIELD
)
1101 && strcasecmp (hp
->name
, "Subject")
1102 && strcasecmp (hp
->name
, "Encrypted")
1103 && strcasecmp (hp
->name
, "Message-ID"))
1104 fprintf (out
, "%s:%s", hp
->name
, hp
->value
);
1105 hp
= hp
->next
; /* next header field */
1111 /******************************************************************************/
1112 /* -clobber support */
1114 enum clobber_policy_t
{
1122 static enum clobber_policy_t clobber_policy
= NMH_CLOBBER_ALWAYS
;
1124 int files_not_clobbered
= 0;
1127 save_clobber_policy (const char *value
) {
1128 if (! strcasecmp (value
, "always")) {
1129 clobber_policy
= NMH_CLOBBER_ALWAYS
;
1130 } else if (! strcasecmp (value
, "auto")) {
1131 clobber_policy
= NMH_CLOBBER_AUTO
;
1132 } else if (! strcasecmp (value
, "suffix")) {
1133 clobber_policy
= NMH_CLOBBER_SUFFIX
;
1134 } else if (! strcasecmp (value
, "ask")) {
1135 clobber_policy
= NMH_CLOBBER_ASK
;
1136 } else if (! strcasecmp (value
, "never")) {
1137 clobber_policy
= NMH_CLOBBER_NEVER
;
1147 next_version (char *file
, enum clobber_policy_t clobber_policy
) {
1148 const size_t max_versions
= 1000000;
1149 /* 8 = log max_versions + one for - or . + one for null terminator */
1150 const size_t buflen
= strlen (file
) + 8;
1151 char *buffer
= mh_xmalloc (buflen
);
1154 char *extension
= NULL
;
1155 if (clobber_policy
== NMH_CLOBBER_AUTO
&&
1156 ((extension
= strrchr (file
, '.')) != NULL
)) {
1157 *extension
++ = '\0';
1160 for (version
= 1; version
< max_versions
; ++version
) {
1163 switch (clobber_policy
) {
1164 case NMH_CLOBBER_AUTO
: {
1165 snprintf (buffer
, buflen
, "%s-%ld%s%s", file
, (long) version
,
1166 extension
== NULL
? "" : ".",
1167 extension
== NULL
? "" : extension
);
1171 case NMH_CLOBBER_SUFFIX
:
1172 snprintf (buffer
, buflen
, "%s.%ld", file
, (long) version
);
1176 /* Should never get here. */
1177 advise (NULL
, "will not overwrite %s, invalid clobber policy", buffer
);
1179 ++files_not_clobbered
;
1183 /* Actually (try to) create the file here to avoid a race
1184 condition on file naming + creation. This won't solve the
1185 problem with old NFS that doesn't support O_EXCL, though.
1186 Let the umask strip off permissions from 0666 as desired.
1187 That's what fopen () would do if it was creating the file. */
1188 if ((fd
= open (buffer
, O_CREAT
| O_EXCL
,
1189 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
|
1190 S_IROTH
| S_IWOTH
)) >= 0) {
1198 if (version
>= max_versions
) {
1199 advise (NULL
, "will not overwrite %s, too many versions", buffer
);
1202 ++files_not_clobbered
;
1210 clobber_check (char *original_file
) {
1211 /* clobber policy return value
1212 * -------------- ------------
1214 * -auto file-<digits>.extension
1215 * -suffix file.<digits>
1216 * -ask file, 0, or another filename/path
1224 if (clobber_policy
== NMH_CLOBBER_ASK
) {
1225 /* Save cwd for possible use in loop below. */
1228 cwd
= add (original_file
, NULL
);
1229 slash
= strrchr (cwd
, '/');
1234 /* original_file wasn't a full path, which shouldn't happen. */
1242 file
= original_file
;
1245 switch (clobber_policy
) {
1246 case NMH_CLOBBER_ALWAYS
:
1249 case NMH_CLOBBER_SUFFIX
:
1250 case NMH_CLOBBER_AUTO
:
1251 if (stat (file
, &st
) == OK
) {
1252 file
= next_version (original_file
, clobber_policy
);
1256 case NMH_CLOBBER_ASK
:
1257 if (stat (file
, &st
) == OK
) {
1258 enum answers
{ NMH_YES
, NMH_NO
, NMH_RENAME
};
1259 static struct swit answer
[4] = {
1260 { "yes", 0, NMH_YES
}, { "no", 0, NMH_NO
}, { "rename", 0, NMH_RENAME
}, { NULL
, 0, 0 } };
1263 if (isatty (fileno (stdin
))) {
1265 concat ("Overwrite \"", file
, "\" [y/n/rename]? ", NULL
);
1266 ans
= getans (prompt
, answer
);
1269 /* Overwrite, that's what nmh used to do. And warn. */
1270 advise (NULL
, "-clobber ask but no tty, so overwrite %s", file
);
1274 switch ((enum answers
) smatch (*ans
, answer
)) {
1280 ++files_not_clobbered
;
1284 printf ("Enter filename or full path of the new file: ");
1285 if (fgets (buf
, sizeof buf
, stdin
) == NULL
||
1288 ++files_not_clobbered
;
1290 char *newline
= strchr (buf
, '\n');
1298 if (buf
[0] == '/') {
1299 /* Full path, use it. */
1300 file
= add (buf
, NULL
);
1302 /* Relative path. */
1303 file
= cwd
? concat (cwd
, "/", buf
, NULL
) : add (buf
, NULL
);
1313 case NMH_CLOBBER_NEVER
:
1314 if (stat (file
, &st
) == OK
) {
1315 /* Keep count of files that would have been clobbered,
1316 and return that as process exit status. */
1317 advise (NULL
, "will not overwrite %s with -clobber never", file
);
1320 ++files_not_clobbered
;
1325 original_file
= file
;
1326 } while (check_again
);
1335 /* -clobber support */
1336 /******************************************************************************/