xref: /csrg-svn/usr.sbin/sendmail/src/util.c (revision 42833)
122717Sdist /*
2*42833Sbostic  * Copyright (c) 1983 Eric P. Allman
333731Sbostic  * Copyright (c) 1988 Regents of the University of California.
433731Sbostic  * All rights reserved.
533731Sbostic  *
6*42833Sbostic  * %sccs.include.redist.c%
733731Sbostic  */
822717Sdist 
922717Sdist #ifndef lint
10*42833Sbostic static char sccsid[] = "@(#)util.c	5.18 (Berkeley) 06/01/90";
1133731Sbostic #endif /* not lint */
1222717Sdist 
133151Seric # include <stdio.h>
144538Seric # include <sys/types.h>
154538Seric # include <sys/stat.h>
16298Seric # include <sysexits.h>
176890Seric # include <errno.h>
186890Seric # include "sendmail.h"
19298Seric 
20298Seric /*
21298Seric **  STRIPQUOTES -- Strip quotes & quote bits from a string.
22298Seric **
23298Seric **	Runs through a string and strips off unquoted quote
24298Seric **	characters and quote bits.  This is done in place.
25298Seric **
26298Seric **	Parameters:
27298Seric **		s -- the string to strip.
284101Seric **		qf -- if set, remove actual `` " '' characters
294101Seric **			as well as the quote bits.
30298Seric **
31298Seric **	Returns:
32298Seric **		none.
33298Seric **
34298Seric **	Side Effects:
35298Seric **		none.
36298Seric **
37298Seric **	Called By:
38298Seric **		deliver
39298Seric */
40298Seric 
414101Seric stripquotes(s, qf)
42298Seric 	char *s;
434101Seric 	bool qf;
44298Seric {
45298Seric 	register char *p;
46298Seric 	register char *q;
47298Seric 	register char c;
48298Seric 
494101Seric 	if (s == NULL)
504101Seric 		return;
514101Seric 
52298Seric 	for (p = q = s; (c = *p++) != '\0'; )
53298Seric 	{
544101Seric 		if (c != '"' || !qf)
55298Seric 			*q++ = c & 0177;
56298Seric 	}
57298Seric 	*q = '\0';
58298Seric }
59298Seric /*
609043Seric **  QSTRLEN -- give me the string length assuming 0200 bits add a char
619043Seric **
629043Seric **	Parameters:
639043Seric **		s -- the string to measure.
649043Seric **
659043Seric **	Reurns:
669043Seric **		The length of s, including space for backslash escapes.
679043Seric **
689043Seric **	Side Effects:
699043Seric **		none.
709043Seric */
719043Seric 
729043Seric qstrlen(s)
739043Seric 	register char *s;
749043Seric {
759043Seric 	register int l = 0;
769043Seric 	register char c;
779043Seric 
789043Seric 	while ((c = *s++) != '\0')
799043Seric 	{
809043Seric 		if (bitset(0200, c))
819043Seric 			l++;
829043Seric 		l++;
839043Seric 	}
849043Seric 	return (l);
859043Seric }
869043Seric /*
872900Seric **  CAPITALIZE -- return a copy of a string, properly capitalized.
882900Seric **
892900Seric **	Parameters:
902900Seric **		s -- the string to capitalize.
912900Seric **
922900Seric **	Returns:
932900Seric **		a pointer to a properly capitalized string.
942900Seric **
952900Seric **	Side Effects:
962900Seric **		none.
972900Seric */
982900Seric 
992900Seric char *
1002900Seric capitalize(s)
1012900Seric 	register char *s;
1022900Seric {
1032900Seric 	static char buf[50];
1042900Seric 	register char *p;
1052900Seric 
1062900Seric 	p = buf;
1072900Seric 
1082900Seric 	for (;;)
1092900Seric 	{
1102900Seric 		while (!isalpha(*s) && *s != '\0')
1112900Seric 			*p++ = *s++;
1122900Seric 		if (*s == '\0')
1132900Seric 			break;
11440999Sbostic 		*p++ = toupper(*s);
11540999Sbostic 		s++;
1162900Seric 		while (isalpha(*s))
1172900Seric 			*p++ = *s++;
1182900Seric 	}
1192900Seric 
1202900Seric 	*p = '\0';
1212900Seric 	return (buf);
1222900Seric }
1232900Seric /*
124298Seric **  XALLOC -- Allocate memory and bitch wildly on failure.
125298Seric **
126298Seric **	THIS IS A CLUDGE.  This should be made to give a proper
127298Seric **	error -- but after all, what can we do?
128298Seric **
129298Seric **	Parameters:
130298Seric **		sz -- size of area to allocate.
131298Seric **
132298Seric **	Returns:
133298Seric **		pointer to data region.
134298Seric **
135298Seric **	Side Effects:
136298Seric **		Memory is allocated.
137298Seric */
138298Seric 
139298Seric char *
140298Seric xalloc(sz)
1417007Seric 	register int sz;
142298Seric {
143298Seric 	register char *p;
14423121Seric 	extern char *malloc();
145298Seric 
14623121Seric 	p = malloc((unsigned) sz);
147298Seric 	if (p == NULL)
148298Seric 	{
149298Seric 		syserr("Out of memory!!");
15010685Seric 		abort();
15110685Seric 		/* exit(EX_UNAVAILABLE); */
152298Seric 	}
153298Seric 	return (p);
154298Seric }
155298Seric /*
1563151Seric **  COPYPLIST -- copy list of pointers.
1573151Seric **
1583151Seric **	This routine is the equivalent of newstr for lists of
1593151Seric **	pointers.
1603151Seric **
1613151Seric **	Parameters:
1623151Seric **		list -- list of pointers to copy.
1633151Seric **			Must be NULL terminated.
1643151Seric **		copycont -- if TRUE, copy the contents of the vector
1653151Seric **			(which must be a string) also.
1663151Seric **
1673151Seric **	Returns:
1683151Seric **		a copy of 'list'.
1693151Seric **
1703151Seric **	Side Effects:
1713151Seric **		none.
1723151Seric */
1733151Seric 
1743151Seric char **
1753151Seric copyplist(list, copycont)
1763151Seric 	char **list;
1773151Seric 	bool copycont;
1783151Seric {
1793151Seric 	register char **vp;
1803151Seric 	register char **newvp;
1813151Seric 
1823151Seric 	for (vp = list; *vp != NULL; vp++)
1833151Seric 		continue;
1843151Seric 
1853151Seric 	vp++;
1863151Seric 
18716897Seric 	newvp = (char **) xalloc((int) (vp - list) * sizeof *vp);
18816897Seric 	bcopy((char *) list, (char *) newvp, (int) (vp - list) * sizeof *vp);
1893151Seric 
1903151Seric 	if (copycont)
1913151Seric 	{
1923151Seric 		for (vp = newvp; *vp != NULL; vp++)
1933151Seric 			*vp = newstr(*vp);
1943151Seric 	}
1953151Seric 
1963151Seric 	return (newvp);
1973151Seric }
1983151Seric /*
1993151Seric **  PRINTAV -- print argument vector.
2003151Seric **
2013151Seric **	Parameters:
2023151Seric **		av -- argument vector.
2033151Seric **
2043151Seric **	Returns:
2053151Seric **		none.
2063151Seric **
2073151Seric **	Side Effects:
2083151Seric **		prints av.
2093151Seric */
2103151Seric 
2113151Seric printav(av)
2123151Seric 	register char **av;
2133151Seric {
2143151Seric 	while (*av != NULL)
2153151Seric 	{
2168063Seric 		if (tTd(0, 44))
2178063Seric 			printf("\n\t%08x=", *av);
2188063Seric 		else
21923105Seric 			(void) putchar(' ');
2203151Seric 		xputs(*av++);
2213151Seric 	}
22223105Seric 	(void) putchar('\n');
2233151Seric }
2243151Seric /*
2253151Seric **  LOWER -- turn letter into lower case.
2263151Seric **
2273151Seric **	Parameters:
2283151Seric **		c -- character to turn into lower case.
2293151Seric **
2303151Seric **	Returns:
2313151Seric **		c, in lower case.
2323151Seric **
2333151Seric **	Side Effects:
2343151Seric **		none.
2353151Seric */
2363151Seric 
2373151Seric char
2383151Seric lower(c)
2393151Seric 	register char c;
2403151Seric {
24133724Sbostic 	return(isascii(c) && isupper(c) ? tolower(c) : c);
2423151Seric }
2433151Seric /*
2443151Seric **  XPUTS -- put string doing control escapes.
2453151Seric **
2463151Seric **	Parameters:
2473151Seric **		s -- string to put.
2483151Seric **
2493151Seric **	Returns:
2503151Seric **		none.
2513151Seric **
2523151Seric **	Side Effects:
2533151Seric **		output to stdout
2543151Seric */
2553151Seric 
2563151Seric xputs(s)
2573151Seric 	register char *s;
2583151Seric {
2593151Seric 	register char c;
2603151Seric 
2618055Seric 	if (s == NULL)
2628055Seric 	{
2638055Seric 		printf("<null>");
2648055Seric 		return;
2658055Seric 	}
26623105Seric 	(void) putchar('"');
2673151Seric 	while ((c = *s++) != '\0')
2683151Seric 	{
2693151Seric 		if (!isascii(c))
2703151Seric 		{
27123105Seric 			(void) putchar('\\');
2723151Seric 			c &= 0177;
2733151Seric 		}
27410326Seric 		if (c < 040 || c >= 0177)
2753151Seric 		{
27623105Seric 			(void) putchar('^');
27710326Seric 			c ^= 0100;
2783151Seric 		}
27923105Seric 		(void) putchar(c);
2803151Seric 	}
28123105Seric 	(void) putchar('"');
2824086Seric 	(void) fflush(stdout);
2833151Seric }
2843151Seric /*
2853151Seric **  MAKELOWER -- Translate a line into lower case
2863151Seric **
2873151Seric **	Parameters:
2883151Seric **		p -- the string to translate.  If NULL, return is
2893151Seric **			immediate.
2903151Seric **
2913151Seric **	Returns:
2923151Seric **		none.
2933151Seric **
2943151Seric **	Side Effects:
2953151Seric **		String pointed to by p is translated to lower case.
2963151Seric **
2973151Seric **	Called By:
2983151Seric **		parse
2993151Seric */
3003151Seric 
3013151Seric makelower(p)
3023151Seric 	register char *p;
3033151Seric {
3043151Seric 	register char c;
3053151Seric 
3063151Seric 	if (p == NULL)
3073151Seric 		return;
3083151Seric 	for (; (c = *p) != '\0'; p++)
3093151Seric 		if (isascii(c) && isupper(c))
31033724Sbostic 			*p = tolower(c);
3113151Seric }
3124059Seric /*
3135196Seric **  BUILDFNAME -- build full name from gecos style entry.
3144375Seric **
3155196Seric **	This routine interprets the strange entry that would appear
3165196Seric **	in the GECOS field of the password file.
3175196Seric **
3184375Seric **	Parameters:
3195196Seric **		p -- name to build.
3205196Seric **		login -- the login name of this user (for &).
3215196Seric **		buf -- place to put the result.
3224375Seric **
3234375Seric **	Returns:
3244375Seric **		none.
3254375Seric **
3264375Seric **	Side Effects:
3274375Seric **		none.
3284375Seric */
3294375Seric 
3305196Seric buildfname(p, login, buf)
3315196Seric 	register char *p;
3325196Seric 	char *login;
3334375Seric 	char *buf;
3344375Seric {
3354375Seric 	register char *bp = buf;
3364375Seric 
3374438Seric 	if (*p == '*')
3384438Seric 		p++;
3396278Seric 	while (*p != '\0' && *p != ',' && *p != ';' && *p != '%')
3404375Seric 	{
3414375Seric 		if (*p == '&')
3424375Seric 		{
3435196Seric 			(void) strcpy(bp, login);
3444375Seric 			*bp = toupper(*bp);
3454375Seric 			while (*bp != '\0')
3464375Seric 				bp++;
3474375Seric 			p++;
3484375Seric 		}
3494375Seric 		else
3504375Seric 			*bp++ = *p++;
3514375Seric 	}
3524375Seric 	*bp = '\0';
3534375Seric }
3544375Seric /*
3554538Seric **  SAFEFILE -- return true if a file exists and is safe for a user.
3564538Seric **
3574538Seric **	Parameters:
3584538Seric **		fn -- filename to check.
3594538Seric **		uid -- uid to compare against.
3604538Seric **		mode -- mode bits that must match.
3614538Seric **
3624538Seric **	Returns:
3634538Seric **		TRUE if fn exists, is owned by uid, and matches mode.
3644538Seric **		FALSE otherwise.
3654538Seric **
3664538Seric **	Side Effects:
3674538Seric **		none.
3684538Seric */
3694538Seric 
3704538Seric bool
3714538Seric safefile(fn, uid, mode)
3724538Seric 	char *fn;
3734538Seric 	int uid;
3744538Seric 	int mode;
3754538Seric {
3764538Seric 	struct stat stbuf;
3774538Seric 
3784538Seric 	if (stat(fn, &stbuf) >= 0 && stbuf.st_uid == uid &&
3794538Seric 	    (stbuf.st_mode & mode) == mode)
3804538Seric 		return (TRUE);
38111936Seric 	errno = 0;
3824538Seric 	return (FALSE);
3834538Seric }
3844538Seric /*
3854557Seric **  FIXCRLF -- fix <CR><LF> in line.
3864557Seric **
3874557Seric **	Looks for the <CR><LF> combination and turns it into the
3884557Seric **	UNIX canonical <NL> character.  It only takes one line,
3894557Seric **	i.e., it is assumed that the first <NL> found is the end
3904557Seric **	of the line.
3914557Seric **
3924557Seric **	Parameters:
3934557Seric **		line -- the line to fix.
3944557Seric **		stripnl -- if true, strip the newline also.
3954557Seric **
3964557Seric **	Returns:
3974557Seric **		none.
3984557Seric **
3994557Seric **	Side Effects:
4004557Seric **		line is changed in place.
4014557Seric */
4024557Seric 
4034557Seric fixcrlf(line, stripnl)
4044557Seric 	char *line;
4054557Seric 	bool stripnl;
4064557Seric {
4074557Seric 	register char *p;
4084557Seric 
4094557Seric 	p = index(line, '\n');
4104557Seric 	if (p == NULL)
4114557Seric 		return;
41236291Sbostic 	if (p > line && p[-1] == '\r')
4134557Seric 		p--;
4144557Seric 	if (!stripnl)
4154557Seric 		*p++ = '\n';
4164557Seric 	*p = '\0';
4174557Seric }
4184557Seric /*
4196890Seric **  DFOPEN -- determined file open
4206890Seric **
4216890Seric **	This routine has the semantics of fopen, except that it will
4226890Seric **	keep trying a few times to make this happen.  The idea is that
4236890Seric **	on very loaded systems, we may run out of resources (inodes,
4246890Seric **	whatever), so this tries to get around it.
4256890Seric */
4266890Seric 
4276890Seric FILE *
4286890Seric dfopen(filename, mode)
4296890Seric 	char *filename;
4306890Seric 	char *mode;
4316890Seric {
4326890Seric 	register int tries;
4336890Seric 	register FILE *fp;
4346890Seric 
4356890Seric 	for (tries = 0; tries < 10; tries++)
4366890Seric 	{
43725618Seric 		sleep((unsigned) (10 * tries));
4386890Seric 		errno = 0;
4396890Seric 		fp = fopen(filename, mode);
4409376Seric 		if (fp != NULL)
4416890Seric 			break;
4429376Seric 		if (errno != ENFILE && errno != EINTR)
4439376Seric 			break;
4446890Seric 	}
44511936Seric 	errno = 0;
4466890Seric 	return (fp);
4476890Seric }
4487124Seric /*
4497124Seric **  PUTLINE -- put a line like fputs obeying SMTP conventions
4507124Seric **
4517753Seric **	This routine always guarantees outputing a newline (or CRLF,
4527753Seric **	as appropriate) at the end of the string.
4537753Seric **
4547124Seric **	Parameters:
4557124Seric **		l -- line to put.
4567124Seric **		fp -- file to put it onto.
45710172Seric **		m -- the mailer used to control output.
4587124Seric **
4597124Seric **	Returns:
4607124Seric **		none
4617124Seric **
4627124Seric **	Side Effects:
4637124Seric **		output of l to fp.
4647124Seric */
4657124Seric 
4667753Seric # define SMTPLINELIM	990	/* maximum line length */
4677124Seric 
46810172Seric putline(l, fp, m)
4697753Seric 	register char *l;
4707124Seric 	FILE *fp;
47110172Seric 	MAILER *m;
4727124Seric {
4737124Seric 	register char *p;
4747753Seric 	char svchar;
4757124Seric 
47611275Seric 	/* strip out 0200 bits -- these can look like TELNET protocol */
47711275Seric 	if (bitnset(M_LIMITS, m->m_flags))
47811275Seric 	{
47911275Seric 		p = l;
48011275Seric 		while ((*p++ &= ~0200) != 0)
48111275Seric 			continue;
48211275Seric 	}
48311275Seric 
4847753Seric 	do
4857124Seric 	{
4867753Seric 		/* find the end of the line */
4877753Seric 		p = index(l, '\n');
4887753Seric 		if (p == NULL)
4897753Seric 			p = &l[strlen(l)];
4907124Seric 
4917753Seric 		/* check for line overflow */
49211275Seric 		while ((p - l) > SMTPLINELIM && bitnset(M_LIMITS, m->m_flags))
4937753Seric 		{
4947753Seric 			register char *q = &l[SMTPLINELIM - 1];
4957124Seric 
4967753Seric 			svchar = *q;
4977753Seric 			*q = '\0';
49810685Seric 			if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
49923105Seric 				(void) putc('.', fp);
5007753Seric 			fputs(l, fp);
50123105Seric 			(void) putc('!', fp);
50210326Seric 			fputs(m->m_eol, fp);
5037753Seric 			*q = svchar;
5047753Seric 			l = q;
5057753Seric 		}
5067124Seric 
5077753Seric 		/* output last part */
5087753Seric 		svchar = *p;
5097753Seric 		*p = '\0';
51010685Seric 		if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
51123105Seric 			(void) putc('.', fp);
5127124Seric 		fputs(l, fp);
51310326Seric 		fputs(m->m_eol, fp);
5147753Seric 		*p = svchar;
5157753Seric 		l = p;
5167753Seric 		if (*l == '\n')
5177753Seric 			l++;
5187753Seric 	} while (l[0] != '\0');
5197124Seric }
5207676Seric /*
5217676Seric **  XUNLINK -- unlink a file, doing logging as appropriate.
5227676Seric **
5237676Seric **	Parameters:
5247676Seric **		f -- name of file to unlink.
5257676Seric **
5267676Seric **	Returns:
5277676Seric **		none.
5287676Seric **
5297676Seric **	Side Effects:
5307676Seric **		f is unlinked.
5317676Seric */
5327676Seric 
5337676Seric xunlink(f)
5347676Seric 	char *f;
5357676Seric {
5367676Seric 	register int i;
5377676Seric 
5387676Seric # ifdef LOG
5397676Seric 	if (LogLevel > 20)
5407812Seric 		syslog(LOG_DEBUG, "%s: unlink %s\n", CurEnv->e_id, f);
5417676Seric # endif LOG
5427676Seric 
5437676Seric 	i = unlink(f);
5447676Seric # ifdef LOG
5457676Seric 	if (i < 0 && LogLevel > 21)
5467942Seric 		syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno);
5477676Seric # endif LOG
5487676Seric }
5497685Seric /*
55014885Seric **  SFGETS -- "safe" fgets -- times out and ignores random interrupts.
5517685Seric **
5527685Seric **	Parameters:
5537685Seric **		buf -- place to put the input line.
5547685Seric **		siz -- size of buf.
5557685Seric **		fp -- file to read from.
5567685Seric **
5577685Seric **	Returns:
55815533Seric **		NULL on error (including timeout).  This will also leave
55915533Seric **			buf containing a null string.
5607685Seric **		buf otherwise.
5617685Seric **
5627685Seric **	Side Effects:
5637685Seric **		none.
5647685Seric */
5657685Seric 
56614885Seric static jmp_buf	CtxReadTimeout;
5677685Seric 
5687685Seric char *
5697685Seric sfgets(buf, siz, fp)
5707685Seric 	char *buf;
5717685Seric 	int siz;
5727685Seric 	FILE *fp;
5737685Seric {
5747942Seric 	register EVENT *ev = NULL;
5757685Seric 	register char *p;
5767685Seric 	extern readtimeout();
5777685Seric 
57814885Seric 	/* set the timeout */
5797942Seric 	if (ReadTimeout != 0)
58014885Seric 	{
58114885Seric 		if (setjmp(CtxReadTimeout) != 0)
58214885Seric 		{
58336233Skarels # ifdef LOG
58436230Skarels 			syslog(LOG_NOTICE,
58536230Skarels 			    "timeout waiting for input from %s\n",
58640998Sbostic 			    RealHostName? RealHostName: "local");
58736233Skarels # endif
58836230Skarels 			errno = 0;
58940964Sbostic 			usrerr("451 timeout waiting for input");
59019037Seric 			buf[0] = '\0';
59114885Seric 			return (NULL);
59214885Seric 		}
59316138Seric 		ev = setevent((time_t) ReadTimeout, readtimeout, 0);
59414885Seric 	}
59514885Seric 
59614885Seric 	/* try to read */
59715533Seric 	p = NULL;
59815533Seric 	while (p == NULL && !feof(fp) && !ferror(fp))
5997942Seric 	{
6007942Seric 		errno = 0;
6017942Seric 		p = fgets(buf, siz, fp);
60215533Seric 		if (errno == EINTR)
60315533Seric 			clearerr(fp);
60415533Seric 	}
60514885Seric 
60614885Seric 	/* clear the event if it has not sprung */
6077685Seric 	clrevent(ev);
60814885Seric 
60914885Seric 	/* clean up the books and exit */
6108055Seric 	LineNumber++;
61115533Seric 	if (p == NULL)
61216880Seric 	{
61315533Seric 		buf[0] = '\0';
61416880Seric 		return (NULL);
61516880Seric 	}
61616880Seric 	for (p = buf; *p != '\0'; p++)
61716880Seric 		*p &= ~0200;
61816880Seric 	return (buf);
6197685Seric }
6207685Seric 
6217685Seric static
6227685Seric readtimeout()
6237685Seric {
62414885Seric 	longjmp(CtxReadTimeout, 1);
6257685Seric }
6267786Seric /*
6277786Seric **  FGETFOLDED -- like fgets, but know about folded lines.
6287786Seric **
6297786Seric **	Parameters:
6307786Seric **		buf -- place to put result.
6317786Seric **		n -- bytes available.
6327786Seric **		f -- file to read from.
6337786Seric **
6347786Seric **	Returns:
6357786Seric **		buf on success, NULL on error or EOF.
6367786Seric **
6377786Seric **	Side Effects:
6387786Seric **		buf gets lines from f, with continuation lines (lines
6397786Seric **		with leading white space) appended.  CRLF's are mapped
6407786Seric **		into single newlines.  Any trailing NL is stripped.
6417786Seric */
6427786Seric 
6437786Seric char *
6447786Seric fgetfolded(buf, n, f)
6457786Seric 	char *buf;
6467786Seric 	register int n;
6477786Seric 	FILE *f;
6487786Seric {
6497786Seric 	register char *p = buf;
6507786Seric 	register int i;
6517786Seric 
6527786Seric 	n--;
65317350Seric 	while ((i = getc(f)) != EOF)
6547786Seric 	{
65517350Seric 		if (i == '\r')
65617350Seric 		{
65717350Seric 			i = getc(f);
65817350Seric 			if (i != '\n')
65917350Seric 			{
66017350Seric 				if (i != EOF)
66123105Seric 					(void) ungetc(i, f);
66217350Seric 				i = '\r';
66317350Seric 			}
66417350Seric 		}
66517350Seric 		if (--n > 0)
66617350Seric 			*p++ = i;
66717350Seric 		if (i == '\n')
66817350Seric 		{
66917350Seric 			LineNumber++;
67017350Seric 			i = getc(f);
67117350Seric 			if (i != EOF)
67223105Seric 				(void) ungetc(i, f);
67317350Seric 			if (i != ' ' && i != '\t')
67417350Seric 			{
67517350Seric 				*--p = '\0';
67617350Seric 				return (buf);
67717350Seric 			}
67817350Seric 		}
6797786Seric 	}
6807786Seric 	return (NULL);
6817786Seric }
6827860Seric /*
6837886Seric **  CURTIME -- return current time.
6847886Seric **
6857886Seric **	Parameters:
6867886Seric **		none.
6877886Seric **
6887886Seric **	Returns:
6897886Seric **		the current time.
6907886Seric **
6917886Seric **	Side Effects:
6927886Seric **		none.
6937886Seric */
6947886Seric 
6957886Seric time_t
6967886Seric curtime()
6977886Seric {
6987886Seric 	auto time_t t;
6997886Seric 
7007886Seric 	(void) time(&t);
7017886Seric 	return (t);
7027886Seric }
7038264Seric /*
7048264Seric **  ATOBOOL -- convert a string representation to boolean.
7058264Seric **
7068264Seric **	Defaults to "TRUE"
7078264Seric **
7088264Seric **	Parameters:
7098264Seric **		s -- string to convert.  Takes "tTyY" as true,
7108264Seric **			others as false.
7118264Seric **
7128264Seric **	Returns:
7138264Seric **		A boolean representation of the string.
7148264Seric **
7158264Seric **	Side Effects:
7168264Seric **		none.
7178264Seric */
7188264Seric 
7198264Seric bool
7208264Seric atobool(s)
7218264Seric 	register char *s;
7228264Seric {
7238264Seric 	if (*s == '\0' || index("tTyY", *s) != NULL)
7248264Seric 		return (TRUE);
7258264Seric 	return (FALSE);
7268264Seric }
7279048Seric /*
7289048Seric **  ATOOCT -- convert a string representation to octal.
7299048Seric **
7309048Seric **	Parameters:
7319048Seric **		s -- string to convert.
7329048Seric **
7339048Seric **	Returns:
7349048Seric **		An integer representing the string interpreted as an
7359048Seric **		octal number.
7369048Seric **
7379048Seric **	Side Effects:
7389048Seric **		none.
7399048Seric */
7409048Seric 
7419048Seric atooct(s)
7429048Seric 	register char *s;
7439048Seric {
7449048Seric 	register int i = 0;
7459048Seric 
7469048Seric 	while (*s >= '0' && *s <= '7')
7479048Seric 		i = (i << 3) | (*s++ - '0');
7489048Seric 	return (i);
7499048Seric }
7509376Seric /*
7519376Seric **  WAITFOR -- wait for a particular process id.
7529376Seric **
7539376Seric **	Parameters:
7549376Seric **		pid -- process id to wait for.
7559376Seric **
7569376Seric **	Returns:
7579376Seric **		status of pid.
7589376Seric **		-1 if pid never shows up.
7599376Seric **
7609376Seric **	Side Effects:
7619376Seric **		none.
7629376Seric */
7639376Seric 
7649376Seric waitfor(pid)
7659376Seric 	int pid;
7669376Seric {
7679376Seric 	auto int st;
7689376Seric 	int i;
7699376Seric 
7709376Seric 	do
7719376Seric 	{
7729376Seric 		errno = 0;
7739376Seric 		i = wait(&st);
7749376Seric 	} while ((i >= 0 || errno == EINTR) && i != pid);
7759376Seric 	if (i < 0)
7769376Seric 		st = -1;
7779376Seric 	return (st);
7789376Seric }
7799376Seric /*
78010685Seric **  BITINTERSECT -- tell if two bitmaps intersect
78110685Seric **
78210685Seric **	Parameters:
78310685Seric **		a, b -- the bitmaps in question
78410685Seric **
78510685Seric **	Returns:
78610685Seric **		TRUE if they have a non-null intersection
78710685Seric **		FALSE otherwise
78810685Seric **
78910685Seric **	Side Effects:
79010685Seric **		none.
79110685Seric */
79210685Seric 
79310685Seric bool
79410685Seric bitintersect(a, b)
79510685Seric 	BITMAP a;
79610685Seric 	BITMAP b;
79710685Seric {
79810685Seric 	int i;
79910685Seric 
80010685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
80110685Seric 		if ((a[i] & b[i]) != 0)
80210685Seric 			return (TRUE);
80310685Seric 	return (FALSE);
80410685Seric }
80510685Seric /*
80610685Seric **  BITZEROP -- tell if a bitmap is all zero
80710685Seric **
80810685Seric **	Parameters:
80910685Seric **		map -- the bit map to check
81010685Seric **
81110685Seric **	Returns:
81210685Seric **		TRUE if map is all zero.
81310685Seric **		FALSE if there are any bits set in map.
81410685Seric **
81510685Seric **	Side Effects:
81610685Seric **		none.
81710685Seric */
81810685Seric 
81910685Seric bool
82010685Seric bitzerop(map)
82110685Seric 	BITMAP map;
82210685Seric {
82310685Seric 	int i;
82410685Seric 
82510685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
82610685Seric 		if (map[i] != 0)
82710685Seric 			return (FALSE);
82810685Seric 	return (TRUE);
82910685Seric }
830