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