- pos = (long) lseek (md, (off_t) 0, SEEK_CUR);
- size = 0;
-
- switch (mbx_style) {
- case MMDF_FORMAT:
- default:
- j = strlen (mmdlm1);
- if (write (md, mmdlm1, j) != j)
- return NOTOK;
- start = lseek (md, (off_t) 0, SEEK_CUR);
-
- if (text) {
- i = strlen (text);
- if (write (md, text, i) != i)
- return NOTOK;
- for (cp = text; *cp++; size++)
- if (*cp == '\n')
- size++;
- }
-
- while ((i = read (fd, buffer, sizeof(buffer))) > 0) {
- for (j = 0;
- (j = stringdex (mmdlm1, buffer)) >= 0;
- buffer[j]++)
- continue;
- for (j = 0;
- (j = stringdex (mmdlm2, buffer)) >= 0;
- buffer[j]++)
- continue;
- if (write (md, buffer, i) != i)
- return NOTOK;
- if (mapping)
- for (cp = buffer; i-- > 0; size++)
- if (*cp++ == '\n')
- size++;
- }
-
- stop = lseek (md, (off_t) 0, SEEK_CUR);
- j = strlen (mmdlm2);
- if (write (md, mmdlm2, j) != j)
- return NOTOK;
- if (mapping)
- map_write (mailbox, md, 0, (long) 0, start, stop, pos, size, noisy);
-
- return (i != NOTOK ? OK : NOTOK);
-
- case MBOX_FORMAT:
- if ((j = dup (fd)) == NOTOK)
- return NOTOK;
- if ((fp = fdopen (j, "r")) == NULL) {
- close (j);
- return NOTOK;
- }
- start = lseek (md, (off_t) 0, SEEK_CUR);
-
- /* If text is given, we add it to top of message */
- if (text) {
- i = strlen (text);
- if (write (md, text, i) != i)
- return NOTOK;
- for (cp = text; *cp++; size++)
- if (*cp == '\n')
- size++;
- }
-
- for (j = 0; fgets (buffer, sizeof(buffer), fp) != NULL; j++) {
-
- /*
- * Check the first line, and make some changes.
- */
- if (j == 0 && !text) {
- /*
- * Change the "Return-Path:" field (if in first line)
- * back to "From ".
- */
- if (!strncmp (buffer, "Return-Path:", 12)) {
- char tmpbuffer[BUFSIZ];
- char *tp, *ep, *fp;
-
- strncpy(tmpbuffer, buffer, sizeof(tmpbuffer));
- ep = tmpbuffer + 13;
- if (!(fp = strchr(ep + 1, ' ')))
- fp = strchr(ep + 1, '\n');
- tp = dctime(dlocaltimenow());
- snprintf (buffer, sizeof(buffer), "From %.*s %s",
- (int)(fp - ep), ep, tp);
- } else if (!strncmp (buffer, "X-Envelope-From:", 16)) {
- /*
- * Change the "X-Envelope-From:" field
- * (if first line) back to "From ".
- */
- char tmpbuffer[BUFSIZ];
- char *ep;
-
- strncpy(tmpbuffer, buffer, sizeof(tmpbuffer));
- ep = tmpbuffer + 17;
- snprintf (buffer, sizeof(buffer), "From %s", ep);
- } else if (strncmp (buffer, "From ", 5)) {
- /*
- * If there is already a "From " line,
- * then leave it alone. Else we add one.
- */
- char tmpbuffer[BUFSIZ];
- char *tp, *ep;
-
- strncpy(tmpbuffer, buffer, sizeof(tmpbuffer));
- ep = "nobody@nowhere";
- tp = dctime(dlocaltimenow());
- snprintf (buffer, sizeof(buffer), "From %s %s%s", ep, tp, tmpbuffer);
- }
- }
-
- /*
- * If this is not first line, and begins with
- * "From ", then prepend line with ">".
- */
- if (j != 0 && strncmp (buffer, "From ", 5) == 0) {
- write (md, ">", 1);
- size++;
- }
- i = strlen (buffer);
- if (write (md, buffer, i) != i) {
- fclose (fp);
- return NOTOK;
- }
- if (mapping)
- for (cp = buffer; i-- > 0; size++)
- if (*cp++ == '\n')
- size++;
- }
- if (write (md, "\n", 1) != 1) {
- fclose (fp);
- return NOTOK;
- }
- if (mapping)
- size += 2;
-
- fclose (fp);
- lseek (fd, (off_t) 0, SEEK_END);
- stop = lseek (md, (off_t) 0, SEEK_CUR);
- if (mapping)
- map_write (mailbox, md, 0, (long) 0, start, stop, pos, size, noisy);
-
- return OK;
- }
-}
-