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