17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
23d7c5785Gary Mills * Copyright 2017 Gary Mills
24b7d62afceastha * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bdstevel@tonic-gate * Use is subject to license terms.
267c478bdstevel@tonic-gate */
277c478bdstevel@tonic-gate
28b7d62afceastha/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29b7d62afceastha/*	  All Rights Reserved  	*/
30b7d62afceastha
317c478bdstevel@tonic-gate#include "mail.h"
32b7d62afceastha
337c478bdstevel@tonic-gate/*
347c478bdstevel@tonic-gate    NAME
357c478bdstevel@tonic-gate	copylet - copy a given letter to a file pointer
367c478bdstevel@tonic-gate
377c478bdstevel@tonic-gate    SYNOPSIS
387c478bdstevel@tonic-gate	int copylet(int letnum, FILE *f, int type)
397c478bdstevel@tonic-gate
407c478bdstevel@tonic-gate    DESCRIPTION
417c478bdstevel@tonic-gate	Copylet() will copy the letter "letnum" to the
427c478bdstevel@tonic-gate	given file pointer.
437c478bdstevel@tonic-gate
447c478bdstevel@tonic-gate		letnum	-> index into: letter table
457c478bdstevel@tonic-gate		f	-> file pointer to copy file to
467c478bdstevel@tonic-gate		type	-> copy type
477c478bdstevel@tonic-gate
487c478bdstevel@tonic-gate	Returns TRUE on a completely successful copy.
497c478bdstevel@tonic-gate*/
507c478bdstevel@tonic-gate
51b7d62afceasthaint
52b7d62afceasthacopylet(int letnum, FILE *f, int type)
537c478bdstevel@tonic-gate{
547c478bdstevel@tonic-gate	int		pos = ftell(f);
557c478bdstevel@tonic-gate	int		rc  = xxxcopylet(letnum, f, type);
567c478bdstevel@tonic-gate
577c478bdstevel@tonic-gate	if (fflush(f) != 0)
587c478bdstevel@tonic-gate		rc = FALSE;
597c478bdstevel@tonic-gate
607c478bdstevel@tonic-gate	/*
617c478bdstevel@tonic-gate	 * On error, truncate the file to its original position so that a
627c478bdstevel@tonic-gate	 * partial message is not left in the mailbox.
637c478bdstevel@tonic-gate	 */
647c478bdstevel@tonic-gate	if (rc == FALSE)
657c478bdstevel@tonic-gate		ftruncate(fileno(f), pos);
667c478bdstevel@tonic-gate
677c478bdstevel@tonic-gate	return(rc);
687c478bdstevel@tonic-gate}
697c478bdstevel@tonic-gate
70b7d62afceasthaint
71b7d62afceasthaxxxcopylet(int letnum, FILE *f, int type)
727c478bdstevel@tonic-gate{
737c478bdstevel@tonic-gate	static char	pn[] = "copylet";
747c478bdstevel@tonic-gate	char	buf[LSIZE], lastc;
757c478bdstevel@tonic-gate	char	wbuf[LSIZE];
767c478bdstevel@tonic-gate	int	n;
777c478bdstevel@tonic-gate	long	i, k;
787c478bdstevel@tonic-gate	int	num;
797c478bdstevel@tonic-gate	int	rtrncont = 1;	/* True: nondelivery&content included, or regular mail */
807c478bdstevel@tonic-gate	int	suppress = FALSE;
817c478bdstevel@tonic-gate	int	sav_suppress = FALSE; /* Did we suppress previous hdr line? */
827c478bdstevel@tonic-gate	int	print_from_struct = FALSE; /* print from hdrlines struct */
837c478bdstevel@tonic-gate					   /* rather than fgets() buffer */
847c478bdstevel@tonic-gate	int	pushrest = FALSE;
857c478bdstevel@tonic-gate	int	ctf = FALSE;
867c478bdstevel@tonic-gate	int	didafflines = FALSE;	/* Did we already put out any */
877c478bdstevel@tonic-gate					/* H_AFWDFROM lines? */
887c478bdstevel@tonic-gate	int	didrcvlines = FALSE;	/* Did we already put out any */
897c478bdstevel@tonic-gate					/* H_RECEIVED lines? */
907c478bdstevel@tonic-gate	long	clen = -1L;
917c478bdstevel@tonic-gate	int	htype;			/* header type */
927c478bdstevel@tonic-gate	struct hdrs *hptr;
937c478bdstevel@tonic-gate
947c478bdstevel@tonic-gate	if (!sending) {
957c478bdstevel@tonic-gate		/* Clear out any saved header info from previous message */
967c478bdstevel@tonic-gate		clr_hinfo();
977c478bdstevel@tonic-gate	}
987c478bdstevel@tonic-gate
997c478bdstevel@tonic-gate	fseek(tmpf, let[letnum].adr, 0);
1007c478bdstevel@tonic-gate	/* Get size of message as stored into tempfile by copymt() */
1017c478bdstevel@tonic-gate	k = let[letnum+1].adr - let[letnum].adr;
1027c478bdstevel@tonic-gate	Dout(pn, 1, "(letnum = %d, type = %d), k = %ld\n", letnum, type, k);
1037c478bdstevel@tonic-gate	while (k>0) {	/* process header */
1047c478bdstevel@tonic-gate		num = ((k < sizeof(buf)) ? k+1 : sizeof(buf));
1057c478bdstevel@tonic-gate		if (fgets (buf, num, tmpf) == NULL) {
1067c478bdstevel@tonic-gate			return (FALSE);
1077c478bdstevel@tonic-gate		}
1087c478bdstevel@tonic-gate		if ((n = strlen (buf)) == 0) {
1097c478bdstevel@tonic-gate			k = 0;
1107c478bdstevel@tonic-gate			break;
1117c478bdstevel@tonic-gate		}
1127c478bdstevel@tonic-gate		k -= n;
1137c478bdstevel@tonic-gate		lastc = buf[n-1];
1147c478bdstevel@tonic-gate		if (pushrest) {
1157c478bdstevel@tonic-gate			pushrest = (lastc != '\n');
1167c478bdstevel@tonic-gate			continue;
1177c478bdstevel@tonic-gate		}
1187c478bdstevel@tonic-gate		htype = isheader (buf, &ctf);
1197c478bdstevel@tonic-gate		Dout(pn, 5, "loop 1: buf = %s, htype= %d/%s\n", buf, htype, header[htype].tag);
1207c478bdstevel@tonic-gate		if (htype == H_CLEN) {
1217c478bdstevel@tonic-gate			if (!sending) {
1227c478bdstevel@tonic-gate				savehdrs(buf,htype);
1237c478bdstevel@tonic-gate			}
1247c478bdstevel@tonic-gate			if ((hptr = hdrlines[H_CLEN].head) !=
1257c478bdstevel@tonic-gate			    (struct hdrs *)NULL) {
1267c478bdstevel@tonic-gate				clen = atol (hptr->value);
1277c478bdstevel@tonic-gate			}
1287c478bdstevel@tonic-gate		}
1297c478bdstevel@tonic-gate		if (type == ZAP) {
1307c478bdstevel@tonic-gate			if (htype != FALSE) {
1317c478bdstevel@tonic-gate				pushrest = (lastc != '\n');
1327c478bdstevel@tonic-gate				continue;
1337c478bdstevel@tonic-gate			}
1347c478bdstevel@tonic-gate			/* end of header.  Print non-blank line and bail. */
1357c478bdstevel@tonic-gate			Dout(pn, 5, "ZAP end header; n=%d, buf[0] = %d\n", n, buf[0]);
1367c478bdstevel@tonic-gate			if (buf[0] != '\n') {
1377c478bdstevel@tonic-gate				if (fwrite(buf,1,n,f) != n) {
1387c478bdstevel@tonic-gate					sav_errno = errno;
1397c478bdstevel@tonic-gate					return(FALSE);
1407c478bdstevel@tonic-gate				}
1417c478bdstevel@tonic-gate			} else {
1427c478bdstevel@tonic-gate				n = 0;
1437c478bdstevel@tonic-gate			}
1447c478bdstevel@tonic-gate			break;
1457c478bdstevel@tonic-gate		}
1467c478bdstevel@tonic-gate		/* Copy From line appropriately */
1477c478bdstevel@tonic-gate		if (fwrite(buf,1,n-1,f) != n-1)  {
1487c478bdstevel@tonic-gate			sav_errno = errno;
1497c478bdstevel@tonic-gate			return(FALSE);
1507c478bdstevel@tonic-gate		}
1517c478bdstevel@tonic-gate		if (lastc != '\n') {
1527c478bdstevel@tonic-gate			if (fwrite(&lastc,1,1,f) != 1) {
1537c478bdstevel@tonic-gate				sav_errno = errno;
1547c478bdstevel@tonic-gate				return(FALSE);
1557c478bdstevel@tonic-gate			}
1567c478bdstevel@tonic-gate			continue;
1577c478bdstevel@tonic-gate		}
1587c478bdstevel@tonic-gate		switch(type) {
1597c478bdstevel@tonic-gate			case REMOTE:
1607c478bdstevel@tonic-gate				if (fprintf(f, rmtmsg, thissys) < 0)
1617c478bdstevel@tonic-gate				{
1627c478bdstevel@tonic-gate					sav_errno = errno;
1637c478bdstevel@tonic-gate					return(FALSE);
1647c478bdstevel@tonic-gate				}
1657c478bdstevel@tonic-gate
1667c478bdstevel@tonic-gate				break;
1677c478bdstevel@tonic-gate
1687c478bdstevel@tonic-gate			case TTY:
1697c478bdstevel@tonic-gate			case ORDINARY:
1707c478bdstevel@tonic-gate			default:
1717c478bdstevel@tonic-gate				if (fprintf(f, "\n") < 0)
1727c478bdstevel@tonic-gate				{
1737c478bdstevel@tonic-gate					sav_errno = errno;
1747c478bdstevel@tonic-gate					return(FALSE);
1757c478bdstevel@tonic-gate				}
1767c478bdstevel@tonic-gate				break;
1777c478bdstevel@tonic-gate		}
1787c478bdstevel@tonic-gate		if ((error > 0) && (dflag == 1)) {
1797c478bdstevel@tonic-gate			Dout(pn, 3, "before gendeliv(), uval = '%s'\n", uval);
1807c478bdstevel@tonic-gate			gendeliv(f, dflag, uval);
1817c478bdstevel@tonic-gate			if (!(ckdlivopts(H_TCOPY, (int*)0) & RETURN)) {
1827c478bdstevel@tonic-gate				rtrncont = 0;
1837c478bdstevel@tonic-gate			} else {
1847c478bdstevel@tonic-gate				/* Account for content-type info */
1857c478bdstevel@tonic-gate				/* of returned msg */
1867c478bdstevel@tonic-gate				if (fprintf(f, "%s %s\n", header[H_CTYPE].tag,
1877c478bdstevel@tonic-gate				    (let[letnum].text == TRUE ? "text/plain" : "application/octet-stream")) < 0)
1887c478bdstevel@tonic-gate				{
1897c478bdstevel@tonic-gate					sav_errno = errno;
1907c478bdstevel@tonic-gate					return(FALSE);
1917c478bdstevel@tonic-gate				}
1927c478bdstevel@tonic-gate
1937c478bdstevel@tonic-gate				/* Compute Content-Length of what's being */
1947c478bdstevel@tonic-gate				/* returned... */
1957c478bdstevel@tonic-gate				i = k;
1967c478bdstevel@tonic-gate				/* Account for H_AFWDFROM, H_AFWDCNT, */
1977c478bdstevel@tonic-gate				/* H_TCOPY, or H_RECEIVED lines which may */
1987c478bdstevel@tonic-gate				/* be added later */
1997c478bdstevel@tonic-gate				if (affcnt > 0) {
2007c478bdstevel@tonic-gate					sprintf(wbuf, "%d", affcnt);
2017c478bdstevel@tonic-gate					i += (affbytecnt
2027c478bdstevel@tonic-gate						+ strlen(header[H_AFWDCNT].tag)
2037c478bdstevel@tonic-gate						+ strlen(wbuf) + 2);
2047c478bdstevel@tonic-gate				}
2057c478bdstevel@tonic-gate				if (orig_tcopy) {
2067c478bdstevel@tonic-gate				    if ((hptr = hdrlines[H_TCOPY].head) !=
2077c478bdstevel@tonic-gate							(struct hdrs *)NULL) {
2087c478bdstevel@tonic-gate				        i +=
2097c478bdstevel@tonic-gate					  strlen(hdrlines[H_TCOPY].head->value);
2107c478bdstevel@tonic-gate				    }
2117c478bdstevel@tonic-gate				}
2127c478bdstevel@tonic-gate				if ((hptr = hdrlines[H_RECEIVED].head) !=
2137c478bdstevel@tonic-gate							(struct hdrs *)NULL) {
2147c478bdstevel@tonic-gate				    i += rcvbytecnt;
2157c478bdstevel@tonic-gate				}
2167c478bdstevel@tonic-gate				/* Add in strlen of MIME-Version:, */
2177c478bdstevel@tonic-gate				/* Content-Length: and Content-Type: */
2187c478bdstevel@tonic-gate				/* values for msg being returned... */
2197c478bdstevel@tonic-gate				if ((hptr = hdrlines[H_MIMEVERS].head) !=
2207c478bdstevel@tonic-gate							(struct hdrs *)NULL) {
2217c478bdstevel@tonic-gate				    i += strlen(hdrlines[H_MIMEVERS].head->value);
2227c478bdstevel@tonic-gate				}
2237c478bdstevel@tonic-gate				if ((hptr = hdrlines[H_CTYPE].head) !=
2247c478bdstevel@tonic-gate							(struct hdrs *)NULL) {
2257c478bdstevel@tonic-gate				    i += strlen(hdrlines[H_CTYPE].head->value);
2267c478bdstevel@tonic-gate				}
2277c478bdstevel@tonic-gate				if ((hptr = hdrlines[H_CLEN].head) !=
2287c478bdstevel@tonic-gate							(struct hdrs *)NULL) {
2297c478bdstevel@tonic-gate				    i += strlen(hdrlines[H_CLEN].head->value);
2307c478bdstevel@tonic-gate				}
2317c478bdstevel@tonic-gate				if (fprintf(f, "%s %ld\n", header[H_CLEN].tag, i) < 0)
2327c478bdstevel@tonic-gate				{
2337c478bdstevel@tonic-gate					sav_errno = errno;
2347c478bdstevel@tonic-gate					return(FALSE);
2357c478bdstevel@tonic-gate				}
2367c478bdstevel@tonic-gate			}
2377c478bdstevel@tonic-gate			if (fprintf(f, "\n") < 0)
2387c478bdstevel@tonic-gate			{
2397c478bdstevel@tonic-gate				sav_errno = errno;
2407c478bdstevel@tonic-gate				return(FALSE);
2417c478bdstevel@tonic-gate			}
2427c478bdstevel@tonic-gate		}
2437c478bdstevel@tonic-gate		if (fflush(f))
2447c478bdstevel@tonic-gate		{
2457c478bdstevel@tonic-gate			sav_errno = errno;
2467c478bdstevel@tonic-gate			return(FALSE);
2477c478bdstevel@tonic-gate		}
2487c478bdstevel@tonic-gate
2497c478bdstevel@tonic-gate		break;
2507c478bdstevel@tonic-gate	}
2517c478bdstevel@tonic-gate	/* if not ZAP, copy balance of header */
2527c478bdstevel@tonic-gate	n = 0;
2537c478bdstevel@tonic-gate	if ((type != ZAP) && rtrncont)
2547c478bdstevel@tonic-gate		while (k>0 || n>0) {
2557c478bdstevel@tonic-gate			if ((n > 0) && !suppress) {
2567c478bdstevel@tonic-gate				if (print_from_struct == TRUE) {
2577c478bdstevel@tonic-gate					if (printhdr (type, htype, hptr, f) < 0) {
2587c478bdstevel@tonic-gate						return (FALSE);
2597c478bdstevel@tonic-gate					}
2607c478bdstevel@tonic-gate				} else {
2617c478bdstevel@tonic-gate				    if (sel_disp(type, htype, buf) >= 0) {
2627c478bdstevel@tonic-gate					if (fwrite(buf,1,n,f) != n)  {
2637c478bdstevel@tonic-gate						sav_errno = errno;
2647c478bdstevel@tonic-gate						return(FALSE);
2657c478bdstevel@tonic-gate					}
2667c478bdstevel@tonic-gate				    }
2677c478bdstevel@tonic-gate				}
2687c478bdstevel@tonic-gate				if (htype == H_DATE) {
2697c478bdstevel@tonic-gate					dumprcv(type, htype,&didrcvlines,&suppress,f);
2707c478bdstevel@tonic-gate					dumpaff(type, htype,&didafflines,&suppress,f);
2717c478bdstevel@tonic-gate				}
2727c478bdstevel@tonic-gate			}
2737c478bdstevel@tonic-gate			if (k <= 0) {
2747c478bdstevel@tonic-gate				/* Can only get here if k=0 && n>0, which occurs */
2757c478bdstevel@tonic-gate				/* in a message with header lines but no content. */
2767c478bdstevel@tonic-gate				/* If we haven't already done it, force out any */
2777c478bdstevel@tonic-gate				/* H_AFWDFROM or H_RECEIVED lines */
2787c478bdstevel@tonic-gate				dumprcv(type, -1,&didrcvlines,&suppress,f);
2797c478bdstevel@tonic-gate				dumpaff(type, -1,&didafflines,&suppress,f);
2807c478bdstevel@tonic-gate				break;
2817c478bdstevel@tonic-gate			}
2827c478bdstevel@tonic-gate			num = ((k < sizeof(buf)) ? k+1 : sizeof(buf));
2837c478bdstevel@tonic-gate			if (fgets (buf, num, tmpf) == NULL) {
2847c478bdstevel@tonic-gate				return (FALSE);
2857c478bdstevel@tonic-gate			}
2867c478bdstevel@tonic-gate			n = strlen (buf);
2877c478bdstevel@tonic-gate			k -= n;
2887c478bdstevel@tonic-gate			lastc = buf[n-1];
2897c478bdstevel@tonic-gate
2907c478bdstevel@tonic-gate			if (pushrest) {
2917c478bdstevel@tonic-gate				pushrest = (lastc != '\n');
2927c478bdstevel@tonic-gate				continue;
2937c478bdstevel@tonic-gate			}
2947c478bdstevel@tonic-gate			sav_suppress = suppress;
2957c478bdstevel@tonic-gate			suppress = FALSE;
2967c478bdstevel@tonic-gate			print_from_struct = FALSE;
2977c478bdstevel@tonic-gate			htype = isheader (buf, &ctf);
2987c478bdstevel@tonic-gate			Dout(pn, 5, "loop 2: buf = %s, htype= %d/%s\n", buf, htype, header[htype].tag);
2997c478bdstevel@tonic-gate			/* The following order is defined in the MTA documents. */
3007c478bdstevel@tonic-gate			switch (htype) {
3017c478bdstevel@tonic-gate			case H_CONT:
3027c478bdstevel@tonic-gate			    if (sending) {
3037c478bdstevel@tonic-gate				suppress = sav_suppress;
3047c478bdstevel@tonic-gate			    }
3057c478bdstevel@tonic-gate			    continue;
3067c478bdstevel@tonic-gate			case H_TCOPY:
3077c478bdstevel@tonic-gate			case H_MIMEVERS:
3087c478bdstevel@tonic-gate			case H_CTYPE:
3097c478bdstevel@tonic-gate			case H_CLEN:
3107c478bdstevel@tonic-gate				if (!sending) {
3117c478bdstevel@tonic-gate					savehdrs(buf,htype);
3127c478bdstevel@tonic-gate				}
3137c478bdstevel@tonic-gate				hptr = hdrlines[htype].head;
3147c478bdstevel@tonic-gate				if (htype == H_CLEN) {
3157c478bdstevel@tonic-gate					clen = atol (hptr->value);
3167c478bdstevel@tonic-gate				}
3177c478bdstevel@tonic-gate				/*
3187c478bdstevel@tonic-gate				 * Use values saved in hdrlines[] structure
3197c478bdstevel@tonic-gate				 * rather than what was read from tmp file.
3207c478bdstevel@tonic-gate				 */
3217c478bdstevel@tonic-gate				print_from_struct = TRUE;
3227c478bdstevel@tonic-gate				/* FALLTHROUGH */
3237c478bdstevel@tonic-gate			case H_EOH:
3247c478bdstevel@tonic-gate			case H_AFWDFROM:
3257c478bdstevel@tonic-gate			case H_AFWDCNT:
3267c478bdstevel@tonic-gate			case H_RECEIVED:
3277c478bdstevel@tonic-gate				dumprcv(type, htype,&didrcvlines,&suppress,f);
3287c478bdstevel@tonic-gate				dumpaff(type, htype,&didafflines,&suppress,f);
3297c478bdstevel@tonic-gate				continue;	/* next header line */
3307c478bdstevel@tonic-gate			default:
3317c478bdstevel@tonic-gate				pushrest = (lastc != '\n');
3327c478bdstevel@tonic-gate				continue;	/* next header line */
3337c478bdstevel@tonic-gate			case FALSE:	/* end of header */
3347c478bdstevel@tonic-gate				break;
3357c478bdstevel@tonic-gate			}
3367c478bdstevel@tonic-gate
3377c478bdstevel@tonic-gate			/* Found the blank line after the headers. */
3387c478bdstevel@tonic-gate			if (n > 0) {
3397c478bdstevel@tonic-gate				if (fwrite(buf,1,n,f) != n)  {
3407c478bdstevel@tonic-gate					sav_errno = errno;
3417c478bdstevel@tonic-gate					return(FALSE);
3427c478bdstevel@tonic-gate				}
3437c478bdstevel@tonic-gate			}
3447c478bdstevel@tonic-gate
3457c478bdstevel@tonic-gate			Dout(pn, 3,", let[%d].text = %s\n",
3467c478bdstevel@tonic-gate				letnum, (let[letnum].text ? "TRUE" : "FALSE"));
3477c478bdstevel@tonic-gate
3487c478bdstevel@tonic-gate			if ((type == TTY) && (let[letnum].text == FALSE) && !pflg) {
3497c478bdstevel@tonic-gate				if (fprintf (f, "\n%s\n", binmsg) < 0)
3507c478bdstevel@tonic-gate				{
3517c478bdstevel@tonic-gate					sav_errno = errno;
3527c478bdstevel@tonic-gate					return(FALSE);
3537c478bdstevel@tonic-gate				}
3547c478bdstevel@tonic-gate				return (TRUE);
3557c478bdstevel@tonic-gate			}
3567c478bdstevel@tonic-gate
3577c478bdstevel@tonic-gate			if (n == 1 && buf[0] == '\n') {
3587c478bdstevel@tonic-gate				n = 0;
3597c478bdstevel@tonic-gate			}
3607c478bdstevel@tonic-gate			break;
3617c478bdstevel@tonic-gate		}
3627c478bdstevel@tonic-gate
3637c478bdstevel@tonic-gate	Dout(pn, 1, "header processed, clen/k/n = %ld/%ld/%d\n", clen, k, n);
3647c478bdstevel@tonic-gate
3657c478bdstevel@tonic-gate	if (clen >= 0) {
3667c478bdstevel@tonic-gate		if (((clen - n) == k) || ((clen - n) == (k - 1))) {
3677c478bdstevel@tonic-gate			k = clen - n;
3687c478bdstevel@tonic-gate		} else {
3697c478bdstevel@tonic-gate			/* probable content-length mismatch. show it ALL! */
3707c478bdstevel@tonic-gate			Dout(pn, 1, "clen conflict. using k = %ld\n", k);
3717c478bdstevel@tonic-gate		}
3727c478bdstevel@tonic-gate	}
3737c478bdstevel@tonic-gate
3747c478bdstevel@tonic-gate	/* copy balance of message */
3757c478bdstevel@tonic-gate	if (rtrncont)
3767c478bdstevel@tonic-gate		while (k > 0) {
3777c478bdstevel@tonic-gate			num = ((k < sizeof(buf)) ? k : sizeof(buf));
3787c478bdstevel@tonic-gate			if ((n = fread (buf, 1, num, tmpf)) <= 0) {
3797c478bdstevel@tonic-gate				Dout(pn, 1, "content-length mismatch. return(FALSE)\n");
3807c478bdstevel@tonic-gate				return(FALSE);
3817c478bdstevel@tonic-gate			}
3827c478bdstevel@tonic-gate			k -= n;
3837c478bdstevel@tonic-gate			if (fwrite(buf,1,n,f) != n)  {
3847c478bdstevel@tonic-gate				sav_errno = errno;
3857c478bdstevel@tonic-gate				return(FALSE);
3867c478bdstevel@tonic-gate			}
3877c478bdstevel@tonic-gate		}
3887c478bdstevel@tonic-gate
3897c478bdstevel@tonic-gate	Dout(pn, 3, "body processed, k=%ld\n", k);
3907c478bdstevel@tonic-gate
3917c478bdstevel@tonic-gate	if (rtrncont && type != ZAP && type != REMOTE) {
3927c478bdstevel@tonic-gate		if (fwrite("\n",1,1,f) != 1)  {
3937c478bdstevel@tonic-gate			sav_errno = errno;
3947c478bdstevel@tonic-gate			return(FALSE);
3957c478bdstevel@tonic-gate		}
3967c478bdstevel@tonic-gate	}
3977c478bdstevel@tonic-gate
3987c478bdstevel@tonic-gate	return(TRUE);
3997c478bdstevel@tonic-gate}
400