Home | History | Annotate | Line # | Download | only in mail
send.c revision 1.3
      1  1.1      cgd /*
      2  1.1      cgd  * Copyright (c) 1980 Regents of the University of California.
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1      cgd  * modification, are permitted provided that the following conditions
      7  1.1      cgd  * are met:
      8  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1      cgd  *    must display the following acknowledgement:
     15  1.1      cgd  *	This product includes software developed by the University of
     16  1.1      cgd  *	California, Berkeley and its contributors.
     17  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1      cgd  *    may be used to endorse or promote products derived from this software
     19  1.1      cgd  *    without specific prior written permission.
     20  1.1      cgd  *
     21  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1      cgd  * SUCH DAMAGE.
     32  1.1      cgd  */
     33  1.1      cgd 
     34  1.1      cgd #ifndef lint
     35  1.2  mycroft /*static char sccsid[] = "from: @(#)send.c	5.23 (Berkeley) 2/9/91";*/
     36  1.3      jtc static char rcsid[] = "$Id: send.c,v 1.3 1993/08/27 20:31:52 jtc Exp $";
     37  1.1      cgd #endif /* not lint */
     38  1.1      cgd 
     39  1.1      cgd #include "rcv.h"
     40  1.1      cgd 
     41  1.1      cgd /*
     42  1.1      cgd  * Mail -- a mail program
     43  1.1      cgd  *
     44  1.1      cgd  * Mail to others.
     45  1.1      cgd  */
     46  1.1      cgd 
     47  1.1      cgd /*
     48  1.1      cgd  * Send message described by the passed pointer to the
     49  1.1      cgd  * passed output buffer.  Return -1 on error.
     50  1.1      cgd  * Adjust the status: field if need be.
     51  1.1      cgd  * If doign is given, suppress ignored header fields.
     52  1.1      cgd  * prefix is a string to prepend to each output line.
     53  1.1      cgd  */
     54  1.1      cgd send(mp, obuf, doign, prefix)
     55  1.1      cgd 	register struct message *mp;
     56  1.1      cgd 	FILE *obuf;
     57  1.1      cgd 	struct ignoretab *doign;
     58  1.1      cgd 	char *prefix;
     59  1.1      cgd {
     60  1.1      cgd 	long count;
     61  1.1      cgd 	register FILE *ibuf;
     62  1.1      cgd 	char line[LINESIZE];
     63  1.1      cgd 	int ishead, infld, ignoring, dostat, firstline;
     64  1.1      cgd 	register char *cp, *cp2;
     65  1.1      cgd 	register int c;
     66  1.1      cgd 	int length;
     67  1.1      cgd 	int prefixlen;
     68  1.1      cgd 
     69  1.1      cgd 	/*
     70  1.1      cgd 	 * Compute the prefix string, without trailing whitespace
     71  1.1      cgd 	 */
     72  1.1      cgd 	if (prefix != NOSTR) {
     73  1.1      cgd 		cp2 = 0;
     74  1.1      cgd 		for (cp = prefix; *cp; cp++)
     75  1.1      cgd 			if (*cp != ' ' && *cp != '\t')
     76  1.1      cgd 				cp2 = cp;
     77  1.1      cgd 		prefixlen = cp2 == 0 ? 0 : cp2 - prefix + 1;
     78  1.1      cgd 	}
     79  1.1      cgd 	ibuf = setinput(mp);
     80  1.1      cgd 	count = mp->m_size;
     81  1.1      cgd 	ishead = 1;
     82  1.1      cgd 	dostat = doign == 0 || !isign("status", doign);
     83  1.1      cgd 	infld = 0;
     84  1.1      cgd 	firstline = 1;
     85  1.1      cgd 	/*
     86  1.1      cgd 	 * Process headers first
     87  1.1      cgd 	 */
     88  1.1      cgd 	while (count > 0 && ishead) {
     89  1.1      cgd 		if (fgets(line, LINESIZE, ibuf) == NULL)
     90  1.1      cgd 			break;
     91  1.1      cgd 		count -= length = strlen(line);
     92  1.1      cgd 		if (firstline) {
     93  1.1      cgd 			/*
     94  1.1      cgd 			 * First line is the From line, so no headers
     95  1.1      cgd 			 * there to worry about
     96  1.1      cgd 			 */
     97  1.1      cgd 			firstline = 0;
     98  1.1      cgd 			ignoring = doign == ignoreall;
     99  1.1      cgd 		} else if (line[0] == '\n') {
    100  1.1      cgd 			/*
    101  1.1      cgd 			 * If line is blank, we've reached end of
    102  1.1      cgd 			 * headers, so force out status: field
    103  1.1      cgd 			 * and note that we are no longer in header
    104  1.1      cgd 			 * fields
    105  1.1      cgd 			 */
    106  1.1      cgd 			if (dostat) {
    107  1.1      cgd 				statusput(mp, obuf, prefix);
    108  1.1      cgd 				dostat = 0;
    109  1.1      cgd 			}
    110  1.1      cgd 			ishead = 0;
    111  1.1      cgd 			ignoring = doign == ignoreall;
    112  1.1      cgd 		} else if (infld && (line[0] == ' ' || line[0] == '\t')) {
    113  1.1      cgd 			/*
    114  1.1      cgd 			 * If this line is a continuation (via space or tab)
    115  1.1      cgd 			 * of a previous header field, just echo it
    116  1.1      cgd 			 * (unless the field should be ignored).
    117  1.1      cgd 			 * In other words, nothing to do.
    118  1.1      cgd 			 */
    119  1.1      cgd 		} else {
    120  1.1      cgd 			/*
    121  1.1      cgd 			 * Pick up the header field if we have one.
    122  1.1      cgd 			 */
    123  1.1      cgd 			for (cp = line; (c = *cp++) && c != ':' && !isspace(c);)
    124  1.1      cgd 				;
    125  1.1      cgd 			cp2 = --cp;
    126  1.1      cgd 			while (isspace(*cp++))
    127  1.1      cgd 				;
    128  1.1      cgd 			if (cp[-1] != ':') {
    129  1.1      cgd 				/*
    130  1.1      cgd 				 * Not a header line, force out status:
    131  1.1      cgd 				 * This happens in uucp style mail where
    132  1.1      cgd 				 * there are no headers at all.
    133  1.1      cgd 				 */
    134  1.1      cgd 				if (dostat) {
    135  1.1      cgd 					statusput(mp, obuf, prefix);
    136  1.1      cgd 					dostat = 0;
    137  1.1      cgd 				}
    138  1.1      cgd 				if (doign != ignoreall)
    139  1.1      cgd 					/* add blank line */
    140  1.1      cgd 					(void) putc('\n', obuf);
    141  1.1      cgd 				ishead = 0;
    142  1.1      cgd 				ignoring = 0;
    143  1.1      cgd 			} else {
    144  1.1      cgd 				/*
    145  1.1      cgd 				 * If it is an ignored field and
    146  1.1      cgd 				 * we care about such things, skip it.
    147  1.1      cgd 				 */
    148  1.1      cgd 				*cp2 = 0;	/* temporarily null terminate */
    149  1.1      cgd 				if (doign && isign(line, doign))
    150  1.1      cgd 					ignoring = 1;
    151  1.1      cgd 				else if ((line[0] == 's' || line[0] == 'S') &&
    152  1.1      cgd 					 strcasecmp(line, "status") == 0) {
    153  1.1      cgd 					/*
    154  1.1      cgd 					 * If the field is "status," go compute
    155  1.1      cgd 					 * and print the real Status: field
    156  1.1      cgd 					 */
    157  1.1      cgd 					if (dostat) {
    158  1.1      cgd 						statusput(mp, obuf, prefix);
    159  1.1      cgd 						dostat = 0;
    160  1.1      cgd 					}
    161  1.1      cgd 					ignoring = 1;
    162  1.1      cgd 				} else {
    163  1.1      cgd 					ignoring = 0;
    164  1.1      cgd 					*cp2 = c;	/* restore */
    165  1.1      cgd 				}
    166  1.1      cgd 				infld = 1;
    167  1.1      cgd 			}
    168  1.1      cgd 		}
    169  1.1      cgd 		if (!ignoring) {
    170  1.1      cgd 			/*
    171  1.1      cgd 			 * Strip trailing whitespace from prefix
    172  1.1      cgd 			 * if line is blank.
    173  1.1      cgd 			 */
    174  1.1      cgd 			if (prefix != NOSTR)
    175  1.1      cgd 				if (length > 1)
    176  1.1      cgd 					fputs(prefix, obuf);
    177  1.1      cgd 				else
    178  1.1      cgd 					(void) fwrite(prefix, sizeof *prefix,
    179  1.1      cgd 							prefixlen, obuf);
    180  1.1      cgd 			(void) fwrite(line, sizeof *line, length, obuf);
    181  1.1      cgd 			if (ferror(obuf))
    182  1.1      cgd 				return -1;
    183  1.1      cgd 		}
    184  1.1      cgd 	}
    185  1.1      cgd 	/*
    186  1.1      cgd 	 * Copy out message body
    187  1.1      cgd 	 */
    188  1.1      cgd 	if (doign == ignoreall)
    189  1.1      cgd 		count--;		/* skip final blank line */
    190  1.1      cgd 	if (prefix != NOSTR)
    191  1.1      cgd 		while (count > 0) {
    192  1.1      cgd 			if (fgets(line, LINESIZE, ibuf) == NULL) {
    193  1.1      cgd 				c = 0;
    194  1.1      cgd 				break;
    195  1.1      cgd 			}
    196  1.1      cgd 			count -= c = strlen(line);
    197  1.1      cgd 			/*
    198  1.1      cgd 			 * Strip trailing whitespace from prefix
    199  1.1      cgd 			 * if line is blank.
    200  1.1      cgd 			 */
    201  1.1      cgd 			if (c > 1)
    202  1.1      cgd 				fputs(prefix, obuf);
    203  1.1      cgd 			else
    204  1.1      cgd 				(void) fwrite(prefix, sizeof *prefix,
    205  1.1      cgd 						prefixlen, obuf);
    206  1.1      cgd 			(void) fwrite(line, sizeof *line, c, obuf);
    207  1.1      cgd 			if (ferror(obuf))
    208  1.1      cgd 				return -1;
    209  1.1      cgd 		}
    210  1.1      cgd 	else
    211  1.1      cgd 		while (count > 0) {
    212  1.1      cgd 			c = count < LINESIZE ? count : LINESIZE;
    213  1.1      cgd 			if ((c = fread(line, sizeof *line, c, ibuf)) <= 0)
    214  1.1      cgd 				break;
    215  1.1      cgd 			count -= c;
    216  1.1      cgd 			if (fwrite(line, sizeof *line, c, obuf) != c)
    217  1.1      cgd 				return -1;
    218  1.1      cgd 		}
    219  1.1      cgd 	if (doign == ignoreall && c > 0 && line[c - 1] != '\n')
    220  1.1      cgd 		/* no final blank line */
    221  1.1      cgd 		if ((c = getc(ibuf)) != EOF && putc(c, obuf) == EOF)
    222  1.1      cgd 			return -1;
    223  1.1      cgd 	return 0;
    224  1.1      cgd }
    225  1.1      cgd 
    226  1.1      cgd /*
    227  1.1      cgd  * Output a reasonable looking status field.
    228  1.1      cgd  */
    229  1.1      cgd statusput(mp, obuf, prefix)
    230  1.1      cgd 	register struct message *mp;
    231  1.1      cgd 	FILE *obuf;
    232  1.1      cgd 	char *prefix;
    233  1.1      cgd {
    234  1.1      cgd 	char statout[3];
    235  1.1      cgd 	register char *cp = statout;
    236  1.1      cgd 
    237  1.1      cgd 	if (mp->m_flag & MREAD)
    238  1.1      cgd 		*cp++ = 'R';
    239  1.1      cgd 	if ((mp->m_flag & MNEW) == 0)
    240  1.1      cgd 		*cp++ = 'O';
    241  1.1      cgd 	*cp = 0;
    242  1.1      cgd 	if (statout[0])
    243  1.1      cgd 		fprintf(obuf, "%sStatus: %s\n",
    244  1.1      cgd 			prefix == NOSTR ? "" : prefix, statout);
    245  1.1      cgd }
    246  1.1      cgd 
    247  1.1      cgd /*
    248  1.1      cgd  * Interface between the argument list and the mail1 routine
    249  1.1      cgd  * which does all the dirty work.
    250  1.1      cgd  */
    251  1.1      cgd mail(to, cc, bcc, smopts, subject)
    252  1.1      cgd 	struct name *to, *cc, *bcc, *smopts;
    253  1.1      cgd 	char *subject;
    254  1.1      cgd {
    255  1.1      cgd 	struct header head;
    256  1.1      cgd 
    257  1.1      cgd 	head.h_to = to;
    258  1.1      cgd 	head.h_subject = subject;
    259  1.1      cgd 	head.h_cc = cc;
    260  1.1      cgd 	head.h_bcc = bcc;
    261  1.1      cgd 	head.h_smopts = smopts;
    262  1.1      cgd 	mail1(&head, 0);
    263  1.1      cgd 	return(0);
    264  1.1      cgd }
    265  1.1      cgd 
    266  1.1      cgd 
    267  1.1      cgd /*
    268  1.1      cgd  * Send mail to a bunch of user names.  The interface is through
    269  1.1      cgd  * the mail routine below.
    270  1.1      cgd  */
    271  1.1      cgd sendmail(str)
    272  1.1      cgd 	char *str;
    273  1.1      cgd {
    274  1.1      cgd 	struct header head;
    275  1.1      cgd 
    276  1.1      cgd 	head.h_to = extract(str, GTO);
    277  1.1      cgd 	head.h_subject = NOSTR;
    278  1.1      cgd 	head.h_cc = NIL;
    279  1.1      cgd 	head.h_bcc = NIL;
    280  1.1      cgd 	head.h_smopts = NIL;
    281  1.1      cgd 	mail1(&head, 0);
    282  1.1      cgd 	return(0);
    283  1.1      cgd }
    284  1.1      cgd 
    285  1.1      cgd /*
    286  1.1      cgd  * Mail a message on standard input to the people indicated
    287  1.1      cgd  * in the passed header.  (Internal interface).
    288  1.1      cgd  */
    289  1.1      cgd mail1(hp, printheaders)
    290  1.1      cgd 	struct header *hp;
    291  1.1      cgd {
    292  1.1      cgd 	char *cp;
    293  1.1      cgd 	int pid;
    294  1.1      cgd 	char **namelist;
    295  1.1      cgd 	struct name *to;
    296  1.1      cgd 	FILE *mtf;
    297  1.1      cgd 
    298  1.1      cgd 	/*
    299  1.1      cgd 	 * Collect user's mail from standard input.
    300  1.1      cgd 	 * Get the result as mtf.
    301  1.1      cgd 	 */
    302  1.1      cgd 	if ((mtf = collect(hp, printheaders)) == NULL)
    303  1.1      cgd 		return;
    304  1.1      cgd 	if (value("interactive") != NOSTR)
    305  1.3      jtc 		if (value("askcc") != NOSTR || value("askbcc") != NOSTR) {
    306  1.3      jtc 			if (value("askcc") != NOSTR)
    307  1.3      jtc 				grabh(hp, GCC);
    308  1.3      jtc 			if (value("askbcc") != NOSTR)
    309  1.3      jtc 				grabh(hp, GBCC);
    310  1.3      jtc 		} else {
    311  1.1      cgd 			printf("EOT\n");
    312  1.1      cgd 			(void) fflush(stdout);
    313  1.1      cgd 		}
    314  1.1      cgd 	if (fsize(mtf) == 0)
    315  1.1      cgd 		if (hp->h_subject == NOSTR)
    316  1.1      cgd 			printf("No message, no subject; hope that's ok\n");
    317  1.1      cgd 		else
    318  1.1      cgd 			printf("Null message body; hope that's ok\n");
    319  1.1      cgd 	/*
    320  1.1      cgd 	 * Now, take the user names from the combined
    321  1.1      cgd 	 * to and cc lists and do all the alias
    322  1.1      cgd 	 * processing.
    323  1.1      cgd 	 */
    324  1.1      cgd 	senderr = 0;
    325  1.1      cgd 	to = usermap(cat(hp->h_bcc, cat(hp->h_to, hp->h_cc)));
    326  1.1      cgd 	if (to == NIL) {
    327  1.1      cgd 		printf("No recipients specified\n");
    328  1.1      cgd 		senderr++;
    329  1.1      cgd 	}
    330  1.1      cgd 	/*
    331  1.1      cgd 	 * Look through the recipient list for names with /'s
    332  1.1      cgd 	 * in them which we write to as files directly.
    333  1.1      cgd 	 */
    334  1.1      cgd 	to = outof(to, mtf, hp);
    335  1.1      cgd 	if (senderr)
    336  1.1      cgd 		savedeadletter(mtf);
    337  1.1      cgd 	to = elide(to);
    338  1.1      cgd 	if (count(to) == 0)
    339  1.1      cgd 		goto out;
    340  1.1      cgd 	fixhead(hp, to);
    341  1.1      cgd 	if ((mtf = infix(hp, mtf)) == NULL) {
    342  1.1      cgd 		fprintf(stderr, ". . . message lost, sorry.\n");
    343  1.1      cgd 		return;
    344  1.1      cgd 	}
    345  1.1      cgd 	namelist = unpack(cat(hp->h_smopts, to));
    346  1.1      cgd 	if (debug) {
    347  1.1      cgd 		char **t;
    348  1.1      cgd 
    349  1.1      cgd 		printf("Sendmail arguments:");
    350  1.1      cgd 		for (t = namelist; *t != NOSTR; t++)
    351  1.1      cgd 			printf(" \"%s\"", *t);
    352  1.1      cgd 		printf("\n");
    353  1.1      cgd 		goto out;
    354  1.1      cgd 	}
    355  1.1      cgd 	if ((cp = value("record")) != NOSTR)
    356  1.1      cgd 		(void) savemail(expand(cp), mtf);
    357  1.1      cgd 	/*
    358  1.1      cgd 	 * Fork, set up the temporary mail file as standard
    359  1.1      cgd 	 * input for "mail", and exec with the user list we generated
    360  1.1      cgd 	 * far above.
    361  1.1      cgd 	 */
    362  1.1      cgd 	pid = fork();
    363  1.1      cgd 	if (pid == -1) {
    364  1.1      cgd 		perror("fork");
    365  1.1      cgd 		savedeadletter(mtf);
    366  1.1      cgd 		goto out;
    367  1.1      cgd 	}
    368  1.1      cgd 	if (pid == 0) {
    369  1.1      cgd 		if (access(_PATH_MAIL_LOG, 0) == 0) {
    370  1.1      cgd 			FILE *postage;
    371  1.1      cgd 
    372  1.1      cgd 			if ((postage = Fopen(_PATH_MAIL_LOG, "a")) != NULL) {
    373  1.1      cgd 				fprintf(postage, "%s %d %ld\n", myname,
    374  1.1      cgd 				    count(to), fsize(mtf));
    375  1.1      cgd 				(void) Fclose(postage);
    376  1.1      cgd 			}
    377  1.1      cgd 		}
    378  1.1      cgd 		prepare_child(sigmask(SIGHUP)|sigmask(SIGINT)|sigmask(SIGQUIT)|
    379  1.1      cgd 			sigmask(SIGTSTP)|sigmask(SIGTTIN)|sigmask(SIGTTOU),
    380  1.1      cgd 			fileno(mtf), -1);
    381  1.1      cgd 		if ((cp = value("sendmail")) != NOSTR)
    382  1.1      cgd 			cp = expand(cp);
    383  1.1      cgd 		else
    384  1.1      cgd 			cp = _PATH_SENDMAIL;
    385  1.1      cgd 		execv(cp, namelist);
    386  1.1      cgd 		perror(cp);
    387  1.1      cgd 		_exit(1);
    388  1.1      cgd 	}
    389  1.1      cgd 	if (value("verbose") != NOSTR)
    390  1.1      cgd 		(void) wait_child(pid);
    391  1.1      cgd 	else
    392  1.1      cgd 		free_child(pid);
    393  1.1      cgd out:
    394  1.1      cgd 	(void) Fclose(mtf);
    395  1.1      cgd }
    396  1.1      cgd 
    397  1.1      cgd /*
    398  1.1      cgd  * Fix the header by glopping all of the expanded names from
    399  1.1      cgd  * the distribution list into the appropriate fields.
    400  1.1      cgd  */
    401  1.1      cgd fixhead(hp, tolist)
    402  1.1      cgd 	struct header *hp;
    403  1.1      cgd 	struct name *tolist;
    404  1.1      cgd {
    405  1.1      cgd 	register struct name *np;
    406  1.1      cgd 
    407  1.1      cgd 	hp->h_to = NIL;
    408  1.1      cgd 	hp->h_cc = NIL;
    409  1.1      cgd 	hp->h_bcc = NIL;
    410  1.1      cgd 	for (np = tolist; np != NIL; np = np->n_flink)
    411  1.1      cgd 		if ((np->n_type & GMASK) == GTO)
    412  1.1      cgd 			hp->h_to =
    413  1.1      cgd 				cat(hp->h_to, nalloc(np->n_name, np->n_type));
    414  1.1      cgd 		else if ((np->n_type & GMASK) == GCC)
    415  1.1      cgd 			hp->h_cc =
    416  1.1      cgd 				cat(hp->h_cc, nalloc(np->n_name, np->n_type));
    417  1.1      cgd 		else if ((np->n_type & GMASK) == GBCC)
    418  1.1      cgd 			hp->h_bcc =
    419  1.1      cgd 				cat(hp->h_bcc, nalloc(np->n_name, np->n_type));
    420  1.1      cgd }
    421  1.1      cgd 
    422  1.1      cgd /*
    423  1.1      cgd  * Prepend a header in front of the collected stuff
    424  1.1      cgd  * and return the new file.
    425  1.1      cgd  */
    426  1.1      cgd FILE *
    427  1.1      cgd infix(hp, fi)
    428  1.1      cgd 	struct header *hp;
    429  1.1      cgd 	FILE *fi;
    430  1.1      cgd {
    431  1.1      cgd 	extern char tempMail[];
    432  1.1      cgd 	register FILE *nfo, *nfi;
    433  1.1      cgd 	register int c;
    434  1.1      cgd 
    435  1.1      cgd 	if ((nfo = Fopen(tempMail, "w")) == NULL) {
    436  1.1      cgd 		perror(tempMail);
    437  1.1      cgd 		return(fi);
    438  1.1      cgd 	}
    439  1.1      cgd 	if ((nfi = Fopen(tempMail, "r")) == NULL) {
    440  1.1      cgd 		perror(tempMail);
    441  1.1      cgd 		(void) Fclose(nfo);
    442  1.1      cgd 		return(fi);
    443  1.1      cgd 	}
    444  1.1      cgd 	(void) rm(tempMail);
    445  1.1      cgd 	(void) puthead(hp, nfo, GTO|GSUBJECT|GCC|GBCC|GNL|GCOMMA);
    446  1.1      cgd 	c = getc(fi);
    447  1.1      cgd 	while (c != EOF) {
    448  1.1      cgd 		(void) putc(c, nfo);
    449  1.1      cgd 		c = getc(fi);
    450  1.1      cgd 	}
    451  1.1      cgd 	if (ferror(fi)) {
    452  1.1      cgd 		perror("read");
    453  1.1      cgd 		rewind(fi);
    454  1.1      cgd 		return(fi);
    455  1.1      cgd 	}
    456  1.1      cgd 	(void) fflush(nfo);
    457  1.1      cgd 	if (ferror(nfo)) {
    458  1.1      cgd 		perror(tempMail);
    459  1.1      cgd 		(void) Fclose(nfo);
    460  1.1      cgd 		(void) Fclose(nfi);
    461  1.1      cgd 		rewind(fi);
    462  1.1      cgd 		return(fi);
    463  1.1      cgd 	}
    464  1.1      cgd 	(void) Fclose(nfo);
    465  1.1      cgd 	(void) Fclose(fi);
    466  1.1      cgd 	rewind(nfi);
    467  1.1      cgd 	return(nfi);
    468  1.1      cgd }
    469  1.1      cgd 
    470  1.1      cgd /*
    471  1.1      cgd  * Dump the to, subject, cc header on the
    472  1.1      cgd  * passed file buffer.
    473  1.1      cgd  */
    474  1.1      cgd puthead(hp, fo, w)
    475  1.1      cgd 	struct header *hp;
    476  1.1      cgd 	FILE *fo;
    477  1.1      cgd {
    478  1.1      cgd 	register int gotcha;
    479  1.1      cgd 
    480  1.1      cgd 	gotcha = 0;
    481  1.1      cgd 	if (hp->h_to != NIL && w & GTO)
    482  1.1      cgd 		fmt("To:", hp->h_to, fo, w&GCOMMA), gotcha++;
    483  1.1      cgd 	if (hp->h_subject != NOSTR && w & GSUBJECT)
    484  1.1      cgd 		fprintf(fo, "Subject: %s\n", hp->h_subject), gotcha++;
    485  1.1      cgd 	if (hp->h_cc != NIL && w & GCC)
    486  1.1      cgd 		fmt("Cc:", hp->h_cc, fo, w&GCOMMA), gotcha++;
    487  1.1      cgd 	if (hp->h_bcc != NIL && w & GBCC)
    488  1.1      cgd 		fmt("Bcc:", hp->h_bcc, fo, w&GCOMMA), gotcha++;
    489  1.1      cgd 	if (gotcha && w & GNL)
    490  1.1      cgd 		(void) putc('\n', fo);
    491  1.1      cgd 	return(0);
    492  1.1      cgd }
    493  1.1      cgd 
    494  1.1      cgd /*
    495  1.1      cgd  * Format the given header line to not exceed 72 characters.
    496  1.1      cgd  */
    497  1.1      cgd fmt(str, np, fo, comma)
    498  1.1      cgd 	char *str;
    499  1.1      cgd 	register struct name *np;
    500  1.1      cgd 	FILE *fo;
    501  1.1      cgd 	int comma;
    502  1.1      cgd {
    503  1.1      cgd 	register col, len;
    504  1.1      cgd 
    505  1.1      cgd 	comma = comma ? 1 : 0;
    506  1.1      cgd 	col = strlen(str);
    507  1.1      cgd 	if (col)
    508  1.1      cgd 		fputs(str, fo);
    509  1.1      cgd 	for (; np != NIL; np = np->n_flink) {
    510  1.1      cgd 		if (np->n_flink == NIL)
    511  1.1      cgd 			comma = 0;
    512  1.1      cgd 		len = strlen(np->n_name);
    513  1.1      cgd 		col++;		/* for the space */
    514  1.1      cgd 		if (col + len + comma > 72 && col > 4) {
    515  1.1      cgd 			fputs("\n    ", fo);
    516  1.1      cgd 			col = 4;
    517  1.1      cgd 		} else
    518  1.1      cgd 			putc(' ', fo);
    519  1.1      cgd 		fputs(np->n_name, fo);
    520  1.1      cgd 		if (comma)
    521  1.1      cgd 			putc(',', fo);
    522  1.1      cgd 		col += len + comma;
    523  1.1      cgd 	}
    524  1.1      cgd 	putc('\n', fo);
    525  1.1      cgd }
    526  1.1      cgd 
    527  1.1      cgd /*
    528  1.1      cgd  * Save the outgoing mail on the passed file.
    529  1.1      cgd  */
    530  1.1      cgd 
    531  1.1      cgd /*ARGSUSED*/
    532  1.1      cgd savemail(name, fi)
    533  1.1      cgd 	char name[];
    534  1.1      cgd 	register FILE *fi;
    535  1.1      cgd {
    536  1.1      cgd 	register FILE *fo;
    537  1.1      cgd 	char buf[BUFSIZ];
    538  1.1      cgd 	register i;
    539  1.1      cgd 	time_t now, time();
    540  1.1      cgd 	char *ctime();
    541  1.1      cgd 
    542  1.1      cgd 	if ((fo = Fopen(name, "a")) == NULL) {
    543  1.1      cgd 		perror(name);
    544  1.1      cgd 		return (-1);
    545  1.1      cgd 	}
    546  1.1      cgd 	(void) time(&now);
    547  1.1      cgd 	fprintf(fo, "From %s %s", myname, ctime(&now));
    548  1.1      cgd 	while ((i = fread(buf, 1, sizeof buf, fi)) > 0)
    549  1.1      cgd 		(void) fwrite(buf, 1, i, fo);
    550  1.1      cgd 	(void) putc('\n', fo);
    551  1.1      cgd 	(void) fflush(fo);
    552  1.1      cgd 	if (ferror(fo))
    553  1.1      cgd 		perror(name);
    554  1.1      cgd 	(void) Fclose(fo);
    555  1.1      cgd 	rewind(fi);
    556  1.1      cgd 	return (0);
    557  1.1      cgd }
    558