xref: /csrg-svn/usr.bin/mail/aux.c (revision 5237)
11219Skas #
21219Skas 
31219Skas #include "rcv.h"
41219Skas #include <sys/stat.h>
51219Skas #include <ctype.h>
61219Skas 
71219Skas /*
81219Skas  * Mail -- a mail program
91219Skas  *
101219Skas  * Auxiliary functions.
111219Skas  */
121219Skas 
13*5237Skurt static char *SccsId = "@(#)aux.c	2.3 12/10/81";
141219Skas 
151219Skas /*
161219Skas  * Return a pointer to a dynamic copy of the argument.
171219Skas  */
181219Skas 
191219Skas char *
201219Skas savestr(str)
211219Skas 	char *str;
221219Skas {
231219Skas 	register char *cp, *cp2, *top;
241219Skas 
251219Skas 	for (cp = str; *cp; cp++)
261219Skas 		;
271219Skas 	top = salloc(cp-str + 1);
281219Skas 	if (top == NOSTR)
291219Skas 		return(NOSTR);
301219Skas 	for (cp = str, cp2 = top; *cp; cp++)
311219Skas 		*cp2++ = *cp;
321219Skas 	*cp2 = 0;
331219Skas 	return(top);
341219Skas }
351219Skas 
361219Skas /*
371219Skas  * Copy the name from the passed header line into the passed
381219Skas  * name buffer.  Null pad the name buffer.
391219Skas  */
401219Skas 
411219Skas copyname(linebuf, nbuf)
421219Skas 	char *linebuf, *nbuf;
431219Skas {
441219Skas 	register char *cp, *cp2;
451219Skas 
461219Skas 	for (cp = linebuf + 5, cp2 = nbuf; *cp != ' ' && cp2-nbuf < 8; cp++)
471219Skas 		*cp2++ = *cp;
481219Skas 	while (cp2-nbuf < 8)
491219Skas 		*cp2++ = 0;
501219Skas }
511219Skas 
521219Skas /*
531219Skas  * Announce a fatal error and die.
541219Skas  */
551219Skas 
561219Skas panic(str)
571219Skas 	char *str;
581219Skas {
591219Skas 	prs("panic: ");
601219Skas 	prs(str);
611219Skas 	prs("\n");
621219Skas 	exit(1);
631219Skas }
641219Skas 
651219Skas /*
661219Skas  * Catch stdio errors and report them more nicely.
671219Skas  */
681219Skas 
691219Skas _error(str)
701219Skas 	char *str;
711219Skas {
721219Skas 	prs("Stdio Error: ");
731219Skas 	prs(str);
741219Skas 	prs("\n");
751219Skas 	abort();
761219Skas }
771219Skas 
781219Skas /*
791219Skas  * Print a string on diagnostic output.
801219Skas  */
811219Skas 
821219Skas prs(str)
831219Skas 	char *str;
841219Skas {
851219Skas 	register char *s;
861219Skas 
871219Skas 	for (s = str; *s; s++)
881219Skas 		;
891219Skas 	write(2, str, s-str);
901219Skas }
911219Skas 
921219Skas /*
931219Skas  * Touch the named message by setting its MTOUCH flag.
941219Skas  * Touched messages have the effect of not being sent
951219Skas  * back to the system mailbox on exit.
961219Skas  */
971219Skas 
981219Skas touch(mesg)
991219Skas {
1001479Skas 	register struct message *mp;
1011479Skas 
1021479Skas 	if (mesg < 1 || mesg > msgCount)
1031479Skas 		return;
1041479Skas 	mp = &message[mesg-1];
1051479Skas 	mp->m_flag |= MTOUCH;
1061479Skas 	if ((mp->m_flag & MREAD) == 0)
1071479Skas 		mp->m_flag |= MREAD|MSTATUS;
1081219Skas }
1091219Skas 
1101219Skas /*
1111219Skas  * Test to see if the passed file name is a directory.
1121219Skas  * Return true if it is.
1131219Skas  */
1141219Skas 
1151219Skas isdir(name)
1161219Skas 	char name[];
1171219Skas {
1181219Skas 	struct stat sbuf;
1191219Skas 
1201219Skas 	if (stat(name, &sbuf) < 0)
1211219Skas 		return(0);
1221219Skas 	return((sbuf.st_mode & S_IFMT) == S_IFDIR);
1231219Skas }
1241219Skas 
1251219Skas /*
1261219Skas  * Compute the size in characters of the passed message
1271219Skas  */
1281219Skas 
1291219Skas unsigned int
1301219Skas msize(messp)
1311219Skas 	struct message *messp;
1321219Skas {
1331219Skas 	register struct message *mp;
1341219Skas 
1351219Skas 	mp = messp;
1361219Skas 	return(mp->m_size);
1371219Skas }
1381219Skas 
1391219Skas /*
1401219Skas  * Count the number of arguments in the given string raw list.
1411219Skas  */
1421219Skas 
1431219Skas argcount(argv)
1441219Skas 	char **argv;
1451219Skas {
1461219Skas 	register char **ap;
1471219Skas 
1481219Skas 	for (ap = argv; *ap != NOSTR; ap++)
1491219Skas 		;
1501219Skas 	return(ap-argv);
1511219Skas }
1521219Skas 
1531219Skas /*
1541219Skas  * Given a file address, determine the
1551219Skas  * block number it represents.
1561219Skas  */
1571219Skas 
1581219Skas blockof(off)
1591219Skas 	off_t off;
1601219Skas {
1611219Skas 	off_t a;
1621219Skas 
1631219Skas 	a = off >> 9;
1641219Skas 	a &= 077777;
1651219Skas 	return((int) a);
1661219Skas }
1671219Skas 
1681219Skas /*
1691219Skas  * Take a file address, and determine
1701219Skas  * its offset in the current block.
1711219Skas  */
1721219Skas 
1731219Skas offsetof(off)
1741219Skas 	off_t off;
1751219Skas {
1761219Skas 	off_t a;
1771219Skas 
1781219Skas 	a = off & 0777;
1791219Skas 	return((int) a);
1801219Skas }
1811219Skas 
1821219Skas /*
1831219Skas  * Determine if the passed file is actually a tty, via a call to
1841219Skas  * gtty.  This is not totally reliable, but . . .
1851219Skas  */
1861219Skas 
1871219Skas isatty(f)
1881219Skas {
1891219Skas 	struct sgttyb buf;
1901219Skas 
1911219Skas 	if (gtty(f, &buf) < 0)
1921219Skas 		return(0);
1931219Skas 	return(1);
1941219Skas }
1951219Skas 
1961219Skas /*
1971219Skas  * Return the desired header line from the passed message
1981219Skas  * pointer (or NOSTR if the desired header field is not available).
1991219Skas  */
2001219Skas 
2011219Skas char *
2021219Skas hfield(field, mp)
2031219Skas 	char field[];
2041219Skas 	struct message *mp;
2051219Skas {
2061219Skas 	register FILE *ibuf;
2071219Skas 	char linebuf[LINESIZE];
2081219Skas 	register int lc;
2091219Skas 
2101219Skas 	ibuf = setinput(mp);
2111219Skas 	if ((lc = mp->m_lines) <= 0)
2121219Skas 		return(NOSTR);
2131219Skas 	if (readline(ibuf, linebuf) < 0)
2141219Skas 		return(NOSTR);
2151219Skas 	lc--;
2161219Skas 	do {
2171219Skas 		lc = gethfield(ibuf, linebuf, lc);
2181219Skas 		if (lc == -1)
2191219Skas 			return(NOSTR);
2201219Skas 		if (ishfield(linebuf, field))
2211219Skas 			return(savestr(hcontents(linebuf)));
2221219Skas 	} while (lc > 0);
2231219Skas 	return(NOSTR);
2241219Skas }
2251219Skas 
2261219Skas /*
2271219Skas  * Return the next header field found in the given message.
2281219Skas  * Return > 0 if something found, <= 0 elsewise.
2291219Skas  * Must deal with \ continuations & other such fraud.
2301219Skas  */
2311219Skas 
2321219Skas gethfield(f, linebuf, rem)
2331219Skas 	register FILE *f;
2341219Skas 	char linebuf[];
2351219Skas 	register int rem;
2361219Skas {
2371219Skas 	char line2[LINESIZE];
2381219Skas 	long loc;
2391219Skas 	register char *cp, *cp2;
2401219Skas 	register int c;
2411219Skas 
2421219Skas 
2431219Skas 	for (;;) {
2441219Skas 		if (rem <= 0)
2451219Skas 			return(-1);
2461219Skas 		if (readline(f, linebuf) < 0)
2471219Skas 			return(-1);
2481219Skas 		rem--;
2491219Skas 		if (strlen(linebuf) == 0)
2501219Skas 			return(-1);
2511219Skas 		if (isspace(linebuf[0]))
2521219Skas 			continue;
2531219Skas 		if (linebuf[0] == '>')
2541219Skas 			continue;
2551219Skas 		cp = index(linebuf, ':');
2561219Skas 		if (cp == NOSTR)
2571219Skas 			continue;
2581219Skas 		for (cp2 = linebuf; cp2 < cp; cp2++)
2591219Skas 			if (isdigit(*cp2))
2601219Skas 				continue;
2611219Skas 
2621219Skas 		/*
2631219Skas 		 * I guess we got a headline.
2641219Skas 		 * Handle wraparounding
2651219Skas 		 */
2661219Skas 
2671219Skas 		for (;;) {
2681219Skas 			if (rem <= 0)
2691219Skas 				break;
2701219Skas #ifdef CANTELL
2711219Skas 			loc = ftell(f);
2721219Skas 			if (readline(f, line2) < 0)
2731219Skas 				break;
2741219Skas 			rem--;
2751219Skas 			if (!isspace(line2[0])) {
2761219Skas 				fseek(f, loc, 0);
2771219Skas 				rem++;
2781219Skas 				break;
2791219Skas 			}
2801219Skas #else
2811219Skas 			c = getc(f);
2821219Skas 			ungetc(c, f);
2831219Skas 			if (!isspace(c) || c == '\n')
2841219Skas 				break;
2851219Skas 			if (readline(f, line2) < 0)
2861219Skas 				break;
2871219Skas 			rem--;
2881219Skas #endif
2891219Skas 			cp2 = line2;
2901219Skas 			for (cp2 = line2; *cp2 != 0 && isspace(*cp2); cp2++)
2911219Skas 				;
2921219Skas 			if (strlen(linebuf) + strlen(cp2) >= LINESIZE-2)
2931219Skas 				break;
2941219Skas 			cp = &linebuf[strlen(linebuf)];
2951219Skas 			while (cp > linebuf &&
2961219Skas 			    (isspace(cp[-1]) || cp[-1] == '\\'))
2971219Skas 				cp--;
2981219Skas 			*cp++ = ' ';
2991219Skas 			for (cp2 = line2; *cp2 != 0 && isspace(*cp2); cp2++)
3001219Skas 				;
3011219Skas 			strcpy(cp, cp2);
3021219Skas 		}
3031219Skas 		if ((c = strlen(linebuf)) > 0) {
3041219Skas 			cp = &linebuf[c-1];
3051219Skas 			while (cp > linebuf && isspace(*cp))
3061219Skas 				cp--;
3071219Skas 			*++cp = 0;
3081219Skas 		}
3091219Skas 		return(rem);
3101219Skas 	}
3111219Skas 	/* NOTREACHED */
3121219Skas }
3131219Skas 
3141219Skas /*
3151219Skas  * Check whether the passed line is a header line of
3161219Skas  * the desired breed.
3171219Skas  */
3181219Skas 
3191219Skas ishfield(linebuf, field)
3201219Skas 	char linebuf[], field[];
3211219Skas {
3221219Skas 	register char *cp;
3231219Skas 	register int c;
3241219Skas 
3251219Skas 	if ((cp = index(linebuf, ':')) == NOSTR)
3261219Skas 		return(0);
3271219Skas 	if (cp == linebuf)
3281219Skas 		return(0);
3291219Skas 	cp--;
3301219Skas 	while (cp > linebuf && isspace(*cp))
3311219Skas 		cp--;
3321219Skas 	c = *++cp;
3331219Skas 	*cp = 0;
3341219Skas 	if (icequal(linebuf ,field)) {
3351219Skas 		*cp = c;
3361219Skas 		return(1);
3371219Skas 	}
3381219Skas 	*cp = c;
3391219Skas 	return(0);
3401219Skas }
3411219Skas 
3421219Skas /*
3431219Skas  * Extract the non label information from the given header field
3441219Skas  * and return it.
3451219Skas  */
3461219Skas 
3471219Skas char *
3481219Skas hcontents(hfield)
3491219Skas 	char hfield[];
3501219Skas {
3511219Skas 	register char *cp;
3521219Skas 
3531219Skas 	if ((cp = index(hfield, ':')) == NOSTR)
3541219Skas 		return(NOSTR);
3551219Skas 	cp++;
3561219Skas 	while (*cp && isspace(*cp))
3571219Skas 		cp++;
3581219Skas 	return(cp);
3591219Skas }
3601219Skas 
3611219Skas /*
3621219Skas  * Compare two strings, ignoring case.
3631219Skas  */
3641219Skas 
3651219Skas icequal(s1, s2)
3661219Skas 	register char *s1, *s2;
3671219Skas {
3681219Skas 
3691219Skas 	while (raise(*s1++) == raise(*s2))
3701219Skas 		if (*s2++ == 0)
3711219Skas 			return(1);
3721219Skas 	return(0);
3731219Skas }
3741219Skas 
3751219Skas /*
3761219Skas  * The following code deals with input stacking to do source
3771219Skas  * commands.  All but the current file pointer are saved on
3781219Skas  * the stack.
3791219Skas  */
3801219Skas 
3811219Skas static	int	ssp = -1;		/* Top of file stack */
3821519Skas struct sstack {
3831519Skas 	FILE	*s_file;		/* File we were in. */
3841519Skas 	int	s_cond;			/* Saved state of conditionals */
3851519Skas } sstack[_NFILE];
3861219Skas 
3871219Skas /*
3881219Skas  * Pushdown current input file and switch to a new one.
3891219Skas  * Set the global flag "sourcing" so that others will realize
3901219Skas  * that they are no longer reading from a tty (in all probability).
3911219Skas  */
3921219Skas 
3931219Skas source(name)
3941219Skas 	char name[];
3951219Skas {
3961219Skas 	register FILE *fi;
3973914Skurt 	register char *cp;
3981219Skas 
3993914Skurt 	if ((cp = expand(name)) == NOSTR)
4001219Skas 		return(1);
4013914Skurt 	if ((fi = fopen(cp, "r")) == NULL) {
4023914Skurt 		perror(cp);
4033914Skurt 		return(1);
4041219Skas 	}
4051219Skas 	if (ssp >= _NFILE-2) {
4061219Skas 		printf("Too much \"sourcing\" going on.\n");
4071219Skas 		fclose(fi);
4081219Skas 		return(1);
4091219Skas 	}
4101519Skas 	sstack[++ssp].s_file = input;
4111519Skas 	sstack[ssp].s_cond = cond;
4121519Skas 	cond = CANY;
4131219Skas 	input = fi;
4141219Skas 	sourcing++;
4151219Skas 	return(0);
4161219Skas }
4171219Skas 
4181219Skas /*
4191219Skas  * Source a file, but do nothing if the file cannot be opened.
4201219Skas  */
4211219Skas 
4221219Skas source1(name)
4231219Skas 	char name[];
4241219Skas {
4251219Skas 	register int f;
4261219Skas 
4271219Skas 	if ((f = open(name, 0)) < 0)
4281219Skas 		return(0);
4291219Skas 	close(f);
4301219Skas 	source(name);
4311219Skas }
4321219Skas 
4331219Skas /*
4341219Skas  * Pop the current input back to the previous level.
4351219Skas  * Update the "sourcing" flag as appropriate.
4361219Skas  */
4371219Skas 
4381219Skas unstack()
4391219Skas {
4401219Skas 	if (ssp < 0) {
4411219Skas 		printf("\"Source\" stack over-pop.\n");
4421219Skas 		sourcing = 0;
4431219Skas 		return(1);
4441219Skas 	}
4451219Skas 	fclose(input);
4461519Skas 	if (cond != CANY)
4471519Skas 		printf("Unmatched \"if\"\n");
4481519Skas 	cond = sstack[ssp].s_cond;
4491519Skas 	input = sstack[ssp--].s_file;
4501219Skas 	if (ssp < 0)
4511219Skas 		sourcing = 0;
4521219Skas 	return(0);
4531219Skas }
4541219Skas 
4551219Skas /*
4561219Skas  * Touch the indicated file.
4571219Skas  * This is nifty for the shell.
4581219Skas  * If we have the utime() system call, this is better served
4591219Skas  * by using that, since it will work for empty files.
4601219Skas  * On non-utime systems, we must sleep a second, then read.
4611219Skas  */
4621219Skas 
4631219Skas alter(name)
4641219Skas 	char name[];
4651219Skas {
4661219Skas #ifdef UTIME
4671219Skas 	struct stat statb;
4681219Skas 	long time();
4691219Skas 	time_t time_p[2];
4701219Skas #else
4711219Skas 	register int pid, f;
4721219Skas 	char w;
4731219Skas #endif UTIME
4741219Skas 
4751219Skas #ifdef UTIME
4761219Skas 	if (stat(name, &statb) < 0)
4771219Skas 		return;
4781219Skas 	time_p[0] = time((long *) 0) + 1;
4791219Skas 	time_p[1] = statb.st_mtime;
4801219Skas 	utime(name, time_p);
4811219Skas #else
4821219Skas 	sleep(1);
4831219Skas 	if ((f = open(name, 0)) < 0)
4844389Skurt 		return;
4851219Skas 	read(f, &w, 1);
4861219Skas 	exit(0);
4871219Skas #endif
4881219Skas }
4891219Skas 
4901219Skas /*
4911219Skas  * Examine the passed line buffer and
4921219Skas  * return true if it is all blanks and tabs.
4931219Skas  */
4941219Skas 
4951219Skas blankline(linebuf)
4961219Skas 	char linebuf[];
4971219Skas {
4981219Skas 	register char *cp;
4991219Skas 
5001219Skas 	for (cp = linebuf; *cp; cp++)
5011219Skas 		if (!any(*cp, " \t"))
5021219Skas 			return(0);
5031219Skas 	return(1);
5041219Skas }
5051219Skas 
5061219Skas /*
5073195Skas  * Get sender's name from this message.  If the message has
5083195Skas  * a bunch of arpanet stuff in it, we may have to skin the name
5093195Skas  * before returning it.
5103195Skas  */
5113195Skas char *
5123195Skas nameof(mp, reptype)
5133195Skas 	register struct message *mp;
5143195Skas {
515*5237Skurt 	register char *cp, *cp2;
5163195Skas 
517*5237Skurt 	cp = skin(name1(mp, reptype));
518*5237Skurt 	if (reptype != 0 || charcount(cp, '!') < 2)
519*5237Skurt 		return(cp);
520*5237Skurt 	cp2 = rindex(cp, '!');
521*5237Skurt 	cp2--;
522*5237Skurt 	while (cp2 > cp && *cp2 != '!')
523*5237Skurt 		cp2--;
524*5237Skurt 	if (*cp2 == '!')
525*5237Skurt 		return(cp2 + 1);
526*5237Skurt 	return(cp);
5273195Skas }
5283195Skas 
5293195Skas /*
5303195Skas  * Skin an arpa net address according to the RFC 733 interpretation
5313195Skas  * of "host-phrase."
5323195Skas  */
5333195Skas char *
5343195Skas skin(name)
5353195Skas 	char *name;
5363195Skas {
5373195Skas 	register int c;
5383195Skas 	register char *cp, *cp2;
5393195Skas 	int gotlt, lastsp;
5403195Skas 	char nbuf[BUFSIZ];
5413195Skas 
5423195Skas 	if (name == NOSTR)
5433195Skas 		return(NOSTR);
5443195Skas 	if (index(name, '(') == NOSTR && index(name, '<') == NOSTR)
5453195Skas 		return(name);
5463195Skas 	gotlt = 0;
5473195Skas 	lastsp = 0;
5483195Skas 	for (cp = name, cp2 = nbuf, c = *cp++; *cp; c = *cp++) {
5493195Skas 		switch (c) {
5503195Skas 		case '(':
5513195Skas 			while (*cp != ')' && *cp != 0)
5523195Skas 				cp++;
5533195Skas 			if (*cp)
5543195Skas 				cp++;
5553195Skas 			break;
5563195Skas 
5573195Skas 		case ' ':
5583195Skas 			lastsp = 1;
5593195Skas 			break;
5603195Skas 
5613195Skas 		case '<':
5623195Skas 			cp2 = nbuf;
5633195Skas 			gotlt++;
5643195Skas 			lastsp = 0;
5653195Skas 			break;
5663195Skas 
5673195Skas 		case '>':
5683195Skas 			if (gotlt)
5693195Skas 				goto done;
5703195Skas 
5713195Skas 			/* Fall into . . . */
5723195Skas 
5733195Skas 		default:
5743195Skas 			if (lastsp) {
5753195Skas 				lastsp = 0;
5763195Skas 				*cp2++ = ' ';
5773195Skas 			}
5783195Skas 			*cp2++ = c;
5793195Skas 			break;
5803195Skas 		}
5813195Skas 	}
5823195Skas done:
5833195Skas 	*cp2 = 0;
5843195Skas 
5853195Skas 	return(savestr(nbuf));
5863195Skas }
5873195Skas 
5883195Skas /*
5891219Skas  * Fetch the sender's name from the passed message.
5903195Skas  * Reptype can be
5913195Skas  *	0 -- get sender's name for display purposes
5923195Skas  *	1 -- get sender's name for reply
5933195Skas  *	2 -- get sender's name for Reply
5941219Skas  */
5951219Skas 
5961219Skas char *
5973195Skas name1(mp, reptype)
5981219Skas 	register struct message *mp;
5991219Skas {
6001219Skas 	char namebuf[LINESIZE];
6011219Skas 	char linebuf[LINESIZE];
6021219Skas 	register char *cp, *cp2;
6031219Skas 	register FILE *ibuf;
6041219Skas 	int first = 1;
6051219Skas 
6063195Skas #ifndef DELIVERMAIL
6073195Skas 	if ((cp = hfield("from", mp)) != NOSTR)
6083195Skas 		return(cp);
6093195Skas 	if (reptype == 0 && (cp = hfield("sender", mp)) != NOSTR)
6103195Skas 		return(cp);
6113195Skas #endif
6121219Skas 	ibuf = setinput(mp);
6131219Skas 	copy("", namebuf);
6141219Skas 	if (readline(ibuf, linebuf) <= 0)
6151219Skas 		return(savestr(namebuf));
6161219Skas newname:
6171219Skas 	for (cp = linebuf; *cp != ' '; cp++)
6181219Skas 		;
6191219Skas 	while (any(*cp, " \t"))
6201219Skas 		cp++;
6211219Skas 	for (cp2 = &namebuf[strlen(namebuf)]; *cp && !any(*cp, " \t") &&
6221219Skas 	    cp2-namebuf < LINESIZE-1; *cp2++ = *cp++)
6231219Skas 		;
6241219Skas 	*cp2 = '\0';
6251219Skas 	if (readline(ibuf, linebuf) <= 0)
6261219Skas 		return(savestr(namebuf));
6271219Skas 	if ((cp = index(linebuf, 'F')) == NULL)
6281219Skas 		return(savestr(namebuf));
6291219Skas 	if (strncmp(cp, "From", 4) != 0)
6301219Skas 		return(savestr(namebuf));
6311219Skas 	while ((cp = index(cp, 'r')) != NULL) {
6321219Skas 		if (strncmp(cp, "remote", 6) == 0) {
6331219Skas 			if ((cp = index(cp, 'f')) == NULL)
6341219Skas 				break;
6351219Skas 			if (strncmp(cp, "from", 4) != 0)
6361219Skas 				break;
6371219Skas 			if ((cp = index(cp, ' ')) == NULL)
6381219Skas 				break;
6391219Skas 			cp++;
6401219Skas 			if (first) {
6411219Skas 				copy(cp, namebuf);
6421219Skas 				first = 0;
6431219Skas 			} else
6441219Skas 				strcpy(rindex(namebuf, '!')+1, cp);
6451219Skas 			strcat(namebuf, "!");
6461219Skas 			goto newname;
6471219Skas 		}
6481219Skas 		cp++;
6491219Skas 	}
6501219Skas 	return(savestr(namebuf));
6511219Skas }
6521219Skas 
6531219Skas /*
654*5237Skurt  * Count the occurances of c in str
655*5237Skurt  */
656*5237Skurt charcount(str, c)
657*5237Skurt 	char *str;
658*5237Skurt {
659*5237Skurt 	register char *cp;
660*5237Skurt 	register int i;
661*5237Skurt 
662*5237Skurt 	for (i = 0, cp = str; *cp; cp++)
663*5237Skurt 		if (*cp == c)
664*5237Skurt 			i++;
665*5237Skurt 	return(i);
666*5237Skurt }
667*5237Skurt 
668*5237Skurt /*
6691219Skas  * Find the rightmost pointer to an instance of the
6701219Skas  * character in the string and return it.
6711219Skas  */
6721219Skas char *
6731219Skas rindex(str, c)
6741219Skas 	char str[];
6751219Skas 	register int c;
6761219Skas {
6771219Skas 	register char *cp, *cp2;
6781219Skas 
6791219Skas 	for (cp = str, cp2 = NOSTR; *cp; cp++)
6801219Skas 		if (c == *cp)
6811219Skas 			cp2 = cp;
6821219Skas 	return(cp2);
6831219Skas }
6841219Skas 
6851219Skas /*
6861219Skas  * See if the string is a number.
6871219Skas  */
6881219Skas 
6891219Skas numeric(str)
6901219Skas 	char str[];
6911219Skas {
6921219Skas 	register char *cp = str;
6931219Skas 
6941219Skas 	while (*cp)
6951219Skas 		if (!isdigit(*cp++))
6961219Skas 			return(0);
6971219Skas 	return(1);
6981219Skas }
6991219Skas 
7001219Skas /*
7011219Skas  * Are any of the characters in the two strings the same?
7021219Skas  */
7031219Skas 
7041219Skas anyof(s1, s2)
7051219Skas 	register char *s1, *s2;
7061219Skas {
7071219Skas 	register int c;
7081219Skas 
7091219Skas 	while (c = *s1++)
7101219Skas 		if (any(c, s2))
7111219Skas 			return(1);
7121219Skas 	return(0);
7131219Skas }
7141219Skas 
7151219Skas /*
7161219Skas  * Determine the leftmost index of the character
7171219Skas  * in the string.
7181219Skas  */
7191219Skas 
7201219Skas char *
7211219Skas index(str, ch)
7221219Skas 	char *str;
7231219Skas {
7241219Skas 	register char *cp;
7251219Skas 	register int c;
7261219Skas 
7271219Skas 	for (c = ch, cp = str; *cp; cp++)
7281219Skas 		if (*cp == c)
7291219Skas 			return(cp);
7301219Skas 	return(NOSTR);
7311219Skas }
7321219Skas 
7331219Skas /*
7341219Skas  * String compare two strings of bounded length.
7351219Skas  */
7361219Skas 
7371219Skas strncmp(as1, as2, an)
7381219Skas 	char *as1, *as2;
7391219Skas {
7401219Skas 	register char *s1, *s2;
7411219Skas 	register int n;
7421219Skas 
7431219Skas 	s1 = as1;
7441219Skas 	s2 = as2;
7451219Skas 	n = an;
7461219Skas 	while (--n >= 0 && *s1 == *s2++)
7471219Skas 		if (*s1++ == '\0')
7481219Skas 			return(0);
7491219Skas 	return(n<0 ? 0 : *s1 - *--s2);
7501219Skas }
7511219Skas 
752