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
523a1cceRoger A. Faulkner * Common Development and Distribution License (the "License").
623a1cceRoger A. Faulkner * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate
227c478bdstevel@tonic-gate/*
2323a1cceRoger A. Faulkner * Copyright (c) 1985, 2010, Oracle and/or its affiliates. All rights reserved.
2448bbca8Daniel Hoffman * Copyright (c) 2016 by Delphix. All rights reserved.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
276c83d09robbin/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
286c83d09robbin/*	  All Rights Reserved  	*/
296c83d09robbin
307c478bdstevel@tonic-gate/*
317c478bdstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bdstevel@tonic-gate * The Regents of the University of California
337c478bdstevel@tonic-gate * All Rights Reserved
347c478bdstevel@tonic-gate *
357c478bdstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
367c478bdstevel@tonic-gate * software developed by the University of California, Berkeley, and its
377c478bdstevel@tonic-gate * contributors.
387c478bdstevel@tonic-gate */
397c478bdstevel@tonic-gate
407c478bdstevel@tonic-gate#include "rcv.h"
417c478bdstevel@tonic-gate#include <locale.h>
427c478bdstevel@tonic-gate
437c478bdstevel@tonic-gate/*
447c478bdstevel@tonic-gate * mailx -- a modified version of a University of California at Berkeley
457c478bdstevel@tonic-gate *	mail program
467c478bdstevel@tonic-gate *
477c478bdstevel@tonic-gate * Mail to others.
487c478bdstevel@tonic-gate */
497c478bdstevel@tonic-gate
507c478bdstevel@tonic-gatestatic void		fmt(register char *str, register FILE *fo);
517c478bdstevel@tonic-gatestatic FILE		*infix(struct header *hp, FILE *fi);
527c478bdstevel@tonic-gatestatic void		statusput(register struct message *mp, register FILE *obuf, int doign, int (*fp)(const char *, FILE *));
537c478bdstevel@tonic-gatestatic int		savemail(char name[], struct header *hp, FILE *fi);
547c478bdstevel@tonic-gatestatic int		sendmail(char *str);
557c478bdstevel@tonic-gatestatic int		Sendmail(char *str);
567c478bdstevel@tonic-gate
577c478bdstevel@tonic-gatestatic off_t textpos;
587c478bdstevel@tonic-gate
597c478bdstevel@tonic-gate/*
607c478bdstevel@tonic-gate * Send message described by the passed pointer to the
617c478bdstevel@tonic-gate * passed output buffer.  Return -1 on error, but normally
627c478bdstevel@tonic-gate * the number of lines written.  Adjust the status: field
637c478bdstevel@tonic-gate * if need be.  If doign is set, suppress ignored header fields.
647c478bdstevel@tonic-gate * Call (*fp)(line, obuf) to print the line.
657c478bdstevel@tonic-gate */
667c478bdstevel@tonic-gatelong
677c478bdstevel@tonic-gatemsend(
687c478bdstevel@tonic-gate	struct message *mailp,
697c478bdstevel@tonic-gate	FILE *obuf,
707c478bdstevel@tonic-gate	int flag,
717c478bdstevel@tonic-gate	int (*fp)(const char *, FILE *))
727c478bdstevel@tonic-gate{
737c478bdstevel@tonic-gate	register struct message *mp;
747c478bdstevel@tonic-gate	long clen, n, c;
757c478bdstevel@tonic-gate	FILE *ibuf;
767c478bdstevel@tonic-gate	char line[LINESIZE], field[BUFSIZ];
777c478bdstevel@tonic-gate	int ishead, infld, fline, dostat, doclen, nread, unused;
787c478bdstevel@tonic-gate	char *cp, *cp2;
797c478bdstevel@tonic-gate	int doign = flag & M_IGNORE;
807c478bdstevel@tonic-gate	int oldign = 0;	/* previous line was ignored */
817c478bdstevel@tonic-gate	long lc;
827c478bdstevel@tonic-gate
837c478bdstevel@tonic-gate	mp = mailp;
847c478bdstevel@tonic-gate	if (mp->m_clen == 0)
857c478bdstevel@tonic-gate		setclen(mp);
867c478bdstevel@tonic-gate	ibuf = setinput(mp);
877c478bdstevel@tonic-gate	c = mp->m_size;
887c478bdstevel@tonic-gate	ishead = 1;
897c478bdstevel@tonic-gate	dostat = 1;
907c478bdstevel@tonic-gate	doclen = 1;
917c478bdstevel@tonic-gate	infld = 0;
927c478bdstevel@tonic-gate	fline = 1;
937c478bdstevel@tonic-gate	lc = 0;
947c478bdstevel@tonic-gate	clearerr(obuf);
957c478bdstevel@tonic-gate	while (c > 0L) {
9623a1cceRoger A. Faulkner		nread = getaline(line, LINESIZE, ibuf, &unused);
977c478bdstevel@tonic-gate		c -= nread;
987c478bdstevel@tonic-gate		lc++;
997c478bdstevel@tonic-gate		if (ishead) {
1007c478bdstevel@tonic-gate			/*
1017c478bdstevel@tonic-gate			 * First line is the From line, so no headers
1027c478bdstevel@tonic-gate			 * there to worry about
1037c478bdstevel@tonic-gate			 */
1047c478bdstevel@tonic-gate			if (fline) {
1057c478bdstevel@tonic-gate				fline = 0;
1067c478bdstevel@tonic-gate				goto writeit;
1077c478bdstevel@tonic-gate			}
1087c478bdstevel@tonic-gate			/*
1097c478bdstevel@tonic-gate			 * If line is blank, we've reached end of
1107c478bdstevel@tonic-gate			 * headers, so force out status: field
1117c478bdstevel@tonic-gate			 * and note that we are no longer in header
1127c478bdstevel@tonic-gate			 * fields.  Also force out Content-Length: field.
1137c478bdstevel@tonic-gate			 */
1147c478bdstevel@tonic-gate			if (line[0] == '\n') {
1157c478bdstevel@tonic-gate				if (dostat) {
1167c478bdstevel@tonic-gate					statusput(mailp, obuf, doign, fp);
1177c478bdstevel@tonic-gate					dostat = 0;
1187c478bdstevel@tonic-gate				}
1197c478bdstevel@tonic-gate				if (doclen &&
1207c478bdstevel@tonic-gate				    !isign("content-length", flag&M_SAVING)) {
1217c478bdstevel@tonic-gate					snprintf(field, sizeof (field),
1227c478bdstevel@tonic-gate						"Content-Length: %ld\n",
1237c478bdstevel@tonic-gate						mp->m_clen - 1);
1247c478bdstevel@tonic-gate					(*fp)(field, obuf);
1257c478bdstevel@tonic-gate					if (ferror(obuf))
1267c478bdstevel@tonic-gate						return(-1);
1277c478bdstevel@tonic-gate					doclen = 0;
1287c478bdstevel@tonic-gate				}
1297c478bdstevel@tonic-gate				ishead = 0;
1307c478bdstevel@tonic-gate				goto writeit;
1317c478bdstevel@tonic-gate			}
1327c478bdstevel@tonic-gate			/*
1337c478bdstevel@tonic-gate			 * If this line is a continuation
1347c478bdstevel@tonic-gate			 * of a previous header field, just echo it.
1357c478bdstevel@tonic-gate			 */
1367c478bdstevel@tonic-gate			if (isspace(line[0]) && infld)
1377c478bdstevel@tonic-gate				if (oldign)
1387c478bdstevel@tonic-gate					continue;
1397c478bdstevel@tonic-gate				else
1407c478bdstevel@tonic-gate					goto writeit;
1417c478bdstevel@tonic-gate			infld = 0;
1427c478bdstevel@tonic-gate			/*
1437c478bdstevel@tonic-gate			 * If we are no longer looking at real
1447c478bdstevel@tonic-gate			 * header lines, force out status:
1457c478bdstevel@tonic-gate			 * This happens in uucp style mail where
1467c478bdstevel@tonic-gate			 * there are no headers at all.
1477c478bdstevel@tonic-gate			 */
1487c478bdstevel@tonic-gate			if (!headerp(line)) {
1497c478bdstevel@tonic-gate				if (dostat) {
1507c478bdstevel@tonic-gate					statusput(mailp, obuf, doign, fp);
1517c478bdstevel@tonic-gate					dostat = 0;
1527c478bdstevel@tonic-gate				}
1537c478bdstevel@tonic-gate				(*fp)("\n", obuf);
1547c478bdstevel@tonic-gate				ishead = 0;
1557c478bdstevel@tonic-gate				goto writeit;
1567c478bdstevel@tonic-gate			}
1577c478bdstevel@tonic-gate			infld++;
1587c478bdstevel@tonic-gate			/*
1597c478bdstevel@tonic-gate			 * Pick up the header field.
1607c478bdstevel@tonic-gate			 * If it is an ignored field and
1617c478bdstevel@tonic-gate			 * we care about such things, skip it.
1627c478bdstevel@tonic-gate			 */
1637c478bdstevel@tonic-gate			cp = line;
1647c478bdstevel@tonic-gate			cp2 = field;
1657c478bdstevel@tonic-gate			while (*cp && *cp != ':' && !isspace(*cp))
1667c478bdstevel@tonic-gate				*cp2++ = *cp++;
1677c478bdstevel@tonic-gate			*cp2 = 0;
1687c478bdstevel@tonic-gate			oldign = doign && isign(field, flag&M_SAVING);
1697c478bdstevel@tonic-gate			if (oldign)
1707c478bdstevel@tonic-gate				continue;
1717c478bdstevel@tonic-gate			/*
1727c478bdstevel@tonic-gate			 * If the field is "status," go compute and print the
1737c478bdstevel@tonic-gate			 * real Status: field
1747c478bdstevel@tonic-gate			 */
1757c478bdstevel@tonic-gate			if (icequal(field, "status")) {
1767c478bdstevel@tonic-gate				if (dostat) {
1777c478bdstevel@tonic-gate					statusput(mailp, obuf, doign, fp);
1787c478bdstevel@tonic-gate					dostat = 0;
1797c478bdstevel@tonic-gate				}
1807c478bdstevel@tonic-gate				continue;
1817c478bdstevel@tonic-gate			}
1827c478bdstevel@tonic-gate			if (icequal(field, "content-length")) {
1837c478bdstevel@tonic-gate				if (doclen) {
1847c478bdstevel@tonic-gate					snprintf(line, sizeof (line),
1857c478bdstevel@tonic-gate						"Content-Length: %ld\n",
1867c478bdstevel@tonic-gate						mp->m_clen - 1);
1877c478bdstevel@tonic-gate					(*fp)(line, obuf);
1887c478bdstevel@tonic-gate					if (ferror(obuf))
1897c478bdstevel@tonic-gate						return(-1);
1907c478bdstevel@tonic-gate					doclen = 0;
1917c478bdstevel@tonic-gate				}
1927c478bdstevel@tonic-gate				continue;
1937c478bdstevel@tonic-gate			}
1947c478bdstevel@tonic-gate		}
1957c478bdstevel@tonic-gatewriteit:
1967c478bdstevel@tonic-gate		if (!ishead && !mp->m_text && mp->m_clen != 0) {
1977c478bdstevel@tonic-gate			if (line[0] == '\n')
1987c478bdstevel@tonic-gate				putc('\n', obuf);
1997c478bdstevel@tonic-gate			clen = mp->m_clen-1;
2007c478bdstevel@tonic-gate			for (;;) {
2017c478bdstevel@tonic-gate				n = clen < sizeof line ? clen : sizeof line;
2027c478bdstevel@tonic-gate				if ((n = fread(line, 1, (int)n, ibuf)) <= 0) {
2037c478bdstevel@tonic-gate					fprintf(stderr, gettext(
2047c478bdstevel@tonic-gate					    "\t(Unexpected end-of-file).\n"));
2057c478bdstevel@tonic-gate					clen = 0;
2067c478bdstevel@tonic-gate				} else {
2077c478bdstevel@tonic-gate					if (fwrite(line, 1, (int)n, obuf) != n) {
2087c478bdstevel@tonic-gate						fprintf(stderr, gettext(
2097c478bdstevel@tonic-gate					"\tError writing to the new file.\n"));
2107c478bdstevel@tonic-gate						fflush(obuf);
2117c478bdstevel@tonic-gate						if (fferror(obuf))
2127c478bdstevel@tonic-gate							return (-1);
2137c478bdstevel@tonic-gate					}
2147c478bdstevel@tonic-gate				}
2157c478bdstevel@tonic-gate				clen -= n;
2167c478bdstevel@tonic-gate				if (clen <= 0) {
2177c478bdstevel@tonic-gate					break;
2187c478bdstevel@tonic-gate				}
2197c478bdstevel@tonic-gate			}
2207c478bdstevel@tonic-gate			c = 0L;
2217c478bdstevel@tonic-gate		} else {
2227c478bdstevel@tonic-gate			(*fp)(line, obuf);
2237c478bdstevel@tonic-gate			if (ferror(obuf))
2247c478bdstevel@tonic-gate				return(-1);
2257c478bdstevel@tonic-gate		}
2267c478bdstevel@tonic-gate	}
2277c478bdstevel@tonic-gate	fflush(obuf);
2287c478bdstevel@tonic-gate	if (ferror(obuf))
2297c478bdstevel@tonic-gate		return(-1);
2307c478bdstevel@tonic-gate	if (ishead && (mailp->m_flag & MSTATUS))
2317c478bdstevel@tonic-gate		printf(gettext("failed to fix up status field\n"));
2327c478bdstevel@tonic-gate	return(lc);
2337c478bdstevel@tonic-gate}
2347c478bdstevel@tonic-gate
2357c478bdstevel@tonic-gate/*
2367c478bdstevel@tonic-gate * Test if the passed line is a header line, RFC 822 style.
2377c478bdstevel@tonic-gate */
23848bbca8Daniel Hoffmanint
2397c478bdstevel@tonic-gateheaderp(register char *line)
2407c478bdstevel@tonic-gate{
2417c478bdstevel@tonic-gate	register char *cp = line;
2427c478bdstevel@tonic-gate
2437c478bdstevel@tonic-gate	while (*cp && *cp != ' ' && *cp != '\t' && *cp != ':')
2447c478bdstevel@tonic-gate		cp++;
2457c478bdstevel@tonic-gate	return(*cp == ':');
2467c478bdstevel@tonic-gate}
2477c478bdstevel@tonic-gate
2487c478bdstevel@tonic-gate/*
2497c478bdstevel@tonic-gate * Output a reasonable looking status field.
2507c478bdstevel@tonic-gate * But if "status" is ignored and doign, forget it.
2517c478bdstevel@tonic-gate */
2527c478bdstevel@tonic-gatestatic void
2537c478bdstevel@tonic-gatestatusput(
2547c478bdstevel@tonic-gate	register struct message *mp,
2557c478bdstevel@tonic-gate	register FILE *obuf,
2567c478bdstevel@tonic-gate	int doign,
2577c478bdstevel@tonic-gate	int (*fp)(const char *, FILE *))
2587c478bdstevel@tonic-gate{
2597c478bdstevel@tonic-gate	char statout[12];
2607c478bdstevel@tonic-gate
2617c478bdstevel@tonic-gate	if (doign && isign("status", 0))
2627c478bdstevel@tonic-gate		return;
2637c478bdstevel@tonic-gate	if ((mp->m_flag & (MNEW|MREAD)) == MNEW)
2647c478bdstevel@tonic-gate		return;
2657c478bdstevel@tonic-gate	strcpy(statout, "Status: ");
2667c478bdstevel@tonic-gate	if (mp->m_flag & MREAD)
2677c478bdstevel@tonic-gate		strcat(statout, "R");
2687c478bdstevel@tonic-gate	if ((mp->m_flag & MNEW) == 0)
2697c478bdstevel@tonic-gate		strcat(statout, "O");
2707c478bdstevel@tonic-gate	strcat(statout, "\n");
2717c478bdstevel@tonic-gate	(*fp)(statout, obuf);
2727c478bdstevel@tonic-gate}
2737c478bdstevel@tonic-gate
2747c478bdstevel@tonic-gate/*
2757c478bdstevel@tonic-gate * Interface between the argument list and the mail1 routine
2767c478bdstevel@tonic-gate * which does all the dirty work.
2777c478bdstevel@tonic-gate */
2787c478bdstevel@tonic-gate
27948bbca8Daniel Hoffmanint
2807c478bdstevel@tonic-gatemail(char **people)
2817c478bdstevel@tonic-gate{
2827c478bdstevel@tonic-gate	register char *cp2, *cp3;
2837c478bdstevel@tonic-gate	register int s;
2847c478bdstevel@tonic-gate	char *buf, **ap;
2857c478bdstevel@tonic-gate	struct header head;
2867c478bdstevel@tonic-gate
2877c478bdstevel@tonic-gate	for (s = 0, ap = people; *ap; ap++)
2887c478bdstevel@tonic-gate		s += strlen(*ap) + 2;
2897c478bdstevel@tonic-gate	buf = (char *)salloc((unsigned)(s+1));
2907c478bdstevel@tonic-gate	cp2 = buf;
2917c478bdstevel@tonic-gate	for (ap = people; *ap; ap++) {
2927c478bdstevel@tonic-gate		for (cp3 = *ap; *cp3; ) {
2937c478bdstevel@tonic-gate			if (*cp3 == ' ' || *cp3 == '\t') {
2947c478bdstevel@tonic-gate				*cp3++ = ',';
2957c478bdstevel@tonic-gate				while (*cp3 == ' ' || *cp3 == '\t')
2967c478bdstevel@tonic-gate					cp3++;
2977c478bdstevel@tonic-gate			} else
2987c478bdstevel@tonic-gate				cp3++;
2997c478bdstevel@tonic-gate		}
3007c478bdstevel@tonic-gate		cp2 = copy(*ap, cp2);
3017c478bdstevel@tonic-gate		*cp2++ = ',';
3027c478bdstevel@tonic-gate		*cp2++ = ' ';
3037c478bdstevel@tonic-gate	}
3047c478bdstevel@tonic-gate	*cp2 = '\0';
3057c478bdstevel@tonic-gate	head.h_to = buf;
3067c478bdstevel@tonic-gate	head.h_subject = head.h_cc = head.h_bcc = head.h_defopt = NOSTR;
3077c478bdstevel@tonic-gate	head.h_others = NOSTRPTR;
3087c478bdstevel@tonic-gate	head.h_seq = 0;
3097c478bdstevel@tonic-gate	mail1(&head, Fflag, NOSTR);
3107c478bdstevel@tonic-gate	return(0);
3117c478bdstevel@tonic-gate}
3127c478bdstevel@tonic-gate
31348bbca8Daniel Hoffmanint
3147c478bdstevel@tonic-gatesendm(char *str)
3157c478bdstevel@tonic-gate{
3167c478bdstevel@tonic-gate	if (value("flipm") != NOSTR)
3177c478bdstevel@tonic-gate		return(Sendmail(str));
3187c478bdstevel@tonic-gate	else return(sendmail(str));
3197c478bdstevel@tonic-gate}
3207c478bdstevel@tonic-gate
32148bbca8Daniel Hoffmanint
3227c478bdstevel@tonic-gateSendm(char *str)
3237c478bdstevel@tonic-gate{
3247c478bdstevel@tonic-gate	if (value("flipm") != NOSTR)
3257c478bdstevel@tonic-gate		return(sendmail(str));
3267c478bdstevel@tonic-gate	else return(Sendmail(str));
3277c478bdstevel@tonic-gate}
3287c478bdstevel@tonic-gate
3297c478bdstevel@tonic-gate/*
3307c478bdstevel@tonic-gate * Interface to the mail1 routine for the -t flag
3317c478bdstevel@tonic-gate * (read headers from text).
3327c478bdstevel@tonic-gate */
33348bbca8Daniel Hoffmanint
3347c478bdstevel@tonic-gatetmail(void)
3357c478bdstevel@tonic-gate{
3367c478bdstevel@tonic-gate	struct header head;
3377c478bdstevel@tonic-gate
3387c478bdstevel@tonic-gate	head.h_to = NOSTR;
3397c478bdstevel@tonic-gate	head.h_subject = head.h_cc = head.h_bcc = head.h_defopt = NOSTR;
3407c478bdstevel@tonic-gate	head.h_others = NOSTRPTR;
3417c478bdstevel@tonic-gate	head.h_seq = 0;
3427c478bdstevel@tonic-gate	mail1(&head, Fflag, NOSTR);
3437c478bdstevel@tonic-gate	return(0);
3447c478bdstevel@tonic-gate}
3457c478bdstevel@tonic-gate
3467c478bdstevel@tonic-gate/*
3477c478bdstevel@tonic-gate * Send mail to a bunch of user names.  The interface is through
3487c478bdstevel@tonic-gate * the mail routine below.
3497c478bdstevel@tonic-gate */
35048bbca8Daniel Hoffmanstatic int
3517c478bdstevel@tonic-gatesendmail(char *str)
3527c478bdstevel@tonic-gate{
3537c478bdstevel@tonic-gate	struct header head;
3547c478bdstevel@tonic-gate
3557c478bdstevel@tonic-gate	if (blankline(str))
3567c478bdstevel@tonic-gate		head.h_to = NOSTR;
3577c478bdstevel@tonic-gate	else
3587c478bdstevel@tonic-gate		head.h_to = addto(NOSTR, str);
3597c478bdstevel@tonic-gate	head.h_subject = head.h_cc = head.h_bcc = head.h_defopt = NOSTR;
3607c478bdstevel@tonic-gate	head.h_others = NOSTRPTR;
3617c478bdstevel@tonic-gate	head.h_seq = 0;
3627c478bdstevel@tonic-gate	mail1(&head, 0, NOSTR);
3637c478bdstevel@tonic-gate	return(0);
3647c478bdstevel@tonic-gate}
3657c478bdstevel@tonic-gate
3667c478bdstevel@tonic-gate/*
3677c478bdstevel@tonic-gate * Send mail to a bunch of user names.  The interface is through
3687c478bdstevel@tonic-gate * the mail routine below.
3697c478bdstevel@tonic-gate * save a copy of the letter
3707c478bdstevel@tonic-gate */
37148bbca8Daniel Hoffmanstatic int
3727c478bdstevel@tonic-gateSendmail(char *str)
3737c478bdstevel@tonic-gate{
3747c478bdstevel@tonic-gate	struct header head;
3757c478bdstevel@tonic-gate
3767c478bdstevel@tonic-gate	if (blankline(str))
3777c478bdstevel@tonic-gate		head.h_to = NOSTR;
3787c478bdstevel@tonic-gate	else
3797c478bdstevel@tonic-gate		head.h_to = addto(NOSTR, str);
3807c478bdstevel@tonic-gate	head.h_subject = head.h_cc = head.h_bcc = head.h_defopt = NOSTR;
3817c478bdstevel@tonic-gate	head.h_others = NOSTRPTR;
3827c478bdstevel@tonic-gate	head.h_seq = 0;
3837c478bdstevel@tonic-gate	mail1(&head, 1, NOSTR);
3847c478bdstevel@tonic-gate	return(0);
3857c478bdstevel@tonic-gate}
3867c478bdstevel@tonic-gate
3877c478bdstevel@tonic-gate/*
3887c478bdstevel@tonic-gate * Walk the list of fds, closing all but one.
3897c478bdstevel@tonic-gate */
3907c478bdstevel@tonic-gatestatic int
3917c478bdstevel@tonic-gateclosefd_walk(void *special_fd, int fd)
3927c478bdstevel@tonic-gate{
3937c478bdstevel@tonic-gate	if (fd > STDERR_FILENO && fd != *(int *)special_fd)
3947c478bdstevel@tonic-gate		(void) close(fd);
3957c478bdstevel@tonic-gate	return (0);
3967c478bdstevel@tonic-gate}
3977c478bdstevel@tonic-gate
3987c478bdstevel@tonic-gate/*
3997c478bdstevel@tonic-gate * Mail a message on standard input to the people indicated
4007c478bdstevel@tonic-gate * in the passed header.  (Internal interface).
4017c478bdstevel@tonic-gate */
40248bbca8Daniel Hoffmanvoid
4037c478bdstevel@tonic-gatemail1(struct header *hp, int use_to, char *orig_to)
4047c478bdstevel@tonic-gate{
4057c478bdstevel@tonic-gate	pid_t p, pid;
4067c478bdstevel@tonic-gate	int i, s, gotcha;
4077c478bdstevel@tonic-gate	char **namelist, *deliver;
4087c478bdstevel@tonic-gate	struct name *to, *np;
4097c478bdstevel@tonic-gate	FILE *mtf, *fp;
4107c478bdstevel@tonic-gate	int remote = rflag != NOSTR || rmail;
4117c478bdstevel@tonic-gate	char **t;
4127c478bdstevel@tonic-gate	char *deadletter;
4137c478bdstevel@tonic-gate	char recfile[PATHSIZE];
4147c478bdstevel@tonic-gate
4157c478bdstevel@tonic-gate	/*
4167c478bdstevel@tonic-gate	 * Collect user's mail from standard input.
4177c478bdstevel@tonic-gate	 * Get the result as mtf.
4187c478bdstevel@tonic-gate	 */
4197c478bdstevel@tonic-gate
4207c478bdstevel@tonic-gate	pid = (pid_t)-1;
4217c478bdstevel@tonic-gate	if ((mtf = collect(hp)) == NULL)
4227c478bdstevel@tonic-gate		return;
4237c478bdstevel@tonic-gate	hp->h_seq = 1;
4247c478bdstevel@tonic-gate	if (hp->h_subject == NOSTR)
4257c478bdstevel@tonic-gate		hp->h_subject = sflag;
4267c478bdstevel@tonic-gate	if (fsize(mtf) == 0 && hp->h_subject == NOSTR) {
4277c478bdstevel@tonic-gate		printf(gettext("No message !?!\n"));
4287c478bdstevel@tonic-gate		goto out;
4297c478bdstevel@tonic-gate	}
4307c478bdstevel@tonic-gate	if (intty) {
4317c478bdstevel@tonic-gate		printf(gettext("EOT\n"));
4327c478bdstevel@tonic-gate		flush();
4337c478bdstevel@tonic-gate	}
4347c478bdstevel@tonic-gate
4357c478bdstevel@tonic-gate	/*
4367c478bdstevel@tonic-gate	 * If we need to use the To: line to determine the record
4377c478bdstevel@tonic-gate	 * file, save a copy of it before it's sorted below.
4387c478bdstevel@tonic-gate	 */
4397c478bdstevel@tonic-gate
4407c478bdstevel@tonic-gate	if (use_to && orig_to == NOSTR && hp->h_to != NOSTR)
4417c478bdstevel@tonic-gate		orig_to = strcpy((char *)salloc(strlen(hp->h_to)+1), hp->h_to);
4427c478bdstevel@tonic-gate	else if (orig_to == NOSTR)
4437c478bdstevel@tonic-gate		orig_to = "";
4447c478bdstevel@tonic-gate
4457c478bdstevel@tonic-gate	/*
4467c478bdstevel@tonic-gate	 * Now, take the user names from the combined
4477c478bdstevel@tonic-gate	 * to and cc lists and do all the alias
4487c478bdstevel@tonic-gate	 * processing.
4497c478bdstevel@tonic-gate	 */
4507c478bdstevel@tonic-gate
4517c478bdstevel@tonic-gate	senderr = 0;
4527c478bdstevel@tonic-gate	to = cat(extract(hp->h_bcc, GBCC),
4537c478bdstevel@tonic-gate	     cat(extract(hp->h_to, GTO),
4547c478bdstevel@tonic-gate	     extract(hp->h_cc, GCC)));
4557c478bdstevel@tonic-gate	to = translate(outpre(elide(usermap(to))));
4567c478bdstevel@tonic-gate	if (!senderr)
4577c478bdstevel@tonic-gate		mapf(to, myname);
4587c478bdstevel@tonic-gate	mechk(to);
4597c478bdstevel@tonic-gate	for (gotcha = 0, np = to; np != NIL; np = np->n_flink)
4607c478bdstevel@tonic-gate		if ((np->n_type & GDEL) == 0)
4617c478bdstevel@tonic-gate			gotcha++;
4627c478bdstevel@tonic-gate	hp->h_to = detract(to, GTO);
4637c478bdstevel@tonic-gate	hp->h_cc = detract(to, GCC);
4647c478bdstevel@tonic-gate	hp->h_bcc = detract(to, GBCC);
4657c478bdstevel@tonic-gate	if ((mtf = infix(hp, mtf)) == NULL) {
4667c478bdstevel@tonic-gate		fprintf(stderr, gettext(". . . message lost, sorry.\n"));
4677c478bdstevel@tonic-gate		return;
4687c478bdstevel@tonic-gate	}
4697c478bdstevel@tonic-gate	rewind(mtf);
4707c478bdstevel@tonic-gate	if (askme && isatty(0)) {
4717c478bdstevel@tonic-gate		char ans[64];
4727c478bdstevel@tonic-gate		puthead(hp, stdout, GTO|GCC|GBCC, 0);
4737c478bdstevel@tonic-gate		printf(gettext("Send? "));
4747c478bdstevel@tonic-gate		printf("[yes] ");
4757c478bdstevel@tonic-gate		if (fgets(ans, sizeof(ans), stdin) && ans[0] &&
4767c478bdstevel@tonic-gate				(tolower(ans[0]) != 'y' && ans[0] != '\n'))
4777c478bdstevel@tonic-gate			goto dead;
4787c478bdstevel@tonic-gate	}
4797c478bdstevel@tonic-gate	if (senderr)
4807c478bdstevel@tonic-gate		goto dead;
4817c478bdstevel@tonic-gate	/*
4827c478bdstevel@tonic-gate	 * Look through the recipient list for names with /'s
4837c478bdstevel@tonic-gate	 * in them which we write to as files directly.
4847c478bdstevel@tonic-gate	 */
4857c478bdstevel@tonic-gate	i = outof(to, mtf);
4867c478bdstevel@tonic-gate	rewind(mtf);
4877c478bdstevel@tonic-gate	if (!gotcha && !i) {
4887c478bdstevel@tonic-gate		printf(gettext("No recipients specified\n"));
4897c478bdstevel@tonic-gate		goto dead;
4907c478bdstevel@tonic-gate	}
4917c478bdstevel@tonic-gate	if (senderr)
4927c478bdstevel@tonic-gate		goto dead;
4937c478bdstevel@tonic-gate
4947c478bdstevel@tonic-gate	getrecf(orig_to, recfile, use_to, sizeof (recfile));
4957c478bdstevel@tonic-gate	if (recfile != NOSTR && *recfile)
4967c478bdstevel@tonic-gate		savemail(safeexpand(recfile), hp, mtf);
4977c478bdstevel@tonic-gate	if (!gotcha)
4987c478bdstevel@tonic-gate		goto out;
4997c478bdstevel@tonic-gate	namelist = unpack(to);
5007c478bdstevel@tonic-gate	if (debug) {
5017c478bdstevel@tonic-gate		fprintf(stderr, "Recipients of message:\n");
5027c478bdstevel@tonic-gate		for (t = namelist; *t != NOSTR; t++)
5037c478bdstevel@tonic-gate			fprintf(stderr, " \"%s\"", *t);
5047c478bdstevel@tonic-gate		fprintf(stderr, "\n");
5057c478bdstevel@tonic-gate		return;
5067c478bdstevel@tonic-gate	}
5077c478bdstevel@tonic-gate
5087c478bdstevel@tonic-gate	/*
5097c478bdstevel@tonic-gate	 * Wait, to absorb a potential zombie, then
5107c478bdstevel@tonic-gate	 * fork, set up the temporary mail file as standard
5117c478bdstevel@tonic-gate	 * input for "mail" and exec with the user list we generated
51248bbca8Daniel Hoffman	 * far above. Return the process id to caller in case it
5137c478bdstevel@tonic-gate	 * wants to await the completion of mail.
5147c478bdstevel@tonic-gate	 */
5157c478bdstevel@tonic-gate
5167c478bdstevel@tonic-gate#ifdef VMUNIX
5177c478bdstevel@tonic-gate	while (wait3((int *)0, WNOHANG, (struct rusage *)0) > 0)
5187c478bdstevel@tonic-gate		;
5197c478bdstevel@tonic-gate#else
5207c478bdstevel@tonic-gate#ifdef preSVr4
5217c478bdstevel@tonic-gate	wait((int *)0);
5227c478bdstevel@tonic-gate#else
5237c478bdstevel@tonic-gate	while (waitpid((pid_t)-1, (int *)0, WNOHANG) > 0)
5247c478bdstevel@tonic-gate		;
5257c478bdstevel@tonic-gate#endif
5267c478bdstevel@tonic-gate#endif
5277c478bdstevel@tonic-gate	rewind(mtf);
5287c478bdstevel@tonic-gate	pid = fork();
5297c478bdstevel@tonic-gate	if (pid == (pid_t)-1) {
5307c478bdstevel@tonic-gate		perror("fork");
5317c478bdstevel@tonic-gatedead:
5327c478bdstevel@tonic-gate		deadletter = Getf("DEAD");
5337c478bdstevel@tonic-gate		if (fp = fopen(deadletter,
5347c478bdstevel@tonic-gate		    value("appenddeadletter") == NOSTR ? "w" : "a")) {
5357c478bdstevel@tonic-gate			chmod(deadletter, DEADPERM);
5367c478bdstevel@tonic-gate			puthead(hp, fp, GMASK|GCLEN, fsize(mtf) - textpos);
5377c478bdstevel@tonic-gate			fseek(mtf, textpos, 0);
5387c478bdstevel@tonic-gate			lcwrite(deadletter, mtf, fp,
5397c478bdstevel@tonic-gate			    value("appenddeadletter") != NOSTR);
5407c478bdstevel@tonic-gate			fclose(fp);
5417c478bdstevel@tonic-gate		} else
5427c478bdstevel@tonic-gate			perror(deadletter);
5437c478bdstevel@tonic-gate		goto out;
5447c478bdstevel@tonic-gate	}
5457c478bdstevel@tonic-gate	if (pid == 0) {
5467c478bdstevel@tonic-gate		sigchild();
5477c478bdstevel@tonic-gate#ifdef SIGTSTP
5487c478bdstevel@tonic-gate		if (remote == 0) {
5497c478bdstevel@tonic-gate			sigset(SIGTSTP, SIG_IGN);
5507c478bdstevel@tonic-gate			sigset(SIGTTIN, SIG_IGN);
5517c478bdstevel@tonic-gate			sigset(SIGTTOU, SIG_IGN);
5527c478bdstevel@tonic-gate		}
5537c478bdstevel@tonic-gate#endif
5547c478bdstevel@tonic-gate		sigset(SIGHUP, SIG_IGN);
5557c478bdstevel@tonic-gate		sigset(SIGINT, SIG_IGN);
5567c478bdstevel@tonic-gate		sigset(SIGQUIT, SIG_IGN);
5577c478bdstevel@tonic-gate		s = fileno(mtf);
5587c478bdstevel@tonic-gate		(void) fdwalk(closefd_walk, &s);
5597c478bdstevel@tonic-gate		close(0);
5607c478bdstevel@tonic-gate		dup(s);
5617c478bdstevel@tonic-gate		close(s);
5627c478bdstevel@tonic-gate#ifdef CC
5637c478bdstevel@tonic-gate		submit(getpid());
5647c478bdstevel@tonic-gate#endif /* CC */
5657c478bdstevel@tonic-gate		if ((deliver = value("sendmail")) == NOSTR)
5667c478bdstevel@tonic-gate#ifdef SENDMAIL
5677c478bdstevel@tonic-gate			deliver = SENDMAIL;
5687c478bdstevel@tonic-gate#else
5697c478bdstevel@tonic-gate			deliver = MAIL;
5707c478bdstevel@tonic-gate#endif
5717c478bdstevel@tonic-gate		execvp(safeexpand(deliver), namelist);
5727c478bdstevel@tonic-gate		perror(deliver);
5737c478bdstevel@tonic-gate		exit(1);
5747c478bdstevel@tonic-gate	}
5757c478bdstevel@tonic-gate
5767c478bdstevel@tonic-gate	if (value("sendwait")!=NOSTR)
5777c478bdstevel@tonic-gate		remote++;
5787c478bdstevel@tonic-gateout:
5797c478bdstevel@tonic-gate	if (remote) {
5807c478bdstevel@tonic-gate		while ((p = wait(&s)) != pid && p != (pid_t)-1)
5817c478bdstevel@tonic-gate			;
5827c478bdstevel@tonic-gate		if (s != 0)
5837c478bdstevel@tonic-gate			senderr++;
5847c478bdstevel@tonic-gate		pid = 0;
5857c478bdstevel@tonic-gate	}
5867c478bdstevel@tonic-gate	fclose(mtf);
5877c478bdstevel@tonic-gate	return;
5887c478bdstevel@tonic-gate}
5897c478bdstevel@tonic-gate
5907c478bdstevel@tonic-gate/*
5917c478bdstevel@tonic-gate * Prepend a header in front of the collected stuff
5927c478bdstevel@tonic-gate * and return the new file.
5937c478bdstevel@tonic-gate */
5947c478bdstevel@tonic-gate
5957c478bdstevel@tonic-gatestatic FILE *
5967c478bdstevel@tonic-gateinfix(struct header *hp, FILE *fi)
5977c478bdstevel@tonic-gate{
5987c478bdstevel@tonic-gate	register FILE *nfo, *nfi;
5997c478bdstevel@tonic-gate	register int c;
6007c478bdstevel@tonic-gate	char *postmark, *returnaddr;
6017c478bdstevel@tonic-gate	int fd = -1;
6027c478bdstevel@tonic-gate
6037c478bdstevel@tonic-gate	rewind(fi);
6047c478bdstevel@tonic-gate	if ((fd = open(tempMail, O_RDWR|O_CREAT|O_EXCL, 0600)) < 0 ||
6057c478bdstevel@tonic-gate	(nfo = fdopen(fd, "w")) == NULL) {
6067c478bdstevel@tonic-gate		perror(tempMail);
6077c478bdstevel@tonic-gate		return(fi);
6087c478bdstevel@tonic-gate	}
6097c478bdstevel@tonic-gate	if ((nfi = fopen(tempMail, "r")) == NULL) {
6107c478bdstevel@tonic-gate		perror(tempMail);
6117c478bdstevel@tonic-gate		fclose(nfo);
6127c478bdstevel@tonic-gate		return(fi);
6137c478bdstevel@tonic-gate	}
6147c478bdstevel@tonic-gate	removefile(tempMail);
6157c478bdstevel@tonic-gate	postmark = value("postmark");
6167c478bdstevel@tonic-gate	returnaddr = value("returnaddr");
6177c478bdstevel@tonic-gate	if ((postmark != NOSTR) || (returnaddr != NOSTR)) {
6187c478bdstevel@tonic-gate		if (returnaddr && *returnaddr)
6197c478bdstevel@tonic-gate			fprintf(nfo, "From: %s", returnaddr);
6207c478bdstevel@tonic-gate		else
6217c478bdstevel@tonic-gate			fprintf(nfo, "From: %s@%s", myname, host);
6227c478bdstevel@tonic-gate		if (postmark && *postmark)
6237c478bdstevel@tonic-gate			fprintf(nfo, " (%s)", postmark);
6247c478bdstevel@tonic-gate		putc('\n', nfo);
6257c478bdstevel@tonic-gate	}
6267c478bdstevel@tonic-gate	puthead(hp, nfo, (GMASK & ~GBCC) | GCLEN, fsize(fi));
6277c478bdstevel@tonic-gate	textpos = ftell(nfo);
6287c478bdstevel@tonic-gate	while ((c = getc(fi)) != EOF)
6297c478bdstevel@tonic-gate		putc(c, nfo);
6307c478bdstevel@tonic-gate	if (ferror(fi)) {
6317c478bdstevel@tonic-gate		perror("read");
6327c478bdstevel@tonic-gate		return(fi);
6337c478bdstevel@tonic-gate	}
6347c478bdstevel@tonic-gate	fflush(nfo);
6357c478bdstevel@tonic-gate	if (fferror(nfo)) {
6367c478bdstevel@tonic-gate		perror(tempMail);
6377c478bdstevel@tonic-gate		fclose(nfo);
6387c478bdstevel@tonic-gate		fclose(nfi);
6397c478bdstevel@tonic-gate		return(fi);
6407c478bdstevel@tonic-gate	}
6417c478bdstevel@tonic-gate	fclose(nfo);
6427c478bdstevel@tonic-gate	fclose(fi);
6437c478bdstevel@tonic-gate	rewind(nfi);
6447c478bdstevel@tonic-gate	return(nfi);
6457c478bdstevel@tonic-gate}
6467c478bdstevel@tonic-gate
6477c478bdstevel@tonic-gate/*
6487c478bdstevel@tonic-gate * Dump the message header on the
6497c478bdstevel@tonic-gate * passed file buffer.
6507c478bdstevel@tonic-gate */
6517c478bdstevel@tonic-gate
6526c83d09robbinint
6537c478bdstevel@tonic-gateputhead(struct header *hp, FILE *fo, int w, long clen)
6547c478bdstevel@tonic-gate{
6557c478bdstevel@tonic-gate	register int gotcha;
6567c478bdstevel@tonic-gate
6577c478bdstevel@tonic-gate	gotcha = 0;
6587c478bdstevel@tonic-gate	if (hp->h_to != NOSTR && (w & GTO))
6597c478bdstevel@tonic-gate		fprintf(fo, "To: "), fmt(hp->h_to, fo), gotcha++;
6607c478bdstevel@tonic-gate	if ((w & GSUBJECT) && (int)value("bsdcompat"))
6617c478bdstevel@tonic-gate		if (hp->h_subject != NOSTR && *hp->h_subject)
6627c478bdstevel@tonic-gate			fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++;
6637c478bdstevel@tonic-gate		else
6647c478bdstevel@tonic-gate			if (sflag && *sflag)
6657c478bdstevel@tonic-gate				fprintf(fo, "Subject: %s\n", sflag), gotcha++;
6667c478bdstevel@tonic-gate	if (hp->h_cc != NOSTR && (w & GCC))
6677c478bdstevel@tonic-gate		fprintf(fo, "Cc: "), fmt(hp->h_cc, fo), gotcha++;
6687c478bdstevel@tonic-gate	if (hp->h_bcc != NOSTR && (w & GBCC))
6697c478bdstevel@tonic-gate		fprintf(fo, "Bcc: "), fmt(hp->h_bcc, fo), gotcha++;
6707c478bdstevel@tonic-gate	if (hp->h_defopt != NOSTR && (w & GDEFOPT))
6717c478bdstevel@tonic-gate		if (receipt_flg)
6727c478bdstevel@tonic-gate			fprintf(fo, "Return-Receipt-To: %s\n",
6737c478bdstevel@tonic-gate				hp->h_defopt), gotcha++;
6747c478bdstevel@tonic-gate		else
6757c478bdstevel@tonic-gate		fprintf(fo, "Default-Options: %s\n", hp->h_defopt), gotcha++;
6767c478bdstevel@tonic-gate	if ((w & GSUBJECT) && !(int)value("bsdcompat"))
6777c478bdstevel@tonic-gate		if (hp->h_subject != NOSTR && *hp->h_subject)
6787c478bdstevel@tonic-gate			fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++;
6797c478bdstevel@tonic-gate		else
6807c478bdstevel@tonic-gate			if (sflag && *sflag)
6817c478bdstevel@tonic-gate				fprintf(fo, "Subject: %s\n", sflag), gotcha++;
6827c478bdstevel@tonic-gate	if (hp->h_others != NOSTRPTR && (w & GOTHER)) {
6837c478bdstevel@tonic-gate		char **p;
6847c478bdstevel@tonic-gate		for (p = hp->h_others; *p; p++)
6857c478bdstevel@tonic-gate			fprintf(fo, "%s\n", *p);
6867c478bdstevel@tonic-gate		gotcha++;
6877c478bdstevel@tonic-gate	}
6887c478bdstevel@tonic-gate#ifndef preSVr4
6897c478bdstevel@tonic-gate	if (w & GCLEN)
6907c478bdstevel@tonic-gate		fprintf(fo, "Content-Length: %ld\n", clen), gotcha++;
6917c478bdstevel@tonic-gate#endif
6927c478bdstevel@tonic-gate	if (gotcha && (w & GNL))
6937c478bdstevel@tonic-gate		putc('\n', fo);
6947c478bdstevel@tonic-gate	return(0);
6957c478bdstevel@tonic-gate}
6967c478bdstevel@tonic-gate
6977c478bdstevel@tonic-gate/*
6987c478bdstevel@tonic-gate * Format the given text to not exceed 78 characters.
6997c478bdstevel@tonic-gate */
7007c478bdstevel@tonic-gatestatic void
7017c478bdstevel@tonic-gatefmt(register char *str, register FILE *fo)
7027c478bdstevel@tonic-gate{
7037c478bdstevel@tonic-gate	register int col = 4;
7047c478bdstevel@tonic-gate	char name[256];
7057c478bdstevel@tonic-gate	int len;
7067c478bdstevel@tonic-gate
7077c478bdstevel@tonic-gate	str = strcpy((char *)salloc(strlen(str)+1), str);
7087c478bdstevel@tonic-gate	while (str = yankword(str, name, sizeof (name), 1)) {
7097c478bdstevel@tonic-gate		len = strlen(name);
7107c478bdstevel@tonic-gate		if (col > 4) {
7117c478bdstevel@tonic-gate			if (col + len > 76) {
7127c478bdstevel@tonic-gate				fputs(",\n    ", fo);
7137c478bdstevel@tonic-gate				col = 4;
7147c478bdstevel@tonic-gate			} else {
7157c478bdstevel@tonic-gate				fputs(", ", fo);
7167c478bdstevel@tonic-gate				col += 2;
7177c478bdstevel@tonic-gate			}
7187c478bdstevel@tonic-gate		}
7197c478bdstevel@tonic-gate		fputs(name, fo);
7207c478bdstevel@tonic-gate		col += len;
7217c478bdstevel@tonic-gate	}
7227c478bdstevel@tonic-gate	putc('\n', fo);
7237c478bdstevel@tonic-gate}
7247c478bdstevel@tonic-gate
7257c478bdstevel@tonic-gate/*
7267c478bdstevel@tonic-gate * Save the outgoing mail on the passed file.
7277c478bdstevel@tonic-gate */
7287c478bdstevel@tonic-gatestatic int
7297c478bdstevel@tonic-gatesavemail(char name[], struct header *hp, FILE *fi)
7307c478bdstevel@tonic-gate{
7317c478bdstevel@tonic-gate	register FILE *fo;
7327c478bdstevel@tonic-gate	time_t now;
7337c478bdstevel@tonic-gate	char *n;
7347c478bdstevel@tonic-gate#ifdef preSVr4
7357c478bdstevel@tonic-gate	char line[BUFSIZ];
7367c478bdstevel@tonic-gate#else
7377c478bdstevel@tonic-gate	int c;
7387c478bdstevel@tonic-gate#endif
7397c478bdstevel@tonic-gate
7407c478bdstevel@tonic-gate	if (debug)
7417c478bdstevel@tonic-gate		fprintf(stderr, gettext("save in '%s'\n"), name);
7427c478bdstevel@tonic-gate	if ((fo = fopen(name, "a")) == NULL) {
7437c478bdstevel@tonic-gate		perror(name);
7447c478bdstevel@tonic-gate		return(-1);
7457c478bdstevel@tonic-gate	}
7467c478bdstevel@tonic-gate	time(&now);
7477c478bdstevel@tonic-gate	n = rflag;
7487c478bdstevel@tonic-gate	if (n == NOSTR)
7497c478bdstevel@tonic-gate		n = myname;
7507c478bdstevel@tonic-gate	fprintf(fo, "From %s %s", n, ctime(&now));
7517c478bdstevel@tonic-gate	puthead(hp, fo, GMASK|GCLEN, fsize(fi) - textpos);
7527c478bdstevel@tonic-gate	fseek(fi, textpos, 0);
7537c478bdstevel@tonic-gate#ifdef preSVr4
7547c478bdstevel@tonic-gate	while (fgets(line, sizeof line, fi)) {
7557c478bdstevel@tonic-gate		if (!strncmp(line, "From ", 5))
7567c478bdstevel@tonic-gate			putc('>', fo);
7577c478bdstevel@tonic-gate		fputs(line, fo);
7587c478bdstevel@tonic-gate	}
7597c478bdstevel@tonic-gate#else
7607c478bdstevel@tonic-gate	while ((c = getc(fi)) != EOF)
7617c478bdstevel@tonic-gate		putc(c, fo);
7627c478bdstevel@tonic-gate#endif
7637c478bdstevel@tonic-gate	putc('\n', fo);
7647c478bdstevel@tonic-gate	fflush(fo);
7657c478bdstevel@tonic-gate	if (fferror(fo))
7667c478bdstevel@tonic-gate		perror(name);
7677c478bdstevel@tonic-gate	fclose(fo);
7687c478bdstevel@tonic-gate	return(0);
7697c478bdstevel@tonic-gate}
770