xref: /onnv-gate/usr/src/cmd/mail/copymt.c (revision 13093:48f2dbca79a2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*13093SRoger.Faulkner@Oracle.COM  * Common Development and Distribution License (the "License").
6*13093SRoger.Faulkner@Oracle.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*13093SRoger.Faulkner@Oracle.COM 
22*13093SRoger.Faulkner@Oracle.COM /*
23*13093SRoger.Faulkner@Oracle.COM  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
24*13093SRoger.Faulkner@Oracle.COM  */
25*13093SRoger.Faulkner@Oracle.COM 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate     NAME
310Sstevel@tonic-gate 	copymt - copy mail (f1) to temp (f2)
320Sstevel@tonic-gate 
330Sstevel@tonic-gate     SYNOPSIS
340Sstevel@tonic-gate 	void copymt(FILE *f1, FILE *f2)
350Sstevel@tonic-gate 
360Sstevel@tonic-gate     DESCRIPTION
370Sstevel@tonic-gate 	The mail messages in /var/mail are copied into
380Sstevel@tonic-gate 	the temp file. The file pointers f1 and f2 point
390Sstevel@tonic-gate 	to the files, respectively.
400Sstevel@tonic-gate */
410Sstevel@tonic-gate #include "mail.h"
420Sstevel@tonic-gate 
copymt(f1,f2)430Sstevel@tonic-gate void copymt(f1, f2)
440Sstevel@tonic-gate register FILE *f1, *f2;
450Sstevel@tonic-gate {
460Sstevel@tonic-gate 	static char pn[] = "copymt";
470Sstevel@tonic-gate 	long nextadr;
480Sstevel@tonic-gate 	int n, newline = 1;
490Sstevel@tonic-gate 	int StartNewMsg = TRUE;
500Sstevel@tonic-gate 	int ToldUser = FALSE;
510Sstevel@tonic-gate 	int mesg = 0;
520Sstevel@tonic-gate 	int ctf = FALSE; 		/* header continuation flag */
530Sstevel@tonic-gate 	long clen = (long)0;
540Sstevel@tonic-gate 	int hdr = 0;
550Sstevel@tonic-gate 	int cflg = 0;			/* found Content-length in header */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	Dout(pn, 0,"entered\n");
580Sstevel@tonic-gate 	if (!let[1].adr) {
590Sstevel@tonic-gate 		nlet = nextadr = 0;
600Sstevel@tonic-gate 		let[0].adr = 0;
610Sstevel@tonic-gate 		let[0].text = TRUE;	/* until proven otherwise.... */
620Sstevel@tonic-gate 		let[0].change = ' ';
630Sstevel@tonic-gate 	} else {
640Sstevel@tonic-gate 		nextadr = let[nlet].adr;
650Sstevel@tonic-gate 	}
660Sstevel@tonic-gate 
67*13093SRoger.Faulkner@Oracle.COM 	while ((n = getaline(line, sizeof line, f1)) > 0) {
680Sstevel@tonic-gate 		if (!newline) {
690Sstevel@tonic-gate 			goto putout;
700Sstevel@tonic-gate 		} else if ((hdr = isheader (line, &ctf)) == FALSE) {
710Sstevel@tonic-gate 			ctf = FALSE;	/* next line can't be cont. */
720Sstevel@tonic-gate 		}
730Sstevel@tonic-gate 		if (!hdr && cflg) {	/* nonheader, Content-length seen */
740Sstevel@tonic-gate 			if (clen < n) {	/* read too much */
750Sstevel@tonic-gate 				/* NB: this only can happen if the content-length
760Sstevel@tonic-gate 				 * says a smaller number than what's seen on the
770Sstevel@tonic-gate 				 * first non-header line.
780Sstevel@tonic-gate 				 */
790Sstevel@tonic-gate 				if (let[nlet-1].text == TRUE) {
800Sstevel@tonic-gate 					let[nlet-1].text = istext((unsigned char*)line,clen);
810Sstevel@tonic-gate 				    Dout(pn, 0, "1, let[%d].text = %s\n",
820Sstevel@tonic-gate 					   nlet-1,
830Sstevel@tonic-gate 					   (let[nlet-1].text ? "TRUE":"FALSE"));
840Sstevel@tonic-gate 				}
850Sstevel@tonic-gate 				if (fwrite(line,1,(int)clen,f2) != clen) {
860Sstevel@tonic-gate 					fclose(f1); fclose(f2);
870Sstevel@tonic-gate 					errmsg(E_FILE,
880Sstevel@tonic-gate 						"Write error in copymt()");
890Sstevel@tonic-gate 					done(0);
900Sstevel@tonic-gate 				}
910Sstevel@tonic-gate 				nextadr += clen;
920Sstevel@tonic-gate 				n -= clen;
930Sstevel@tonic-gate 				strmove (line, line+clen);
940Sstevel@tonic-gate 				cflg = 0;
950Sstevel@tonic-gate 				ctf = FALSE;
960Sstevel@tonic-gate 				hdr = isheader(line, &ctf);
970Sstevel@tonic-gate 				goto dohdr;
980Sstevel@tonic-gate 			}
990Sstevel@tonic-gate 			/* here, clen >= n */
1000Sstevel@tonic-gate 			if (n == 1 && line[0] == '\n'){	/* leading empty line */
1010Sstevel@tonic-gate 				clen++;		/* cheat */
1020Sstevel@tonic-gate 			}
1030Sstevel@tonic-gate 			nextadr += clen;
1040Sstevel@tonic-gate 			for (;;) {
1050Sstevel@tonic-gate 				if (let[nlet-1].text == TRUE) {
1060Sstevel@tonic-gate 					let[nlet-1].text = istext((unsigned char*)line,n);
1070Sstevel@tonic-gate 				  Dout(pn, 0, "2, let[%d].text = %s\n",
1080Sstevel@tonic-gate 					nlet-1,
1090Sstevel@tonic-gate 					(let[nlet-1].text ? "TRUE" : "FALSE"));
1100Sstevel@tonic-gate 				}
1110Sstevel@tonic-gate 				if (fwrite(line,1,n,f2) != n) {
1120Sstevel@tonic-gate 					fclose(f1); fclose(f2);
1130Sstevel@tonic-gate 					errmsg(E_FILE,
1140Sstevel@tonic-gate 						"Write error in copymt()");
1150Sstevel@tonic-gate 					done(0);
1160Sstevel@tonic-gate 				}
1170Sstevel@tonic-gate 				clen -= n;
1180Sstevel@tonic-gate 				if (clen <= 0) {
1190Sstevel@tonic-gate 					break;
1200Sstevel@tonic-gate 				}
1210Sstevel@tonic-gate 				n = clen < sizeof line ? clen : sizeof line;
1220Sstevel@tonic-gate 				if ((n = fread (line, 1, n, f1)) <= 0) {
1230Sstevel@tonic-gate 				    fprintf(stderr,
1240Sstevel@tonic-gate 		"%c%s:\tYour mailfile was found to be corrupted.\n",
1250Sstevel@tonic-gate 						BELL, program);
1260Sstevel@tonic-gate 				    fprintf(stderr,
1270Sstevel@tonic-gate 					"\t(Unexpected end-of-file).\n");
1280Sstevel@tonic-gate 				    fprintf(stderr,
1290Sstevel@tonic-gate 					"\tMessage #%d may be truncated.%c\n\n",
1300Sstevel@tonic-gate 						nlet, BELL);
1310Sstevel@tonic-gate 					nextadr -= clen;
1320Sstevel@tonic-gate 					clen = 0; /* stop the loop */
1330Sstevel@tonic-gate 				}
1340Sstevel@tonic-gate 			}
1350Sstevel@tonic-gate 			/* All done, go to top for next message */
1360Sstevel@tonic-gate 			cflg = 0;
1370Sstevel@tonic-gate 			StartNewMsg = TRUE;
1380Sstevel@tonic-gate 			continue;
1390Sstevel@tonic-gate 		}
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate dohdr:
1420Sstevel@tonic-gate 		switch (hdr) {
1430Sstevel@tonic-gate 		case H_FROM:
1440Sstevel@tonic-gate 			if(nlet >= (MAXLET-2)) {
1450Sstevel@tonic-gate 				if (!mesg) {
1460Sstevel@tonic-gate 					fprintf(stderr,"%s: Too many letters, overflowing letters concatenated\n\n",program);
1470Sstevel@tonic-gate 					mesg++;
1480Sstevel@tonic-gate 				}
1490Sstevel@tonic-gate 			} else {
1500Sstevel@tonic-gate 				let[nlet++].adr = nextadr;
1510Sstevel@tonic-gate 				let[nlet].text = TRUE;
1520Sstevel@tonic-gate 				let[nlet].change = ' ';
1530Sstevel@tonic-gate 			}
1540Sstevel@tonic-gate 			Dout(pn, 5, "setting StartNewMsg to FALSE\n");
1550Sstevel@tonic-gate 			StartNewMsg = FALSE;
1560Sstevel@tonic-gate 			ToldUser = FALSE;
1570Sstevel@tonic-gate 			break;
1580Sstevel@tonic-gate 		case H_CLEN:
1590Sstevel@tonic-gate 			if (cflg) {
1600Sstevel@tonic-gate 				break;
1610Sstevel@tonic-gate 			}
1620Sstevel@tonic-gate 			cflg = TRUE;	/* mark for clen processing */
1630Sstevel@tonic-gate 			clen = atol (strpbrk (line, ":")+1);
1640Sstevel@tonic-gate 			break;
1650Sstevel@tonic-gate 		default:
1660Sstevel@tonic-gate 			break;
1670Sstevel@tonic-gate 		}
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate putout:
1700Sstevel@tonic-gate 		if (nlet == 0) {
1710Sstevel@tonic-gate 			fclose(f1);
1720Sstevel@tonic-gate 			fclose(f2);
1730Sstevel@tonic-gate 			errmsg(E_FILE,"mailfile does not begin with a 'From' line");
1740Sstevel@tonic-gate 			done(0);
1750Sstevel@tonic-gate 		}
1760Sstevel@tonic-gate 		nextadr += n;
1770Sstevel@tonic-gate 		if (let[nlet-1].text == TRUE) {
1780Sstevel@tonic-gate 			let[nlet-1].text = istext((unsigned char*)line,n);
1790Sstevel@tonic-gate 		        Dout(pn, 5,"3, let[%d].text = %s\n",
1800Sstevel@tonic-gate 				nlet-1, (let[nlet-1].text ? "TRUE" : "FALSE"));
1810Sstevel@tonic-gate 		}
1820Sstevel@tonic-gate 		if (fwrite(line,1,n,f2) != n) {
1830Sstevel@tonic-gate 			fclose(f1);
1840Sstevel@tonic-gate 			fclose(f2);
1850Sstevel@tonic-gate 			errmsg(E_FILE,"Write error in copymt()");
1860Sstevel@tonic-gate 			done(0);
1870Sstevel@tonic-gate 		}
1880Sstevel@tonic-gate 		if (line[n-1] == '\n') {
1890Sstevel@tonic-gate 			newline = 1;
1900Sstevel@tonic-gate 			if (n == 1) { /* Blank line. Skip StartNewMsg */
1910Sstevel@tonic-gate 				      /* check below                  */
1920Sstevel@tonic-gate 				continue;
1930Sstevel@tonic-gate 			}
1940Sstevel@tonic-gate 		} else {
1950Sstevel@tonic-gate 			newline = 0;
1960Sstevel@tonic-gate 		}
1970Sstevel@tonic-gate 		if (StartNewMsg == TRUE && ToldUser == FALSE) {
1980Sstevel@tonic-gate 			fprintf(stderr,
1990Sstevel@tonic-gate 			     "%c%s:\tYour mailfile was found to be corrupted\n",
2000Sstevel@tonic-gate 			     BELL, program);
2010Sstevel@tonic-gate 			fprintf(stderr, "\t(Content-length mismatch).\n");
2020Sstevel@tonic-gate 			fprintf(stderr,"\tMessage #%d may be truncated,\n",
2030Sstevel@tonic-gate 			     nlet);
2040Sstevel@tonic-gate 			fprintf(stderr,
2050Sstevel@tonic-gate 			    "\twith another message concatenated to it.%c\n\n",
2060Sstevel@tonic-gate 			    BELL);
2070Sstevel@tonic-gate 			ToldUser = TRUE;
2080Sstevel@tonic-gate 		}
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	/*
2120Sstevel@tonic-gate 		last plus 1
2130Sstevel@tonic-gate 	*/
2140Sstevel@tonic-gate 	let[nlet].adr = nextadr;
2150Sstevel@tonic-gate 	let[nlet].change = ' ';
2160Sstevel@tonic-gate 	let[nlet].text = TRUE;
2170Sstevel@tonic-gate }
218