xref: /onnv-gate/usr/src/cmd/mail/gethead.c (revision 13093:48f2dbca79a2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*13093SRoger.Faulkner@Oracle.COM  * Common Development and Distribution License (the "License").
6*13093SRoger.Faulkner@Oracle.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*13093SRoger.Faulkner@Oracle.COM 
22373Sceastha /*
23*13093SRoger.Faulkner@Oracle.COM  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
24373Sceastha  */
25373Sceastha 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include "mail.h"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #define	MAXHDRSIZE	100	/* Maximum length of header line */
320Sstevel@tonic-gate #define	MAXUNAME	20	/* Maximum length of user name */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate  *	display headers, indicating current and status
360Sstevel@tonic-gate  *
370Sstevel@tonic-gate  *	current is the displacement into the mailfile of the
380Sstevel@tonic-gate  *	current letter
390Sstevel@tonic-gate  *
400Sstevel@tonic-gate  *	all indicates how many headers should be shown.
410Sstevel@tonic-gate  *		0	->	show window +/-6 around current
420Sstevel@tonic-gate  *		1	->	show all messages
430Sstevel@tonic-gate  *		2	->	show deleted messages
440Sstevel@tonic-gate  *
450Sstevel@tonic-gate  *	Only 100 characters of the From (first) header line will
460Sstevel@tonic-gate  *	be read in.  This line is assumed to be in the following
470Sstevel@tonic-gate  *	format:
480Sstevel@tonic-gate  *		From <sender address> <date>
490Sstevel@tonic-gate  *	where
500Sstevel@tonic-gate  *		<sender address> is either a UUCP-style (sysa!sysb!user)
510Sstevel@tonic-gate  *		or domain-style address (user@host).
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  *	If the sender address contains a UUCP-style address, then
540Sstevel@tonic-gate  *	the user name displayed is made up of the characters following
550Sstevel@tonic-gate  *	the final '!' in the sender address, otherwise the sender
560Sstevel@tonic-gate  *	address is considered to be the user name.
570Sstevel@tonic-gate  *
580Sstevel@tonic-gate  *	The maximum number of characters of a user name displayed
590Sstevel@tonic-gate  *	is 19.
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  */
620Sstevel@tonic-gate int
gethead(int current,int all)630Sstevel@tonic-gate gethead(int current, int all)
640Sstevel@tonic-gate {
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	int	displayed = 0;
670Sstevel@tonic-gate 	FILE	*file;
680Sstevel@tonic-gate 	char	*hold;
690Sstevel@tonic-gate 	char	holdval[MAXHDRSIZE];
700Sstevel@tonic-gate 	char	*wline;
710Sstevel@tonic-gate 	char	wlineval[MAXHDRSIZE];
720Sstevel@tonic-gate 	int	ln;
730Sstevel@tonic-gate 	char	mark;
740Sstevel@tonic-gate 	int	rc, size, start, stop, ix;
750Sstevel@tonic-gate 	char	userval[MAXUNAME];
760Sstevel@tonic-gate 	char	*uucpptr;
770Sstevel@tonic-gate 	int	uucpstart;
780Sstevel@tonic-gate 	int	unamechars = MAXUNAME - 1;
790Sstevel@tonic-gate 	int	sender_size;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	hold = holdval;
820Sstevel@tonic-gate 	wline = wlineval;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	printf("%d letters found in %s, %d scheduled for deletion, "
850Sstevel@tonic-gate 	    "%d newly arrived\n", nlet, mailfile, changed, nlet - onlet);
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	if (all == 2 && !changed)
880Sstevel@tonic-gate 		return (0);
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	file = doopen(lettmp, "r", E_TMP);
910Sstevel@tonic-gate 	if (!flgr) {
920Sstevel@tonic-gate 		stop = current - 6;
930Sstevel@tonic-gate 		if (stop < -1) stop = -1;
940Sstevel@tonic-gate 		start = current + 5;
950Sstevel@tonic-gate 		if (start > nlet - 1) start = nlet - 1;
960Sstevel@tonic-gate 		if (all) {
970Sstevel@tonic-gate 			start = nlet -1;
980Sstevel@tonic-gate 			stop = -1;
990Sstevel@tonic-gate 		}
1000Sstevel@tonic-gate 	} else {
1010Sstevel@tonic-gate 		stop = current + 6;
1020Sstevel@tonic-gate 		if (stop > nlet) stop = nlet;
1030Sstevel@tonic-gate 		start = current - 5;
1040Sstevel@tonic-gate 		if (start < 0) start = 0;
1050Sstevel@tonic-gate 		if (all) {
1060Sstevel@tonic-gate 			start = 0;
1070Sstevel@tonic-gate 			stop = nlet;
1080Sstevel@tonic-gate 		}
1090Sstevel@tonic-gate 	}
1100Sstevel@tonic-gate 	for (ln = start; ln != stop; ln = flgr ? ln + 1 : ln - 1) {
1110Sstevel@tonic-gate 		size = let[ln+1].adr - let[ln].adr;
1120Sstevel@tonic-gate 		if ((rc = fseek(file, let[ln].adr, 0)) != 0) {
1130Sstevel@tonic-gate 			errmsg(E_FILE, "Cannot seek header");
1140Sstevel@tonic-gate 			fclose(file);
1150Sstevel@tonic-gate 			return (1);
1160Sstevel@tonic-gate 		}
1170Sstevel@tonic-gate 		if (fgets(wline, MAXHDRSIZE, file) == NULL) {
1180Sstevel@tonic-gate 			errmsg(E_FILE, "Cannot read header");
1190Sstevel@tonic-gate 			fclose(file);
1200Sstevel@tonic-gate 			return (1);
1210Sstevel@tonic-gate 		}
1220Sstevel@tonic-gate 		if ((rc = strncmp(wline, header[H_FROM].tag, 5)) != SAME) {
1230Sstevel@tonic-gate 			errmsg(E_FILE, "Invalid header encountered");
1240Sstevel@tonic-gate 			fclose(file);
1250Sstevel@tonic-gate 			return (1);
1260Sstevel@tonic-gate 		}
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 		/* skip past trailing white space after header tag */
129*13093SRoger.Faulkner@Oracle.COM 		for (rc = 5; wline[rc] == ' ' || wline[rc] == '\t'; ++rc)
130*13093SRoger.Faulkner@Oracle.COM 			;
1310Sstevel@tonic-gate 		(void) strlcpy(hold, wline + rc, MAXHDRSIZE);
1320Sstevel@tonic-gate 		fgets(wline, MAXHDRSIZE, file);
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 		while (((rc = strncmp(wline,
1350Sstevel@tonic-gate 		    header[H_FROM1].tag, 6)) == SAME) &&
1360Sstevel@tonic-gate 		    (substr(wline, "remote from ") != -1)) {
1370Sstevel@tonic-gate 			(void) strlcpy(hold, wline + 6, MAXHDRSIZE);
1380Sstevel@tonic-gate 			fgets(wline, MAXHDRSIZE, file);
1390Sstevel@tonic-gate 		}
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 		/*
1430Sstevel@tonic-gate 		 * If UUCP-style sender address, then read past
1440Sstevel@tonic-gate 		 * last "!" to get the start of the user name.
1450Sstevel@tonic-gate 		 */
1460Sstevel@tonic-gate 		sender_size = strcspn(hold, " \t");
1470Sstevel@tonic-gate 		uucpstart = 0;
1480Sstevel@tonic-gate 		if ((uucpptr = strrchr(hold, '!')) != NULL) {
1490Sstevel@tonic-gate 			uucpstart = uucpptr - hold + 1;
1500Sstevel@tonic-gate 			if (uucpstart > sender_size) {
1510Sstevel@tonic-gate 				uucpstart = 0;
1520Sstevel@tonic-gate 			}
1530Sstevel@tonic-gate 		}
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 		/* Get the user name out of the sender address. */
1560Sstevel@tonic-gate 		for (ix = 0, rc = uucpstart; ix < unamechars &&
1570Sstevel@tonic-gate 		    hold[rc] != ' ' && hold[rc] != '\t' &&
1580Sstevel@tonic-gate 		    rc < sender_size; ++rc) {
1590Sstevel@tonic-gate 			userval[ix++] = hold[rc];
1600Sstevel@tonic-gate 		}
1610Sstevel@tonic-gate 		if ((ix > 0) && (userval[ix - 1] == '\n')) {
1620Sstevel@tonic-gate 			userval[ix - 1] = '\0';
1630Sstevel@tonic-gate 		} else {
1640Sstevel@tonic-gate 			userval[ix] = '\0';
1650Sstevel@tonic-gate 		}
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 		/*
1680Sstevel@tonic-gate 		 * Skip past the rest of the sender address, and
1690Sstevel@tonic-gate 		 * delimiting white space.
1700Sstevel@tonic-gate 		 */
1710Sstevel@tonic-gate 		for (; hold[rc] != '\0' && hold[rc] != ' ' &&
172*13093SRoger.Faulkner@Oracle.COM 		    hold[rc] != '\t'; ++rc)
173*13093SRoger.Faulkner@Oracle.COM 			;
174*13093SRoger.Faulkner@Oracle.COM 		for (; hold[rc] == ' ' || hold[rc] == '\t'; ++rc)
175*13093SRoger.Faulkner@Oracle.COM 			;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 		/* Get the date information. */
1780Sstevel@tonic-gate 		(void) strlcpy(wline, hold + rc, MAXHDRSIZE);
179*13093SRoger.Faulkner@Oracle.COM 		for (rc = 0; wline[rc] != '\0' && wline[rc] != '\n'; ++rc)
180*13093SRoger.Faulkner@Oracle.COM 			;
1810Sstevel@tonic-gate 		wline[rc] = '\0';
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 		if (!flgh && current == ln) mark = '>';
1840Sstevel@tonic-gate 		else mark = ' ';
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 		if (all == 2) {
1870Sstevel@tonic-gate 			if (displayed >= changed) {
1880Sstevel@tonic-gate 				fclose(file);
1890Sstevel@tonic-gate 				return (0);
1900Sstevel@tonic-gate 			}
1910Sstevel@tonic-gate 			if (let[ln].change == ' ') continue;
1920Sstevel@tonic-gate 		}
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 		printf("%c %3d  %c  %-5d  %-10s  %s\n", mark, ln + 1,
1950Sstevel@tonic-gate 		    let[ln].change, size, userval, wline);
1960Sstevel@tonic-gate 		displayed++;
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 	fclose(file);
1990Sstevel@tonic-gate 	return (0);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate void
tmperr(void)203373Sceastha tmperr(void)
2040Sstevel@tonic-gate {
2050Sstevel@tonic-gate 	fclose(tmpf);
2060Sstevel@tonic-gate 	errmsg(E_TMP, "");
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate /*
2100Sstevel@tonic-gate  *	Write a string out to tmp file, with error checking.
2110Sstevel@tonic-gate  *	Return 1 on success, else 0
2120Sstevel@tonic-gate  */
213373Sceastha int
wtmpf(char * str,int length)214373Sceastha wtmpf(char *str, int length)
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate 	if (fwrite(str, 1, length, tmpf) != length) {
2170Sstevel@tonic-gate 		tmperr();
2180Sstevel@tonic-gate 		return (0);
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate 	return (1);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate /*
2240Sstevel@tonic-gate  *	Read a line from stdin, assign it to line and
2250Sstevel@tonic-gate  *	return number of bytes in length
2260Sstevel@tonic-gate  */
2270Sstevel@tonic-gate int
getaline(char * ptr2line,int max,FILE * f)228*13093SRoger.Faulkner@Oracle.COM getaline(char *ptr2line, int max, FILE *f)
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate 	int	i, ch;
2310Sstevel@tonic-gate 	for (i = 0; i < max-1 && (ch = getc(f)) != EOF; )
2320Sstevel@tonic-gate 		if ((ptr2line[i++] = ch) == '\n') break;
2330Sstevel@tonic-gate 	ptr2line[i] = '\0';
2340Sstevel@tonic-gate 	return (i);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate /*
2380Sstevel@tonic-gate  *	Make temporary file for letter
2390Sstevel@tonic-gate  */
2400Sstevel@tonic-gate void
mktmp(void)241373Sceastha mktmp(void)
2420Sstevel@tonic-gate {
2430Sstevel@tonic-gate 	static char tmpl[] = "/var/tmp/mailXXXXXX";
2440Sstevel@tonic-gate 	int fd = mkstemp(lettmp = tmpl);
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	if (fd < 0 || (tmpf = fdopen(fd, "w+")) == NULL) {
247*13093SRoger.Faulkner@Oracle.COM 		fprintf(stderr,
2480Sstevel@tonic-gate 		    "%s: Can't open '%s', type: w+\n", program, lettmp);
249*13093SRoger.Faulkner@Oracle.COM 		done(0);
2500Sstevel@tonic-gate 	}
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate /*
2540Sstevel@tonic-gate  * Get a number from user's reply,
2550Sstevel@tonic-gate  * return its value or zero if none present, -1 on error
2560Sstevel@tonic-gate  */
257373Sceastha int
getnumbr(char * s)258373Sceastha getnumbr(char *s)
2590Sstevel@tonic-gate {
2600Sstevel@tonic-gate 	int	k = 0;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	while (*s == ' ' || *s == '\t') s++;
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	if (*s != '\0') {
2650Sstevel@tonic-gate 		if ((k = atoi(s)) != 0)
2660Sstevel@tonic-gate 			if (!validmsg(k))
2670Sstevel@tonic-gate 				return (-1);
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 		for (; *s >= '0' && *s <= '9'; ) s++;
2700Sstevel@tonic-gate 		if (*s != '\0' && *s != '\n') {
2710Sstevel@tonic-gate 			printf("Illegal numeric\n");
2720Sstevel@tonic-gate 			return (-1);
2730Sstevel@tonic-gate 		}
2740Sstevel@tonic-gate 		return (k);
2750Sstevel@tonic-gate 	}
2760Sstevel@tonic-gate 	return (0);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate /*
2800Sstevel@tonic-gate  *	If valid msgnum return 1,
2810Sstevel@tonic-gate  *		else print message and return 0
2820Sstevel@tonic-gate  */
283373Sceastha int
validmsg(int i)284373Sceastha validmsg(int i)
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate 	if ((i < 0) || (i > nlet)) {
2870Sstevel@tonic-gate 		printf("No such message\n");
2880Sstevel@tonic-gate 		return (0);
2890Sstevel@tonic-gate 	}
2900Sstevel@tonic-gate 	return (1);
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate /*
2940Sstevel@tonic-gate  *	Set letter to passed status, and adjust changed as necessary
2950Sstevel@tonic-gate  */
2960Sstevel@tonic-gate void
setletr(int letter,int status)297373Sceastha setletr(int letter, int status)
2980Sstevel@tonic-gate {
2990Sstevel@tonic-gate 	if (status == ' ') {
3000Sstevel@tonic-gate 		if (let[letter].change != ' ')
3010Sstevel@tonic-gate 			if (changed) changed--;
3020Sstevel@tonic-gate 	} else {
3030Sstevel@tonic-gate 		if (let[letter].change == ' ') changed++;
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate 	let[letter].change = status;
3060Sstevel@tonic-gate }
307