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