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