xref: /titanic_50/usr/src/cmd/mailx/main.c (revision 5422785d352a2bb398daceab3d1898a8aa64d006)
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
5*96ccc8cbSesaxe  * Common Development and Distribution License (the "License").
6*96ccc8cbSesaxe  * 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 /*
23*96ccc8cbSesaxe  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
246c83d09fSrobbin  * Use is subject to license terms.
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 #ifndef preSVr4
427c478bd9Sstevel@tonic-gate #include <locale.h>
437c478bd9Sstevel@tonic-gate #endif
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * mailx -- a modified version of a University of California at Berkeley
477c478bd9Sstevel@tonic-gate  *	mail program
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  * Startup -- interface with user.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static void		hdrstop(int);
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate static jmp_buf	hdrjmp;
557c478bd9Sstevel@tonic-gate 
56*96ccc8cbSesaxe const char *const version = "mailx version 5.0";
57*96ccc8cbSesaxe 
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate  * Find out who the user is, copy his mail file (if exists) into
607c478bd9Sstevel@tonic-gate  * /tmp/Rxxxxx and set up the message pointers.  Then, print out the
617c478bd9Sstevel@tonic-gate  * message headers and read user commands.
627c478bd9Sstevel@tonic-gate  *
637c478bd9Sstevel@tonic-gate  * Command line syntax:
647c478bd9Sstevel@tonic-gate  *	mailx [ -i ] [ -r address ] [ -h number ] [ -f [ name ] ]
657c478bd9Sstevel@tonic-gate  * or:
667c478bd9Sstevel@tonic-gate  *	mailx [ -i ] [ -r address ] [ -h number ] people ...
677c478bd9Sstevel@tonic-gate  *
687c478bd9Sstevel@tonic-gate  * and a bunch of other options.
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)727c478bd9Sstevel@tonic-gate main(int argc, char **argv)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	register char *ef;
757c478bd9Sstevel@tonic-gate 	register int argp;
767c478bd9Sstevel@tonic-gate 	int mustsend, f, goerr = 0;
777c478bd9Sstevel@tonic-gate 	void (*prevint)(int);
787c478bd9Sstevel@tonic-gate 	int loaded = 0;
797c478bd9Sstevel@tonic-gate 	struct termio tbuf;
807c478bd9Sstevel@tonic-gate 	struct termios tbufs;
817c478bd9Sstevel@tonic-gate 	int c;
827c478bd9Sstevel@tonic-gate 	char *cwd, *mf;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	/*
857c478bd9Sstevel@tonic-gate 	 * Set up a reasonable environment.
867c478bd9Sstevel@tonic-gate 	 * Figure out whether we are being run interactively, set up
877c478bd9Sstevel@tonic-gate 	 * all the temporary files, buffer standard output, and so forth.
887c478bd9Sstevel@tonic-gate 	 */
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #ifndef preSVr4
917c478bd9Sstevel@tonic-gate 	(void)setlocale(LC_ALL, "");
927c478bd9Sstevel@tonic-gate #endif
937c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
947c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
957c478bd9Sstevel@tonic-gate #endif
967c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate #ifdef SIGCONT
997c478bd9Sstevel@tonic-gate 	sigset(SIGCONT, SIG_DFL);
1007c478bd9Sstevel@tonic-gate #endif
1017c478bd9Sstevel@tonic-gate 	rpterr = 0;	/* initialize; set when we output to stderr */
1027c478bd9Sstevel@tonic-gate 	progname = argv[0];
1037c478bd9Sstevel@tonic-gate 	if (progname[strlen(progname) - 1] != 'x') {
1047c478bd9Sstevel@tonic-gate 		assign("bsdcompat", "");
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 	myegid = getegid();
1077c478bd9Sstevel@tonic-gate 	myrgid = getgid();
1087c478bd9Sstevel@tonic-gate 	myeuid = geteuid();
1097c478bd9Sstevel@tonic-gate 	myruid = getuid();
1107c478bd9Sstevel@tonic-gate 	mypid = getpid();
1117c478bd9Sstevel@tonic-gate 	setgid(myrgid);
1127c478bd9Sstevel@tonic-gate 	setuid(myruid);
1137c478bd9Sstevel@tonic-gate 	inithost();
1147c478bd9Sstevel@tonic-gate 	intty = isatty(0);
1157c478bd9Sstevel@tonic-gate 	if (ioctl(1, TCGETS, &tbufs) < 0) {
1167c478bd9Sstevel@tonic-gate 		if (ioctl(1, TCGETA, &tbuf)==0) {
1177c478bd9Sstevel@tonic-gate 			outtty = 1;
1187c478bd9Sstevel@tonic-gate 			baud = tbuf.c_cflag & CBAUD;
1197c478bd9Sstevel@tonic-gate 		} else
1207c478bd9Sstevel@tonic-gate 			baud = B9600;
1217c478bd9Sstevel@tonic-gate 	} else {
1227c478bd9Sstevel@tonic-gate 		outtty = 1;
1237c478bd9Sstevel@tonic-gate 		baud = cfgetospeed(&tbufs);
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 	image = -1;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	/*
1287c478bd9Sstevel@tonic-gate 	 * Now, determine how we are being used.
1297c478bd9Sstevel@tonic-gate 	 * We successively pick off instances of -r, -h, -f, and -i.
1307c478bd9Sstevel@tonic-gate 	 * If called as "rmail" we note this fact for letter sending.
1317c478bd9Sstevel@tonic-gate 	 * If there is anything left, it is the base of the list
1327c478bd9Sstevel@tonic-gate 	 * of users to mail to.  Argp will be set to point to the
1337c478bd9Sstevel@tonic-gate 	 * first of these users.
1347c478bd9Sstevel@tonic-gate 	 */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	ef = NOSTR;
1377c478bd9Sstevel@tonic-gate 	argp = -1;
1387c478bd9Sstevel@tonic-gate 	mustsend = 0;
1397c478bd9Sstevel@tonic-gate 	if (argc > 0 && **argv == 'r')
1407c478bd9Sstevel@tonic-gate 		rmail++;
1417c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "b:Bc:defFh:HiInNr:s:u:UtT:vV~")) != EOF)
1427c478bd9Sstevel@tonic-gate 		switch (c) {
1437c478bd9Sstevel@tonic-gate 		case 'e':
1447c478bd9Sstevel@tonic-gate 			/*
1457c478bd9Sstevel@tonic-gate 			 * exit status only
1467c478bd9Sstevel@tonic-gate 			 */
1477c478bd9Sstevel@tonic-gate 			exitflg++;
1487c478bd9Sstevel@tonic-gate 			break;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 		case 'r':
1517c478bd9Sstevel@tonic-gate 			/*
1527c478bd9Sstevel@tonic-gate 			 * Next argument is address to be sent along
1537c478bd9Sstevel@tonic-gate 			 * to the mailer.
1547c478bd9Sstevel@tonic-gate 			 */
1557c478bd9Sstevel@tonic-gate 			mustsend++;
1567c478bd9Sstevel@tonic-gate 			rflag = optarg;
1577c478bd9Sstevel@tonic-gate 			break;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 		case 'T':
1607c478bd9Sstevel@tonic-gate 			/*
1617c478bd9Sstevel@tonic-gate 			 * Next argument is temp file to write which
1627c478bd9Sstevel@tonic-gate 			 * articles have been read/deleted for netnews.
1637c478bd9Sstevel@tonic-gate 			 */
1647c478bd9Sstevel@tonic-gate 			Tflag = optarg;
1657c478bd9Sstevel@tonic-gate 			if ((f = creat(Tflag, TEMPPERM)) < 0) {
1667c478bd9Sstevel@tonic-gate 				perror(Tflag);
1677c478bd9Sstevel@tonic-gate 				exit(1);
1687c478bd9Sstevel@tonic-gate 			}
1697c478bd9Sstevel@tonic-gate 			close(f);
1707c478bd9Sstevel@tonic-gate 			/* fall through for -I too */
1717c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 		case 'I':
1747c478bd9Sstevel@tonic-gate 			/*
1757c478bd9Sstevel@tonic-gate 			 * print newsgroup in header summary
1767c478bd9Sstevel@tonic-gate 			 */
1777c478bd9Sstevel@tonic-gate 			newsflg++;
1787c478bd9Sstevel@tonic-gate 			break;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 		case 'u':
1817c478bd9Sstevel@tonic-gate 			/*
1827c478bd9Sstevel@tonic-gate 			 * Next argument is person's mailbox to use.
1837c478bd9Sstevel@tonic-gate 			 * Treated the same as "-f /var/mail/user".
1847c478bd9Sstevel@tonic-gate 			 */
1857c478bd9Sstevel@tonic-gate 			{
1867c478bd9Sstevel@tonic-gate 			static char u[PATHSIZE];
1877c478bd9Sstevel@tonic-gate 			snprintf(u, sizeof (u), "%s%s", maildir, optarg);
1887c478bd9Sstevel@tonic-gate 			ef = u;
1897c478bd9Sstevel@tonic-gate 			break;
1907c478bd9Sstevel@tonic-gate 			}
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 		case 'i':
1937c478bd9Sstevel@tonic-gate 			/*
1947c478bd9Sstevel@tonic-gate 			 * User wants to ignore interrupts.
1957c478bd9Sstevel@tonic-gate 			 * Set the variable "ignore"
1967c478bd9Sstevel@tonic-gate 			 */
1977c478bd9Sstevel@tonic-gate 			assign("ignore", "");
1987c478bd9Sstevel@tonic-gate 			break;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 		case 'U':
2017c478bd9Sstevel@tonic-gate 			UnUUCP++;
2027c478bd9Sstevel@tonic-gate 			break;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 		case 'd':
2057c478bd9Sstevel@tonic-gate 			assign("debug", "");
2067c478bd9Sstevel@tonic-gate 			break;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 		case 'h':
2097c478bd9Sstevel@tonic-gate 			/*
2107c478bd9Sstevel@tonic-gate 			 * Specified sequence number for network.
2117c478bd9Sstevel@tonic-gate 			 * This is the number of "hops" made so
2127c478bd9Sstevel@tonic-gate 			 * far (count of times message has been
2137c478bd9Sstevel@tonic-gate 			 * forwarded) to help avoid infinite mail loops.
2147c478bd9Sstevel@tonic-gate 			 */
2157c478bd9Sstevel@tonic-gate 			mustsend++;
2167c478bd9Sstevel@tonic-gate 			hflag = atoi(optarg);
2177c478bd9Sstevel@tonic-gate 			if (hflag == 0) {
2187c478bd9Sstevel@tonic-gate 				fprintf(stderr,
2197c478bd9Sstevel@tonic-gate 				    gettext("-h needs non-zero number\n"));
2207c478bd9Sstevel@tonic-gate 				goerr++;
2217c478bd9Sstevel@tonic-gate 			}
2227c478bd9Sstevel@tonic-gate 			break;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 		case 's':
2257c478bd9Sstevel@tonic-gate 			/*
2267c478bd9Sstevel@tonic-gate 			 * Give a subject field for sending from
2277c478bd9Sstevel@tonic-gate 			 * non terminal
2287c478bd9Sstevel@tonic-gate 			 */
2297c478bd9Sstevel@tonic-gate 			mustsend++;
2307c478bd9Sstevel@tonic-gate 			sflag = optarg;
2317c478bd9Sstevel@tonic-gate 			break;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 		case 'c':	/* Cc: from command line */
2347c478bd9Sstevel@tonic-gate 			mustsend++;
2357c478bd9Sstevel@tonic-gate 			cflag = optarg;
2367c478bd9Sstevel@tonic-gate 			break;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 		case 'b':	/* Bcc: from command line */
2397c478bd9Sstevel@tonic-gate 			mustsend++;
2407c478bd9Sstevel@tonic-gate 			bflag = optarg;
2417c478bd9Sstevel@tonic-gate 			break;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 		case 'f':
2447c478bd9Sstevel@tonic-gate 			/*
2457c478bd9Sstevel@tonic-gate 			 * User is specifying file to "edit" with mailx,
2467c478bd9Sstevel@tonic-gate 			 * as opposed to reading system mailbox.
2477c478bd9Sstevel@tonic-gate 			 * If no argument is given after -f, we read his/her
2487c478bd9Sstevel@tonic-gate 			 * $MBOX file or mbox in his/her home directory.
2497c478bd9Sstevel@tonic-gate 			 */
2507c478bd9Sstevel@tonic-gate 			ef = (argc == optind || *argv[optind] == '-')
2517c478bd9Sstevel@tonic-gate 				? "" : argv[optind++];
2527c478bd9Sstevel@tonic-gate 			if (*ef && *ef != '/' && *ef != '+')
2537c478bd9Sstevel@tonic-gate 				cwd = getcwd(NOSTR, PATHSIZE);
2547c478bd9Sstevel@tonic-gate 			break;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 		case 'F':
2577c478bd9Sstevel@tonic-gate 			Fflag++;
2587c478bd9Sstevel@tonic-gate 			mustsend++;
2597c478bd9Sstevel@tonic-gate 			break;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 		case 'n':
2627c478bd9Sstevel@tonic-gate 			/*
2637c478bd9Sstevel@tonic-gate 			 * User doesn't want to source
2647c478bd9Sstevel@tonic-gate 			 *	/etc/mail/mailx.rc
2657c478bd9Sstevel@tonic-gate 			 */
2667c478bd9Sstevel@tonic-gate 			nosrc++;
2677c478bd9Sstevel@tonic-gate 			break;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 		case 'N':
2707c478bd9Sstevel@tonic-gate 			/*
2717c478bd9Sstevel@tonic-gate 			 * Avoid initial header printing.
2727c478bd9Sstevel@tonic-gate 			 */
2737c478bd9Sstevel@tonic-gate 			noheader++;
2747c478bd9Sstevel@tonic-gate 			break;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 		case 'H':
2777c478bd9Sstevel@tonic-gate 			/*
2787c478bd9Sstevel@tonic-gate 			 * Print headers and exit
2797c478bd9Sstevel@tonic-gate 			 */
2807c478bd9Sstevel@tonic-gate 			Hflag++;
2817c478bd9Sstevel@tonic-gate 			break;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 		case 'V':
2847c478bd9Sstevel@tonic-gate 			puts(version);
2857c478bd9Sstevel@tonic-gate 			return 0;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 		case '~':
2887c478bd9Sstevel@tonic-gate 			/*
2897c478bd9Sstevel@tonic-gate 			 * Permit tildas no matter where
2907c478bd9Sstevel@tonic-gate 			 * the input is coming from.
2917c478bd9Sstevel@tonic-gate 			 */
2927c478bd9Sstevel@tonic-gate 			assign("escapeok", "");
2937c478bd9Sstevel@tonic-gate 			break;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 		case 'v':
2967c478bd9Sstevel@tonic-gate 			/*
2977c478bd9Sstevel@tonic-gate 			 * Send mailer verbose flag
2987c478bd9Sstevel@tonic-gate 			 */
2997c478bd9Sstevel@tonic-gate 			assign("verbose", "");
3007c478bd9Sstevel@tonic-gate 			break;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 		case 'B':
3037c478bd9Sstevel@tonic-gate 			/*
3047c478bd9Sstevel@tonic-gate 			 * Don't buffer output
3057c478bd9Sstevel@tonic-gate 			 * (Line buffered is good enough)
3067c478bd9Sstevel@tonic-gate 			 */
3077c478bd9Sstevel@tonic-gate 			setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
3087c478bd9Sstevel@tonic-gate 			setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
3097c478bd9Sstevel@tonic-gate 			break;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 		case 't':
3127c478bd9Sstevel@tonic-gate 			/*
3137c478bd9Sstevel@tonic-gate 			 * Like sendmail -t, read headers from text
3147c478bd9Sstevel@tonic-gate 			 */
3157c478bd9Sstevel@tonic-gate 			tflag++;
3167c478bd9Sstevel@tonic-gate 			mustsend++;
3177c478bd9Sstevel@tonic-gate 			break;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 		case '?':
3207c478bd9Sstevel@tonic-gate 		default:
3217c478bd9Sstevel@tonic-gate 			goerr++;
3227c478bd9Sstevel@tonic-gate 			break;
3237c478bd9Sstevel@tonic-gate 		}
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	if (optind != argc)
3267c478bd9Sstevel@tonic-gate 		argp = optind;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	/*
3297c478bd9Sstevel@tonic-gate 	 * Check for inconsistent arguments.
3307c478bd9Sstevel@tonic-gate 	 */
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	if (newsflg && ef==NOSTR) {
3337c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Need -f with -I flag\n"));
3347c478bd9Sstevel@tonic-gate 		goerr++;
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 	if (ef != NOSTR && argp != -1) {
3377c478bd9Sstevel@tonic-gate 		fprintf(stderr,
3387c478bd9Sstevel@tonic-gate 		    gettext("Cannot give -f and people to send to.\n"));
3397c478bd9Sstevel@tonic-gate 		goerr++;
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 	if (exitflg && (mustsend || argp != -1))
3427c478bd9Sstevel@tonic-gate 		exit(1);	/* nonsense flags involving -e simply exit */
3437c478bd9Sstevel@tonic-gate 	if (tflag && argp != -1) {
3447c478bd9Sstevel@tonic-gate 		fprintf(stderr,
3457c478bd9Sstevel@tonic-gate 		    gettext("Ignoring recipients on command line with -t\n"));
3467c478bd9Sstevel@tonic-gate 		argp = -1;
3477c478bd9Sstevel@tonic-gate 	} else if (!tflag && mustsend && argp == -1) {
3487c478bd9Sstevel@tonic-gate 		fprintf(stderr,
3497c478bd9Sstevel@tonic-gate 	    gettext("The flags you gave are used only when sending mail.\n"));
3507c478bd9Sstevel@tonic-gate 		goerr++;
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 	if (goerr) {
3537c478bd9Sstevel@tonic-gate 		fprintf(stderr,
3547c478bd9Sstevel@tonic-gate gettext("Usage: %s -eiIUdFntBNHvV~ -T FILE -u USER -h hops -r address\n"),
3557c478bd9Sstevel@tonic-gate 		    progname);
3567c478bd9Sstevel@tonic-gate 		fprintf(stderr,
3577c478bd9Sstevel@tonic-gate 		    gettext("\t\t-s SUBJECT -f FILE users\n"));
3587c478bd9Sstevel@tonic-gate 		exit(1);
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 	tinit();
3617c478bd9Sstevel@tonic-gate 	input = stdin;
3627c478bd9Sstevel@tonic-gate 	rcvmode = !tflag && argp == -1;
3637c478bd9Sstevel@tonic-gate 	if (!nosrc)
3647c478bd9Sstevel@tonic-gate 		load(MASTER);
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	if (!rcvmode) {
3677c478bd9Sstevel@tonic-gate 		load(Getf("MAILRC"));
3687c478bd9Sstevel@tonic-gate 		if (tflag)
3697c478bd9Sstevel@tonic-gate 			tmail();
3707c478bd9Sstevel@tonic-gate 		else
3717c478bd9Sstevel@tonic-gate 			mail(&argv[argp]);
3727c478bd9Sstevel@tonic-gate 		exit(senderr ? senderr : rpterr);
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	/*
3767c478bd9Sstevel@tonic-gate 	 * Ok, we are reading mail.
3777c478bd9Sstevel@tonic-gate 	 * Decide whether we are editing a mailbox or reading
3787c478bd9Sstevel@tonic-gate 	 * the system mailbox, and open up the right stuff.
3797c478bd9Sstevel@tonic-gate 	 *
3807c478bd9Sstevel@tonic-gate 	 * Do this before sourcing the MAILRC, because there might be
3817c478bd9Sstevel@tonic-gate 	 * a 'chdir' there that breaks the -f option.  But if the
3827c478bd9Sstevel@tonic-gate 	 * file specified with -f is a folder name, go ahead and
3837c478bd9Sstevel@tonic-gate 	 * source the MAILRC anyway so that "folder" will be defined.
3847c478bd9Sstevel@tonic-gate 	 */
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	nstrcpy(origname, PATHSIZE, mailname);
3877c478bd9Sstevel@tonic-gate 	editfile = mailname;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	if (ef != NOSTR) {
3907c478bd9Sstevel@tonic-gate 		if (ef == NOSTR || *ef == '\0' || *ef == '+') {
3917c478bd9Sstevel@tonic-gate 			load(Getf("MAILRC"));
3927c478bd9Sstevel@tonic-gate 			loaded++;
3937c478bd9Sstevel@tonic-gate 		}
3947c478bd9Sstevel@tonic-gate 		ef = *ef ? safeexpand(ef) : Getf("MBOX");
3957c478bd9Sstevel@tonic-gate 		nstrcpy(origname, PATHSIZE, ef);
3967c478bd9Sstevel@tonic-gate 		if (ef[0] != '/') {
3977c478bd9Sstevel@tonic-gate 			if (cwd == NOSTR)
3987c478bd9Sstevel@tonic-gate 				cwd = getcwd(NOSTR, PATHSIZE);
3997c478bd9Sstevel@tonic-gate 			nstrcat(cwd, PATHSIZE, "/");
4007c478bd9Sstevel@tonic-gate 			nstrcat(cwd, PATHSIZE, ef);
4017c478bd9Sstevel@tonic-gate 			ef = cwd;
4027c478bd9Sstevel@tonic-gate 		}
4037c478bd9Sstevel@tonic-gate 		editfile = ef;
4047c478bd9Sstevel@tonic-gate 		edit++;
4057c478bd9Sstevel@tonic-gate 	}
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	if (setfile(editfile, edit) < 0)
4087c478bd9Sstevel@tonic-gate 		exit(1);
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	if (!loaded)
4117c478bd9Sstevel@tonic-gate 		load(Getf("MAILRC"));
4127c478bd9Sstevel@tonic-gate 	if (msgCount > 0 && !noheader && value("header") != NOSTR) {
4137c478bd9Sstevel@tonic-gate 		if (setjmp(hdrjmp) == 0) {
4147c478bd9Sstevel@tonic-gate 			if ((prevint = sigset(SIGINT, SIG_IGN)) != SIG_IGN)
4157c478bd9Sstevel@tonic-gate 				sigset(SIGINT, hdrstop);
4167c478bd9Sstevel@tonic-gate 			announce();
4177c478bd9Sstevel@tonic-gate 			fflush(stdout);
4187c478bd9Sstevel@tonic-gate 			sigset(SIGINT, prevint);
4197c478bd9Sstevel@tonic-gate 		}
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate 	if (Hflag || (!edit && msgCount == 0)) {
4227c478bd9Sstevel@tonic-gate 		if (!Hflag) {
4237c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("No mail for %s\n"), myname);
4247c478bd9Sstevel@tonic-gate 			Verhogen();
4257c478bd9Sstevel@tonic-gate 		}
4267c478bd9Sstevel@tonic-gate 		fflush(stdout);
4277c478bd9Sstevel@tonic-gate 		exit(rpterr);
4287c478bd9Sstevel@tonic-gate 	}
4297c478bd9Sstevel@tonic-gate 	commands();
4307c478bd9Sstevel@tonic-gate 	sigset(SIGHUP, SIG_IGN);
4317c478bd9Sstevel@tonic-gate 	sigset(SIGINT, SIG_IGN);
4327c478bd9Sstevel@tonic-gate 	sigset(SIGQUIT, SIG_IGN);
4337c478bd9Sstevel@tonic-gate 	if (!outtty)
4347c478bd9Sstevel@tonic-gate 		sigset(SIGPIPE, SIG_IGN);
4357c478bd9Sstevel@tonic-gate 	if (edit)
4367c478bd9Sstevel@tonic-gate 		edstop(0);
4377c478bd9Sstevel@tonic-gate 	else {
4387c478bd9Sstevel@tonic-gate 		quit(0);
4397c478bd9Sstevel@tonic-gate 		Verhogen();
4407c478bd9Sstevel@tonic-gate 	}
4416c83d09fSrobbin 	return (rpterr);
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate /*
4457c478bd9Sstevel@tonic-gate  * Interrupt printing of the headers.
4467c478bd9Sstevel@tonic-gate  */
4477c478bd9Sstevel@tonic-gate static void
4487c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
hdrstop(int)4497c478bd9Sstevel@tonic-gate hdrstop(int)
4507c478bd9Sstevel@tonic-gate #else
4517c478bd9Sstevel@tonic-gate /* ARGSUSED */
4527c478bd9Sstevel@tonic-gate hdrstop(int s)
4537c478bd9Sstevel@tonic-gate #endif
4547c478bd9Sstevel@tonic-gate {
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	fflush(stdout);
4577c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext("\nInterrupt\n"));
4587c478bd9Sstevel@tonic-gate # ifdef OLD_BSD_SIGS
4597c478bd9Sstevel@tonic-gate 	sigrelse(SIGINT);
4607c478bd9Sstevel@tonic-gate # endif
4617c478bd9Sstevel@tonic-gate 	longjmp(hdrjmp, 1);
4627c478bd9Sstevel@tonic-gate }
463