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