xref: /illumos-gate/usr/src/cmd/mailx/fio.c (revision 5422785d)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate 
23196c7f05SJoshua M. Clulow /*
24196c7f05SJoshua M. Clulow  * Copyright 2014 Joyent, Inc.
25196c7f05SJoshua M. Clulow  */
26196c7f05SJoshua M. Clulow 
277c478bd9Sstevel@tonic-gate /*
286c83d09fSrobbin  * Copyright 1999 Sun Microsystems, Inc.  All rights reserved.
296c83d09fSrobbin  * Use is subject to license terms.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
326c83d09fSrobbin /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
336c83d09fSrobbin /*	  All Rights Reserved  	*/
346c83d09fSrobbin 
357c478bd9Sstevel@tonic-gate #include "rcv.h"
367c478bd9Sstevel@tonic-gate #include <locale.h>
377c478bd9Sstevel@tonic-gate #include <wordexp.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * mailx -- a modified version of a University of California at Berkeley
417c478bd9Sstevel@tonic-gate  *	mail program
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  * File I/O.
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate static int getln(char *line, int max, FILE *f);
477c478bd9Sstevel@tonic-gate static int linecount(char *lp, long size);
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * Set up the input pointers while copying the mail file into
517c478bd9Sstevel@tonic-gate  * /tmp.
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate void
setptr(register FILE * ibuf)557c478bd9Sstevel@tonic-gate setptr(register FILE *ibuf)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate 	int n, newline = 1, blankline = 1;
587c478bd9Sstevel@tonic-gate 	int StartNewMsg = TRUE;
597c478bd9Sstevel@tonic-gate 	int ToldUser = FALSE;
607c478bd9Sstevel@tonic-gate 	long clen = -1L;
617c478bd9Sstevel@tonic-gate 	int hdr = 0;
627c478bd9Sstevel@tonic-gate 	int cflg = 0;			/* found Content-length in header */
637c478bd9Sstevel@tonic-gate 	register char *cp;
647c478bd9Sstevel@tonic-gate 	register int l;
657c478bd9Sstevel@tonic-gate 	register long s;
667c478bd9Sstevel@tonic-gate 	off_t offset;
677c478bd9Sstevel@tonic-gate 	char linebuf[LINESIZE];
687c478bd9Sstevel@tonic-gate 	int inhead, newmail, Odot;
697c478bd9Sstevel@tonic-gate 	short flag;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	if (!space) {
727c478bd9Sstevel@tonic-gate 		msgCount = 0;
737c478bd9Sstevel@tonic-gate 		offset = 0;
747c478bd9Sstevel@tonic-gate 		space = 32;
757c478bd9Sstevel@tonic-gate 		newmail = 0;
767c478bd9Sstevel@tonic-gate 		message =
777c478bd9Sstevel@tonic-gate 		    (struct message *)calloc(space, sizeof (struct message));
787c478bd9Sstevel@tonic-gate 		if (message == NULL) {
797c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(
807c478bd9Sstevel@tonic-gate 			    "calloc: insufficient memory for %d messages\n"),
817c478bd9Sstevel@tonic-gate 			    space);
827c478bd9Sstevel@tonic-gate 			exit(1);
837c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
847c478bd9Sstevel@tonic-gate 		}
857c478bd9Sstevel@tonic-gate 		dot = message;
867c478bd9Sstevel@tonic-gate 	} else {
877c478bd9Sstevel@tonic-gate 		newmail = 1;
887c478bd9Sstevel@tonic-gate 		offset = fsize(otf);
897c478bd9Sstevel@tonic-gate 	}
907c478bd9Sstevel@tonic-gate 	s = 0L;
917c478bd9Sstevel@tonic-gate 	l = 0;
927c478bd9Sstevel@tonic-gate 	/*
937c478bd9Sstevel@tonic-gate 	 * Set default flags.  When reading from
947c478bd9Sstevel@tonic-gate 	 * a folder, assume the message has been
957c478bd9Sstevel@tonic-gate 	 * previously read.
967c478bd9Sstevel@tonic-gate 	 */
977c478bd9Sstevel@tonic-gate 	if (edit)
987c478bd9Sstevel@tonic-gate 		flag = MUSED|MREAD;
997c478bd9Sstevel@tonic-gate 	else
1007c478bd9Sstevel@tonic-gate 		flag = MUSED|MNEW;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	inhead = 0;
1037c478bd9Sstevel@tonic-gate 	while ((n = getln(linebuf, sizeof (linebuf), ibuf)) > 0) {
1047c478bd9Sstevel@tonic-gate 		if (!newline) {
1057c478bd9Sstevel@tonic-gate 			goto putout;
1067c478bd9Sstevel@tonic-gate 		}
1077c478bd9Sstevel@tonic-gate 	top:
1087c478bd9Sstevel@tonic-gate 		hdr = inhead && (headerp(linebuf) ||
1097c478bd9Sstevel@tonic-gate 		    (linebuf[0] == ' ' || linebuf[0] == '\t'));
1107c478bd9Sstevel@tonic-gate 		if (!hdr && cflg) {	/* nonheader, Content-length seen */
1117c478bd9Sstevel@tonic-gate 			if (clen > 0 && clen < n) {	/* read too much */
1127c478bd9Sstevel@tonic-gate 				/*
1137c478bd9Sstevel@tonic-gate 				 * NB: this only can happen if there is a
1147c478bd9Sstevel@tonic-gate 				 * small content that is NOT \n terminated
1157c478bd9Sstevel@tonic-gate 				 * and has no leading blank line, i.e., never.
1167c478bd9Sstevel@tonic-gate 				 */
1177c478bd9Sstevel@tonic-gate 				if (fwrite(linebuf, 1, (int)clen, otf) !=
1187c478bd9Sstevel@tonic-gate 					clen) {
1197c478bd9Sstevel@tonic-gate 					fclose(ibuf);
1207c478bd9Sstevel@tonic-gate 					fflush(otf);
1217c478bd9Sstevel@tonic-gate 				} else {
1227c478bd9Sstevel@tonic-gate 					l += linecount(linebuf, clen);
1237c478bd9Sstevel@tonic-gate 				}
1247c478bd9Sstevel@tonic-gate 				offset += clen;
1257c478bd9Sstevel@tonic-gate 				s += clen;
1267c478bd9Sstevel@tonic-gate 				n -= (int)clen;
1277c478bd9Sstevel@tonic-gate 				/* shift line to the left, copy null as well */
1287c478bd9Sstevel@tonic-gate 				memcpy(linebuf, linebuf+clen, n+1);
1297c478bd9Sstevel@tonic-gate 				cflg = 0;
1307c478bd9Sstevel@tonic-gate 				message[msgCount-1].m_clen = clen + 1;
1317c478bd9Sstevel@tonic-gate 				blankline = 1;
1327c478bd9Sstevel@tonic-gate 				StartNewMsg = TRUE;
1337c478bd9Sstevel@tonic-gate 				goto top;
1347c478bd9Sstevel@tonic-gate 			}
1357c478bd9Sstevel@tonic-gate 			/* here, clen == 0 or clen >= n */
1367c478bd9Sstevel@tonic-gate 			if (n == 1 && linebuf[0] == '\n') {
1377c478bd9Sstevel@tonic-gate 				/* leading empty line */
1387c478bd9Sstevel@tonic-gate 				clen++;		/* cheat */
1397c478bd9Sstevel@tonic-gate 				inhead = 0;
1407c478bd9Sstevel@tonic-gate 			}
1417c478bd9Sstevel@tonic-gate 			offset += clen;
1427c478bd9Sstevel@tonic-gate 			s += (long)clen;
1437c478bd9Sstevel@tonic-gate 			message[msgCount-1].m_clen = clen;
1447c478bd9Sstevel@tonic-gate 			for (;;) {
1457c478bd9Sstevel@tonic-gate 				if (fwrite(linebuf, 1, n, otf) != n) {
1467c478bd9Sstevel@tonic-gate 					fclose(ibuf);
1477c478bd9Sstevel@tonic-gate 					fflush(otf);
1487c478bd9Sstevel@tonic-gate 				} else {
1497c478bd9Sstevel@tonic-gate 					l += linecount(linebuf, n);
1507c478bd9Sstevel@tonic-gate 				}
1517c478bd9Sstevel@tonic-gate 				clen -= n;
1527c478bd9Sstevel@tonic-gate 				if (clen <= 0) {
1537c478bd9Sstevel@tonic-gate 					break;
1547c478bd9Sstevel@tonic-gate 				}
1557c478bd9Sstevel@tonic-gate 				n = clen < sizeof (linebuf) ?
1567c478bd9Sstevel@tonic-gate 				    (int)clen : (int)sizeof (linebuf);
1577c478bd9Sstevel@tonic-gate 				if ((n = fread(linebuf, 1, n, ibuf)) <= 0) {
1587c478bd9Sstevel@tonic-gate 					fprintf(stderr, gettext(
1597c478bd9Sstevel@tonic-gate 			"%s:\tYour mailfile was found to be corrupted.\n"),
1607c478bd9Sstevel@tonic-gate 					    progname);
1617c478bd9Sstevel@tonic-gate 					fprintf(stderr, gettext(
1627c478bd9Sstevel@tonic-gate 					    "\t(Unexpected end-of-file).\n"));
1637c478bd9Sstevel@tonic-gate 					fprintf(stderr, gettext(
1647c478bd9Sstevel@tonic-gate 					"\tMessage #%d may be truncated.\n\n"),
1657c478bd9Sstevel@tonic-gate 					    msgCount);
1667c478bd9Sstevel@tonic-gate 					offset -= clen;
1677c478bd9Sstevel@tonic-gate 					s -= clen;
1687c478bd9Sstevel@tonic-gate 					clen = 0; /* stop the loop */
1697c478bd9Sstevel@tonic-gate 				}
1707c478bd9Sstevel@tonic-gate 			}
1717c478bd9Sstevel@tonic-gate 			/* All done, go to top for next message */
1727c478bd9Sstevel@tonic-gate 			cflg = 0;
1737c478bd9Sstevel@tonic-gate 			blankline = 1;
1747c478bd9Sstevel@tonic-gate 			StartNewMsg = TRUE;
1757c478bd9Sstevel@tonic-gate 			continue;
1767c478bd9Sstevel@tonic-gate 		}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 		/* Look for a From line that starts a new message */
179196c7f05SJoshua M. Clulow 		if (blankline && linebuf[0] == 'F' && is_headline(linebuf)) {
1807c478bd9Sstevel@tonic-gate 			if (msgCount > 0 && !newmail) {
1817c478bd9Sstevel@tonic-gate 				message[msgCount-1].m_size = s;
1827c478bd9Sstevel@tonic-gate 				message[msgCount-1].m_lines = l;
1837c478bd9Sstevel@tonic-gate 				message[msgCount-1].m_flag = flag;
1847c478bd9Sstevel@tonic-gate 			}
1857c478bd9Sstevel@tonic-gate 			if (msgCount >= space) {
1867c478bd9Sstevel@tonic-gate 				/*
1877c478bd9Sstevel@tonic-gate 				 * Limit the speed at which the
1887c478bd9Sstevel@tonic-gate 				 * allocated space grows.
1897c478bd9Sstevel@tonic-gate 				 */
1907c478bd9Sstevel@tonic-gate 				if (space < 512)
1917c478bd9Sstevel@tonic-gate 					space = space*2;
1927c478bd9Sstevel@tonic-gate 				else
1937c478bd9Sstevel@tonic-gate 					space += 512;
1947c478bd9Sstevel@tonic-gate 				errno = 0;
1957c478bd9Sstevel@tonic-gate 				Odot = dot - &(message[0]);
1967c478bd9Sstevel@tonic-gate 				message = (struct message *)
1977c478bd9Sstevel@tonic-gate 				    realloc(message,
1987c478bd9Sstevel@tonic-gate 					space*(sizeof (struct message)));
1997c478bd9Sstevel@tonic-gate 				if (message == NULL) {
2007c478bd9Sstevel@tonic-gate 					perror("realloc failed");
2017c478bd9Sstevel@tonic-gate 					fprintf(stderr, gettext(
2027c478bd9Sstevel@tonic-gate 			"realloc: insufficient memory for %d messages\n"),
2037c478bd9Sstevel@tonic-gate 					    space);
2047c478bd9Sstevel@tonic-gate 					exit(1);
2057c478bd9Sstevel@tonic-gate 				}
2067c478bd9Sstevel@tonic-gate 				dot = &message[Odot];
2077c478bd9Sstevel@tonic-gate 			}
2087c478bd9Sstevel@tonic-gate 			message[msgCount].m_offset = offset;
2097c478bd9Sstevel@tonic-gate 			message[msgCount].m_text = TRUE;
2107c478bd9Sstevel@tonic-gate 			message[msgCount].m_clen = 0;
2117c478bd9Sstevel@tonic-gate 			newmail = 0;
2127c478bd9Sstevel@tonic-gate 			msgCount++;
2137c478bd9Sstevel@tonic-gate 			if (edit)
2147c478bd9Sstevel@tonic-gate 				flag = MUSED|MREAD;
2157c478bd9Sstevel@tonic-gate 			else
2167c478bd9Sstevel@tonic-gate 				flag = MUSED|MNEW;
2177c478bd9Sstevel@tonic-gate 			inhead = 1;
2187c478bd9Sstevel@tonic-gate 			s = 0L;
2197c478bd9Sstevel@tonic-gate 			l = 0;
2207c478bd9Sstevel@tonic-gate 			StartNewMsg = FALSE;
2217c478bd9Sstevel@tonic-gate 			ToldUser = FALSE;
2227c478bd9Sstevel@tonic-gate 			goto putout;
2237c478bd9Sstevel@tonic-gate 		}
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 		/* if didn't get a header line, we're no longer in the header */
2267c478bd9Sstevel@tonic-gate 		if (!hdr)
2277c478bd9Sstevel@tonic-gate 			inhead = 0;
2287c478bd9Sstevel@tonic-gate 		if (!inhead)
2297c478bd9Sstevel@tonic-gate 			goto putout;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 		/*
2327c478bd9Sstevel@tonic-gate 		 * Look for Status: line.  Do quick check for second character,
2337c478bd9Sstevel@tonic-gate 		 * many headers start with "S" but few have "t" as second char.
2347c478bd9Sstevel@tonic-gate 		 */
2357c478bd9Sstevel@tonic-gate 		if ((linebuf[1] == 't' || linebuf[1] == 'T') &&
2367c478bd9Sstevel@tonic-gate 		    ishfield(linebuf, "status")) {
2377c478bd9Sstevel@tonic-gate 			cp = hcontents(linebuf);
2387c478bd9Sstevel@tonic-gate 			flag = MUSED|MNEW;
2397c478bd9Sstevel@tonic-gate 			if (strchr(cp, 'R'))
2407c478bd9Sstevel@tonic-gate 				flag |= MREAD;
2417c478bd9Sstevel@tonic-gate 			if (strchr(cp, 'O'))
2427c478bd9Sstevel@tonic-gate 				flag &= ~MNEW;
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 		/*
2457c478bd9Sstevel@tonic-gate 		 * Look for Content-Length and Content-Type headers.  Like
2467c478bd9Sstevel@tonic-gate 		 * above, do a quick check for the "-", which is rare.
2477c478bd9Sstevel@tonic-gate 		 */
2487c478bd9Sstevel@tonic-gate 		if (linebuf[7] == '-') {
2497c478bd9Sstevel@tonic-gate 			if (ishfield(linebuf, "content-length")) {
2507c478bd9Sstevel@tonic-gate 				if (!cflg) {
2517c478bd9Sstevel@tonic-gate 					clen = atol(hcontents(linebuf));
2527c478bd9Sstevel@tonic-gate 					cflg = clen >= 0;
2537c478bd9Sstevel@tonic-gate 				}
2547c478bd9Sstevel@tonic-gate 			} else if (ishfield(linebuf, "content-type")) {
2557c478bd9Sstevel@tonic-gate 				char word[LINESIZE];
2567c478bd9Sstevel@tonic-gate 				char *cp2;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 				cp = hcontents(linebuf);
2597c478bd9Sstevel@tonic-gate 				cp2 = word;
2607c478bd9Sstevel@tonic-gate 				while (!isspace(*cp))
2617c478bd9Sstevel@tonic-gate 					*cp2++ = *cp++;
2627c478bd9Sstevel@tonic-gate 				*cp2 = '\0';
2637c478bd9Sstevel@tonic-gate 				if (icequal(word, "binary"))
2647c478bd9Sstevel@tonic-gate 					message[msgCount-1].m_text = FALSE;
2657c478bd9Sstevel@tonic-gate 			}
2667c478bd9Sstevel@tonic-gate 		}
2677c478bd9Sstevel@tonic-gate putout:
2687c478bd9Sstevel@tonic-gate 		offset += n;
2697c478bd9Sstevel@tonic-gate 		s += (long)n;
2707c478bd9Sstevel@tonic-gate 		if (fwrite(linebuf, 1, n, otf) != n) {
2717c478bd9Sstevel@tonic-gate 			fclose(ibuf);
2727c478bd9Sstevel@tonic-gate 			fflush(otf);
2737c478bd9Sstevel@tonic-gate 		} else {
2747c478bd9Sstevel@tonic-gate 			l++;
2757c478bd9Sstevel@tonic-gate 		}
2767c478bd9Sstevel@tonic-gate 		if (ferror(otf)) {
2777c478bd9Sstevel@tonic-gate 			perror("/tmp");
2787c478bd9Sstevel@tonic-gate 			exit(1);
2797c478bd9Sstevel@tonic-gate 		}
2807c478bd9Sstevel@tonic-gate 		if (msgCount == 0) {
2817c478bd9Sstevel@tonic-gate 			fclose(ibuf);
2827c478bd9Sstevel@tonic-gate 			fflush(otf);
2837c478bd9Sstevel@tonic-gate 		}
2847c478bd9Sstevel@tonic-gate 		if (linebuf[n-1] == '\n') {
2857c478bd9Sstevel@tonic-gate 			blankline = newline && n == 1;
2867c478bd9Sstevel@tonic-gate 			newline = 1;
2877c478bd9Sstevel@tonic-gate 			if (n == 1) {
2887c478bd9Sstevel@tonic-gate 				/* Blank line. Skip StartNewMsg check below */
2897c478bd9Sstevel@tonic-gate 				continue;
2907c478bd9Sstevel@tonic-gate 			}
2917c478bd9Sstevel@tonic-gate 		} else {
2927c478bd9Sstevel@tonic-gate 			newline = 0;
2937c478bd9Sstevel@tonic-gate 		}
2947c478bd9Sstevel@tonic-gate 		if (StartNewMsg && !ToldUser) {
2957c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(
2967c478bd9Sstevel@tonic-gate 			    "%s:\tYour mailfile was found to be corrupted\n"),
2977c478bd9Sstevel@tonic-gate 			    progname);
2987c478bd9Sstevel@tonic-gate 			fprintf(stderr,
2997c478bd9Sstevel@tonic-gate 			    gettext("\t(Content-length mismatch).\n"));
3007c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(
3017c478bd9Sstevel@tonic-gate 			    "\tMessage #%d may be truncated,\n"), msgCount);
3027c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext(
3037c478bd9Sstevel@tonic-gate 			    "\twith another message concatenated to it.\n\n"));
3047c478bd9Sstevel@tonic-gate 			ToldUser = TRUE;
3057c478bd9Sstevel@tonic-gate 		}
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	if (n == 0) {
3097c478bd9Sstevel@tonic-gate 		fflush(otf);
3107c478bd9Sstevel@tonic-gate 		if (fferror(otf)) {
3117c478bd9Sstevel@tonic-gate 			perror("/tmp");
3127c478bd9Sstevel@tonic-gate 			exit(1);
3137c478bd9Sstevel@tonic-gate 		}
3147c478bd9Sstevel@tonic-gate 		if (msgCount) {
3157c478bd9Sstevel@tonic-gate 			message[msgCount-1].m_size = s;
3167c478bd9Sstevel@tonic-gate 			message[msgCount-1].m_lines = l;
3177c478bd9Sstevel@tonic-gate 			message[msgCount-1].m_flag = flag;
3187c478bd9Sstevel@tonic-gate 		}
3197c478bd9Sstevel@tonic-gate 		fclose(ibuf);
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate /*
3247c478bd9Sstevel@tonic-gate  * Compute the content length of a message and set it into m_clen.
3257c478bd9Sstevel@tonic-gate  */
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate void
setclen(register struct message * mp)3287c478bd9Sstevel@tonic-gate setclen(register struct message *mp)
3297c478bd9Sstevel@tonic-gate {
3307c478bd9Sstevel@tonic-gate 	long c;
3317c478bd9Sstevel@tonic-gate 	FILE *ibuf;
3327c478bd9Sstevel@tonic-gate 	char line[LINESIZE];
3337c478bd9Sstevel@tonic-gate 	int fline, nread;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	ibuf = setinput(mp);
3367c478bd9Sstevel@tonic-gate 	c = mp->m_size;
3377c478bd9Sstevel@tonic-gate 	fline = 1;
3387c478bd9Sstevel@tonic-gate 	while (c > 0L) {
3397c478bd9Sstevel@tonic-gate 		nread = getln(line, sizeof (line), ibuf);
3407c478bd9Sstevel@tonic-gate 		c -= nread;
3417c478bd9Sstevel@tonic-gate 		/*
3427c478bd9Sstevel@tonic-gate 		 * First line is the From line, so no headers
3437c478bd9Sstevel@tonic-gate 		 * there to worry about.
3447c478bd9Sstevel@tonic-gate 		 */
3457c478bd9Sstevel@tonic-gate 		if (fline) {
3467c478bd9Sstevel@tonic-gate 			fline = 0;
3477c478bd9Sstevel@tonic-gate 			continue;
3487c478bd9Sstevel@tonic-gate 		}
3497c478bd9Sstevel@tonic-gate 		/*
3507c478bd9Sstevel@tonic-gate 		 * If line is blank, we've reached end of headers.
3517c478bd9Sstevel@tonic-gate 		 */
3527c478bd9Sstevel@tonic-gate 		if (line[0] == '\n')
3537c478bd9Sstevel@tonic-gate 			break;
3547c478bd9Sstevel@tonic-gate 		/*
3557c478bd9Sstevel@tonic-gate 		 * If this line is a continuation
3567c478bd9Sstevel@tonic-gate 		 * of a previous header field, keep going.
3577c478bd9Sstevel@tonic-gate 		 */
3587c478bd9Sstevel@tonic-gate 		if (isspace(line[0]))
3597c478bd9Sstevel@tonic-gate 			continue;
3607c478bd9Sstevel@tonic-gate 		/*
3617c478bd9Sstevel@tonic-gate 		 * If we are no longer looking at real
3627c478bd9Sstevel@tonic-gate 		 * header lines, we're done.
3637c478bd9Sstevel@tonic-gate 		 * This happens in uucp style mail where
3647c478bd9Sstevel@tonic-gate 		 * there are no headers at all.
3657c478bd9Sstevel@tonic-gate 		 */
3667c478bd9Sstevel@tonic-gate 		if (!headerp(line)) {
3677c478bd9Sstevel@tonic-gate 			c += nread;
3687c478bd9Sstevel@tonic-gate 			break;
3697c478bd9Sstevel@tonic-gate 		}
3707c478bd9Sstevel@tonic-gate 	}
3717c478bd9Sstevel@tonic-gate 	if (c == 0)
3727c478bd9Sstevel@tonic-gate 		c = 1;
3737c478bd9Sstevel@tonic-gate 	mp->m_clen = c;
3747c478bd9Sstevel@tonic-gate }
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate static int
getln(char * line,int max,FILE * f)3777c478bd9Sstevel@tonic-gate getln(char *line, int max, FILE *f)
3787c478bd9Sstevel@tonic-gate {
3797c478bd9Sstevel@tonic-gate 	register int c;
3807c478bd9Sstevel@tonic-gate 	register char *cp, *ecp;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	cp = line;
3837c478bd9Sstevel@tonic-gate 	ecp = cp + max - 1;
3847c478bd9Sstevel@tonic-gate 	while (cp < ecp && (c = getc(f)) != EOF)
3857c478bd9Sstevel@tonic-gate 		if ((*cp++ = (char)c) == '\n')
3867c478bd9Sstevel@tonic-gate 			break;
3877c478bd9Sstevel@tonic-gate 	*cp = '\0';
3887c478bd9Sstevel@tonic-gate 	return (cp - line);
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate /*
3927c478bd9Sstevel@tonic-gate  * Read up a line from the specified input into the line
3937c478bd9Sstevel@tonic-gate  * buffer.  Return the number of characters read.  Do not
3947c478bd9Sstevel@tonic-gate  * include the newline at the end.
3957c478bd9Sstevel@tonic-gate  */
3967c478bd9Sstevel@tonic-gate 
3976c83d09fSrobbin int
readline(FILE * ibuf,char * linebuf)3987c478bd9Sstevel@tonic-gate readline(FILE *ibuf, char *linebuf)
3997c478bd9Sstevel@tonic-gate {
4007c478bd9Sstevel@tonic-gate 	register char *cp;
4017c478bd9Sstevel@tonic-gate 	register int c;
4027c478bd9Sstevel@tonic-gate 	int seennulls = 0;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	clearerr(ibuf);
4057c478bd9Sstevel@tonic-gate 	c = getc(ibuf);
4067c478bd9Sstevel@tonic-gate 	for (cp = linebuf; c != '\n' && c != EOF; c = getc(ibuf)) {
4077c478bd9Sstevel@tonic-gate 		if (c == 0) {
4087c478bd9Sstevel@tonic-gate 			if (!seennulls) {
4097c478bd9Sstevel@tonic-gate 				fprintf(stderr,
4107c478bd9Sstevel@tonic-gate 				    gettext("mailx: NUL changed to @\n"));
4117c478bd9Sstevel@tonic-gate 				seennulls++;
4127c478bd9Sstevel@tonic-gate 			}
4137c478bd9Sstevel@tonic-gate 			c = '@';
4147c478bd9Sstevel@tonic-gate 		}
4157c478bd9Sstevel@tonic-gate 		if (cp - linebuf < LINESIZE-2)
4167c478bd9Sstevel@tonic-gate 			*cp++ = (char)c;
4177c478bd9Sstevel@tonic-gate 	}
4187c478bd9Sstevel@tonic-gate 	*cp = 0;
4197c478bd9Sstevel@tonic-gate 	if (c == EOF && cp == linebuf)
4207c478bd9Sstevel@tonic-gate 		return (0);
4217c478bd9Sstevel@tonic-gate 	return (cp - linebuf + 1);
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate /*
4257c478bd9Sstevel@tonic-gate  * linecount - determine the number of lines in a printable file.
4267c478bd9Sstevel@tonic-gate  */
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate static int
linecount(char * lp,long size)4297c478bd9Sstevel@tonic-gate linecount(char *lp, long size)
4307c478bd9Sstevel@tonic-gate {
4317c478bd9Sstevel@tonic-gate 	register char *cp, *ecp;
4327c478bd9Sstevel@tonic-gate 	register int count;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	count = 0;
4357c478bd9Sstevel@tonic-gate 	cp = lp;
4367c478bd9Sstevel@tonic-gate 	ecp = cp + size;
4377c478bd9Sstevel@tonic-gate 	while (cp < ecp)
4387c478bd9Sstevel@tonic-gate 		if (*cp++ == '\n')
4397c478bd9Sstevel@tonic-gate 			count++;
4407c478bd9Sstevel@tonic-gate 	return (count);
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate /*
4447c478bd9Sstevel@tonic-gate  * Return a file buffer all ready to read up the
4457c478bd9Sstevel@tonic-gate  * passed message pointer.
4467c478bd9Sstevel@tonic-gate  */
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate FILE *
setinput(register struct message * mp)4497c478bd9Sstevel@tonic-gate setinput(register struct message *mp)
4507c478bd9Sstevel@tonic-gate {
4517c478bd9Sstevel@tonic-gate 	fflush(otf);
4527c478bd9Sstevel@tonic-gate 	if (fseek(itf, mp->m_offset, 0) < 0) {
4537c478bd9Sstevel@tonic-gate 		perror("fseek");
4547c478bd9Sstevel@tonic-gate 		panic("temporary file seek");
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate 	return (itf);
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate /*
4617c478bd9Sstevel@tonic-gate  * Delete a file, but only if the file is a plain file.
4627c478bd9Sstevel@tonic-gate  */
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate int
removefile(char name[])4657c478bd9Sstevel@tonic-gate removefile(char name[])
4667c478bd9Sstevel@tonic-gate {
4677c478bd9Sstevel@tonic-gate 	struct stat statb;
4687c478bd9Sstevel@tonic-gate 	extern int errno;
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	if (stat(name, &statb) < 0)
4717c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
4727c478bd9Sstevel@tonic-gate 			return (0);	/* it's already gone, no error */
4737c478bd9Sstevel@tonic-gate 		else
4747c478bd9Sstevel@tonic-gate 			return (-1);
4757c478bd9Sstevel@tonic-gate 	if ((statb.st_mode & S_IFMT) != S_IFREG) {
4767c478bd9Sstevel@tonic-gate 		errno = EISDIR;
4777c478bd9Sstevel@tonic-gate 		return (-1);
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 	return (unlink(name));
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate /*
4837c478bd9Sstevel@tonic-gate  * Terminate an editing session by attempting to write out the user's
4847c478bd9Sstevel@tonic-gate  * file from the temporary.  Save any new stuff appended to the file.
4857c478bd9Sstevel@tonic-gate  */
4867c478bd9Sstevel@tonic-gate int
edstop(int noremove)4877c478bd9Sstevel@tonic-gate edstop(
4887c478bd9Sstevel@tonic-gate     int noremove	/* don't allow the file to be removed, trunc instead */
4897c478bd9Sstevel@tonic-gate )
4907c478bd9Sstevel@tonic-gate {
4917c478bd9Sstevel@tonic-gate 	register int gotcha, c;
4927c478bd9Sstevel@tonic-gate 	register struct message *mp;
4937c478bd9Sstevel@tonic-gate 	FILE *obuf, *ibuf, *tbuf = 0, *readstat;
4947c478bd9Sstevel@tonic-gate 	struct stat statb;
4957c478bd9Sstevel@tonic-gate 	char tempname[STSIZ], *id;
4967c478bd9Sstevel@tonic-gate 	int tmpfd = -1;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	if (readonly)
4997c478bd9Sstevel@tonic-gate 		return (0);
5007c478bd9Sstevel@tonic-gate 	holdsigs();
5017c478bd9Sstevel@tonic-gate 	if (Tflag != NOSTR) {
5027c478bd9Sstevel@tonic-gate 		if ((readstat = fopen(Tflag, "w")) == NULL)
5037c478bd9Sstevel@tonic-gate 			Tflag = NOSTR;
5047c478bd9Sstevel@tonic-gate 	}
5057c478bd9Sstevel@tonic-gate 	for (mp = &message[0], gotcha = 0; mp < &message[msgCount]; mp++) {
5067c478bd9Sstevel@tonic-gate 		if (mp->m_flag & MNEW) {
5077c478bd9Sstevel@tonic-gate 			mp->m_flag &= ~MNEW;
5087c478bd9Sstevel@tonic-gate 			mp->m_flag |= MSTATUS;
5097c478bd9Sstevel@tonic-gate 		}
5107c478bd9Sstevel@tonic-gate 		if (mp->m_flag & (MODIFY|MDELETED|MSTATUS))
5117c478bd9Sstevel@tonic-gate 			gotcha++;
5127c478bd9Sstevel@tonic-gate 		if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) {
5137c478bd9Sstevel@tonic-gate 			if ((id = hfield("article-id", mp, addone)) != NOSTR)
5147c478bd9Sstevel@tonic-gate 				fprintf(readstat, "%s\n", id);
5157c478bd9Sstevel@tonic-gate 		}
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 	if (Tflag != NOSTR)
5187c478bd9Sstevel@tonic-gate 		fclose(readstat);
5197c478bd9Sstevel@tonic-gate 	if (!gotcha || Tflag != NOSTR)
5207c478bd9Sstevel@tonic-gate 		goto done;
5217c478bd9Sstevel@tonic-gate 	if ((ibuf = fopen(editfile, "r+")) == NULL) {
5227c478bd9Sstevel@tonic-gate 		perror(editfile);
5237c478bd9Sstevel@tonic-gate 		relsesigs();
5247c478bd9Sstevel@tonic-gate 		longjmp(srbuf, 1);
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate 	lock(ibuf, "r+", 1);
5277c478bd9Sstevel@tonic-gate 	if (fstat(fileno(ibuf), &statb) >= 0 && statb.st_size > mailsize) {
5287c478bd9Sstevel@tonic-gate 		nstrcpy(tempname, STSIZ, "/tmp/mboxXXXXXX");
5297c478bd9Sstevel@tonic-gate 		if ((tmpfd = mkstemp(tempname)) == -1) {
5307c478bd9Sstevel@tonic-gate 			perror(tempname);
5317c478bd9Sstevel@tonic-gate 			fclose(ibuf);
5327c478bd9Sstevel@tonic-gate 			relsesigs();
5337c478bd9Sstevel@tonic-gate 			longjmp(srbuf, 1);
5347c478bd9Sstevel@tonic-gate 		}
5357c478bd9Sstevel@tonic-gate 		if ((obuf = fdopen(tmpfd, "w")) == NULL) {
5367c478bd9Sstevel@tonic-gate 			perror(tempname);
5377c478bd9Sstevel@tonic-gate 			fclose(ibuf);
5387c478bd9Sstevel@tonic-gate 			removefile(tempname);
5397c478bd9Sstevel@tonic-gate 			relsesigs();
5407c478bd9Sstevel@tonic-gate 			(void) close(tmpfd);
5417c478bd9Sstevel@tonic-gate 			longjmp(srbuf, 1);
5427c478bd9Sstevel@tonic-gate 		}
5437c478bd9Sstevel@tonic-gate 		fseek(ibuf, mailsize, 0);
5447c478bd9Sstevel@tonic-gate 		while ((c = getc(ibuf)) != EOF)
5457c478bd9Sstevel@tonic-gate 			putc(c, obuf);
5467c478bd9Sstevel@tonic-gate 		fclose(obuf);
5477c478bd9Sstevel@tonic-gate 		if ((tbuf = fopen(tempname, "r")) == NULL) {
5487c478bd9Sstevel@tonic-gate 			perror(tempname);
5497c478bd9Sstevel@tonic-gate 			fclose(ibuf);
5507c478bd9Sstevel@tonic-gate 			removefile(tempname);
5517c478bd9Sstevel@tonic-gate 			relsesigs();
5527c478bd9Sstevel@tonic-gate 			longjmp(srbuf, 1);
5537c478bd9Sstevel@tonic-gate 		}
5547c478bd9Sstevel@tonic-gate 		removefile(tempname);
5557c478bd9Sstevel@tonic-gate 	}
5567c478bd9Sstevel@tonic-gate 	if ((obuf = fopen(editfile, "r+")) == NULL) {
5577c478bd9Sstevel@tonic-gate 		if ((obuf = fopen(editfile, "w")) == NULL) {
5587c478bd9Sstevel@tonic-gate 			perror(editfile);
5597c478bd9Sstevel@tonic-gate 			fclose(ibuf);
5607c478bd9Sstevel@tonic-gate 			if (tbuf)
5617c478bd9Sstevel@tonic-gate 				fclose(tbuf);
5627c478bd9Sstevel@tonic-gate 			relsesigs();
5637c478bd9Sstevel@tonic-gate 			longjmp(srbuf, 1);
5647c478bd9Sstevel@tonic-gate 		}
5657c478bd9Sstevel@tonic-gate 	}
5667c478bd9Sstevel@tonic-gate 	printf("\"%s\" ", editfile);
5677c478bd9Sstevel@tonic-gate 	flush();
5687c478bd9Sstevel@tonic-gate 	c = 0;
5697c478bd9Sstevel@tonic-gate 	for (mp = &message[0]; mp < &message[msgCount]; mp++) {
5707c478bd9Sstevel@tonic-gate 		if ((mp->m_flag & MDELETED) != 0)
5717c478bd9Sstevel@tonic-gate 			continue;
5727c478bd9Sstevel@tonic-gate 		c++;
5737c478bd9Sstevel@tonic-gate 		if (msend(mp, obuf, 0, fputs) < 0) {
5747c478bd9Sstevel@tonic-gate 			perror(editfile);
5757c478bd9Sstevel@tonic-gate 			fclose(ibuf);
5767c478bd9Sstevel@tonic-gate 			fclose(obuf);
5777c478bd9Sstevel@tonic-gate 			if (tbuf)
5787c478bd9Sstevel@tonic-gate 				fclose(tbuf);
5797c478bd9Sstevel@tonic-gate 			relsesigs();
5807c478bd9Sstevel@tonic-gate 			longjmp(srbuf, 1);
5817c478bd9Sstevel@tonic-gate 		}
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 	gotcha = (c == 0 && tbuf == NULL);
5847c478bd9Sstevel@tonic-gate 	if (tbuf != NULL) {
5857c478bd9Sstevel@tonic-gate 		while ((c = getc(tbuf)) != EOF)
5867c478bd9Sstevel@tonic-gate 			putc(c, obuf);
5877c478bd9Sstevel@tonic-gate 		fclose(tbuf);
5887c478bd9Sstevel@tonic-gate 	}
5897c478bd9Sstevel@tonic-gate 	fflush(obuf);
5907c478bd9Sstevel@tonic-gate 	if (fferror(obuf)) {
5917c478bd9Sstevel@tonic-gate 		perror(editfile);
5927c478bd9Sstevel@tonic-gate 		fclose(ibuf);
5937c478bd9Sstevel@tonic-gate 		fclose(obuf);
5947c478bd9Sstevel@tonic-gate 		relsesigs();
5957c478bd9Sstevel@tonic-gate 		longjmp(srbuf, 1);
5967c478bd9Sstevel@tonic-gate 	}
5977c478bd9Sstevel@tonic-gate 	if (gotcha && !noremove && (value("keep") == NOSTR)) {
5987c478bd9Sstevel@tonic-gate 		removefile(editfile);
5997c478bd9Sstevel@tonic-gate 		printf(gettext("removed.\n"));
6007c478bd9Sstevel@tonic-gate 	}
6017c478bd9Sstevel@tonic-gate 	else
6027c478bd9Sstevel@tonic-gate 		printf(gettext("updated.\n"));
6037c478bd9Sstevel@tonic-gate 	fclose(ibuf);
6047c478bd9Sstevel@tonic-gate 	trunc(obuf);
6057c478bd9Sstevel@tonic-gate 	fclose(obuf);
6067c478bd9Sstevel@tonic-gate 	flush();
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate done:
6097c478bd9Sstevel@tonic-gate 	relsesigs();
6107c478bd9Sstevel@tonic-gate 	return (1);
6117c478bd9Sstevel@tonic-gate }
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate #ifndef OLD_BSD_SIGS
6147c478bd9Sstevel@tonic-gate static int sigdepth = 0;		/* depth of holdsigs() */
6157c478bd9Sstevel@tonic-gate #ifdef VMUNIX
6167c478bd9Sstevel@tonic-gate static int omask = 0;
6177c478bd9Sstevel@tonic-gate #else
6187c478bd9Sstevel@tonic-gate static	sigset_t mask, omask;
6197c478bd9Sstevel@tonic-gate #endif
6207c478bd9Sstevel@tonic-gate #endif
6217c478bd9Sstevel@tonic-gate /*
6227c478bd9Sstevel@tonic-gate  * Hold signals SIGHUP - SIGQUIT.
6237c478bd9Sstevel@tonic-gate  */
6247c478bd9Sstevel@tonic-gate void
holdsigs(void)6257c478bd9Sstevel@tonic-gate holdsigs(void)
6267c478bd9Sstevel@tonic-gate {
6277c478bd9Sstevel@tonic-gate #ifndef OLD_BSD_SIGS
6287c478bd9Sstevel@tonic-gate 	if (sigdepth++ == 0) {
6297c478bd9Sstevel@tonic-gate #ifdef VMUNIX
6307c478bd9Sstevel@tonic-gate 		omask = sigblock(sigmask(SIGHUP) |
6317c478bd9Sstevel@tonic-gate 		    sigmask(SIGINT)|sigmask(SIGQUIT));
6327c478bd9Sstevel@tonic-gate #else
6337c478bd9Sstevel@tonic-gate 		sigemptyset(&mask);
6347c478bd9Sstevel@tonic-gate 		sigaddset(&mask, SIGHUP);
6357c478bd9Sstevel@tonic-gate 		sigaddset(&mask, SIGINT);
6367c478bd9Sstevel@tonic-gate 		sigaddset(&mask, SIGQUIT);
6377c478bd9Sstevel@tonic-gate 		sigprocmask(SIG_BLOCK, &mask, &omask);
6387c478bd9Sstevel@tonic-gate #endif
6397c478bd9Sstevel@tonic-gate 	}
6407c478bd9Sstevel@tonic-gate #else
6417c478bd9Sstevel@tonic-gate 	sighold(SIGHUP);
6427c478bd9Sstevel@tonic-gate 	sighold(SIGINT);
6437c478bd9Sstevel@tonic-gate 	sighold(SIGQUIT);
6447c478bd9Sstevel@tonic-gate #endif
6457c478bd9Sstevel@tonic-gate }
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate /*
6487c478bd9Sstevel@tonic-gate  * Release signals SIGHUP - SIGQUIT
6497c478bd9Sstevel@tonic-gate  */
6507c478bd9Sstevel@tonic-gate void
relsesigs(void)6517c478bd9Sstevel@tonic-gate relsesigs(void)
6527c478bd9Sstevel@tonic-gate {
6537c478bd9Sstevel@tonic-gate #ifndef OLD_BSD_SIGS
6547c478bd9Sstevel@tonic-gate 	if (--sigdepth == 0)
6557c478bd9Sstevel@tonic-gate #ifdef VMUNIX
6567c478bd9Sstevel@tonic-gate 		sigsetmask(omask);
6577c478bd9Sstevel@tonic-gate #else
6587c478bd9Sstevel@tonic-gate 		sigprocmask(SIG_SETMASK, &omask, NULL);
6597c478bd9Sstevel@tonic-gate #endif
6607c478bd9Sstevel@tonic-gate #else
6617c478bd9Sstevel@tonic-gate 	sigrelse(SIGHUP);
6627c478bd9Sstevel@tonic-gate 	sigrelse(SIGINT);
6637c478bd9Sstevel@tonic-gate 	sigrelse(SIGQUIT);
6647c478bd9Sstevel@tonic-gate #endif
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate #if !defined(OLD_BSD_SIGS) && !defined(VMUNIX)
6687c478bd9Sstevel@tonic-gate void
sigset(int sig,void (* act)(int))6697c478bd9Sstevel@tonic-gate (*sigset(int sig, void (*act)(int)))(int)
6707c478bd9Sstevel@tonic-gate {
6717c478bd9Sstevel@tonic-gate 	struct sigaction sa, osa;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	sa.sa_handler = (void (*)())act;
6747c478bd9Sstevel@tonic-gate 	sigemptyset(&sa.sa_mask);
6757c478bd9Sstevel@tonic-gate 	sa.sa_flags = SA_RESTART;
6767c478bd9Sstevel@tonic-gate 	if (sigaction(sig, &sa, &osa) < 0)
6777c478bd9Sstevel@tonic-gate 		return ((void (*)(int))-1);
6787c478bd9Sstevel@tonic-gate 	return ((void (*)(int))osa.sa_handler);
6797c478bd9Sstevel@tonic-gate }
6807c478bd9Sstevel@tonic-gate #endif
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate /*
6837c478bd9Sstevel@tonic-gate  * Flush the standard output.
6847c478bd9Sstevel@tonic-gate  */
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate void
flush(void)6877c478bd9Sstevel@tonic-gate flush(void)
6887c478bd9Sstevel@tonic-gate {
6897c478bd9Sstevel@tonic-gate 	fflush(stdout);
6907c478bd9Sstevel@tonic-gate 	fflush(stderr);
6917c478bd9Sstevel@tonic-gate }
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate /*
6947c478bd9Sstevel@tonic-gate  * Determine the size of the file possessed by
6957c478bd9Sstevel@tonic-gate  * the passed buffer.
6967c478bd9Sstevel@tonic-gate  */
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate off_t
fsize(FILE * iob)6997c478bd9Sstevel@tonic-gate fsize(FILE *iob)
7007c478bd9Sstevel@tonic-gate {
7017c478bd9Sstevel@tonic-gate 	register int f;
7027c478bd9Sstevel@tonic-gate 	struct stat sbuf;
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	f = fileno(iob);
7057c478bd9Sstevel@tonic-gate 	if (fstat(f, &sbuf) < 0)
7067c478bd9Sstevel@tonic-gate 		return (0);
7077c478bd9Sstevel@tonic-gate 	return (sbuf.st_size);
7087c478bd9Sstevel@tonic-gate }
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate /*
7117c478bd9Sstevel@tonic-gate  * Check for either a stdio recognized error, or
7127c478bd9Sstevel@tonic-gate  * a possibly delayed write error (in case it's
7137c478bd9Sstevel@tonic-gate  * an NFS file, for instance).
7147c478bd9Sstevel@tonic-gate  */
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate int
fferror(FILE * iob)7177c478bd9Sstevel@tonic-gate fferror(FILE *iob)
7187c478bd9Sstevel@tonic-gate {
7197c478bd9Sstevel@tonic-gate 	return (ferror(iob) || fsync(fileno(iob)) < 0);
7207c478bd9Sstevel@tonic-gate }
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate /*
7237c478bd9Sstevel@tonic-gate  * Take a file name, possibly with shell meta characters
7247c478bd9Sstevel@tonic-gate  * in it and expand it by using wordexp().
7257c478bd9Sstevel@tonic-gate  * Return the file name as a dynamic string.
7267c478bd9Sstevel@tonic-gate  * If the name cannot be expanded (for whatever reason)
7277c478bd9Sstevel@tonic-gate  * return NULL.
7287c478bd9Sstevel@tonic-gate  */
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate char *
expand(char * name)7317c478bd9Sstevel@tonic-gate expand(char *name)
7327c478bd9Sstevel@tonic-gate {
7337c478bd9Sstevel@tonic-gate 	char xname[BUFSIZ];
7347c478bd9Sstevel@tonic-gate 	char foldbuf[BUFSIZ];
7357c478bd9Sstevel@tonic-gate 	register char *cp;
7367c478bd9Sstevel@tonic-gate 	register int l;
7377c478bd9Sstevel@tonic-gate 	wordexp_t wrdexp_buf;
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	if (debug) fprintf(stderr, "expand(%s)=", name);
7407c478bd9Sstevel@tonic-gate 	cp = strchr(name, '\0') - 1;	/* pointer to last char of name */
7417c478bd9Sstevel@tonic-gate 	if (isspace(*cp)) {
7427c478bd9Sstevel@tonic-gate 		/* strip off trailing blanks */
7437c478bd9Sstevel@tonic-gate 		while (cp > name && isspace(*cp))
7447c478bd9Sstevel@tonic-gate 			cp--;
7457c478bd9Sstevel@tonic-gate 		l = *++cp;	/* save char */
7467c478bd9Sstevel@tonic-gate 		*cp = '\0';
7477c478bd9Sstevel@tonic-gate 		name = savestr(name);
7487c478bd9Sstevel@tonic-gate 		*cp = (char)l;	/* restore char */
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate 	if (name[0] == '+' && getfold(foldbuf) >= 0) {
7517c478bd9Sstevel@tonic-gate 		snprintf(xname, sizeof (xname), "%s/%s", foldbuf, name + 1);
7527c478bd9Sstevel@tonic-gate 		cp = safeexpand(savestr(xname));
7537c478bd9Sstevel@tonic-gate 		if (debug) fprintf(stderr, "%s\n", cp);
7547c478bd9Sstevel@tonic-gate 		return (cp);
7557c478bd9Sstevel@tonic-gate 	}
7567c478bd9Sstevel@tonic-gate 	if (!anyof(name, "~{[*?$`'\"\\")) {
7577c478bd9Sstevel@tonic-gate 		if (debug) fprintf(stderr, "%s\n", name);
7587c478bd9Sstevel@tonic-gate 		return (name);
7597c478bd9Sstevel@tonic-gate 	}
760*5422785dSRobert Mustacchi 	if (wordexp(name, &wrdexp_buf, WRDE_NOCMD) != 0) {
7617c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Syntax error in \"%s\"\n"), name);
7627c478bd9Sstevel@tonic-gate 		fflush(stderr);
7637c478bd9Sstevel@tonic-gate 		return (NOSTR);
7647c478bd9Sstevel@tonic-gate 	}
7657c478bd9Sstevel@tonic-gate 	if (wrdexp_buf.we_wordc > 1) {
7667c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("\"%s\": Ambiguous\n"), name);
7677c478bd9Sstevel@tonic-gate 		fflush(stderr);
7687c478bd9Sstevel@tonic-gate 		return (NOSTR);
7697c478bd9Sstevel@tonic-gate 	}
7707c478bd9Sstevel@tonic-gate 	if (debug) fprintf(stderr, "%s\n", wrdexp_buf.we_wordv[0]);
7717c478bd9Sstevel@tonic-gate 	return (savestr(wrdexp_buf.we_wordv[0]));
7727c478bd9Sstevel@tonic-gate }
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate /*
7757c478bd9Sstevel@tonic-gate  * Take a file name, possibly with shell meta characters
7767c478bd9Sstevel@tonic-gate  * in it and expand it by using "sh -c echo filename"
7777c478bd9Sstevel@tonic-gate  * Return the file name as a dynamic string.
7787c478bd9Sstevel@tonic-gate  * If the name cannot be expanded (for whatever reason)
7797c478bd9Sstevel@tonic-gate  * return the original file name.
7807c478bd9Sstevel@tonic-gate  */
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate char *
safeexpand(char name[])7837c478bd9Sstevel@tonic-gate safeexpand(char name[])
7847c478bd9Sstevel@tonic-gate {
7857c478bd9Sstevel@tonic-gate 	char *t = expand(name);
7867c478bd9Sstevel@tonic-gate 	return (t) ? t : savestr(name);
7877c478bd9Sstevel@tonic-gate }
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate /*
7907c478bd9Sstevel@tonic-gate  * Determine the current folder directory name.
7917c478bd9Sstevel@tonic-gate  */
7926c83d09fSrobbin int
getfold(char * name)7937c478bd9Sstevel@tonic-gate getfold(char *name)
7947c478bd9Sstevel@tonic-gate {
7957c478bd9Sstevel@tonic-gate 	char *folder;
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	if ((folder = value("folder")) == NOSTR || *folder == '\0')
7987c478bd9Sstevel@tonic-gate 		return (-1);
7997c478bd9Sstevel@tonic-gate 	/*
8007c478bd9Sstevel@tonic-gate 	 * If name looks like a folder name, don't try
8017c478bd9Sstevel@tonic-gate 	 * to expand it, to prevent infinite recursion.
8027c478bd9Sstevel@tonic-gate 	 */
8037c478bd9Sstevel@tonic-gate 	if (*folder != '+' && (folder = expand(folder)) == NOSTR ||
8047c478bd9Sstevel@tonic-gate 	    *folder == '\0')
8057c478bd9Sstevel@tonic-gate 		return (-1);
8067c478bd9Sstevel@tonic-gate 	if (*folder == '/') {
8077c478bd9Sstevel@tonic-gate 		nstrcpy(name, BUFSIZ, folder);
8087c478bd9Sstevel@tonic-gate 	} else
8097c478bd9Sstevel@tonic-gate 		snprintf(name, BUFSIZ, "%s/%s", homedir, folder);
8107c478bd9Sstevel@tonic-gate 	return (0);
8117c478bd9Sstevel@tonic-gate }
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate /*
8147c478bd9Sstevel@tonic-gate  * A nicer version of Fdopen, which allows us to fclose
8157c478bd9Sstevel@tonic-gate  * without losing the open file.
8167c478bd9Sstevel@tonic-gate  */
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate FILE *
Fdopen(int fildes,char * mode)8197c478bd9Sstevel@tonic-gate Fdopen(int fildes, char *mode)
8207c478bd9Sstevel@tonic-gate {
8217c478bd9Sstevel@tonic-gate 	register int f;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	f = dup(fildes);
8247c478bd9Sstevel@tonic-gate 	if (f < 0) {
8257c478bd9Sstevel@tonic-gate 		perror("dup");
8267c478bd9Sstevel@tonic-gate 		return (NULL);
8277c478bd9Sstevel@tonic-gate 	}
8287c478bd9Sstevel@tonic-gate 	return (fdopen(f, mode));
8297c478bd9Sstevel@tonic-gate }
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate /*
8327c478bd9Sstevel@tonic-gate  * return the filename associated with "s".  This function always
8337c478bd9Sstevel@tonic-gate  * returns a non-null string (no error checking is done on the receiving end)
8347c478bd9Sstevel@tonic-gate  */
8357c478bd9Sstevel@tonic-gate char *
Getf(register char * s)8367c478bd9Sstevel@tonic-gate Getf(register char *s)
8377c478bd9Sstevel@tonic-gate {
8387c478bd9Sstevel@tonic-gate 	register char *cp;
8397c478bd9Sstevel@tonic-gate 	static char defbuf[PATHSIZE];
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	if (((cp = value(s)) != 0) && *cp) {
8427c478bd9Sstevel@tonic-gate 		return (safeexpand(cp));
8437c478bd9Sstevel@tonic-gate 	} else if (strcmp(s, "MBOX") == 0) {
8447c478bd9Sstevel@tonic-gate 		snprintf(defbuf, sizeof (defbuf), "%s/%s", Getf("HOME"),
8457c478bd9Sstevel@tonic-gate 			"mbox");
8467c478bd9Sstevel@tonic-gate 		return (defbuf);
8477c478bd9Sstevel@tonic-gate 	} else if (strcmp(s, "DEAD") == 0) {
8487c478bd9Sstevel@tonic-gate 		snprintf(defbuf, sizeof (defbuf), "%s/%s", Getf("HOME"),
8497c478bd9Sstevel@tonic-gate 			"dead.letter");
8507c478bd9Sstevel@tonic-gate 		return (defbuf);
8517c478bd9Sstevel@tonic-gate 	} else if (strcmp(s, "MAILRC") == 0) {
8527c478bd9Sstevel@tonic-gate 		snprintf(defbuf, sizeof (defbuf), "%s/%s", Getf("HOME"),
8537c478bd9Sstevel@tonic-gate 			".mailrc");
8547c478bd9Sstevel@tonic-gate 		return (defbuf);
8557c478bd9Sstevel@tonic-gate 	} else if (strcmp(s, "HOME") == 0) {
8567c478bd9Sstevel@tonic-gate 		/* no recursion allowed! */
8577c478bd9Sstevel@tonic-gate 		return (".");
8587c478bd9Sstevel@tonic-gate 	}
8597c478bd9Sstevel@tonic-gate 	return ("DEAD");	/* "cannot happen" */
8607c478bd9Sstevel@tonic-gate }
861