+ int status;
+ char buffer[BUFSIZ], file[BUFSIZ];
+ struct stat sts;
+ char *original_draft; /* name of original draft file */
+ char *p; /* string pointer for building file name */
+
+ /*
+ * Save the original name of the draft file. The name of the
+ * draft file is changed to a temporary file containing the built
+ * MIME message if there are attachments. We need the original
+ * name so that it can be renamed after the message is sent.
+ */
+
+ original_draft = drft;
+
+ /*
+ * There might be attachments if a header field name for
+ * attachments is supplied. Convert the draft to a MIME message.
+ * Use the mhbuild composition file for the draft if there was
+ * a successful conversion because that now contains the MIME
+ * message. A nice side effect of this is that it leaves the
+ * original draft file untouched so that it can be retrieved
+ * and modified if desired.
+ */
+
+ if (attachment_header_field_name != (char *)0) {
+ switch (attach(attachment_header_field_name, drft, attachformat)) {
+ case OK:
+ drft = composition_file_name;
+ break;
+
+ case NOTOK:
+ return (NOTOK);
+
+ case DONE:
+ break;
+ }
+ }
+
+ done=armed_done;
+ switch (setjmp (env)) {
+ case OK:
+ /*
+ * If given -push and -unique (which is undocumented), then
+ * rename the draft file. I'm not quite sure why.
+ */
+ if (pushsw && unique) {
+ char *cp = m_mktemp2(drft, invo_name, NULL, NULL);
+ if (cp == NULL) {
+ adios ("sendsbr", "unable to create temporary file");
+ }
+ if (rename (drft, strncpy(file, cp, sizeof(file))) == NOTOK)
+ adios (file, "unable to rename %s to", drft);
+ drft = file;
+ }
+
+ /*
+ * Check if we need to split the message into
+ * multiple messages of type "message/partial".
+ */
+ if (splitsw >= 0 && !distfile && stat (drft, &sts) != NOTOK
+ && sts.st_size >= CPERMSG) {
+ status = splitmsg (vec, vecp, drft, st, splitsw) ? NOTOK : OK;
+ } else {
+ status = sendaux (vec, vecp, drft, st) ? NOTOK : OK;
+ }
+
+ /* rename the original draft */
+ if (rename_drft && status == OK &&
+ rename (original_draft, strncpy (buffer, m_backup (original_draft), sizeof(buffer))) == NOTOK)
+ advise (buffer, "unable to rename %s to", drft);
+ break;
+
+ default:
+ status = DONE;
+ break;
+ }
+
+ done=exit;
+ if (distfile)
+ unlink (distfile);
+
+ /*
+ * Get rid of any temporary files that we created for attachments.
+ * Also get rid of the renamed composition file that mhbuild
+ * leaves as a turd. It looks confusing, but we use the body
+ * file name to help build the renamed composition file name.
+ */
+
+ if (drft == composition_file_name) {
+ clean_up_temporary_files();
+
+ if (strlen(composition_file_name) >= sizeof (composition_file_name) - 6)
+ advise((char *)0, "unable to remove original composition file.");
+
+ else {
+ if ((p = strrchr(composition_file_name, '/')) == (char *)0)
+ p = composition_file_name;
+ else
+ p++;
+
+ (void)strcpy(body_file_name, p);
+ *p++ = ',';
+ (void)strcpy(p, body_file_name);
+ (void)strcat(p, ".orig");
+
+ (void)unlink(composition_file_name);
+ }
+ }
+
+ return status;
+}
+
+static int
+attach(char *attachment_header_field_name, char *draft_file_name,
+ int attachformat)
+{
+ char buf[MAXPATHLEN + 6]; /* miscellaneous buffer */
+ int c; /* current character for body copy */
+ int has_attachment; /* draft has at least one attachment */
+ int has_body; /* draft has a message body */
+ int length; /* length of attachment header field name */
+ char *p; /* miscellaneous string pointer */
+
+ /*
+ * Open up the draft file.
+ */
+
+ if ((draft_file = fopen(draft_file_name, "r")) == (FILE *)0)
+ adios((char *)0, "can't open draft file `%s'.", draft_file_name);
+
+ /*
+ * Allocate a buffer to hold the header components as they're read in.
+ * This buffer might need to be quite large, so we grow it as needed.
+ */
+
+ field = (char *)mh_xmalloc(field_size = 256);
+
+ /*
+ * Scan the draft file for a header field name that matches the
+ * -attach argument. The existence of one indicates that the
+ * draft has attachments. Bail out if there are no attachments
+ * because we're done. Read to the end of the headers even if
+ * we have no attachments.
+ */
+
+ length = strlen(attachment_header_field_name);
+
+ has_attachment = 0;
+
+ while (get_line() != EOF && *field != '\0' && *field != '-')
+ if (strncasecmp(field, attachment_header_field_name, length) == 0 && field[length] == ':')
+ has_attachment = 1;
+
+ if (has_attachment == 0)
+ return (DONE);
+
+ /*
+ * We have at least one attachment. Look for at least one
+ * non-blank line in the body of the message which indicates
+ * content in the body.
+ */
+
+ has_body = 0;
+
+ while (get_line() != EOF) {
+ for (p = field; *p != '\0'; p++) {
+ if (*p != ' ' && *p != '\t') {
+ has_body = 1;
+ break;
+ }
+ }
+
+ if (has_body)
+ break;
+ }
+
+ /*
+ * Make names for the temporary files.
+ */
+
+ (void)strncpy(body_file_name,
+ m_mktemp(m_maildir(invo_name), NULL, NULL),
+ sizeof (body_file_name));
+ (void)strncpy(composition_file_name,
+ m_mktemp(m_maildir(invo_name), NULL, NULL),
+ sizeof (composition_file_name));
+
+ if (has_body)
+ body_file = fopen(body_file_name, "w");
+
+ composition_file = fopen(composition_file_name, "w");
+
+ if ((has_body && body_file == (FILE *)0) || composition_file == (FILE *)0) {
+ clean_up_temporary_files();
+ adios((char *)0, "unable to open all of the temporary files.");
+ }
+
+ /*
+ * Start at the beginning of the draft file. Copy all
+ * non-attachment header fields to the temporary composition file.
+ * Then add the dashed line separator.
+ */
+
+ rewind(draft_file);
+
+ while (get_line() != EOF && *field != '\0' && *field != '-')
+ if (strncasecmp(field, attachment_header_field_name, length) != 0 || field[length] != ':')
+ (void)fprintf(composition_file, "%s\n", field);
+
+ (void)fputs("--------\n", composition_file);