Home | History | Annotate | Line # | Download | only in msgs
msgs.c revision 1.5
      1 /*-
      2  * Copyright (c) 1980 The Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 char copyright[] =
     36 "@(#) Copyright (c) 1980 The Regents of the University of California.\n\
     37  All rights reserved.\n";
     38 #endif /* not lint */
     39 
     40 #ifndef lint
     41 /*static char sccsid[] = "from: @(#)msgs.c	5.8 (Berkeley) 2/4/91";*/
     42 static char rcsid[] = "$Id: msgs.c,v 1.5 1994/01/07 16:22:20 mycroft Exp $";
     43 #endif /* not lint */
     44 
     45 /*
     46  * msgs - a user bulletin board program
     47  *
     48  * usage:
     49  *	msgs [fhlopqr] [[-]number]	to read messages
     50  *	msgs -s				to place messages
     51  *	msgs -c [-days]			to clean up the bulletin board
     52  *
     53  * prompt commands are:
     54  *	y	print message
     55  *	n	flush message, go to next message
     56  *	q	flush message, quit
     57  *	p	print message, turn on 'pipe thru more' mode
     58  *	P	print message, turn off 'pipe thru more' mode
     59  *	-	reprint last message
     60  *	s[-][<num>] [<filename>]	save message
     61  *	m[-][<num>]	mail with message in temp mbox
     62  *	x	exit without flushing this message
     63  *	<num>	print message number <num>
     64  */
     65 
     66 #define V7		/* will look for TERM in the environment */
     67 #define OBJECT		/* will object to messages without Subjects */
     68 /* #define REJECT	/* will reject messages without Subjects
     69 			   (OBJECT must be defined also) */
     70 /* #define UNBUFFERED	/* use unbuffered output */
     71 
     72 #include <sys/param.h>
     73 #include <sys/stat.h>
     74 #include <dirent.h>
     75 #include <ctype.h>
     76 #include <errno.h>
     77 #include <pwd.h>
     78 #include <setjmp.h>
     79 #include <sgtty.h>
     80 #include <signal.h>
     81 #include <stdio.h>
     82 #include <stdlib.h>
     83 #include <string.h>
     84 #include <time.h>
     85 #include "pathnames.h"
     86 
     87 #define CMODE	0666		/* bounds file creation mode */
     88 #define NO	0
     89 #define YES	1
     90 #define SUPERUSER	0	/* superuser uid */
     91 #define DAEMON		1	/* daemon uid */
     92 #define NLINES	24		/* default number of lines/crt screen */
     93 #define NDAYS	21		/* default keep time for messages */
     94 #define DAYS	*24*60*60	/* seconds/day */
     95 #define MSGSRC	".msgsrc"	/* user's rc file */
     96 #define BOUNDS	"bounds"	/* message bounds file */
     97 #define NEXT	"Next message? [yq]"
     98 #define MORE	"More? [ynq]"
     99 #define NOMORE	"(No more) [q] ?"
    100 
    101 typedef	char	bool;
    102 
    103 FILE	*msgsrc;
    104 FILE	*newmsg;
    105 char	*sep = "-";
    106 char	inbuf[BUFSIZ];
    107 char	fname[128];
    108 char	cmdbuf[128];
    109 char	subj[128];
    110 char	from[128];
    111 char	date[128];
    112 char	*ptr;
    113 char	*in;
    114 bool	local;
    115 bool	ruptible;
    116 bool	totty;
    117 bool	seenfrom;
    118 bool	seensubj;
    119 bool	blankline;
    120 bool	printing = NO;
    121 bool	mailing = NO;
    122 bool	quitit = NO;
    123 bool	sending = NO;
    124 bool	intrpflg = NO;
    125 int	uid;
    126 int	msg;
    127 int	prevmsg;
    128 int	lct;
    129 int	nlines;
    130 int	Lpp = 0;
    131 time_t	t;
    132 time_t	keep;
    133 struct	sgttyb	otty;
    134 
    135 char	*mktemp();
    136 char	*nxtfld();
    137 void	onintr();
    138 void	onsusp();
    139 
    140 /* option initialization */
    141 bool	hdrs = NO;
    142 bool	qopt = NO;
    143 bool	hush = NO;
    144 bool	send_msg = NO;
    145 bool	locomode = NO;
    146 bool	use_pager = NO;
    147 bool	clean = NO;
    148 bool	lastcmd = NO;
    149 bool	restricted = NO;
    150 jmp_buf	tstpbuf;
    151 
    152 main(argc, argv)
    153 int argc; char *argv[];
    154 {
    155 	bool newrc, already;
    156 	int rcfirst = 0;		/* first message to print (from .rc) */
    157 	int rcback = 0;			/* amount to back off of rcfirst */
    158 	int firstmsg, nextmsg, lastmsg = 0;
    159 	int blast = 0;
    160 	FILE *bounds;
    161 
    162 #ifdef UNBUFFERED
    163 	setbuf(stdout, NULL);
    164 #endif
    165 
    166 	gtty(fileno(stdout), &otty);
    167 	time(&t);
    168 	setuid(uid = getuid());
    169 	ruptible = (signal(SIGINT, SIG_IGN) == SIG_DFL);
    170 	if (ruptible)
    171 		signal(SIGINT, SIG_DFL);
    172 
    173 	argc--, argv++;
    174 	while (argc > 0) {
    175 		if (isdigit(argv[0][0])) {	/* starting message # */
    176 			rcfirst = atoi(argv[0]);
    177 		}
    178 		else if (isdigit(argv[0][1])) {	/* backward offset */
    179 			rcback = atoi( &( argv[0][1] ) );
    180 		}
    181 		else {
    182 			ptr = *argv;
    183 			while (*ptr) switch (*ptr++) {
    184 
    185 			case '-':
    186 				break;
    187 
    188 			case 'c':
    189 				if (uid != SUPERUSER && uid != DAEMON) {
    190 					fprintf(stderr, "Sorry\n");
    191 					exit(1);
    192 				}
    193 				clean = YES;
    194 				break;
    195 
    196 			case 'f':		/* silently */
    197 				hush = YES;
    198 				break;
    199 
    200 			case 'h':		/* headers only */
    201 				hdrs = YES;
    202 				break;
    203 
    204 			case 'l':		/* local msgs only */
    205 				locomode = YES;
    206 				break;
    207 
    208 			case 'o':		/* option to save last message */
    209 				lastcmd = YES;
    210 				break;
    211 
    212 			case 'p':		/* pipe thru 'more' during long msgs */
    213 				use_pager = YES;
    214 				break;
    215 
    216 			case 'q':		/* query only */
    217 				qopt = YES;
    218 				break;
    219 
    220 			case 'r':		/* restricted */
    221 				restricted = YES;
    222 				break;
    223 
    224 			case 's':		/* sending TO msgs */
    225 				send_msg = YES;
    226 				break;
    227 
    228 			default:
    229 				fprintf(stderr,
    230 					"usage: msgs [fhlopqr] [[-]number]\n");
    231 				exit(1);
    232 			}
    233 		}
    234 		argc--, argv++;
    235 	}
    236 
    237 	/*
    238 	 * determine current message bounds
    239 	 */
    240 	sprintf(fname, "%s/%s", _PATH_MSGS, BOUNDS);
    241 	bounds = fopen(fname, "r");
    242 
    243 	if (bounds != NULL) {
    244 		fscanf(bounds, "%d %d\n", &firstmsg, &lastmsg);
    245 		fclose(bounds);
    246 		blast = lastmsg;	/* save upper bound */
    247 	}
    248 
    249 	if (clean)
    250 		keep = t - (rcback? rcback : NDAYS) DAYS;
    251 
    252 	if (clean || bounds == NULL) {	/* relocate message bounds */
    253 		struct dirent *dp;
    254 		struct stat stbuf;
    255 		bool seenany = NO;
    256 		DIR	*dirp;
    257 
    258 		dirp = opendir(_PATH_MSGS);
    259 		if (dirp == NULL) {
    260 			perror(_PATH_MSGS);
    261 			exit(errno);
    262 		}
    263 
    264 		firstmsg = 32767;
    265 		lastmsg = 0;
    266 
    267 		for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)){
    268 			register char *cp = dp->d_name;
    269 			register int i = 0;
    270 
    271 			if (dp->d_ino == 0)
    272 				continue;
    273 			if (dp->d_namlen == 0)
    274 				continue;
    275 
    276 			if (clean)
    277 				sprintf(inbuf, "%s/%s", _PATH_MSGS, cp);
    278 
    279 			while (isdigit(*cp))
    280 				i = i * 10 + *cp++ - '0';
    281 			if (*cp)
    282 				continue;	/* not a message! */
    283 
    284 			if (clean) {
    285 				if (stat(inbuf, &stbuf) != 0)
    286 					continue;
    287 				if (stbuf.st_mtime < keep
    288 				    && stbuf.st_mode&S_IWRITE) {
    289 					unlink(inbuf);
    290 					continue;
    291 				}
    292 			}
    293 
    294 			if (i > lastmsg)
    295 				lastmsg = i;
    296 			if (i < firstmsg)
    297 				firstmsg = i;
    298 			seenany = YES;
    299 		}
    300 		closedir(dirp);
    301 
    302 		if (!seenany) {
    303 			if (blast != 0)	/* never lower the upper bound! */
    304 				lastmsg = blast;
    305 			firstmsg = lastmsg + 1;
    306 		}
    307 		else if (blast > lastmsg)
    308 			lastmsg = blast;
    309 
    310 		if (!send_msg) {
    311 			bounds = fopen(fname, "w");
    312 			if (bounds == NULL) {
    313 				perror(fname);
    314 				exit(errno);
    315 			}
    316 			chmod(fname, CMODE);
    317 			fprintf(bounds, "%d %d\n", firstmsg, lastmsg);
    318 			fclose(bounds);
    319 		}
    320 	}
    321 
    322 	if (send_msg) {
    323 		/*
    324 		 * Send mode - place msgs in _PATH_MSGS
    325 		 */
    326 		bounds = fopen(fname, "w");
    327 		if (bounds == NULL) {
    328 			perror(fname);
    329 			exit(errno);
    330 		}
    331 
    332 		nextmsg = lastmsg + 1;
    333 		sprintf(fname, "%s/%d", _PATH_MSGS, nextmsg);
    334 		newmsg = fopen(fname, "w");
    335 		if (newmsg == NULL) {
    336 			perror(fname);
    337 			exit(errno);
    338 		}
    339 		chmod(fname, 0644);
    340 
    341 		fprintf(bounds, "%d %d\n", firstmsg, nextmsg);
    342 		fclose(bounds);
    343 
    344 		sending = YES;
    345 		if (ruptible)
    346 			signal(SIGINT, onintr);
    347 
    348 		if (isatty(fileno(stdin))) {
    349 			ptr = getpwuid(uid)->pw_name;
    350 			printf("Message %d:\nFrom %s %sSubject: ",
    351 				nextmsg, ptr, ctime(&t));
    352 			fflush(stdout);
    353 			fgets(inbuf, sizeof inbuf, stdin);
    354 			putchar('\n');
    355 			fflush(stdout);
    356 			fprintf(newmsg, "From %s %sSubject: %s\n",
    357 				ptr, ctime(&t), inbuf);
    358 			blankline = seensubj = YES;
    359 		}
    360 		else
    361 			blankline = seensubj = NO;
    362 		for (;;) {
    363 			fgets(inbuf, sizeof inbuf, stdin);
    364 			if (feof(stdin) || ferror(stdin))
    365 				break;
    366 			blankline = (blankline || (inbuf[0] == '\n'));
    367 			seensubj = (seensubj || (!blankline && (strncmp(inbuf, "Subj", 4) == 0)));
    368 			fputs(inbuf, newmsg);
    369 		}
    370 #ifdef OBJECT
    371 		if (!seensubj) {
    372 			printf("NOTICE: Messages should have a Subject field!\n");
    373 #ifdef REJECT
    374 			unlink(fname);
    375 #endif
    376 			exit(1);
    377 		}
    378 #endif
    379 		exit(ferror(stdin));
    380 	}
    381 	if (clean)
    382 		exit(0);
    383 
    384 	/*
    385 	 * prepare to display messages
    386 	 */
    387 	totty = (isatty(fileno(stdout)) != 0);
    388 	use_pager = use_pager && totty;
    389 
    390 	sprintf(fname, "%s/%s", getenv("HOME"), MSGSRC);
    391 	msgsrc = fopen(fname, "r+");
    392 	if (msgsrc) {
    393 		newrc = NO;
    394 		if (fscanf(msgsrc, "%d\n", &nextmsg) != 1 ||
    395 		    nextmsg > lastmsg+1) {
    396 			printf("Warning: bounds have been reset (%d, %d)\n",
    397 				firstmsg, lastmsg);
    398 			truncate(fname, (off_t)0);
    399 			newrc = YES;
    400 		}
    401 		else if (!rcfirst)
    402 			rcfirst = nextmsg - rcback;
    403 	}
    404 	else {
    405 		msgsrc = fopen(fname, "w+");
    406 		newrc = YES;
    407 	}
    408 	if (msgsrc == NULL) {
    409 		perror(fname);
    410 		exit(errno);
    411 	}
    412 	if (rcfirst) {
    413 		if (rcfirst > lastmsg+1) {
    414 			printf("Warning: the last message is number %d.\n",
    415 				lastmsg);
    416 			rcfirst = nextmsg;
    417 		}
    418 		if (rcfirst > firstmsg)
    419 			firstmsg = rcfirst;	/* don't set below first msg */
    420 	}
    421 	if (newrc) {
    422 		nextmsg = firstmsg;
    423 		fseek(msgsrc, 0L, 0);
    424 		fprintf(msgsrc, "%d\n", nextmsg);
    425 		fflush(msgsrc);
    426 	}
    427 
    428 #ifdef V7
    429 	if (totty) {
    430 		struct winsize win;
    431 		if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1)
    432 			Lpp = win.ws_row;
    433 		if (Lpp <= 0) {
    434 			if (tgetent(inbuf, getenv("TERM")) <= 0
    435 			    || (Lpp = tgetnum("li")) <= 0) {
    436 				Lpp = NLINES;
    437 			}
    438 		}
    439 	}
    440 #endif
    441 	Lpp -= 6;	/* for headers, etc. */
    442 
    443 	already = NO;
    444 	prevmsg = firstmsg;
    445 	printing = YES;
    446 	if (ruptible)
    447 		signal(SIGINT, onintr);
    448 
    449 	/*
    450 	 * Main program loop
    451 	 */
    452 	for (msg = firstmsg; msg <= lastmsg; msg++) {
    453 
    454 		sprintf(fname, "%s/%d", _PATH_MSGS, msg);
    455 		newmsg = fopen(fname, "r");
    456 		if (newmsg == NULL)
    457 			continue;
    458 
    459 		gfrsub(newmsg);		/* get From and Subject fields */
    460 		if (locomode && !local) {
    461 			fclose(newmsg);
    462 			continue;
    463 		}
    464 
    465 		if (qopt) {	/* This has to be located here */
    466 			printf("There are new messages.\n");
    467 			exit(0);
    468 		}
    469 
    470 		if (already && !hdrs)
    471 			putchar('\n');
    472 
    473 		/*
    474 		 * Print header
    475 		 */
    476 		if (totty)
    477 			signal(SIGTSTP, onsusp);
    478 		(void) setjmp(tstpbuf);
    479 		already = YES;
    480 		nlines = 2;
    481 		if (seenfrom) {
    482 			printf("Message %d:\nFrom %s %s", msg, from, date);
    483 			nlines++;
    484 		}
    485 		if (seensubj) {
    486 			printf("Subject: %s", subj);
    487 			nlines++;
    488 		}
    489 		else {
    490 			if (seenfrom) {
    491 				putchar('\n');
    492 				nlines++;
    493 			}
    494 			while (nlines < 6
    495 			    && fgets(inbuf, sizeof inbuf, newmsg)
    496 			    && inbuf[0] != '\n') {
    497 				fputs(inbuf, stdout);
    498 				nlines++;
    499 			}
    500 		}
    501 
    502 		lct = linecnt(newmsg);
    503 		if (lct)
    504 			printf("(%d%slines) ", lct, seensubj? " " : " more ");
    505 
    506 		if (hdrs) {
    507 			printf("\n-----\n");
    508 			fclose(newmsg);
    509 			continue;
    510 		}
    511 
    512 		/*
    513 		 * Ask user for command
    514 		 */
    515 		if (totty)
    516 			ask(lct? MORE : (msg==lastmsg? NOMORE : NEXT));
    517 		else
    518 			inbuf[0] = 'y';
    519 		if (totty)
    520 			signal(SIGTSTP, SIG_DFL);
    521 cmnd:
    522 		in = inbuf;
    523 		switch (*in) {
    524 			case 'x':
    525 			case 'X':
    526 				exit(0);
    527 
    528 			case 'q':
    529 			case 'Q':
    530 				quitit = YES;
    531 				printf("--Postponed--\n");
    532 				exit(0);
    533 				/* intentional fall-thru */
    534 			case 'n':
    535 			case 'N':
    536 				if (msg >= nextmsg) sep = "Flushed";
    537 				prevmsg = msg;
    538 				break;
    539 
    540 			case 'p':
    541 			case 'P':
    542 				use_pager = (*in++ == 'p');
    543 				/* intentional fallthru */
    544 			case '\n':
    545 			case 'y':
    546 			default:
    547 				if (*in == '-') {
    548 					msg = prevmsg-1;
    549 					sep = "replay";
    550 					break;
    551 				}
    552 				if (isdigit(*in)) {
    553 					msg = next(in);
    554 					sep = in;
    555 					break;
    556 				}
    557 
    558 				prmesg(nlines + lct + (seensubj? 1 : 0));
    559 				prevmsg = msg;
    560 
    561 		}
    562 
    563 		printf("--%s--\n", sep);
    564 		sep = "-";
    565 		if (msg >= nextmsg) {
    566 			nextmsg = msg + 1;
    567 			fseek(msgsrc, 0L, 0);
    568 			fprintf(msgsrc, "%d\n", nextmsg);
    569 			fflush(msgsrc);
    570 		}
    571 		if (newmsg)
    572 			fclose(newmsg);
    573 		if (quitit)
    574 			break;
    575 	}
    576 
    577 	/*
    578 	 * Make sure .rc file gets updated
    579 	 */
    580 	if (--msg >= nextmsg) {
    581 		nextmsg = msg + 1;
    582 		fseek(msgsrc, 0L, 0);
    583 		fprintf(msgsrc, "%d\n", nextmsg);
    584 		fflush(msgsrc);
    585 	}
    586 	if (already && !quitit && lastcmd && totty) {
    587 		/*
    588 		 * save or reply to last message?
    589 		 */
    590 		msg = prevmsg;
    591 		ask(NOMORE);
    592 		if (inbuf[0] == '-' || isdigit(inbuf[0]))
    593 			goto cmnd;
    594 	}
    595 	if (!(already || hush || qopt))
    596 		printf("No new messages.\n");
    597 	exit(0);
    598 }
    599 
    600 prmesg(length)
    601 int length;
    602 {
    603 	FILE *outf;
    604 	char *env_pager;
    605 
    606 	if (use_pager && length > Lpp) {
    607 		signal(SIGPIPE, SIG_IGN);
    608 		signal(SIGQUIT, SIG_IGN);
    609 		if ((env_pager = getenv("PAGER")) == NULL) {
    610 			sprintf(cmdbuf, _PATH_PAGER, Lpp);
    611 		} else {
    612 			strcpy(cmdbuf, env_pager);
    613 		}
    614 		outf = popen(cmdbuf, "w");
    615 		if (!outf)
    616 			outf = stdout;
    617 		else
    618 			setbuf(outf, (char *)NULL);
    619 	}
    620 	else
    621 		outf = stdout;
    622 
    623 	if (seensubj)
    624 		putc('\n', outf);
    625 
    626 	while (fgets(inbuf, sizeof inbuf, newmsg)) {
    627 		fputs(inbuf, outf);
    628 		if (ferror(outf)) {
    629 			clearerr(outf);
    630 			break;
    631 		}
    632 	}
    633 
    634 	if (outf != stdout) {
    635 		pclose(outf);
    636 		signal(SIGPIPE, SIG_DFL);
    637 		signal(SIGQUIT, SIG_DFL);
    638 	}
    639 	else {
    640 		fflush(stdout);
    641 	}
    642 
    643 	/* trick to force wait on output */
    644 	stty(fileno(stdout), &otty);
    645 }
    646 
    647 void
    648 onintr()
    649 {
    650 	signal(SIGINT, onintr);
    651 	if (mailing)
    652 		unlink(fname);
    653 	if (sending) {
    654 		unlink(fname);
    655 		puts("--Killed--");
    656 		exit(1);
    657 	}
    658 	if (printing) {
    659 		putchar('\n');
    660 		if (hdrs)
    661 			exit(0);
    662 		sep = "Interrupt";
    663 		if (newmsg)
    664 			fseek(newmsg, 0L, 2);
    665 		intrpflg = YES;
    666 	}
    667 }
    668 
    669 /*
    670  * We have just gotten a susp.  Suspend and prepare to resume.
    671  */
    672 void
    673 onsusp()
    674 {
    675 
    676 	signal(SIGTSTP, SIG_DFL);
    677 	sigsetmask(0);
    678 	kill(0, SIGTSTP);
    679 	signal(SIGTSTP, onsusp);
    680 	if (!mailing)
    681 		longjmp(tstpbuf, 0);
    682 }
    683 
    684 linecnt(f)
    685 FILE *f;
    686 {
    687 	off_t oldpos = ftell(f);
    688 	int l = 0;
    689 	char lbuf[BUFSIZ];
    690 
    691 	while (fgets(lbuf, sizeof lbuf, f))
    692 		l++;
    693 	clearerr(f);
    694 	fseek(f, oldpos, 0);
    695 	return (l);
    696 }
    697 
    698 next(buf)
    699 char *buf;
    700 {
    701 	int i;
    702 	sscanf(buf, "%d", &i);
    703 	sprintf(buf, "Goto %d", i);
    704 	return(--i);
    705 }
    706 
    707 ask(prompt)
    708 char *prompt;
    709 {
    710 	char	inch;
    711 	int	n, cmsg;
    712 	off_t	oldpos;
    713 	FILE	*cpfrom, *cpto;
    714 
    715 	printf("%s ", prompt);
    716 	fflush(stdout);
    717 	intrpflg = NO;
    718 	(void) fgets(inbuf, sizeof inbuf, stdin);
    719 	if ((n = strlen(inbuf)) > 0 && inbuf[n - 1] == '\n')
    720 		inbuf[n - 1] = '\0';
    721 	if (intrpflg)
    722 		inbuf[0] = 'x';
    723 
    724 	/*
    725 	 * Handle 'mail' and 'save' here.
    726 	 */
    727 	if (((inch = inbuf[0]) == 's' || inch == 'm') && !restricted) {
    728 		if (inbuf[1] == '-')
    729 			cmsg = prevmsg;
    730 		else if (isdigit(inbuf[1]))
    731 			cmsg = atoi(&inbuf[1]);
    732 		else
    733 			cmsg = msg;
    734 		sprintf(fname, "%s/%d", _PATH_MSGS, cmsg);
    735 
    736 		oldpos = ftell(newmsg);
    737 
    738 		cpfrom = fopen(fname, "r");
    739 		if (!cpfrom) {
    740 			printf("Message %d not found\n", cmsg);
    741 			ask (prompt);
    742 			return;
    743 		}
    744 
    745 		if (inch == 's') {
    746 			in = nxtfld(inbuf);
    747 			if (*in) {
    748 				for (n=0; in[n] > ' '; n++) { /* sizeof fname? */
    749 					fname[n] = in[n];
    750 				}
    751 				fname[n] = NULL;
    752 			}
    753 			else
    754 				strcpy(fname, "Messages");
    755 		}
    756 		else {
    757 			strcpy(fname, _PATH_TMP);
    758 			mktemp(fname);
    759 			sprintf(cmdbuf, _PATH_MAIL, fname);
    760 			mailing = YES;
    761 		}
    762 		cpto = fopen(fname, "a");
    763 		if (!cpto) {
    764 			perror(fname);
    765 			mailing = NO;
    766 			fseek(newmsg, oldpos, 0);
    767 			ask(prompt);
    768 			return;
    769 		}
    770 
    771 		while (n = fread(inbuf, 1, sizeof inbuf, cpfrom))
    772 			fwrite(inbuf, 1, n, cpto);
    773 
    774 		fclose(cpfrom);
    775 		fclose(cpto);
    776 		fseek(newmsg, oldpos, 0);	/* reposition current message */
    777 		if (inch == 's')
    778 			printf("Message %d saved in \"%s\"\n", cmsg, fname);
    779 		else {
    780 			system(cmdbuf);
    781 			unlink(fname);
    782 			mailing = NO;
    783 		}
    784 		ask(prompt);
    785 	}
    786 }
    787 
    788 gfrsub(infile)
    789 FILE *infile;
    790 {
    791 	off_t frompos;
    792 
    793 	seensubj = seenfrom = NO;
    794 	local = YES;
    795 	subj[0] = from[0] = date[0] = NULL;
    796 
    797 	/*
    798 	 * Is this a normal message?
    799 	 */
    800 	if (fgets(inbuf, sizeof inbuf, infile)) {
    801 		if (strncmp(inbuf, "From", 4)==0) {
    802 			/*
    803 			 * expected form starts with From
    804 			 */
    805 			seenfrom = YES;
    806 			frompos = ftell(infile);
    807 			ptr = from;
    808 			in = nxtfld(inbuf);
    809 			if (*in) while (*in && *in > ' ') {
    810 				if (*in == ':' || *in == '@' || *in == '!')
    811 					local = NO;
    812 				*ptr++ = *in++;
    813 				/* what about sizeof from ? */
    814 			}
    815 			*ptr = NULL;
    816 			if (*(in = nxtfld(in)))
    817 				strncpy(date, in, sizeof date);
    818 			else {
    819 				date[0] = '\n';
    820 				date[1] = NULL;
    821 			}
    822 		}
    823 		else {
    824 			/*
    825 			 * not the expected form
    826 			 */
    827 			fseek(infile, 0L, 0);
    828 			return;
    829 		}
    830 	}
    831 	else
    832 		/*
    833 		 * empty file ?
    834 		 */
    835 		return;
    836 
    837 	/*
    838 	 * look for Subject line until EOF or a blank line
    839 	 */
    840 	while (fgets(inbuf, sizeof inbuf, infile)
    841 	    && !(blankline = (inbuf[0] == '\n'))) {
    842 		/*
    843 		 * extract Subject line
    844 		 */
    845 		if (!seensubj && strncmp(inbuf, "Subj", 4)==0) {
    846 			seensubj = YES;
    847 			frompos = ftell(infile);
    848 			strncpy(subj, nxtfld(inbuf), sizeof subj);
    849 		}
    850 	}
    851 	if (!blankline)
    852 		/*
    853 		 * ran into EOF
    854 		 */
    855 		fseek(infile, frompos, 0);
    856 
    857 	if (!seensubj)
    858 		/*
    859 		 * for possible use with Mail
    860 		 */
    861 		strncpy(subj, "(No Subject)\n", sizeof subj);
    862 }
    863 
    864 char *
    865 nxtfld(s)
    866 char *s;
    867 {
    868 	if (*s) while (*s && *s > ' ') s++;	/* skip over this field */
    869 	if (*s) while (*s && *s <= ' ') s++;	/* find start of next field */
    870 	return (s);
    871 }
    872