+ cp = (!(cp = strrchr(file_name, '/'))) ? file_name : cp + 1;
+ fprintf(composition_file,
+ "#%s; name=\"%s\" <> [%s] {attachment} %s\n",
+ content_type, cp, cp, file_name);
+
+ return;
+}
+
+/*
+** The back-end of the message sending back-end.
+** Annotate original message, and call `spost' to send message.
+*/
+static int
+sendaux(char **vec, int vecp, char *drft, struct stat *st)
+{
+ pid_t child_id;
+ int status;
+ char backup[BUFSIZ];
+
+ vec[vecp++] = drft;
+ if (distfile && distout(drft, distfile, backup) == NOTOK) {
+ return DONE;
+ }
+ vec[vecp] = NULL;
+
+ switch (child_id = fork()) {
+ case -1:
+ /* oops -- fork error */
+ advise("fork", "unable to");
+ return DONE;
+
+ case 0:
+ /* child process -- send it */
+ execvp(*vec, vec);
+ fprintf(stderr, "unable to exec ");
+ perror(*vec);
+ _exit(-1);
+ break; /* NOT REACHED */
+
+ default:
+ /* parent process -- wait for it */
+ status = pidwait(child_id, NOTOK);
+ if (WIFEXITED(status) && WEXITSTATUS(status) == EX_OK) {
+ if (annotext) {
+ anno(st);
+ }
+ } else {
+ /* spost failed */
+ advise(NULL, "%s", strexit(status));
+ if (distfile) {
+ unlink(drft);
+ if (rename(backup, drft) == NOTOK) {
+ advise(drft, "unable to rename %s to",
+ backup);
+ }
+ }
+ }
+ }
+
+
+ return status ? NOTOK : status;
+}
+
+
+static void
+anno(struct stat *st)
+{
+ struct stat st2;
+ char *msgs, *folder;
+ char buf[BUFSIZ];
+ char *vec[MAXARGS];
+ int vecp = 0;
+ char *cp, *dp;
+
+ if (altmsg && (stat(altmsg, &st2) == NOTOK ||
+ st->st_mtime != st2.st_mtime ||
+ st->st_dev != st2.st_dev ||
+ st->st_ino != st2.st_ino)) {
+ if (debugsw) {
+ admonish(NULL, "$mhaltmsg mismatch");
+ }
+ return;
+ }
+
+ if (!(folder = getenv("mhfolder")) || !*folder) {
+ if (debugsw) {
+ admonish(NULL, "$mhfolder not set");
+ }
+ return;
+ }
+ if (!(msgs = getenv("mhmessages")) || !*msgs) {
+ if (debugsw) {
+ admonish(NULL, "$mhmessages not set");
+ }
+ return;
+ }
+ if (debugsw) {
+ advise(NULL, "annotate as `%s': %s %s", annotext,
+ folder, msgs);
+ }
+ vec[vecp++] = "anno";
+ vec[vecp++] = "-comp";
+ vec[vecp++] = annotext;
+ snprintf(buf, sizeof buf, "+%s", folder);
+ vec[vecp++] = buf;
+
+ while (isspace(*msgs)) {
+ msgs++;
+ }
+ for (cp=dp=msgs; *cp; cp++) {
+ if (isspace(*cp)) {
+ while (isspace(*cp)) {
+ *cp++ = '\0';
+ }
+ vec[vecp++] = dp;
+ dp = cp;
+ }
+ }
+ vec[vecp++] = dp;
+ vec[vecp] = NULL;
+ if (execprog(*vec, vec) != 0) {
+ advise(NULL, "unable to annotate");
+ }
+}
+
+
+char*
+strexit(int status)
+{
+ if (WIFSIGNALED(status)) {
+ return "spost or sendmail killed by signal";
+ }
+ if (!WIFEXITED(status)) {
+ return "message not delivered to anyone";
+ }
+ switch (WEXITSTATUS(status)) {
+ case EX_TEMPFAIL:
+ return "Temporary error, maybe the MTA has queued the message";
+ default:
+ return "message not delivered to anyone";
+ }