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