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