xref: /titanic_52/usr/src/cmd/mail/copymt.c (revision 23a1ccea6aac035f084a7a4cdc968687d1b02daf)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*23a1cceaSRoger A. Faulkner  * Common Development and Distribution License (the "License").
6*23a1cceaSRoger A. Faulkner  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*23a1cceaSRoger A. Faulkner 
22*23a1cceaSRoger A. Faulkner /*
23*23a1cceaSRoger A. Faulkner  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
24*23a1cceaSRoger A. Faulkner  */
25*23a1cceaSRoger A. Faulkner 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate     NAME
317c478bd9Sstevel@tonic-gate 	copymt - copy mail (f1) to temp (f2)
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate     SYNOPSIS
347c478bd9Sstevel@tonic-gate 	void copymt(FILE *f1, FILE *f2)
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate     DESCRIPTION
377c478bd9Sstevel@tonic-gate 	The mail messages in /var/mail are copied into
387c478bd9Sstevel@tonic-gate 	the temp file. The file pointers f1 and f2 point
397c478bd9Sstevel@tonic-gate 	to the files, respectively.
407c478bd9Sstevel@tonic-gate */
417c478bd9Sstevel@tonic-gate #include "mail.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate void copymt(f1, f2)
447c478bd9Sstevel@tonic-gate register FILE *f1, *f2;
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate 	static char pn[] = "copymt";
477c478bd9Sstevel@tonic-gate 	long nextadr;
487c478bd9Sstevel@tonic-gate 	int n, newline = 1;
497c478bd9Sstevel@tonic-gate 	int StartNewMsg = TRUE;
507c478bd9Sstevel@tonic-gate 	int ToldUser = FALSE;
517c478bd9Sstevel@tonic-gate 	int mesg = 0;
527c478bd9Sstevel@tonic-gate 	int ctf = FALSE; 		/* header continuation flag */
537c478bd9Sstevel@tonic-gate 	long clen = (long)0;
547c478bd9Sstevel@tonic-gate 	int hdr = 0;
557c478bd9Sstevel@tonic-gate 	int cflg = 0;			/* found Content-length in header */
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	Dout(pn, 0,"entered\n");
587c478bd9Sstevel@tonic-gate 	if (!let[1].adr) {
597c478bd9Sstevel@tonic-gate 		nlet = nextadr = 0;
607c478bd9Sstevel@tonic-gate 		let[0].adr = 0;
617c478bd9Sstevel@tonic-gate 		let[0].text = TRUE;	/* until proven otherwise.... */
627c478bd9Sstevel@tonic-gate 		let[0].change = ' ';
637c478bd9Sstevel@tonic-gate 	} else {
647c478bd9Sstevel@tonic-gate 		nextadr = let[nlet].adr;
657c478bd9Sstevel@tonic-gate 	}
667c478bd9Sstevel@tonic-gate 
67*23a1cceaSRoger A. Faulkner 	while ((n = getaline(line, sizeof line, f1)) > 0) {
687c478bd9Sstevel@tonic-gate 		if (!newline) {
697c478bd9Sstevel@tonic-gate 			goto putout;
707c478bd9Sstevel@tonic-gate 		} else if ((hdr = isheader (line, &ctf)) == FALSE) {
717c478bd9Sstevel@tonic-gate 			ctf = FALSE;	/* next line can't be cont. */
727c478bd9Sstevel@tonic-gate 		}
737c478bd9Sstevel@tonic-gate 		if (!hdr && cflg) {	/* nonheader, Content-length seen */
747c478bd9Sstevel@tonic-gate 			if (clen < n) {	/* read too much */
757c478bd9Sstevel@tonic-gate 				/* NB: this only can happen if the content-length
767c478bd9Sstevel@tonic-gate 				 * says a smaller number than what's seen on the
777c478bd9Sstevel@tonic-gate 				 * first non-header line.
787c478bd9Sstevel@tonic-gate 				 */
797c478bd9Sstevel@tonic-gate 				if (let[nlet-1].text == TRUE) {
807c478bd9Sstevel@tonic-gate 					let[nlet-1].text = istext((unsigned char*)line,clen);
817c478bd9Sstevel@tonic-gate 				    Dout(pn, 0, "1, let[%d].text = %s\n",
827c478bd9Sstevel@tonic-gate 					   nlet-1,
837c478bd9Sstevel@tonic-gate 					   (let[nlet-1].text ? "TRUE":"FALSE"));
847c478bd9Sstevel@tonic-gate 				}
857c478bd9Sstevel@tonic-gate 				if (fwrite(line,1,(int)clen,f2) != clen) {
867c478bd9Sstevel@tonic-gate 					fclose(f1); fclose(f2);
877c478bd9Sstevel@tonic-gate 					errmsg(E_FILE,
887c478bd9Sstevel@tonic-gate 						"Write error in copymt()");
897c478bd9Sstevel@tonic-gate 					done(0);
907c478bd9Sstevel@tonic-gate 				}
917c478bd9Sstevel@tonic-gate 				nextadr += clen;
927c478bd9Sstevel@tonic-gate 				n -= clen;
937c478bd9Sstevel@tonic-gate 				strmove (line, line+clen);
947c478bd9Sstevel@tonic-gate 				cflg = 0;
957c478bd9Sstevel@tonic-gate 				ctf = FALSE;
967c478bd9Sstevel@tonic-gate 				hdr = isheader(line, &ctf);
977c478bd9Sstevel@tonic-gate 				goto dohdr;
987c478bd9Sstevel@tonic-gate 			}
997c478bd9Sstevel@tonic-gate 			/* here, clen >= n */
1007c478bd9Sstevel@tonic-gate 			if (n == 1 && line[0] == '\n'){	/* leading empty line */
1017c478bd9Sstevel@tonic-gate 				clen++;		/* cheat */
1027c478bd9Sstevel@tonic-gate 			}
1037c478bd9Sstevel@tonic-gate 			nextadr += clen;
1047c478bd9Sstevel@tonic-gate 			for (;;) {
1057c478bd9Sstevel@tonic-gate 				if (let[nlet-1].text == TRUE) {
1067c478bd9Sstevel@tonic-gate 					let[nlet-1].text = istext((unsigned char*)line,n);
1077c478bd9Sstevel@tonic-gate 				  Dout(pn, 0, "2, let[%d].text = %s\n",
1087c478bd9Sstevel@tonic-gate 					nlet-1,
1097c478bd9Sstevel@tonic-gate 					(let[nlet-1].text ? "TRUE" : "FALSE"));
1107c478bd9Sstevel@tonic-gate 				}
1117c478bd9Sstevel@tonic-gate 				if (fwrite(line,1,n,f2) != n) {
1127c478bd9Sstevel@tonic-gate 					fclose(f1); fclose(f2);
1137c478bd9Sstevel@tonic-gate 					errmsg(E_FILE,
1147c478bd9Sstevel@tonic-gate 						"Write error in copymt()");
1157c478bd9Sstevel@tonic-gate 					done(0);
1167c478bd9Sstevel@tonic-gate 				}
1177c478bd9Sstevel@tonic-gate 				clen -= n;
1187c478bd9Sstevel@tonic-gate 				if (clen <= 0) {
1197c478bd9Sstevel@tonic-gate 					break;
1207c478bd9Sstevel@tonic-gate 				}
1217c478bd9Sstevel@tonic-gate 				n = clen < sizeof line ? clen : sizeof line;
1227c478bd9Sstevel@tonic-gate 				if ((n = fread (line, 1, n, f1)) <= 0) {
1237c478bd9Sstevel@tonic-gate 				    fprintf(stderr,
1247c478bd9Sstevel@tonic-gate 		"%c%s:\tYour mailfile was found to be corrupted.\n",
1257c478bd9Sstevel@tonic-gate 						BELL, program);
1267c478bd9Sstevel@tonic-gate 				    fprintf(stderr,
1277c478bd9Sstevel@tonic-gate 					"\t(Unexpected end-of-file).\n");
1287c478bd9Sstevel@tonic-gate 				    fprintf(stderr,
1297c478bd9Sstevel@tonic-gate 					"\tMessage #%d may be truncated.%c\n\n",
1307c478bd9Sstevel@tonic-gate 						nlet, BELL);
1317c478bd9Sstevel@tonic-gate 					nextadr -= clen;
1327c478bd9Sstevel@tonic-gate 					clen = 0; /* stop the loop */
1337c478bd9Sstevel@tonic-gate 				}
1347c478bd9Sstevel@tonic-gate 			}
1357c478bd9Sstevel@tonic-gate 			/* All done, go to top for next message */
1367c478bd9Sstevel@tonic-gate 			cflg = 0;
1377c478bd9Sstevel@tonic-gate 			StartNewMsg = TRUE;
1387c478bd9Sstevel@tonic-gate 			continue;
1397c478bd9Sstevel@tonic-gate 		}
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate dohdr:
1427c478bd9Sstevel@tonic-gate 		switch (hdr) {
1437c478bd9Sstevel@tonic-gate 		case H_FROM:
1447c478bd9Sstevel@tonic-gate 			if(nlet >= (MAXLET-2)) {
1457c478bd9Sstevel@tonic-gate 				if (!mesg) {
1467c478bd9Sstevel@tonic-gate 					fprintf(stderr,"%s: Too many letters, overflowing letters concatenated\n\n",program);
1477c478bd9Sstevel@tonic-gate 					mesg++;
1487c478bd9Sstevel@tonic-gate 				}
1497c478bd9Sstevel@tonic-gate 			} else {
1507c478bd9Sstevel@tonic-gate 				let[nlet++].adr = nextadr;
1517c478bd9Sstevel@tonic-gate 				let[nlet].text = TRUE;
1527c478bd9Sstevel@tonic-gate 				let[nlet].change = ' ';
1537c478bd9Sstevel@tonic-gate 			}
1547c478bd9Sstevel@tonic-gate 			Dout(pn, 5, "setting StartNewMsg to FALSE\n");
1557c478bd9Sstevel@tonic-gate 			StartNewMsg = FALSE;
1567c478bd9Sstevel@tonic-gate 			ToldUser = FALSE;
1577c478bd9Sstevel@tonic-gate 			break;
1587c478bd9Sstevel@tonic-gate 		case H_CLEN:
1597c478bd9Sstevel@tonic-gate 			if (cflg) {
1607c478bd9Sstevel@tonic-gate 				break;
1617c478bd9Sstevel@tonic-gate 			}
1627c478bd9Sstevel@tonic-gate 			cflg = TRUE;	/* mark for clen processing */
1637c478bd9Sstevel@tonic-gate 			clen = atol (strpbrk (line, ":")+1);
1647c478bd9Sstevel@tonic-gate 			break;
1657c478bd9Sstevel@tonic-gate 		default:
1667c478bd9Sstevel@tonic-gate 			break;
1677c478bd9Sstevel@tonic-gate 		}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate putout:
1707c478bd9Sstevel@tonic-gate 		if (nlet == 0) {
1717c478bd9Sstevel@tonic-gate 			fclose(f1);
1727c478bd9Sstevel@tonic-gate 			fclose(f2);
1737c478bd9Sstevel@tonic-gate 			errmsg(E_FILE,"mailfile does not begin with a 'From' line");
1747c478bd9Sstevel@tonic-gate 			done(0);
1757c478bd9Sstevel@tonic-gate 		}
1767c478bd9Sstevel@tonic-gate 		nextadr += n;
1777c478bd9Sstevel@tonic-gate 		if (let[nlet-1].text == TRUE) {
1787c478bd9Sstevel@tonic-gate 			let[nlet-1].text = istext((unsigned char*)line,n);
1797c478bd9Sstevel@tonic-gate 		        Dout(pn, 5,"3, let[%d].text = %s\n",
1807c478bd9Sstevel@tonic-gate 				nlet-1, (let[nlet-1].text ? "TRUE" : "FALSE"));
1817c478bd9Sstevel@tonic-gate 		}
1827c478bd9Sstevel@tonic-gate 		if (fwrite(line,1,n,f2) != n) {
1837c478bd9Sstevel@tonic-gate 			fclose(f1);
1847c478bd9Sstevel@tonic-gate 			fclose(f2);
1857c478bd9Sstevel@tonic-gate 			errmsg(E_FILE,"Write error in copymt()");
1867c478bd9Sstevel@tonic-gate 			done(0);
1877c478bd9Sstevel@tonic-gate 		}
1887c478bd9Sstevel@tonic-gate 		if (line[n-1] == '\n') {
1897c478bd9Sstevel@tonic-gate 			newline = 1;
1907c478bd9Sstevel@tonic-gate 			if (n == 1) { /* Blank line. Skip StartNewMsg */
1917c478bd9Sstevel@tonic-gate 				      /* check below                  */
1927c478bd9Sstevel@tonic-gate 				continue;
1937c478bd9Sstevel@tonic-gate 			}
1947c478bd9Sstevel@tonic-gate 		} else {
1957c478bd9Sstevel@tonic-gate 			newline = 0;
1967c478bd9Sstevel@tonic-gate 		}
1977c478bd9Sstevel@tonic-gate 		if (StartNewMsg == TRUE && ToldUser == FALSE) {
1987c478bd9Sstevel@tonic-gate 			fprintf(stderr,
1997c478bd9Sstevel@tonic-gate 			     "%c%s:\tYour mailfile was found to be corrupted\n",
2007c478bd9Sstevel@tonic-gate 			     BELL, program);
2017c478bd9Sstevel@tonic-gate 			fprintf(stderr, "\t(Content-length mismatch).\n");
2027c478bd9Sstevel@tonic-gate 			fprintf(stderr,"\tMessage #%d may be truncated,\n",
2037c478bd9Sstevel@tonic-gate 			     nlet);
2047c478bd9Sstevel@tonic-gate 			fprintf(stderr,
2057c478bd9Sstevel@tonic-gate 			    "\twith another message concatenated to it.%c\n\n",
2067c478bd9Sstevel@tonic-gate 			    BELL);
2077c478bd9Sstevel@tonic-gate 			ToldUser = TRUE;
2087c478bd9Sstevel@tonic-gate 		}
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	/*
2127c478bd9Sstevel@tonic-gate 		last plus 1
2137c478bd9Sstevel@tonic-gate 	*/
2147c478bd9Sstevel@tonic-gate 	let[nlet].adr = nextadr;
2157c478bd9Sstevel@tonic-gate 	let[nlet].change = ' ';
2167c478bd9Sstevel@tonic-gate 	let[nlet].text = TRUE;
2177c478bd9Sstevel@tonic-gate }
218