#include "mail.h"
void copymt(f1, f2)
register FILE *f1, *f2;
{
static char pn[] = "copymt";
long nextadr;
int n, newline = 1;
int StartNewMsg = TRUE;
int ToldUser = FALSE;
int mesg = 0;
int ctf = FALSE;
long clen = (long)0;
int hdr = 0;
int cflg = 0;
Dout(pn, 0,"entered\n");
if (!let[1].adr) {
nlet = nextadr = 0;
let[0].adr = 0;
let[0].text = TRUE;
let[0].change = ' ';
} else {
nextadr = let[nlet].adr;
}
while ((n = getaline(line, sizeof line, f1)) > 0) {
if (!newline) {
goto putout;
} else if ((hdr = isheader (line, &ctf)) == FALSE) {
ctf = FALSE;
}
if (!hdr && cflg) {
if (clen < n) {
if (let[nlet-1].text == TRUE) {
let[nlet-1].text = istext((unsigned char*)line,clen);
Dout(pn, 0, "1, let[%d].text = %s\n",
nlet-1,
(let[nlet-1].text ? "TRUE":"FALSE"));
}
if (fwrite(line,1,(int)clen,f2) != clen) {
fclose(f1); fclose(f2);
errmsg(E_FILE,
"Write error in copymt()");
done(0);
}
nextadr += clen;
n -= clen;
strmove (line, line+clen);
cflg = 0;
ctf = FALSE;
hdr = isheader(line, &ctf);
goto dohdr;
}
if (n == 1 && line[0] == '\n'){
clen++;
}
nextadr += clen;
for (;;) {
if (let[nlet-1].text == TRUE) {
let[nlet-1].text = istext((unsigned char*)line,n);
Dout(pn, 0, "2, let[%d].text = %s\n",
nlet-1,
(let[nlet-1].text ? "TRUE" : "FALSE"));
}
if (fwrite(line,1,n,f2) != n) {
fclose(f1); fclose(f2);
errmsg(E_FILE,
"Write error in copymt()");
done(0);
}
clen -= n;
if (clen <= 0) {
break;
}
n = clen < sizeof line ? clen : sizeof line;
if ((n = fread (line, 1, n, f1)) <= 0) {
fprintf(stderr,
"%c%s:\tYour mailfile was found to be corrupted.\n",
BELL, program);
fprintf(stderr,
"\t(Unexpected end-of-file).\n");
fprintf(stderr,
"\tMessage #%d may be truncated.%c\n\n",
nlet, BELL);
nextadr -= clen;
clen = 0;
}
}
cflg = 0;
StartNewMsg = TRUE;
continue;
}
dohdr:
switch (hdr) {
case H_FROM:
if(nlet >= (MAXLET-2)) {
if (!mesg) {
fprintf(stderr,"%s: Too many letters, overflowing letters concatenated\n\n",program);
mesg++;
}
} else {
let[nlet++].adr = nextadr;
let[nlet].text = TRUE;
let[nlet].change = ' ';
}
Dout(pn, 5, "setting StartNewMsg to FALSE\n");
StartNewMsg = FALSE;
ToldUser = FALSE;
break;
case H_CLEN:
if (cflg) {
break;
}
cflg = TRUE;
clen = atol (strpbrk (line, ":")+1);
break;
default:
break;
}
putout:
if (nlet == 0) {
fclose(f1);
fclose(f2);
errmsg(E_FILE,"mailfile does not begin with a 'From' line");
done(0);
}
nextadr += n;
if (let[nlet-1].text == TRUE) {
let[nlet-1].text = istext((unsigned char*)line,n);
Dout(pn, 5,"3, let[%d].text = %s\n",
nlet-1, (let[nlet-1].text ? "TRUE" : "FALSE"));
}
if (fwrite(line,1,n,f2) != n) {
fclose(f1);
fclose(f2);
errmsg(E_FILE,"Write error in copymt()");
done(0);
}
if (line[n-1] == '\n') {
newline = 1;
if (n == 1) {
continue;
}
} else {
newline = 0;
}
if (StartNewMsg == TRUE && ToldUser == FALSE) {
fprintf(stderr,
"%c%s:\tYour mailfile was found to be corrupted\n",
BELL, program);
fprintf(stderr, "\t(Content-length mismatch).\n");
fprintf(stderr,"\tMessage #%d may be truncated,\n",
nlet);
fprintf(stderr,
"\twith another message concatenated to it.%c\n\n",
BELL);
ToldUser = TRUE;
}
}
let[nlet].adr = nextadr;
let[nlet].change = ' ';
let[nlet].text = TRUE;
}