+ int filterstate;
+ char *file, buffer[BUFSIZ];
+ long pos, last;
+ FILE *fp;
+
+ /*
+ ** If the pathname contains directories, make sure
+ ** all of them exist.
+ */
+ if (strchr(ct->c_storage, '/') && make_intermediates(ct->c_storage)
+ == NOTOK)
+ return NOTOK;
+
+ if (ct->c_encoding != CE_7BIT) {
+ int cc, fd;
+
+ if (!ct->c_ceopenfnx) {
+ advise(NULL, "don't know how to decode part %s of message %s", ct->c_partno, ct->c_file);
+ return NOTOK;
+ }
+
+ file = appending || strcmp(ct->c_storage, "-")==0 ?
+ NULL : ct->c_storage;
+ if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
+ return NOTOK;
+ if (strcmp(file, ct->c_storage)==0) {
+ (*ct->c_ceclosefnx) (ct);
+ return OK;
+ }
+
+ /*
+ ** Send to standard output
+ */
+ if (strcmp(ct->c_storage, "-")==0) {
+ int gd;
+
+ if ((gd = dup(fileno(stdout))) == NOTOK) {
+ advise("stdout", "unable to dup");
+losing:
+ (*ct->c_ceclosefnx) (ct);
+ return NOTOK;
+ }
+ if ((fp = fdopen(gd, appending ? "a" : "w")) == NULL) {
+ advise("stdout", "unable to fdopen (%d, \"%s\") from", gd, appending ? "a" : "w");
+ close(gd);
+ goto losing;
+ }
+ } else {
+ /*
+ ** Open output file
+ */
+ if ((fp = fopen(ct->c_storage, appending ? "a" : "w"))
+ == NULL) {
+ advise(ct->c_storage, "unable to fopen for %s",
+ appending ?
+ "appending" : "writing");
+ goto losing;
+ }
+ }
+
+ /*
+ ** Filter the header fields of the initial enclosing
+ ** message/partial into the file.
+ */
+ if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
+ struct partial *pm = (struct partial *) ct->c_ctparams;
+
+ if (pm->pm_partno == 1)
+ copy_some_headers(fp, ct);
+ }
+
+ for (;;) {
+ switch (cc = read(fd, buffer, sizeof(buffer))) {
+ case NOTOK:
+ advise(file, "error reading content from");
+ break;
+
+ case OK:
+ break;
+
+ default:
+ fwrite(buffer, sizeof(*buffer), cc, fp);
+ continue;
+ }
+ break;
+ }
+
+ (*ct->c_ceclosefnx) (ct);
+
+ if (cc != NOTOK && fflush(fp))
+ advise(ct->c_storage, "error writing to");
+
+ fclose(fp);
+
+ return (cc != NOTOK ? OK : NOTOK);
+ }
+
+ if (!ct->c_fp && (ct->c_fp = fopen(ct->c_file, "r")) == NULL) {
+ advise(ct->c_file, "unable to open for reading");
+ return NOTOK;
+ }
+
+ pos = ct->c_begin;
+ last = ct->c_end;
+ fseek(ct->c_fp, pos, SEEK_SET);
+
+ if (strcmp(ct->c_storage, "-")==0) {
+ int gd;
+
+ if ((gd = dup(fileno(stdout))) == NOTOK) {
+ advise("stdout", "unable to dup");
+ return NOTOK;
+ }
+ if ((fp = fdopen(gd, appending ? "a" : "w")) == NULL) {
+ advise("stdout", "unable to fdopen (%d, \"%s\") from",
+ gd, appending ? "a" : "w");
+ close(gd);
+ return NOTOK;
+ }
+ } else {
+ if ((fp = fopen(ct->c_storage, appending ? "a" : "w"))
+ == NULL) {
+ advise(ct->c_storage, "unable to fopen for %s",
+ appending ? "appending" : "writing");
+ return NOTOK;
+ }
+ }
+
+ /*
+ ** Copy a few of the header fields of the initial
+ ** enclosing message/partial into the file.
+ */
+ filterstate = 0;
+ if (ct->c_type == CT_MESSAGE && ct->c_subtype == MESSAGE_PARTIAL) {
+ struct partial *pm = (struct partial *) ct->c_ctparams;
+
+ if (pm->pm_partno == 1) {
+ copy_some_headers(fp, ct);
+ filterstate = 1;
+ }
+ }
+
+ while (fgets(buffer, sizeof(buffer) - 1, ct->c_fp)) {
+ if ((pos += strlen(buffer)) > last) {
+ int diff;
+
+ diff = strlen(buffer) - (pos - last);
+ if (diff >= 0)
+ buffer[diff] = '\0';
+ }
+ /*
+ ** If this is the first content of a group of
+ ** message/partial contents, then we only copy a few
+ ** of the header fields of the enclosed message.
+ */
+ if (filterstate) {
+ switch (buffer[0]) {
+ case ' ':
+ case '\t':
+ if (filterstate < 0)
+ buffer[0] = 0;
+ break;
+
+ case '\n':
+ filterstate = 0;
+ break;
+
+ default:
+ if (!uprf(buffer, XXX_FIELD_PRF) && !uprf(buffer, VRSN_FIELD) && !uprf(buffer, "Subject:") && !uprf(buffer, "Message-ID:")) {
+ filterstate = -1;
+ buffer[0] = 0;
+ break;
+ }
+ filterstate = 1;
+ break;
+ }
+ }
+ fputs(buffer, fp);
+ if (pos >= last)
+ break;
+ }
+
+ if (fflush(fp))
+ advise(ct->c_storage, "error writing to");
+
+ fclose(fp);
+ fclose(ct->c_fp);
+ ct->c_fp = NULL;
+ return OK;
+}
+
+
+/*
+** Add a file to a folder.
+**
+** Return the new message number of the file
+** when added to the folder. Return -1, if
+** there is an error.
+*/
+
+static int
+output_content_folder(char *folder, char *filename)
+{
+ int msgnum;
+ struct msgs *mp;
+
+ /* Read the folder. */
+ if ((mp = folder_read(folder))) {
+ /* Link file into folder */
+ msgnum = folder_addmsg(&mp, filename, 0, 0, 0, 0, NULL);
+ } else {
+ advise(NULL, "unable to read folder %s", folder);
+ return NOTOK;
+ }
+
+ /* free folder structure */
+ folder_free(mp);
+
+ /*
+ ** Return msgnum. We are relying on the fact that
+ ** msgnum will be -1, if folder_addmsg() had an error.
+ */
+ return msgnum;
+}
+
+
+/*
+** Parse and expand the storage formatting string
+** pointed to by "cp" into "buffer".
+*/
+
+static int
+parse_format_string(CT ct, char *cp, char *buffer, int buflen, char *dir)
+{
+ int len;
+ char *bp;
+ CI ci = &ct->c_ctinfo;
+
+ /*
+ ** If storage string is "-", just copy it, and
+ ** return (send content to standard output).
+ */
+ if (cp[0] == '-' && cp[1] == '\0') {
+ strncpy(buffer, cp, buflen);
+ return 0;
+ }
+
+ bp = buffer;
+ bp[0] = '\0';
+
+ /*
+ ** If formatting string is a pathname that doesn't
+ ** begin with '/', then preface the path with the
+ ** appropriate directory.
+ */
+ if (*cp != '/' && *cp != '|' && *cp != '!') {
+ snprintf(bp, buflen, "%s/", dir[1] ? dir : "");
+ len = strlen(bp);
+ bp += len;
+ buflen -= len;
+ }
+
+ for (; *cp; cp++) {
+
+ /* We are processing a storage escape */
+ if (*cp == '%') {
+ switch (*++cp) {
+ case 'a':
+ /*
+ ** Insert parameters from Content-Type.
+ ** This is only valid for '|' commands.
+ */
+ if (buffer[0] != '|' && buffer[0] != '!') {
+ *bp++ = *--cp;
+ *bp = '\0';
+ buflen--;
+ continue;
+ } else {
+ char **ap, **ep;
+ char *s = "";
+
+ for (ap=ci->ci_attrs, ep=ci->ci_values;
+ *ap; ap++, ep++) {
+ snprintf(bp, buflen,
+ "%s%s=\"%s\"",
+ s, *ap, *ep);
+ len = strlen(bp);
+ bp += len;
+ buflen -= len;
+ s = " ";
+ }
+ }
+ break;
+
+ case 'm':
+ /* insert message number */
+ snprintf(bp, buflen, "%s",
+ mhbasename(ct->c_file));
+ break;
+
+ case 'P':
+ /* insert part number with leading dot */
+ if (ct->c_partno)
+ snprintf(bp, buflen, ".%s",
+ ct->c_partno);
+ break;
+
+ case 'p':
+ /* insert part number withouth leading dot */
+ if (ct->c_partno)
+ strncpy(bp, ct->c_partno, buflen);
+ break;
+
+ case 't':
+ /* insert content type */
+ strncpy(bp, ci->ci_type, buflen);
+ break;
+
+ case 's':
+ /* insert content subtype */
+ strncpy(bp, ci->ci_subtype, buflen);
+ break;
+
+ case '%':
+ /* insert the character % */
+ goto raw;
+
+ default:
+ *bp++ = *--cp;
+ *bp = '\0';
+ buflen--;
+ continue;
+ }
+
+ /* Advance bp and decrement buflen */
+ len = strlen(bp);
+ bp += len;
+ buflen -= len;
+
+ } else {
+raw:
+ *bp++ = *cp;
+ *bp = '\0';
+ buflen--;
+ }
+ }
+
+ return 0;
+}
+
+
+/*
+** Copy some of the header fields of the initial message/partial
+** message into the header of the reassembled message.
+*/
+
+static int
+copy_some_headers(FILE *out, CT ct)
+{
+ HF hp;
+
+ hp = ct->c_first_hf; /* start at first header field */
+
+ while (hp) {
+ /*
+ ** A few of the header fields of the enclosing
+ ** messages are not copied.
+ */
+ if (!uprf(hp->name, XXX_FIELD_PRF) &&
+ mh_strcasecmp(hp->name, VRSN_FIELD) &&
+ mh_strcasecmp(hp->name, "Subject") &&
+ mh_strcasecmp(hp->name, "Message-ID"))
+ fprintf(out, "%s:%s", hp->name, hp->value);
+ hp = hp->next; /* next header field */
+ }
+
+ return OK;