xref: /csrg-svn/usr.bin/mail/aux.c (revision 22444)
1*22444Sdist /*
2*22444Sdist  * Copyright (c) 1980 Regents of the University of California.
3*22444Sdist  * All rights reserved.  The Berkeley software License Agreement
4*22444Sdist  * specifies the terms and conditions for redistribution.
5*22444Sdist  */
6*22444Sdist 
714522Ssam #ifndef lint
8*22444Sdist static char sccsid[] = "@(#)aux.c	5.1 (Berkeley) 06/06/85";
9*22444Sdist #endif not lint
101219Skas 
111219Skas #include "rcv.h"
121219Skas #include <sys/stat.h>
131219Skas #include <ctype.h>
141219Skas 
151219Skas /*
161219Skas  * Mail -- a mail program
171219Skas  *
181219Skas  * Auxiliary functions.
191219Skas  */
201219Skas 
211219Skas /*
221219Skas  * Return a pointer to a dynamic copy of the argument.
231219Skas  */
241219Skas 
251219Skas char *
261219Skas savestr(str)
271219Skas 	char *str;
281219Skas {
291219Skas 	register char *cp, *cp2, *top;
301219Skas 
311219Skas 	for (cp = str; *cp; cp++)
321219Skas 		;
331219Skas 	top = salloc(cp-str + 1);
341219Skas 	if (top == NOSTR)
351219Skas 		return(NOSTR);
361219Skas 	for (cp = str, cp2 = top; *cp; cp++)
371219Skas 		*cp2++ = *cp;
381219Skas 	*cp2 = 0;
391219Skas 	return(top);
401219Skas }
411219Skas 
421219Skas /*
431219Skas  * Copy the name from the passed header line into the passed
441219Skas  * name buffer.  Null pad the name buffer.
451219Skas  */
461219Skas 
471219Skas copyname(linebuf, nbuf)
481219Skas 	char *linebuf, *nbuf;
491219Skas {
501219Skas 	register char *cp, *cp2;
511219Skas 
521219Skas 	for (cp = linebuf + 5, cp2 = nbuf; *cp != ' ' && cp2-nbuf < 8; cp++)
531219Skas 		*cp2++ = *cp;
541219Skas 	while (cp2-nbuf < 8)
551219Skas 		*cp2++ = 0;
561219Skas }
571219Skas 
581219Skas /*
591219Skas  * Announce a fatal error and die.
601219Skas  */
611219Skas 
621219Skas panic(str)
631219Skas 	char *str;
641219Skas {
651219Skas 	prs("panic: ");
661219Skas 	prs(str);
671219Skas 	prs("\n");
681219Skas 	exit(1);
691219Skas }
701219Skas 
711219Skas /*
721219Skas  * Catch stdio errors and report them more nicely.
731219Skas  */
741219Skas 
751219Skas _error(str)
761219Skas 	char *str;
771219Skas {
781219Skas 	prs("Stdio Error: ");
791219Skas 	prs(str);
801219Skas 	prs("\n");
811219Skas 	abort();
821219Skas }
831219Skas 
841219Skas /*
851219Skas  * Print a string on diagnostic output.
861219Skas  */
871219Skas 
881219Skas prs(str)
891219Skas 	char *str;
901219Skas {
911219Skas 	register char *s;
921219Skas 
931219Skas 	for (s = str; *s; s++)
941219Skas 		;
951219Skas 	write(2, str, s-str);
961219Skas }
971219Skas 
981219Skas /*
991219Skas  * Touch the named message by setting its MTOUCH flag.
1001219Skas  * Touched messages have the effect of not being sent
1011219Skas  * back to the system mailbox on exit.
1021219Skas  */
1031219Skas 
1041219Skas touch(mesg)
1051219Skas {
1061479Skas 	register struct message *mp;
1071479Skas 
1081479Skas 	if (mesg < 1 || mesg > msgCount)
1091479Skas 		return;
1101479Skas 	mp = &message[mesg-1];
1111479Skas 	mp->m_flag |= MTOUCH;
1121479Skas 	if ((mp->m_flag & MREAD) == 0)
1131479Skas 		mp->m_flag |= MREAD|MSTATUS;
1141219Skas }
1151219Skas 
1161219Skas /*
1171219Skas  * Test to see if the passed file name is a directory.
1181219Skas  * Return true if it is.
1191219Skas  */
1201219Skas 
1211219Skas isdir(name)
1221219Skas 	char name[];
1231219Skas {
1241219Skas 	struct stat sbuf;
1251219Skas 
1261219Skas 	if (stat(name, &sbuf) < 0)
1271219Skas 		return(0);
1281219Skas 	return((sbuf.st_mode & S_IFMT) == S_IFDIR);
1291219Skas }
1301219Skas 
1311219Skas /*
1321219Skas  * Count the number of arguments in the given string raw list.
1331219Skas  */
1341219Skas 
1351219Skas argcount(argv)
1361219Skas 	char **argv;
1371219Skas {
1381219Skas 	register char **ap;
1391219Skas 
1401219Skas 	for (ap = argv; *ap != NOSTR; ap++)
1411219Skas 		;
1421219Skas 	return(ap-argv);
1431219Skas }
1441219Skas 
1451219Skas /*
1461219Skas  * Given a file address, determine the
1471219Skas  * block number it represents.
1481219Skas  */
1491219Skas 
1501219Skas blockof(off)
1511219Skas 	off_t off;
1521219Skas {
1531219Skas 	off_t a;
1541219Skas 
1551219Skas 	a = off >> 9;
1561219Skas 	a &= 077777;
1571219Skas 	return((int) a);
1581219Skas }
1591219Skas 
1601219Skas /*
1611219Skas  * Take a file address, and determine
1621219Skas  * its offset in the current block.
1631219Skas  */
1641219Skas 
1651219Skas offsetof(off)
1661219Skas 	off_t off;
1671219Skas {
1681219Skas 	off_t a;
1691219Skas 
1701219Skas 	a = off & 0777;
1711219Skas 	return((int) a);
1721219Skas }
1731219Skas 
1741219Skas /*
1751219Skas  * Determine if the passed file is actually a tty, via a call to
1761219Skas  * gtty.  This is not totally reliable, but . . .
1771219Skas  */
1781219Skas 
1791219Skas isatty(f)
1801219Skas {
1811219Skas 	struct sgttyb buf;
1821219Skas 
1831219Skas 	if (gtty(f, &buf) < 0)
1841219Skas 		return(0);
1851219Skas 	return(1);
1861219Skas }
1871219Skas 
1881219Skas /*
1891219Skas  * Return the desired header line from the passed message
1901219Skas  * pointer (or NOSTR if the desired header field is not available).
1911219Skas  */
1921219Skas 
1931219Skas char *
1941219Skas hfield(field, mp)
1951219Skas 	char field[];
1961219Skas 	struct message *mp;
1971219Skas {
1981219Skas 	register FILE *ibuf;
1991219Skas 	char linebuf[LINESIZE];
2001219Skas 	register int lc;
2011219Skas 
2021219Skas 	ibuf = setinput(mp);
2031219Skas 	if ((lc = mp->m_lines) <= 0)
2041219Skas 		return(NOSTR);
2051219Skas 	if (readline(ibuf, linebuf) < 0)
2061219Skas 		return(NOSTR);
2071219Skas 	lc--;
2081219Skas 	do {
2091219Skas 		lc = gethfield(ibuf, linebuf, lc);
2101219Skas 		if (lc == -1)
2111219Skas 			return(NOSTR);
2121219Skas 		if (ishfield(linebuf, field))
2131219Skas 			return(savestr(hcontents(linebuf)));
2141219Skas 	} while (lc > 0);
2151219Skas 	return(NOSTR);
2161219Skas }
2171219Skas 
2181219Skas /*
2191219Skas  * Return the next header field found in the given message.
2201219Skas  * Return > 0 if something found, <= 0 elsewise.
2211219Skas  * Must deal with \ continuations & other such fraud.
2221219Skas  */
2231219Skas 
2241219Skas gethfield(f, linebuf, rem)
2251219Skas 	register FILE *f;
2261219Skas 	char linebuf[];
2271219Skas 	register int rem;
2281219Skas {
2291219Skas 	char line2[LINESIZE];
2301219Skas 	long loc;
2311219Skas 	register char *cp, *cp2;
2321219Skas 	register int c;
2331219Skas 
2341219Skas 
2351219Skas 	for (;;) {
2361219Skas 		if (rem <= 0)
2371219Skas 			return(-1);
2381219Skas 		if (readline(f, linebuf) < 0)
2391219Skas 			return(-1);
2401219Skas 		rem--;
2411219Skas 		if (strlen(linebuf) == 0)
2421219Skas 			return(-1);
2431219Skas 		if (isspace(linebuf[0]))
2441219Skas 			continue;
2451219Skas 		if (linebuf[0] == '>')
2461219Skas 			continue;
2471219Skas 		cp = index(linebuf, ':');
2481219Skas 		if (cp == NOSTR)
2491219Skas 			continue;
2501219Skas 		for (cp2 = linebuf; cp2 < cp; cp2++)
2511219Skas 			if (isdigit(*cp2))
2521219Skas 				continue;
2531219Skas 
2541219Skas 		/*
2551219Skas 		 * I guess we got a headline.
2561219Skas 		 * Handle wraparounding
2571219Skas 		 */
2581219Skas 
2591219Skas 		for (;;) {
2601219Skas 			if (rem <= 0)
2611219Skas 				break;
2621219Skas #ifdef CANTELL
2631219Skas 			loc = ftell(f);
2641219Skas 			if (readline(f, line2) < 0)
2651219Skas 				break;
2661219Skas 			rem--;
2671219Skas 			if (!isspace(line2[0])) {
2681219Skas 				fseek(f, loc, 0);
2691219Skas 				rem++;
2701219Skas 				break;
2711219Skas 			}
2721219Skas #else
2731219Skas 			c = getc(f);
2741219Skas 			ungetc(c, f);
2751219Skas 			if (!isspace(c) || c == '\n')
2761219Skas 				break;
2771219Skas 			if (readline(f, line2) < 0)
2781219Skas 				break;
2791219Skas 			rem--;
2801219Skas #endif
2811219Skas 			cp2 = line2;
2821219Skas 			for (cp2 = line2; *cp2 != 0 && isspace(*cp2); cp2++)
2831219Skas 				;
2841219Skas 			if (strlen(linebuf) + strlen(cp2) >= LINESIZE-2)
2851219Skas 				break;
2861219Skas 			cp = &linebuf[strlen(linebuf)];
2871219Skas 			while (cp > linebuf &&
2881219Skas 			    (isspace(cp[-1]) || cp[-1] == '\\'))
2891219Skas 				cp--;
2901219Skas 			*cp++ = ' ';
2911219Skas 			for (cp2 = line2; *cp2 != 0 && isspace(*cp2); cp2++)
2921219Skas 				;
2931219Skas 			strcpy(cp, cp2);
2941219Skas 		}
2951219Skas 		if ((c = strlen(linebuf)) > 0) {
2961219Skas 			cp = &linebuf[c-1];
2971219Skas 			while (cp > linebuf && isspace(*cp))
2981219Skas 				cp--;
2991219Skas 			*++cp = 0;
3001219Skas 		}
3011219Skas 		return(rem);
3021219Skas 	}
3031219Skas 	/* NOTREACHED */
3041219Skas }
3051219Skas 
3061219Skas /*
3071219Skas  * Check whether the passed line is a header line of
3081219Skas  * the desired breed.
3091219Skas  */
3101219Skas 
3111219Skas ishfield(linebuf, field)
3121219Skas 	char linebuf[], field[];
3131219Skas {
3141219Skas 	register char *cp;
3151219Skas 	register int c;
3161219Skas 
3171219Skas 	if ((cp = index(linebuf, ':')) == NOSTR)
3181219Skas 		return(0);
3191219Skas 	if (cp == linebuf)
3201219Skas 		return(0);
3211219Skas 	cp--;
3221219Skas 	while (cp > linebuf && isspace(*cp))
3231219Skas 		cp--;
3241219Skas 	c = *++cp;
3251219Skas 	*cp = 0;
3261219Skas 	if (icequal(linebuf ,field)) {
3271219Skas 		*cp = c;
3281219Skas 		return(1);
3291219Skas 	}
3301219Skas 	*cp = c;
3311219Skas 	return(0);
3321219Skas }
3331219Skas 
3341219Skas /*
3351219Skas  * Extract the non label information from the given header field
3361219Skas  * and return it.
3371219Skas  */
3381219Skas 
3391219Skas char *
3401219Skas hcontents(hfield)
3411219Skas 	char hfield[];
3421219Skas {
3431219Skas 	register char *cp;
3441219Skas 
3451219Skas 	if ((cp = index(hfield, ':')) == NOSTR)
3461219Skas 		return(NOSTR);
3471219Skas 	cp++;
3481219Skas 	while (*cp && isspace(*cp))
3491219Skas 		cp++;
3501219Skas 	return(cp);
3511219Skas }
3521219Skas 
3531219Skas /*
3541219Skas  * Compare two strings, ignoring case.
3551219Skas  */
3561219Skas 
3571219Skas icequal(s1, s2)
3581219Skas 	register char *s1, *s2;
3591219Skas {
3601219Skas 
3611219Skas 	while (raise(*s1++) == raise(*s2))
3621219Skas 		if (*s2++ == 0)
3631219Skas 			return(1);
3641219Skas 	return(0);
3651219Skas }
3661219Skas 
3671219Skas /*
3687571Skurt  * Copy a string, lowercasing it as we go.
3697571Skurt  */
3707571Skurt istrcpy(dest, src)
3717571Skurt 	char *dest, *src;
3727571Skurt {
3737571Skurt 	register char *cp, *cp2;
3747571Skurt 
3757571Skurt 	cp2 = dest;
3767571Skurt 	cp = src;
3777571Skurt 	do {
3787571Skurt 		*cp2++ = little(*cp);
3797571Skurt 	} while (*cp++ != 0);
3807571Skurt }
3817571Skurt 
3827571Skurt /*
3831219Skas  * The following code deals with input stacking to do source
3841219Skas  * commands.  All but the current file pointer are saved on
3851219Skas  * the stack.
3861219Skas  */
3871219Skas 
3881219Skas static	int	ssp = -1;		/* Top of file stack */
3891519Skas struct sstack {
3901519Skas 	FILE	*s_file;		/* File we were in. */
3911519Skas 	int	s_cond;			/* Saved state of conditionals */
3925785Skurt 	int	s_loading;		/* Loading .mailrc, etc. */
39318661Sserge } sstack[NOFILE];
3941219Skas 
3951219Skas /*
3961219Skas  * Pushdown current input file and switch to a new one.
3971219Skas  * Set the global flag "sourcing" so that others will realize
3981219Skas  * that they are no longer reading from a tty (in all probability).
3991219Skas  */
4001219Skas 
4011219Skas source(name)
4021219Skas 	char name[];
4031219Skas {
4041219Skas 	register FILE *fi;
4053914Skurt 	register char *cp;
4061219Skas 
4073914Skurt 	if ((cp = expand(name)) == NOSTR)
4081219Skas 		return(1);
4093914Skurt 	if ((fi = fopen(cp, "r")) == NULL) {
4103914Skurt 		perror(cp);
4113914Skurt 		return(1);
4121219Skas 	}
41318661Sserge 	if (ssp >= NOFILE - 2) {
4141219Skas 		printf("Too much \"sourcing\" going on.\n");
4151219Skas 		fclose(fi);
4161219Skas 		return(1);
4171219Skas 	}
4181519Skas 	sstack[++ssp].s_file = input;
4191519Skas 	sstack[ssp].s_cond = cond;
4205785Skurt 	sstack[ssp].s_loading = loading;
4215785Skurt 	loading = 0;
4221519Skas 	cond = CANY;
4231219Skas 	input = fi;
4241219Skas 	sourcing++;
4251219Skas 	return(0);
4261219Skas }
4271219Skas 
4281219Skas /*
4291219Skas  * Source a file, but do nothing if the file cannot be opened.
4301219Skas  */
4311219Skas 
4321219Skas source1(name)
4331219Skas 	char name[];
4341219Skas {
4351219Skas 	register int f;
4361219Skas 
4371219Skas 	if ((f = open(name, 0)) < 0)
4381219Skas 		return(0);
4391219Skas 	close(f);
4401219Skas 	source(name);
4411219Skas }
4421219Skas 
4431219Skas /*
4441219Skas  * Pop the current input back to the previous level.
4451219Skas  * Update the "sourcing" flag as appropriate.
4461219Skas  */
4471219Skas 
4481219Skas unstack()
4491219Skas {
4501219Skas 	if (ssp < 0) {
4511219Skas 		printf("\"Source\" stack over-pop.\n");
4521219Skas 		sourcing = 0;
4531219Skas 		return(1);
4541219Skas 	}
4551219Skas 	fclose(input);
4561519Skas 	if (cond != CANY)
4571519Skas 		printf("Unmatched \"if\"\n");
4581519Skas 	cond = sstack[ssp].s_cond;
4595785Skurt 	loading = sstack[ssp].s_loading;
4601519Skas 	input = sstack[ssp--].s_file;
4611219Skas 	if (ssp < 0)
4625785Skurt 		sourcing = loading;
4631219Skas 	return(0);
4641219Skas }
4651219Skas 
4661219Skas /*
4671219Skas  * Touch the indicated file.
4681219Skas  * This is nifty for the shell.
4691219Skas  * If we have the utime() system call, this is better served
4701219Skas  * by using that, since it will work for empty files.
4711219Skas  * On non-utime systems, we must sleep a second, then read.
4721219Skas  */
4731219Skas 
4741219Skas alter(name)
4751219Skas 	char name[];
4761219Skas {
4771219Skas #ifdef UTIME
4781219Skas 	struct stat statb;
4791219Skas 	long time();
4801219Skas 	time_t time_p[2];
4811219Skas #else
4821219Skas 	register int pid, f;
4831219Skas 	char w;
4841219Skas #endif UTIME
4851219Skas 
4861219Skas #ifdef UTIME
4871219Skas 	if (stat(name, &statb) < 0)
4881219Skas 		return;
4891219Skas 	time_p[0] = time((long *) 0) + 1;
4901219Skas 	time_p[1] = statb.st_mtime;
4911219Skas 	utime(name, time_p);
4921219Skas #else
4931219Skas 	sleep(1);
4941219Skas 	if ((f = open(name, 0)) < 0)
4954389Skurt 		return;
4961219Skas 	read(f, &w, 1);
4971219Skas 	exit(0);
4981219Skas #endif
4991219Skas }
5001219Skas 
5011219Skas /*
5021219Skas  * Examine the passed line buffer and
5031219Skas  * return true if it is all blanks and tabs.
5041219Skas  */
5051219Skas 
5061219Skas blankline(linebuf)
5071219Skas 	char linebuf[];
5081219Skas {
5091219Skas 	register char *cp;
5101219Skas 
5111219Skas 	for (cp = linebuf; *cp; cp++)
51218661Sserge 		if (*cp != ' ' && *cp != '\t')
5131219Skas 			return(0);
5141219Skas 	return(1);
5151219Skas }
5161219Skas 
5171219Skas /*
5183195Skas  * Get sender's name from this message.  If the message has
5193195Skas  * a bunch of arpanet stuff in it, we may have to skin the name
5203195Skas  * before returning it.
5213195Skas  */
5223195Skas char *
5233195Skas nameof(mp, reptype)
5243195Skas 	register struct message *mp;
5253195Skas {
5265237Skurt 	register char *cp, *cp2;
5273195Skas 
5285237Skurt 	cp = skin(name1(mp, reptype));
5295237Skurt 	if (reptype != 0 || charcount(cp, '!') < 2)
5305237Skurt 		return(cp);
5315237Skurt 	cp2 = rindex(cp, '!');
5325237Skurt 	cp2--;
5335237Skurt 	while (cp2 > cp && *cp2 != '!')
5345237Skurt 		cp2--;
5355237Skurt 	if (*cp2 == '!')
5365237Skurt 		return(cp2 + 1);
5375237Skurt 	return(cp);
5383195Skas }
5393195Skas 
5403195Skas /*
5413195Skas  * Skin an arpa net address according to the RFC 733 interpretation
5423195Skas  * of "host-phrase."
5433195Skas  */
5443195Skas char *
5453195Skas skin(name)
5463195Skas 	char *name;
5473195Skas {
5483195Skas 	register int c;
5493195Skas 	register char *cp, *cp2;
5503195Skas 	int gotlt, lastsp;
5513195Skas 	char nbuf[BUFSIZ];
55218661Sserge 	int nesting;
5533195Skas 
5543195Skas 	if (name == NOSTR)
5553195Skas 		return(NOSTR);
55612819Sleres 	if (index(name, '(') == NOSTR && index(name, '<') == NOSTR
55712819Sleres 	&& index(name, ' ') == NOSTR)
5583195Skas 		return(name);
5593195Skas 	gotlt = 0;
5603195Skas 	lastsp = 0;
56112819Sleres 	for (cp = name, cp2 = nbuf; c = *cp++; ) {
5623195Skas 		switch (c) {
5633195Skas 		case '(':
56418661Sserge 			nesting = 1;
56518661Sserge 			while (*cp != '\0') {
56618661Sserge 				switch (*cp++) {
56718661Sserge 				case '(':
56818661Sserge 					nesting++;
56918661Sserge 					break;
57018661Sserge 
57118661Sserge 				case ')':
57218661Sserge 					--nesting;
57318661Sserge 					break;
57418661Sserge 				}
57518661Sserge 
57618661Sserge 				if (nesting <= 0)
57718661Sserge 					break;
57818661Sserge 			}
57912819Sleres 			lastsp = 0;
5803195Skas 			break;
5813195Skas 
5823195Skas 		case ' ':
58312819Sleres 			if (cp[0] == 'a' && cp[1] == 't' && cp[2] == ' ')
58412819Sleres 				cp += 3, *cp2++ = '@';
58512819Sleres 			else
58612819Sleres 			if (cp[0] == '@' && cp[1] == ' ')
58712819Sleres 				cp += 2, *cp2++ = '@';
58812819Sleres 			else
58912819Sleres 				lastsp = 1;
5903195Skas 			break;
5913195Skas 
5923195Skas 		case '<':
5933195Skas 			cp2 = nbuf;
5943195Skas 			gotlt++;
5953195Skas 			lastsp = 0;
5963195Skas 			break;
5973195Skas 
5983195Skas 		case '>':
5993195Skas 			if (gotlt)
6003195Skas 				goto done;
6013195Skas 
6023195Skas 			/* Fall into . . . */
6033195Skas 
6043195Skas 		default:
6053195Skas 			if (lastsp) {
6063195Skas 				lastsp = 0;
6073195Skas 				*cp2++ = ' ';
6083195Skas 			}
6093195Skas 			*cp2++ = c;
6103195Skas 			break;
6113195Skas 		}
6123195Skas 	}
6133195Skas done:
6143195Skas 	*cp2 = 0;
6153195Skas 
6163195Skas 	return(savestr(nbuf));
6173195Skas }
6183195Skas 
6193195Skas /*
6201219Skas  * Fetch the sender's name from the passed message.
6213195Skas  * Reptype can be
6223195Skas  *	0 -- get sender's name for display purposes
6233195Skas  *	1 -- get sender's name for reply
6243195Skas  *	2 -- get sender's name for Reply
6251219Skas  */
6261219Skas 
6271219Skas char *
6283195Skas name1(mp, reptype)
6291219Skas 	register struct message *mp;
6301219Skas {
6311219Skas 	char namebuf[LINESIZE];
6321219Skas 	char linebuf[LINESIZE];
6331219Skas 	register char *cp, *cp2;
6341219Skas 	register FILE *ibuf;
6351219Skas 	int first = 1;
6361219Skas 
6373195Skas 	if ((cp = hfield("from", mp)) != NOSTR)
6383195Skas 		return(cp);
6393195Skas 	if (reptype == 0 && (cp = hfield("sender", mp)) != NOSTR)
6403195Skas 		return(cp);
6411219Skas 	ibuf = setinput(mp);
6421219Skas 	copy("", namebuf);
6431219Skas 	if (readline(ibuf, linebuf) <= 0)
6441219Skas 		return(savestr(namebuf));
6451219Skas newname:
6461219Skas 	for (cp = linebuf; *cp != ' '; cp++)
6471219Skas 		;
6481219Skas 	while (any(*cp, " \t"))
6491219Skas 		cp++;
6501219Skas 	for (cp2 = &namebuf[strlen(namebuf)]; *cp && !any(*cp, " \t") &&
6511219Skas 	    cp2-namebuf < LINESIZE-1; *cp2++ = *cp++)
6521219Skas 		;
6531219Skas 	*cp2 = '\0';
6541219Skas 	if (readline(ibuf, linebuf) <= 0)
6551219Skas 		return(savestr(namebuf));
6561219Skas 	if ((cp = index(linebuf, 'F')) == NULL)
6571219Skas 		return(savestr(namebuf));
6581219Skas 	if (strncmp(cp, "From", 4) != 0)
6591219Skas 		return(savestr(namebuf));
6601219Skas 	while ((cp = index(cp, 'r')) != NULL) {
6611219Skas 		if (strncmp(cp, "remote", 6) == 0) {
6621219Skas 			if ((cp = index(cp, 'f')) == NULL)
6631219Skas 				break;
6641219Skas 			if (strncmp(cp, "from", 4) != 0)
6651219Skas 				break;
6661219Skas 			if ((cp = index(cp, ' ')) == NULL)
6671219Skas 				break;
6681219Skas 			cp++;
6691219Skas 			if (first) {
6701219Skas 				copy(cp, namebuf);
6711219Skas 				first = 0;
6721219Skas 			} else
6731219Skas 				strcpy(rindex(namebuf, '!')+1, cp);
6741219Skas 			strcat(namebuf, "!");
6751219Skas 			goto newname;
6761219Skas 		}
6771219Skas 		cp++;
6781219Skas 	}
6791219Skas 	return(savestr(namebuf));
6801219Skas }
6811219Skas 
6821219Skas /*
6835237Skurt  * Count the occurances of c in str
6845237Skurt  */
6855237Skurt charcount(str, c)
6865237Skurt 	char *str;
6875237Skurt {
6885237Skurt 	register char *cp;
6895237Skurt 	register int i;
6905237Skurt 
6915237Skurt 	for (i = 0, cp = str; *cp; cp++)
6925237Skurt 		if (*cp == c)
6935237Skurt 			i++;
6945237Skurt 	return(i);
6955237Skurt }
6965237Skurt 
6975237Skurt /*
6981219Skas  * Find the rightmost pointer to an instance of the
6991219Skas  * character in the string and return it.
7001219Skas  */
7011219Skas char *
7021219Skas rindex(str, c)
7031219Skas 	char str[];
7041219Skas 	register int c;
7051219Skas {
7061219Skas 	register char *cp, *cp2;
7071219Skas 
7081219Skas 	for (cp = str, cp2 = NOSTR; *cp; cp++)
7091219Skas 		if (c == *cp)
7101219Skas 			cp2 = cp;
7111219Skas 	return(cp2);
7121219Skas }
7131219Skas 
7141219Skas /*
7151219Skas  * See if the string is a number.
7161219Skas  */
7171219Skas 
7181219Skas numeric(str)
7191219Skas 	char str[];
7201219Skas {
7211219Skas 	register char *cp = str;
7221219Skas 
7231219Skas 	while (*cp)
7241219Skas 		if (!isdigit(*cp++))
7251219Skas 			return(0);
7261219Skas 	return(1);
7271219Skas }
7281219Skas 
7291219Skas /*
7301219Skas  * Are any of the characters in the two strings the same?
7311219Skas  */
7321219Skas 
7331219Skas anyof(s1, s2)
7341219Skas 	register char *s1, *s2;
7351219Skas {
7361219Skas 	register int c;
7371219Skas 
7381219Skas 	while (c = *s1++)
7391219Skas 		if (any(c, s2))
7401219Skas 			return(1);
7411219Skas 	return(0);
7421219Skas }
7431219Skas 
7441219Skas /*
7451219Skas  * Determine the leftmost index of the character
7461219Skas  * in the string.
7471219Skas  */
7481219Skas 
7491219Skas char *
7501219Skas index(str, ch)
7511219Skas 	char *str;
7521219Skas {
7531219Skas 	register char *cp;
7541219Skas 	register int c;
7551219Skas 
7561219Skas 	for (c = ch, cp = str; *cp; cp++)
7571219Skas 		if (*cp == c)
7581219Skas 			return(cp);
7591219Skas 	return(NOSTR);
7601219Skas }
7611219Skas 
7621219Skas /*
7631219Skas  * String compare two strings of bounded length.
7641219Skas  */
7651219Skas 
7661219Skas strncmp(as1, as2, an)
7671219Skas 	char *as1, *as2;
7681219Skas {
7691219Skas 	register char *s1, *s2;
7701219Skas 	register int n;
7711219Skas 
7721219Skas 	s1 = as1;
7731219Skas 	s2 = as2;
7741219Skas 	n = an;
7751219Skas 	while (--n >= 0 && *s1 == *s2++)
7761219Skas 		if (*s1++ == '\0')
7771219Skas 			return(0);
7781219Skas 	return(n<0 ? 0 : *s1 - *--s2);
7791219Skas }
7801219Skas 
7817538Skurt /*
7827571Skurt  * See if the given header field is supposed to be ignored.
7837571Skurt  */
7847571Skurt isign(field)
7857571Skurt 	char *field;
7867571Skurt {
7877571Skurt 	char realfld[BUFSIZ];
7887571Skurt 
78918661Sserge 	/*
79018661Sserge 	 * Lower-case the string, so that "Status" and "status"
79118661Sserge 	 * will hash to the same place.
79218661Sserge 	 */
7937571Skurt 	istrcpy(realfld, field);
79418661Sserge 
79518661Sserge 	if (nretained > 0)
79618661Sserge 		return (!member(realfld, retain));
79718661Sserge 	else
79818661Sserge 		return (member(realfld, ignore));
7997571Skurt }
80018661Sserge 
80118661Sserge member(realfield, table)
80218661Sserge 	register char *realfield;
80318661Sserge 	register struct ignore **table;
80418661Sserge {
80518661Sserge 	register struct ignore *igp;
80618661Sserge 
80718661Sserge 	for (igp = table[hash(realfield)]; igp != 0; igp = igp->i_link)
80818661Sserge 		if (equal(igp->i_field, realfield))
80918661Sserge 			return (1);
81018661Sserge 
81118661Sserge 	return (0);
81218661Sserge }
813