xref: /csrg-svn/usr.sbin/sendmail/src/util.c (revision 22717)
1*22717Sdist /*
2*22717Sdist **  Sendmail
3*22717Sdist **  Copyright (c) 1983  Eric P. Allman
4*22717Sdist **  Berkeley, California
5*22717Sdist **
6*22717Sdist **  Copyright (c) 1983 Regents of the University of California.
7*22717Sdist **  All rights reserved.  The Berkeley software License Agreement
8*22717Sdist **  specifies the terms and conditions for redistribution.
9*22717Sdist */
10*22717Sdist 
11*22717Sdist #ifndef lint
12*22717Sdist static char	SccsId[] = "@(#)util.c	5.1 (Berkeley) 06/07/85";
13*22717Sdist #endif not lint
14*22717Sdist 
153151Seric # include <stdio.h>
164538Seric # include <sys/types.h>
174538Seric # include <sys/stat.h>
18298Seric # include <sysexits.h>
196890Seric # include <errno.h>
202900Seric # include <ctype.h>
216890Seric # include "sendmail.h"
22298Seric 
23*22717Sdist SCCSID(@(#)util.c	5.1		06/07/85);
24409Seric 
25298Seric /*
26298Seric **  STRIPQUOTES -- Strip quotes & quote bits from a string.
27298Seric **
28298Seric **	Runs through a string and strips off unquoted quote
29298Seric **	characters and quote bits.  This is done in place.
30298Seric **
31298Seric **	Parameters:
32298Seric **		s -- the string to strip.
334101Seric **		qf -- if set, remove actual `` " '' characters
344101Seric **			as well as the quote bits.
35298Seric **
36298Seric **	Returns:
37298Seric **		none.
38298Seric **
39298Seric **	Side Effects:
40298Seric **		none.
41298Seric **
42298Seric **	Called By:
43298Seric **		deliver
44298Seric */
45298Seric 
464101Seric stripquotes(s, qf)
47298Seric 	char *s;
484101Seric 	bool qf;
49298Seric {
50298Seric 	register char *p;
51298Seric 	register char *q;
52298Seric 	register char c;
53298Seric 
544101Seric 	if (s == NULL)
554101Seric 		return;
564101Seric 
57298Seric 	for (p = q = s; (c = *p++) != '\0'; )
58298Seric 	{
594101Seric 		if (c != '"' || !qf)
60298Seric 			*q++ = c & 0177;
61298Seric 	}
62298Seric 	*q = '\0';
63298Seric }
64298Seric /*
659043Seric **  QSTRLEN -- give me the string length assuming 0200 bits add a char
669043Seric **
679043Seric **	Parameters:
689043Seric **		s -- the string to measure.
699043Seric **
709043Seric **	Reurns:
719043Seric **		The length of s, including space for backslash escapes.
729043Seric **
739043Seric **	Side Effects:
749043Seric **		none.
759043Seric */
769043Seric 
779043Seric qstrlen(s)
789043Seric 	register char *s;
799043Seric {
809043Seric 	register int l = 0;
819043Seric 	register char c;
829043Seric 
839043Seric 	while ((c = *s++) != '\0')
849043Seric 	{
859043Seric 		if (bitset(0200, c))
869043Seric 			l++;
879043Seric 		l++;
889043Seric 	}
899043Seric 	return (l);
909043Seric }
919043Seric /*
922900Seric **  CAPITALIZE -- return a copy of a string, properly capitalized.
932900Seric **
942900Seric **	Parameters:
952900Seric **		s -- the string to capitalize.
962900Seric **
972900Seric **	Returns:
982900Seric **		a pointer to a properly capitalized string.
992900Seric **
1002900Seric **	Side Effects:
1012900Seric **		none.
1022900Seric */
1032900Seric 
1042900Seric char *
1052900Seric capitalize(s)
1062900Seric 	register char *s;
1072900Seric {
1082900Seric 	static char buf[50];
1092900Seric 	register char *p;
1102900Seric 
1112900Seric 	p = buf;
1122900Seric 
1132900Seric 	for (;;)
1142900Seric 	{
1152900Seric 		while (!isalpha(*s) && *s != '\0')
1162900Seric 			*p++ = *s++;
1172900Seric 		if (*s == '\0')
1182900Seric 			break;
1192900Seric 		*p++ = toupper(*s++);
1202900Seric 		while (isalpha(*s))
1212900Seric 			*p++ = *s++;
1222900Seric 	}
1232900Seric 
1242900Seric 	*p = '\0';
1252900Seric 	return (buf);
1262900Seric }
1272900Seric /*
128298Seric **  XALLOC -- Allocate memory and bitch wildly on failure.
129298Seric **
130298Seric **	THIS IS A CLUDGE.  This should be made to give a proper
131298Seric **	error -- but after all, what can we do?
132298Seric **
133298Seric **	Parameters:
134298Seric **		sz -- size of area to allocate.
135298Seric **
136298Seric **	Returns:
137298Seric **		pointer to data region.
138298Seric **
139298Seric **	Side Effects:
140298Seric **		Memory is allocated.
141298Seric */
142298Seric 
143298Seric char *
144298Seric xalloc(sz)
1457007Seric 	register int sz;
146298Seric {
147298Seric 	register char *p;
148298Seric 
149298Seric 	p = malloc(sz);
150298Seric 	if (p == NULL)
151298Seric 	{
152298Seric 		syserr("Out of memory!!");
15310685Seric 		abort();
15410685Seric 		/* exit(EX_UNAVAILABLE); */
155298Seric 	}
156298Seric 	return (p);
157298Seric }
158298Seric /*
1593151Seric **  COPYPLIST -- copy list of pointers.
1603151Seric **
1613151Seric **	This routine is the equivalent of newstr for lists of
1623151Seric **	pointers.
1633151Seric **
1643151Seric **	Parameters:
1653151Seric **		list -- list of pointers to copy.
1663151Seric **			Must be NULL terminated.
1673151Seric **		copycont -- if TRUE, copy the contents of the vector
1683151Seric **			(which must be a string) also.
1693151Seric **
1703151Seric **	Returns:
1713151Seric **		a copy of 'list'.
1723151Seric **
1733151Seric **	Side Effects:
1743151Seric **		none.
1753151Seric */
1763151Seric 
1773151Seric char **
1783151Seric copyplist(list, copycont)
1793151Seric 	char **list;
1803151Seric 	bool copycont;
1813151Seric {
1823151Seric 	register char **vp;
1833151Seric 	register char **newvp;
1843151Seric 
1853151Seric 	for (vp = list; *vp != NULL; vp++)
1863151Seric 		continue;
1873151Seric 
1883151Seric 	vp++;
1893151Seric 
19016897Seric 	newvp = (char **) xalloc((int) (vp - list) * sizeof *vp);
19116897Seric 	bcopy((char *) list, (char *) newvp, (int) (vp - list) * sizeof *vp);
1923151Seric 
1933151Seric 	if (copycont)
1943151Seric 	{
1953151Seric 		for (vp = newvp; *vp != NULL; vp++)
1963151Seric 			*vp = newstr(*vp);
1973151Seric 	}
1983151Seric 
1993151Seric 	return (newvp);
2003151Seric }
2013151Seric /*
2023151Seric **  PRINTAV -- print argument vector.
2033151Seric **
2043151Seric **	Parameters:
2053151Seric **		av -- argument vector.
2063151Seric **
2073151Seric **	Returns:
2083151Seric **		none.
2093151Seric **
2103151Seric **	Side Effects:
2113151Seric **		prints av.
2123151Seric */
2133151Seric 
2143151Seric printav(av)
2153151Seric 	register char **av;
2163151Seric {
2173151Seric 	while (*av != NULL)
2183151Seric 	{
2198063Seric 		if (tTd(0, 44))
2208063Seric 			printf("\n\t%08x=", *av);
2218063Seric 		else
2228063Seric 			putchar(' ');
2233151Seric 		xputs(*av++);
2243151Seric 	}
2258063Seric 	putchar('\n');
2263151Seric }
2273151Seric /*
2283151Seric **  LOWER -- turn letter into lower case.
2293151Seric **
2303151Seric **	Parameters:
2313151Seric **		c -- character to turn into lower case.
2323151Seric **
2333151Seric **	Returns:
2343151Seric **		c, in lower case.
2353151Seric **
2363151Seric **	Side Effects:
2373151Seric **		none.
2383151Seric */
2393151Seric 
2403151Seric char
2413151Seric lower(c)
2423151Seric 	register char c;
2433151Seric {
2443151Seric 	if (isascii(c) && isupper(c))
2453151Seric 		c = c - 'A' + 'a';
2463151Seric 	return (c);
2473151Seric }
2483151Seric /*
2493151Seric **  XPUTS -- put string doing control escapes.
2503151Seric **
2513151Seric **	Parameters:
2523151Seric **		s -- string to put.
2533151Seric **
2543151Seric **	Returns:
2553151Seric **		none.
2563151Seric **
2573151Seric **	Side Effects:
2583151Seric **		output to stdout
2593151Seric */
2603151Seric 
2613151Seric xputs(s)
2623151Seric 	register char *s;
2633151Seric {
2643151Seric 	register char c;
2653151Seric 
2668055Seric 	if (s == NULL)
2678055Seric 	{
2688055Seric 		printf("<null>");
2698055Seric 		return;
2708055Seric 	}
2718063Seric 	putchar('"');
2723151Seric 	while ((c = *s++) != '\0')
2733151Seric 	{
2743151Seric 		if (!isascii(c))
2753151Seric 		{
2763151Seric 			putchar('\\');
2773151Seric 			c &= 0177;
2783151Seric 		}
27910326Seric 		if (c < 040 || c >= 0177)
2803151Seric 		{
2813151Seric 			putchar('^');
28210326Seric 			c ^= 0100;
2833151Seric 		}
2843151Seric 		putchar(c);
2853151Seric 	}
2868063Seric 	putchar('"');
2874086Seric 	(void) fflush(stdout);
2883151Seric }
2893151Seric /*
2903151Seric **  MAKELOWER -- Translate a line into lower case
2913151Seric **
2923151Seric **	Parameters:
2933151Seric **		p -- the string to translate.  If NULL, return is
2943151Seric **			immediate.
2953151Seric **
2963151Seric **	Returns:
2973151Seric **		none.
2983151Seric **
2993151Seric **	Side Effects:
3003151Seric **		String pointed to by p is translated to lower case.
3013151Seric **
3023151Seric **	Called By:
3033151Seric **		parse
3043151Seric */
3053151Seric 
3063151Seric makelower(p)
3073151Seric 	register char *p;
3083151Seric {
3093151Seric 	register char c;
3103151Seric 
3113151Seric 	if (p == NULL)
3123151Seric 		return;
3133151Seric 	for (; (c = *p) != '\0'; p++)
3143151Seric 		if (isascii(c) && isupper(c))
3153151Seric 			*p = c - 'A' + 'a';
3163151Seric }
3174059Seric /*
3184059Seric **  SAMEWORD -- return TRUE if the words are the same
3194059Seric **
3204059Seric **	Ignores case.
3214059Seric **
3224059Seric **	Parameters:
3234059Seric **		a, b -- the words to compare.
3244059Seric **
3254059Seric **	Returns:
3264059Seric **		TRUE if a & b match exactly (modulo case)
3274059Seric **		FALSE otherwise.
3284059Seric **
3294059Seric **	Side Effects:
3304059Seric **		none.
3314059Seric */
3324059Seric 
3334059Seric bool
3344059Seric sameword(a, b)
3354059Seric 	register char *a, *b;
3364059Seric {
33717350Seric 	char ca, cb;
33817350Seric 
33917350Seric 	do
3404059Seric 	{
34117350Seric 		ca = *a++;
34217350Seric 		cb = *b++;
34317350Seric 		if (isascii(ca) && isupper(ca))
34417350Seric 			ca = ca - 'A' + 'a';
34517350Seric 		if (isascii(cb) && isupper(cb))
34617350Seric 			cb = cb - 'A' + 'a';
34717350Seric 	} while (ca != '\0' && ca == cb);
34817350Seric 	return (ca == cb);
3494059Seric }
3504086Seric /*
3515196Seric **  BUILDFNAME -- build full name from gecos style entry.
3524375Seric **
3535196Seric **	This routine interprets the strange entry that would appear
3545196Seric **	in the GECOS field of the password file.
3555196Seric **
3564375Seric **	Parameters:
3575196Seric **		p -- name to build.
3585196Seric **		login -- the login name of this user (for &).
3595196Seric **		buf -- place to put the result.
3604375Seric **
3614375Seric **	Returns:
3624375Seric **		none.
3634375Seric **
3644375Seric **	Side Effects:
3654375Seric **		none.
3664375Seric */
3674375Seric 
3685196Seric buildfname(p, login, buf)
3695196Seric 	register char *p;
3705196Seric 	char *login;
3714375Seric 	char *buf;
3724375Seric {
3734375Seric 	register char *bp = buf;
3744375Seric 
3754438Seric 	if (*p == '*')
3764438Seric 		p++;
3776278Seric 	while (*p != '\0' && *p != ',' && *p != ';' && *p != '%')
3784375Seric 	{
3794375Seric 		if (*p == '&')
3804375Seric 		{
3815196Seric 			(void) strcpy(bp, login);
3824375Seric 			*bp = toupper(*bp);
3834375Seric 			while (*bp != '\0')
3844375Seric 				bp++;
3854375Seric 			p++;
3864375Seric 		}
3874375Seric 		else
3884375Seric 			*bp++ = *p++;
3894375Seric 	}
3904375Seric 	*bp = '\0';
3914375Seric }
3924375Seric /*
3934538Seric **  SAFEFILE -- return true if a file exists and is safe for a user.
3944538Seric **
3954538Seric **	Parameters:
3964538Seric **		fn -- filename to check.
3974538Seric **		uid -- uid to compare against.
3984538Seric **		mode -- mode bits that must match.
3994538Seric **
4004538Seric **	Returns:
4014538Seric **		TRUE if fn exists, is owned by uid, and matches mode.
4024538Seric **		FALSE otherwise.
4034538Seric **
4044538Seric **	Side Effects:
4054538Seric **		none.
4064538Seric */
4074538Seric 
4084538Seric bool
4094538Seric safefile(fn, uid, mode)
4104538Seric 	char *fn;
4114538Seric 	int uid;
4124538Seric 	int mode;
4134538Seric {
4144538Seric 	struct stat stbuf;
4154538Seric 
4164538Seric 	if (stat(fn, &stbuf) >= 0 && stbuf.st_uid == uid &&
4174538Seric 	    (stbuf.st_mode & mode) == mode)
4184538Seric 		return (TRUE);
41911936Seric 	errno = 0;
4204538Seric 	return (FALSE);
4214538Seric }
4224538Seric /*
4234557Seric **  FIXCRLF -- fix <CR><LF> in line.
4244557Seric **
4254557Seric **	Looks for the <CR><LF> combination and turns it into the
4264557Seric **	UNIX canonical <NL> character.  It only takes one line,
4274557Seric **	i.e., it is assumed that the first <NL> found is the end
4284557Seric **	of the line.
4294557Seric **
4304557Seric **	Parameters:
4314557Seric **		line -- the line to fix.
4324557Seric **		stripnl -- if true, strip the newline also.
4334557Seric **
4344557Seric **	Returns:
4354557Seric **		none.
4364557Seric **
4374557Seric **	Side Effects:
4384557Seric **		line is changed in place.
4394557Seric */
4404557Seric 
4414557Seric fixcrlf(line, stripnl)
4424557Seric 	char *line;
4434557Seric 	bool stripnl;
4444557Seric {
4454557Seric 	register char *p;
4464557Seric 
4474557Seric 	p = index(line, '\n');
4484557Seric 	if (p == NULL)
4494557Seric 		return;
4504794Seric 	if (p[-1] == '\r')
4514557Seric 		p--;
4524557Seric 	if (!stripnl)
4534557Seric 		*p++ = '\n';
4544557Seric 	*p = '\0';
4554557Seric }
4564557Seric /*
4576890Seric **  DFOPEN -- determined file open
4586890Seric **
4596890Seric **	This routine has the semantics of fopen, except that it will
4606890Seric **	keep trying a few times to make this happen.  The idea is that
4616890Seric **	on very loaded systems, we may run out of resources (inodes,
4626890Seric **	whatever), so this tries to get around it.
4636890Seric */
4646890Seric 
4656890Seric FILE *
4666890Seric dfopen(filename, mode)
4676890Seric 	char *filename;
4686890Seric 	char *mode;
4696890Seric {
4706890Seric 	register int tries;
4716890Seric 	register FILE *fp;
4726890Seric 
4736890Seric 	for (tries = 0; tries < 10; tries++)
4746890Seric 	{
4756890Seric 		sleep(10 * tries);
4766890Seric 		errno = 0;
4776890Seric 		fp = fopen(filename, mode);
4789376Seric 		if (fp != NULL)
4796890Seric 			break;
4809376Seric 		if (errno != ENFILE && errno != EINTR)
4819376Seric 			break;
4826890Seric 	}
48311936Seric 	errno = 0;
4846890Seric 	return (fp);
4856890Seric }
4867124Seric /*
4877124Seric **  PUTLINE -- put a line like fputs obeying SMTP conventions
4887124Seric **
4897753Seric **	This routine always guarantees outputing a newline (or CRLF,
4907753Seric **	as appropriate) at the end of the string.
4917753Seric **
4927124Seric **	Parameters:
4937124Seric **		l -- line to put.
4947124Seric **		fp -- file to put it onto.
49510172Seric **		m -- the mailer used to control output.
4967124Seric **
4977124Seric **	Returns:
4987124Seric **		none
4997124Seric **
5007124Seric **	Side Effects:
5017124Seric **		output of l to fp.
5027124Seric */
5037124Seric 
5047753Seric # define SMTPLINELIM	990	/* maximum line length */
5057124Seric 
50610172Seric putline(l, fp, m)
5077753Seric 	register char *l;
5087124Seric 	FILE *fp;
50910172Seric 	MAILER *m;
5107124Seric {
5117124Seric 	register char *p;
5127753Seric 	char svchar;
5137124Seric 
51411275Seric 	/* strip out 0200 bits -- these can look like TELNET protocol */
51511275Seric 	if (bitnset(M_LIMITS, m->m_flags))
51611275Seric 	{
51711275Seric 		p = l;
51811275Seric 		while ((*p++ &= ~0200) != 0)
51911275Seric 			continue;
52011275Seric 	}
52111275Seric 
5227753Seric 	do
5237124Seric 	{
5247753Seric 		/* find the end of the line */
5257753Seric 		p = index(l, '\n');
5267753Seric 		if (p == NULL)
5277753Seric 			p = &l[strlen(l)];
5287124Seric 
5297753Seric 		/* check for line overflow */
53011275Seric 		while ((p - l) > SMTPLINELIM && bitnset(M_LIMITS, m->m_flags))
5317753Seric 		{
5327753Seric 			register char *q = &l[SMTPLINELIM - 1];
5337124Seric 
5347753Seric 			svchar = *q;
5357753Seric 			*q = '\0';
53610685Seric 			if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
53710172Seric 				fputc('.', fp);
5387753Seric 			fputs(l, fp);
53910066Seric 			fputc('!', fp);
54010326Seric 			fputs(m->m_eol, fp);
5417753Seric 			*q = svchar;
5427753Seric 			l = q;
5437753Seric 		}
5447124Seric 
5457753Seric 		/* output last part */
5467753Seric 		svchar = *p;
5477753Seric 		*p = '\0';
54810685Seric 		if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
54910172Seric 			fputc('.', fp);
5507124Seric 		fputs(l, fp);
55110326Seric 		fputs(m->m_eol, fp);
5527753Seric 		*p = svchar;
5537753Seric 		l = p;
5547753Seric 		if (*l == '\n')
5557753Seric 			l++;
5567753Seric 	} while (l[0] != '\0');
5577124Seric }
5587676Seric /*
5597676Seric **  XUNLINK -- unlink a file, doing logging as appropriate.
5607676Seric **
5617676Seric **	Parameters:
5627676Seric **		f -- name of file to unlink.
5637676Seric **
5647676Seric **	Returns:
5657676Seric **		none.
5667676Seric **
5677676Seric **	Side Effects:
5687676Seric **		f is unlinked.
5697676Seric */
5707676Seric 
5717676Seric xunlink(f)
5727676Seric 	char *f;
5737676Seric {
5747676Seric 	register int i;
5757676Seric 
5767676Seric # ifdef LOG
5777676Seric 	if (LogLevel > 20)
5787812Seric 		syslog(LOG_DEBUG, "%s: unlink %s\n", CurEnv->e_id, f);
5797676Seric # endif LOG
5807676Seric 
5817676Seric 	i = unlink(f);
5827676Seric # ifdef LOG
5837676Seric 	if (i < 0 && LogLevel > 21)
5847942Seric 		syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno);
5857676Seric # endif LOG
5867676Seric }
5877685Seric /*
58814885Seric **  SFGETS -- "safe" fgets -- times out and ignores random interrupts.
5897685Seric **
5907685Seric **	Parameters:
5917685Seric **		buf -- place to put the input line.
5927685Seric **		siz -- size of buf.
5937685Seric **		fp -- file to read from.
5947685Seric **
5957685Seric **	Returns:
59615533Seric **		NULL on error (including timeout).  This will also leave
59715533Seric **			buf containing a null string.
5987685Seric **		buf otherwise.
5997685Seric **
6007685Seric **	Side Effects:
6017685Seric **		none.
6027685Seric */
6037685Seric 
60414885Seric static jmp_buf	CtxReadTimeout;
6057685Seric 
60616138Seric #ifndef ETIMEDOUT
60716138Seric #define ETIMEDOUT	EINTR
60816138Seric #endif
60916138Seric 
6107685Seric char *
6117685Seric sfgets(buf, siz, fp)
6127685Seric 	char *buf;
6137685Seric 	int siz;
6147685Seric 	FILE *fp;
6157685Seric {
6167942Seric 	register EVENT *ev = NULL;
6177685Seric 	register char *p;
6187685Seric 	extern readtimeout();
6197685Seric 
62014885Seric 	/* set the timeout */
6217942Seric 	if (ReadTimeout != 0)
62214885Seric 	{
62314885Seric 		if (setjmp(CtxReadTimeout) != 0)
62414885Seric 		{
62516138Seric 			errno = ETIMEDOUT;
62614885Seric 			syserr("sfgets: timeout on read (mailer may be hung)");
62719037Seric 			buf[0] = '\0';
62814885Seric 			return (NULL);
62914885Seric 		}
63016138Seric 		ev = setevent((time_t) ReadTimeout, readtimeout, 0);
63114885Seric 	}
63214885Seric 
63314885Seric 	/* try to read */
63415533Seric 	p = NULL;
63515533Seric 	while (p == NULL && !feof(fp) && !ferror(fp))
6367942Seric 	{
6377942Seric 		errno = 0;
6387942Seric 		p = fgets(buf, siz, fp);
63915533Seric 		if (errno == EINTR)
64015533Seric 			clearerr(fp);
64115533Seric 	}
64214885Seric 
64314885Seric 	/* clear the event if it has not sprung */
6447685Seric 	clrevent(ev);
64514885Seric 
64614885Seric 	/* clean up the books and exit */
6478055Seric 	LineNumber++;
64815533Seric 	if (p == NULL)
64916880Seric 	{
65015533Seric 		buf[0] = '\0';
65116880Seric 		return (NULL);
65216880Seric 	}
65316880Seric 	for (p = buf; *p != '\0'; p++)
65416880Seric 		*p &= ~0200;
65516880Seric 	return (buf);
6567685Seric }
6577685Seric 
6587685Seric static
6597685Seric readtimeout()
6607685Seric {
66114885Seric 	longjmp(CtxReadTimeout, 1);
6627685Seric }
6637786Seric /*
6647786Seric **  FGETFOLDED -- like fgets, but know about folded lines.
6657786Seric **
6667786Seric **	Parameters:
6677786Seric **		buf -- place to put result.
6687786Seric **		n -- bytes available.
6697786Seric **		f -- file to read from.
6707786Seric **
6717786Seric **	Returns:
6727786Seric **		buf on success, NULL on error or EOF.
6737786Seric **
6747786Seric **	Side Effects:
6757786Seric **		buf gets lines from f, with continuation lines (lines
6767786Seric **		with leading white space) appended.  CRLF's are mapped
6777786Seric **		into single newlines.  Any trailing NL is stripped.
6787786Seric */
6797786Seric 
6807786Seric char *
6817786Seric fgetfolded(buf, n, f)
6827786Seric 	char *buf;
6837786Seric 	register int n;
6847786Seric 	FILE *f;
6857786Seric {
6867786Seric 	register char *p = buf;
6877786Seric 	register int i;
6887786Seric 
6897786Seric 	n--;
69017350Seric 	while ((i = getc(f)) != EOF)
6917786Seric 	{
69217350Seric 		if (i == '\r')
69317350Seric 		{
69417350Seric 			i = getc(f);
69517350Seric 			if (i != '\n')
69617350Seric 			{
69717350Seric 				if (i != EOF)
69817350Seric 					ungetc(i, f);
69917350Seric 				i = '\r';
70017350Seric 			}
70117350Seric 		}
70217350Seric 		if (--n > 0)
70317350Seric 			*p++ = i;
70417350Seric 		if (i == '\n')
70517350Seric 		{
70617350Seric 			LineNumber++;
70717350Seric 			i = getc(f);
70817350Seric 			if (i != EOF)
70917350Seric 				ungetc(i, f);
71017350Seric 			if (i != ' ' && i != '\t')
71117350Seric 			{
71217350Seric 				*--p = '\0';
71317350Seric 				return (buf);
71417350Seric 			}
71517350Seric 		}
7167786Seric 	}
7177786Seric 	return (NULL);
7187786Seric }
7197860Seric /*
7207886Seric **  CURTIME -- return current time.
7217886Seric **
7227886Seric **	Parameters:
7237886Seric **		none.
7247886Seric **
7257886Seric **	Returns:
7267886Seric **		the current time.
7277886Seric **
7287886Seric **	Side Effects:
7297886Seric **		none.
7307886Seric */
7317886Seric 
7327886Seric time_t
7337886Seric curtime()
7347886Seric {
7357886Seric 	auto time_t t;
7367886Seric 
7377886Seric 	(void) time(&t);
7387886Seric 	return (t);
7397886Seric }
7408264Seric /*
7418264Seric **  ATOBOOL -- convert a string representation to boolean.
7428264Seric **
7438264Seric **	Defaults to "TRUE"
7448264Seric **
7458264Seric **	Parameters:
7468264Seric **		s -- string to convert.  Takes "tTyY" as true,
7478264Seric **			others as false.
7488264Seric **
7498264Seric **	Returns:
7508264Seric **		A boolean representation of the string.
7518264Seric **
7528264Seric **	Side Effects:
7538264Seric **		none.
7548264Seric */
7558264Seric 
7568264Seric bool
7578264Seric atobool(s)
7588264Seric 	register char *s;
7598264Seric {
7608264Seric 	if (*s == '\0' || index("tTyY", *s) != NULL)
7618264Seric 		return (TRUE);
7628264Seric 	return (FALSE);
7638264Seric }
7649048Seric /*
7659048Seric **  ATOOCT -- convert a string representation to octal.
7669048Seric **
7679048Seric **	Parameters:
7689048Seric **		s -- string to convert.
7699048Seric **
7709048Seric **	Returns:
7719048Seric **		An integer representing the string interpreted as an
7729048Seric **		octal number.
7739048Seric **
7749048Seric **	Side Effects:
7759048Seric **		none.
7769048Seric */
7779048Seric 
7789048Seric atooct(s)
7799048Seric 	register char *s;
7809048Seric {
7819048Seric 	register int i = 0;
7829048Seric 
7839048Seric 	while (*s >= '0' && *s <= '7')
7849048Seric 		i = (i << 3) | (*s++ - '0');
7859048Seric 	return (i);
7869048Seric }
7879376Seric /*
7889376Seric **  WAITFOR -- wait for a particular process id.
7899376Seric **
7909376Seric **	Parameters:
7919376Seric **		pid -- process id to wait for.
7929376Seric **
7939376Seric **	Returns:
7949376Seric **		status of pid.
7959376Seric **		-1 if pid never shows up.
7969376Seric **
7979376Seric **	Side Effects:
7989376Seric **		none.
7999376Seric */
8009376Seric 
8019376Seric waitfor(pid)
8029376Seric 	int pid;
8039376Seric {
8049376Seric 	auto int st;
8059376Seric 	int i;
8069376Seric 
8079376Seric 	do
8089376Seric 	{
8099376Seric 		errno = 0;
8109376Seric 		i = wait(&st);
8119376Seric 	} while ((i >= 0 || errno == EINTR) && i != pid);
8129376Seric 	if (i < 0)
8139376Seric 		st = -1;
8149376Seric 	return (st);
8159376Seric }
8169376Seric /*
81710685Seric **  BITINTERSECT -- tell if two bitmaps intersect
81810685Seric **
81910685Seric **	Parameters:
82010685Seric **		a, b -- the bitmaps in question
82110685Seric **
82210685Seric **	Returns:
82310685Seric **		TRUE if they have a non-null intersection
82410685Seric **		FALSE otherwise
82510685Seric **
82610685Seric **	Side Effects:
82710685Seric **		none.
82810685Seric */
82910685Seric 
83010685Seric bool
83110685Seric bitintersect(a, b)
83210685Seric 	BITMAP a;
83310685Seric 	BITMAP b;
83410685Seric {
83510685Seric 	int i;
83610685Seric 
83710685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
83810685Seric 		if ((a[i] & b[i]) != 0)
83910685Seric 			return (TRUE);
84010685Seric 	return (FALSE);
84110685Seric }
84210685Seric /*
84310685Seric **  BITZEROP -- tell if a bitmap is all zero
84410685Seric **
84510685Seric **	Parameters:
84610685Seric **		map -- the bit map to check
84710685Seric **
84810685Seric **	Returns:
84910685Seric **		TRUE if map is all zero.
85010685Seric **		FALSE if there are any bits set in map.
85110685Seric **
85210685Seric **	Side Effects:
85310685Seric **		none.
85410685Seric */
85510685Seric 
85610685Seric bool
85710685Seric bitzerop(map)
85810685Seric 	BITMAP map;
85910685Seric {
86010685Seric 	int i;
86110685Seric 
86210685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
86310685Seric 		if (map[i] != 0)
86410685Seric 			return (FALSE);
86510685Seric 	return (TRUE);
86610685Seric }
867