xref: /titanic_53/usr/src/cmd/mail/copymt.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI" 	/* SVr4.0 2.1	*/
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate     NAME
29*7c478bd9Sstevel@tonic-gate 	copymt - copy mail (f1) to temp (f2)
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate     SYNOPSIS
32*7c478bd9Sstevel@tonic-gate 	void copymt(FILE *f1, FILE *f2)
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate     DESCRIPTION
35*7c478bd9Sstevel@tonic-gate 	The mail messages in /var/mail are copied into
36*7c478bd9Sstevel@tonic-gate 	the temp file. The file pointers f1 and f2 point
37*7c478bd9Sstevel@tonic-gate 	to the files, respectively.
38*7c478bd9Sstevel@tonic-gate */
39*7c478bd9Sstevel@tonic-gate #include "mail.h"
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate void copymt(f1, f2)
42*7c478bd9Sstevel@tonic-gate register FILE *f1, *f2;
43*7c478bd9Sstevel@tonic-gate {
44*7c478bd9Sstevel@tonic-gate 	static char pn[] = "copymt";
45*7c478bd9Sstevel@tonic-gate 	long nextadr;
46*7c478bd9Sstevel@tonic-gate 	int n, newline = 1;
47*7c478bd9Sstevel@tonic-gate 	int StartNewMsg = TRUE;
48*7c478bd9Sstevel@tonic-gate 	int ToldUser = FALSE;
49*7c478bd9Sstevel@tonic-gate 	int mesg = 0;
50*7c478bd9Sstevel@tonic-gate 	int ctf = FALSE; 		/* header continuation flag */
51*7c478bd9Sstevel@tonic-gate 	long clen = (long)0;
52*7c478bd9Sstevel@tonic-gate 	int hdr = 0;
53*7c478bd9Sstevel@tonic-gate 	int cflg = 0;			/* found Content-length in header */
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	Dout(pn, 0,"entered\n");
56*7c478bd9Sstevel@tonic-gate 	if (!let[1].adr) {
57*7c478bd9Sstevel@tonic-gate 		nlet = nextadr = 0;
58*7c478bd9Sstevel@tonic-gate 		let[0].adr = 0;
59*7c478bd9Sstevel@tonic-gate 		let[0].text = TRUE;	/* until proven otherwise.... */
60*7c478bd9Sstevel@tonic-gate 		let[0].change = ' ';
61*7c478bd9Sstevel@tonic-gate 	} else {
62*7c478bd9Sstevel@tonic-gate 		nextadr = let[nlet].adr;
63*7c478bd9Sstevel@tonic-gate 	}
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	while ((n = getline(line, sizeof line, f1)) > 0) {
66*7c478bd9Sstevel@tonic-gate 		if (!newline) {
67*7c478bd9Sstevel@tonic-gate 			goto putout;
68*7c478bd9Sstevel@tonic-gate 		} else if ((hdr = isheader (line, &ctf)) == FALSE) {
69*7c478bd9Sstevel@tonic-gate 			ctf = FALSE;	/* next line can't be cont. */
70*7c478bd9Sstevel@tonic-gate 		}
71*7c478bd9Sstevel@tonic-gate 		if (!hdr && cflg) {	/* nonheader, Content-length seen */
72*7c478bd9Sstevel@tonic-gate 			if (clen < n) {	/* read too much */
73*7c478bd9Sstevel@tonic-gate 				/* NB: this only can happen if the content-length
74*7c478bd9Sstevel@tonic-gate 				 * says a smaller number than what's seen on the
75*7c478bd9Sstevel@tonic-gate 				 * first non-header line.
76*7c478bd9Sstevel@tonic-gate 				 */
77*7c478bd9Sstevel@tonic-gate 				if (let[nlet-1].text == TRUE) {
78*7c478bd9Sstevel@tonic-gate 					let[nlet-1].text = istext((unsigned char*)line,clen);
79*7c478bd9Sstevel@tonic-gate 				    Dout(pn, 0, "1, let[%d].text = %s\n",
80*7c478bd9Sstevel@tonic-gate 					   nlet-1,
81*7c478bd9Sstevel@tonic-gate 					   (let[nlet-1].text ? "TRUE":"FALSE"));
82*7c478bd9Sstevel@tonic-gate 				}
83*7c478bd9Sstevel@tonic-gate 				if (fwrite(line,1,(int)clen,f2) != clen) {
84*7c478bd9Sstevel@tonic-gate 					fclose(f1); fclose(f2);
85*7c478bd9Sstevel@tonic-gate 					errmsg(E_FILE,
86*7c478bd9Sstevel@tonic-gate 						"Write error in copymt()");
87*7c478bd9Sstevel@tonic-gate 					done(0);
88*7c478bd9Sstevel@tonic-gate 				}
89*7c478bd9Sstevel@tonic-gate 				nextadr += clen;
90*7c478bd9Sstevel@tonic-gate 				n -= clen;
91*7c478bd9Sstevel@tonic-gate 				strmove (line, line+clen);
92*7c478bd9Sstevel@tonic-gate 				cflg = 0;
93*7c478bd9Sstevel@tonic-gate 				ctf = FALSE;
94*7c478bd9Sstevel@tonic-gate 				hdr = isheader(line, &ctf);
95*7c478bd9Sstevel@tonic-gate 				goto dohdr;
96*7c478bd9Sstevel@tonic-gate 			}
97*7c478bd9Sstevel@tonic-gate 			/* here, clen >= n */
98*7c478bd9Sstevel@tonic-gate 			if (n == 1 && line[0] == '\n'){	/* leading empty line */
99*7c478bd9Sstevel@tonic-gate 				clen++;		/* cheat */
100*7c478bd9Sstevel@tonic-gate 			}
101*7c478bd9Sstevel@tonic-gate 			nextadr += clen;
102*7c478bd9Sstevel@tonic-gate 			for (;;) {
103*7c478bd9Sstevel@tonic-gate 				if (let[nlet-1].text == TRUE) {
104*7c478bd9Sstevel@tonic-gate 					let[nlet-1].text = istext((unsigned char*)line,n);
105*7c478bd9Sstevel@tonic-gate 				  Dout(pn, 0, "2, let[%d].text = %s\n",
106*7c478bd9Sstevel@tonic-gate 					nlet-1,
107*7c478bd9Sstevel@tonic-gate 					(let[nlet-1].text ? "TRUE" : "FALSE"));
108*7c478bd9Sstevel@tonic-gate 				}
109*7c478bd9Sstevel@tonic-gate 				if (fwrite(line,1,n,f2) != n) {
110*7c478bd9Sstevel@tonic-gate 					fclose(f1); fclose(f2);
111*7c478bd9Sstevel@tonic-gate 					errmsg(E_FILE,
112*7c478bd9Sstevel@tonic-gate 						"Write error in copymt()");
113*7c478bd9Sstevel@tonic-gate 					done(0);
114*7c478bd9Sstevel@tonic-gate 				}
115*7c478bd9Sstevel@tonic-gate 				clen -= n;
116*7c478bd9Sstevel@tonic-gate 				if (clen <= 0) {
117*7c478bd9Sstevel@tonic-gate 					break;
118*7c478bd9Sstevel@tonic-gate 				}
119*7c478bd9Sstevel@tonic-gate 				n = clen < sizeof line ? clen : sizeof line;
120*7c478bd9Sstevel@tonic-gate 				if ((n = fread (line, 1, n, f1)) <= 0) {
121*7c478bd9Sstevel@tonic-gate 				    fprintf(stderr,
122*7c478bd9Sstevel@tonic-gate 		"%c%s:\tYour mailfile was found to be corrupted.\n",
123*7c478bd9Sstevel@tonic-gate 						BELL, program);
124*7c478bd9Sstevel@tonic-gate 				    fprintf(stderr,
125*7c478bd9Sstevel@tonic-gate 					"\t(Unexpected end-of-file).\n");
126*7c478bd9Sstevel@tonic-gate 				    fprintf(stderr,
127*7c478bd9Sstevel@tonic-gate 					"\tMessage #%d may be truncated.%c\n\n",
128*7c478bd9Sstevel@tonic-gate 						nlet, BELL);
129*7c478bd9Sstevel@tonic-gate 					nextadr -= clen;
130*7c478bd9Sstevel@tonic-gate 					clen = 0; /* stop the loop */
131*7c478bd9Sstevel@tonic-gate 				}
132*7c478bd9Sstevel@tonic-gate 			}
133*7c478bd9Sstevel@tonic-gate 			/* All done, go to top for next message */
134*7c478bd9Sstevel@tonic-gate 			cflg = 0;
135*7c478bd9Sstevel@tonic-gate 			StartNewMsg = TRUE;
136*7c478bd9Sstevel@tonic-gate 			continue;
137*7c478bd9Sstevel@tonic-gate 		}
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate dohdr:
140*7c478bd9Sstevel@tonic-gate 		switch (hdr) {
141*7c478bd9Sstevel@tonic-gate 		case H_FROM:
142*7c478bd9Sstevel@tonic-gate 			if(nlet >= (MAXLET-2)) {
143*7c478bd9Sstevel@tonic-gate 				if (!mesg) {
144*7c478bd9Sstevel@tonic-gate 					fprintf(stderr,"%s: Too many letters, overflowing letters concatenated\n\n",program);
145*7c478bd9Sstevel@tonic-gate 					mesg++;
146*7c478bd9Sstevel@tonic-gate 				}
147*7c478bd9Sstevel@tonic-gate 			} else {
148*7c478bd9Sstevel@tonic-gate 				let[nlet++].adr = nextadr;
149*7c478bd9Sstevel@tonic-gate 				let[nlet].text = TRUE;
150*7c478bd9Sstevel@tonic-gate 				let[nlet].change = ' ';
151*7c478bd9Sstevel@tonic-gate 			}
152*7c478bd9Sstevel@tonic-gate 			Dout(pn, 5, "setting StartNewMsg to FALSE\n");
153*7c478bd9Sstevel@tonic-gate 			StartNewMsg = FALSE;
154*7c478bd9Sstevel@tonic-gate 			ToldUser = FALSE;
155*7c478bd9Sstevel@tonic-gate 			break;
156*7c478bd9Sstevel@tonic-gate 		case H_CLEN:
157*7c478bd9Sstevel@tonic-gate 			if (cflg) {
158*7c478bd9Sstevel@tonic-gate 				break;
159*7c478bd9Sstevel@tonic-gate 			}
160*7c478bd9Sstevel@tonic-gate 			cflg = TRUE;	/* mark for clen processing */
161*7c478bd9Sstevel@tonic-gate 			clen = atol (strpbrk (line, ":")+1);
162*7c478bd9Sstevel@tonic-gate 			break;
163*7c478bd9Sstevel@tonic-gate 		default:
164*7c478bd9Sstevel@tonic-gate 			break;
165*7c478bd9Sstevel@tonic-gate 		}
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate putout:
168*7c478bd9Sstevel@tonic-gate 		if (nlet == 0) {
169*7c478bd9Sstevel@tonic-gate 			fclose(f1);
170*7c478bd9Sstevel@tonic-gate 			fclose(f2);
171*7c478bd9Sstevel@tonic-gate 			errmsg(E_FILE,"mailfile does not begin with a 'From' line");
172*7c478bd9Sstevel@tonic-gate 			done(0);
173*7c478bd9Sstevel@tonic-gate 		}
174*7c478bd9Sstevel@tonic-gate 		nextadr += n;
175*7c478bd9Sstevel@tonic-gate 		if (let[nlet-1].text == TRUE) {
176*7c478bd9Sstevel@tonic-gate 			let[nlet-1].text = istext((unsigned char*)line,n);
177*7c478bd9Sstevel@tonic-gate 		        Dout(pn, 5,"3, let[%d].text = %s\n",
178*7c478bd9Sstevel@tonic-gate 				nlet-1, (let[nlet-1].text ? "TRUE" : "FALSE"));
179*7c478bd9Sstevel@tonic-gate 		}
180*7c478bd9Sstevel@tonic-gate 		if (fwrite(line,1,n,f2) != n) {
181*7c478bd9Sstevel@tonic-gate 			fclose(f1);
182*7c478bd9Sstevel@tonic-gate 			fclose(f2);
183*7c478bd9Sstevel@tonic-gate 			errmsg(E_FILE,"Write error in copymt()");
184*7c478bd9Sstevel@tonic-gate 			done(0);
185*7c478bd9Sstevel@tonic-gate 		}
186*7c478bd9Sstevel@tonic-gate 		if (line[n-1] == '\n') {
187*7c478bd9Sstevel@tonic-gate 			newline = 1;
188*7c478bd9Sstevel@tonic-gate 			if (n == 1) { /* Blank line. Skip StartNewMsg */
189*7c478bd9Sstevel@tonic-gate 				      /* check below                  */
190*7c478bd9Sstevel@tonic-gate 				continue;
191*7c478bd9Sstevel@tonic-gate 			}
192*7c478bd9Sstevel@tonic-gate 		} else {
193*7c478bd9Sstevel@tonic-gate 			newline = 0;
194*7c478bd9Sstevel@tonic-gate 		}
195*7c478bd9Sstevel@tonic-gate 		if (StartNewMsg == TRUE && ToldUser == FALSE) {
196*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
197*7c478bd9Sstevel@tonic-gate 			     "%c%s:\tYour mailfile was found to be corrupted\n",
198*7c478bd9Sstevel@tonic-gate 			     BELL, program);
199*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, "\t(Content-length mismatch).\n");
200*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,"\tMessage #%d may be truncated,\n",
201*7c478bd9Sstevel@tonic-gate 			     nlet);
202*7c478bd9Sstevel@tonic-gate 			fprintf(stderr,
203*7c478bd9Sstevel@tonic-gate 			    "\twith another message concatenated to it.%c\n\n",
204*7c478bd9Sstevel@tonic-gate 			    BELL);
205*7c478bd9Sstevel@tonic-gate 			ToldUser = TRUE;
206*7c478bd9Sstevel@tonic-gate 		}
207*7c478bd9Sstevel@tonic-gate 	}
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	/*
210*7c478bd9Sstevel@tonic-gate 		last plus 1
211*7c478bd9Sstevel@tonic-gate 	*/
212*7c478bd9Sstevel@tonic-gate 	let[nlet].adr = nextadr;
213*7c478bd9Sstevel@tonic-gate 	let[nlet].change = ' ';
214*7c478bd9Sstevel@tonic-gate 	let[nlet].text = TRUE;
215*7c478bd9Sstevel@tonic-gate }
216