xref: /csrg-svn/usr.sbin/sendmail/src/util.c (revision 57123)
122717Sdist /*
242833Sbostic  * Copyright (c) 1983 Eric P. Allman
333731Sbostic  * Copyright (c) 1988 Regents of the University of California.
433731Sbostic  * All rights reserved.
533731Sbostic  *
642833Sbostic  * %sccs.include.redist.c%
733731Sbostic  */
822717Sdist 
922717Sdist #ifndef lint
10*57123Seric static char sccsid[] = "@(#)util.c	5.33 (Berkeley) 12/14/92";
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.
28298Seric **
29298Seric **	Returns:
30298Seric **		none.
31298Seric **
32298Seric **	Side Effects:
33298Seric **		none.
34298Seric **
35298Seric **	Called By:
36298Seric **		deliver
37298Seric */
38298Seric 
3954983Seric stripquotes(s)
40298Seric 	char *s;
41298Seric {
42298Seric 	register char *p;
43298Seric 	register char *q;
44298Seric 	register char c;
45298Seric 
464101Seric 	if (s == NULL)
474101Seric 		return;
484101Seric 
4954983Seric 	p = q = s;
5054983Seric 	do
51298Seric 	{
5254983Seric 		c = *p++;
5354983Seric 		if (c == '\\')
5454983Seric 			c = *p++;
5554983Seric 		else if (c == '"')
5654983Seric 			continue;
5754983Seric 		*q++ = c;
5854983Seric 	} while (c != '\0');
59298Seric }
60298Seric /*
612900Seric **  CAPITALIZE -- return a copy of a string, properly capitalized.
622900Seric **
632900Seric **	Parameters:
642900Seric **		s -- the string to capitalize.
652900Seric **
662900Seric **	Returns:
672900Seric **		a pointer to a properly capitalized string.
682900Seric **
692900Seric **	Side Effects:
702900Seric **		none.
712900Seric */
722900Seric 
732900Seric char *
742900Seric capitalize(s)
752900Seric 	register char *s;
762900Seric {
772900Seric 	static char buf[50];
782900Seric 	register char *p;
792900Seric 
802900Seric 	p = buf;
812900Seric 
822900Seric 	for (;;)
832900Seric 	{
842900Seric 		while (!isalpha(*s) && *s != '\0')
852900Seric 			*p++ = *s++;
862900Seric 		if (*s == '\0')
872900Seric 			break;
8840999Sbostic 		*p++ = toupper(*s);
8940999Sbostic 		s++;
902900Seric 		while (isalpha(*s))
912900Seric 			*p++ = *s++;
922900Seric 	}
932900Seric 
942900Seric 	*p = '\0';
952900Seric 	return (buf);
962900Seric }
972900Seric /*
98298Seric **  XALLOC -- Allocate memory and bitch wildly on failure.
99298Seric **
100298Seric **	THIS IS A CLUDGE.  This should be made to give a proper
101298Seric **	error -- but after all, what can we do?
102298Seric **
103298Seric **	Parameters:
104298Seric **		sz -- size of area to allocate.
105298Seric **
106298Seric **	Returns:
107298Seric **		pointer to data region.
108298Seric **
109298Seric **	Side Effects:
110298Seric **		Memory is allocated.
111298Seric */
112298Seric 
113298Seric char *
114298Seric xalloc(sz)
1157007Seric 	register int sz;
116298Seric {
117298Seric 	register char *p;
118298Seric 
11923121Seric 	p = malloc((unsigned) sz);
120298Seric 	if (p == NULL)
121298Seric 	{
122298Seric 		syserr("Out of memory!!");
12310685Seric 		abort();
12410685Seric 		/* exit(EX_UNAVAILABLE); */
125298Seric 	}
126298Seric 	return (p);
127298Seric }
128298Seric /*
1293151Seric **  COPYPLIST -- copy list of pointers.
1303151Seric **
1313151Seric **	This routine is the equivalent of newstr for lists of
1323151Seric **	pointers.
1333151Seric **
1343151Seric **	Parameters:
1353151Seric **		list -- list of pointers to copy.
1363151Seric **			Must be NULL terminated.
1373151Seric **		copycont -- if TRUE, copy the contents of the vector
1383151Seric **			(which must be a string) also.
1393151Seric **
1403151Seric **	Returns:
1413151Seric **		a copy of 'list'.
1423151Seric **
1433151Seric **	Side Effects:
1443151Seric **		none.
1453151Seric */
1463151Seric 
1473151Seric char **
1483151Seric copyplist(list, copycont)
1493151Seric 	char **list;
1503151Seric 	bool copycont;
1513151Seric {
1523151Seric 	register char **vp;
1533151Seric 	register char **newvp;
1543151Seric 
1553151Seric 	for (vp = list; *vp != NULL; vp++)
1563151Seric 		continue;
1573151Seric 
1583151Seric 	vp++;
1593151Seric 
16016897Seric 	newvp = (char **) xalloc((int) (vp - list) * sizeof *vp);
16116897Seric 	bcopy((char *) list, (char *) newvp, (int) (vp - list) * sizeof *vp);
1623151Seric 
1633151Seric 	if (copycont)
1643151Seric 	{
1653151Seric 		for (vp = newvp; *vp != NULL; vp++)
1663151Seric 			*vp = newstr(*vp);
1673151Seric 	}
1683151Seric 
1693151Seric 	return (newvp);
1703151Seric }
1713151Seric /*
1723151Seric **  PRINTAV -- print argument vector.
1733151Seric **
1743151Seric **	Parameters:
1753151Seric **		av -- argument vector.
1763151Seric **
1773151Seric **	Returns:
1783151Seric **		none.
1793151Seric **
1803151Seric **	Side Effects:
1813151Seric **		prints av.
1823151Seric */
1833151Seric 
1843151Seric printav(av)
1853151Seric 	register char **av;
1863151Seric {
1873151Seric 	while (*av != NULL)
1883151Seric 	{
1898063Seric 		if (tTd(0, 44))
1908063Seric 			printf("\n\t%08x=", *av);
1918063Seric 		else
19223105Seric 			(void) putchar(' ');
1933151Seric 		xputs(*av++);
1943151Seric 	}
19523105Seric 	(void) putchar('\n');
1963151Seric }
1973151Seric /*
1983151Seric **  LOWER -- turn letter into lower case.
1993151Seric **
2003151Seric **	Parameters:
2013151Seric **		c -- character to turn into lower case.
2023151Seric **
2033151Seric **	Returns:
2043151Seric **		c, in lower case.
2053151Seric **
2063151Seric **	Side Effects:
2073151Seric **		none.
2083151Seric */
2093151Seric 
2103151Seric char
2113151Seric lower(c)
2123151Seric 	register char c;
2133151Seric {
21433724Sbostic 	return(isascii(c) && isupper(c) ? tolower(c) : c);
2153151Seric }
2163151Seric /*
2173151Seric **  XPUTS -- put string doing control escapes.
2183151Seric **
2193151Seric **	Parameters:
2203151Seric **		s -- string to put.
2213151Seric **
2223151Seric **	Returns:
2233151Seric **		none.
2243151Seric **
2253151Seric **	Side Effects:
2263151Seric **		output to stdout
2273151Seric */
2283151Seric 
2293151Seric xputs(s)
2303151Seric 	register char *s;
2313151Seric {
2323151Seric 	register char c;
23351781Seric 	register struct metamac *mp;
23451781Seric 	extern struct metamac MetaMacros[];
2353151Seric 
2368055Seric 	if (s == NULL)
2378055Seric 	{
2388055Seric 		printf("<null>");
2398055Seric 		return;
2408055Seric 	}
24151781Seric 	c = *s;
24251781Seric 	if (c == MATCHREPL && isdigit(s[1]) && s[2] == '\0')
24351781Seric 	{
24451781Seric 		printf("$%c", s[1]);
24551781Seric 		return;
24651781Seric 	}
24751781Seric 	for (mp = MetaMacros; mp->metaname != NULL; mp++)
24851781Seric 	{
24951781Seric 		if (mp->metaval == c)
25051781Seric 		{
25151781Seric 			printf("$%c%s", mp->metaname, ++s);
25251781Seric 			return;
25351781Seric 		}
25451781Seric 	}
25523105Seric 	(void) putchar('"');
2563151Seric 	while ((c = *s++) != '\0')
2573151Seric 	{
2583151Seric 		if (!isascii(c))
2593151Seric 		{
26023105Seric 			(void) putchar('\\');
2613151Seric 			c &= 0177;
2623151Seric 		}
26310326Seric 		if (c < 040 || c >= 0177)
2643151Seric 		{
26552050Seric 			switch (c)
26652050Seric 			{
26752050Seric 			  case '\n':
26852050Seric 				c = 'n';
26952050Seric 				break;
27052050Seric 
27152050Seric 			  case '\r':
27252050Seric 				c = 'r';
27352050Seric 				break;
27452050Seric 
27552050Seric 			  case '\t':
27652050Seric 				c = 't';
27752050Seric 				break;
27852050Seric 
27952637Seric 			  case '\001':
28052637Seric 				(void) putchar('$');
28152637Seric 				continue;
28252637Seric 
28352050Seric 			  default:
28452050Seric 				(void) putchar('^');
28552050Seric 				(void) putchar(c ^ 0100);
28652050Seric 				continue;
28752050Seric 			}
28852050Seric 			(void) putchar('\\');
2893151Seric 		}
29023105Seric 		(void) putchar(c);
2913151Seric 	}
29223105Seric 	(void) putchar('"');
2934086Seric 	(void) fflush(stdout);
2943151Seric }
2953151Seric /*
2963151Seric **  MAKELOWER -- Translate a line into lower case
2973151Seric **
2983151Seric **	Parameters:
2993151Seric **		p -- the string to translate.  If NULL, return is
3003151Seric **			immediate.
3013151Seric **
3023151Seric **	Returns:
3033151Seric **		none.
3043151Seric **
3053151Seric **	Side Effects:
3063151Seric **		String pointed to by p is translated to lower case.
3073151Seric **
3083151Seric **	Called By:
3093151Seric **		parse
3103151Seric */
3113151Seric 
3123151Seric makelower(p)
3133151Seric 	register char *p;
3143151Seric {
3153151Seric 	register char c;
3163151Seric 
3173151Seric 	if (p == NULL)
3183151Seric 		return;
3193151Seric 	for (; (c = *p) != '\0'; p++)
3203151Seric 		if (isascii(c) && isupper(c))
32133724Sbostic 			*p = tolower(c);
3223151Seric }
3234059Seric /*
3245196Seric **  BUILDFNAME -- build full name from gecos style entry.
3254375Seric **
3265196Seric **	This routine interprets the strange entry that would appear
3275196Seric **	in the GECOS field of the password file.
3285196Seric **
3294375Seric **	Parameters:
3305196Seric **		p -- name to build.
3315196Seric **		login -- the login name of this user (for &).
3325196Seric **		buf -- place to put the result.
3334375Seric **
3344375Seric **	Returns:
3354375Seric **		none.
3364375Seric **
3374375Seric **	Side Effects:
3384375Seric **		none.
3394375Seric */
3404375Seric 
34154984Seric buildfname(gecos, login, buf)
34254984Seric 	register char *gecos;
3435196Seric 	char *login;
3444375Seric 	char *buf;
3454375Seric {
34654984Seric 	register char *p;
3474375Seric 	register char *bp = buf;
34854984Seric 	int l;
3494375Seric 
35054984Seric 	if (*gecos == '*')
35154984Seric 		gecos++;
35254984Seric 
353*57123Seric 	/* find length of final string */
35454984Seric 	l = 0;
35554984Seric 	for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
35654984Seric 	{
35754984Seric 		if (*p == '&')
35854984Seric 			l += strlen(login);
35954984Seric 		else
36054984Seric 			l++;
36154984Seric 	}
36254984Seric 
36354984Seric 	/* now fill in buf */
36455193Seric 	for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
3654375Seric 	{
3664375Seric 		if (*p == '&')
3674375Seric 		{
3685196Seric 			(void) strcpy(bp, login);
3694375Seric 			*bp = toupper(*bp);
3704375Seric 			while (*bp != '\0')
3714375Seric 				bp++;
3724375Seric 		}
3734375Seric 		else
37455193Seric 			*bp++ = *p;
3754375Seric 	}
3764375Seric 	*bp = '\0';
3774375Seric }
3784375Seric /*
3794538Seric **  SAFEFILE -- return true if a file exists and is safe for a user.
3804538Seric **
3814538Seric **	Parameters:
3824538Seric **		fn -- filename to check.
3834538Seric **		uid -- uid to compare against.
3844538Seric **		mode -- mode bits that must match.
3854538Seric **
3864538Seric **	Returns:
3874538Seric **		TRUE if fn exists, is owned by uid, and matches mode.
3884538Seric **		FALSE otherwise.
3894538Seric **
3904538Seric **	Side Effects:
3914538Seric **		none.
3924538Seric */
3934538Seric 
3944538Seric bool
3954538Seric safefile(fn, uid, mode)
3964538Seric 	char *fn;
39755372Seric 	uid_t uid;
3984538Seric 	int mode;
3994538Seric {
4004538Seric 	struct stat stbuf;
4014538Seric 
4024538Seric 	if (stat(fn, &stbuf) >= 0 && stbuf.st_uid == uid &&
4034538Seric 	    (stbuf.st_mode & mode) == mode)
4044538Seric 		return (TRUE);
40511936Seric 	errno = 0;
4064538Seric 	return (FALSE);
4074538Seric }
4084538Seric /*
4094557Seric **  FIXCRLF -- fix <CR><LF> in line.
4104557Seric **
4114557Seric **	Looks for the <CR><LF> combination and turns it into the
4124557Seric **	UNIX canonical <NL> character.  It only takes one line,
4134557Seric **	i.e., it is assumed that the first <NL> found is the end
4144557Seric **	of the line.
4154557Seric **
4164557Seric **	Parameters:
4174557Seric **		line -- the line to fix.
4184557Seric **		stripnl -- if true, strip the newline also.
4194557Seric **
4204557Seric **	Returns:
4214557Seric **		none.
4224557Seric **
4234557Seric **	Side Effects:
4244557Seric **		line is changed in place.
4254557Seric */
4264557Seric 
4274557Seric fixcrlf(line, stripnl)
4284557Seric 	char *line;
4294557Seric 	bool stripnl;
4304557Seric {
4314557Seric 	register char *p;
4324557Seric 
43356795Seric 	p = strchr(line, '\n');
4344557Seric 	if (p == NULL)
4354557Seric 		return;
43636291Sbostic 	if (p > line && p[-1] == '\r')
4374557Seric 		p--;
4384557Seric 	if (!stripnl)
4394557Seric 		*p++ = '\n';
4404557Seric 	*p = '\0';
4414557Seric }
4424557Seric /*
4436890Seric **  DFOPEN -- determined file open
4446890Seric **
4456890Seric **	This routine has the semantics of fopen, except that it will
4466890Seric **	keep trying a few times to make this happen.  The idea is that
4476890Seric **	on very loaded systems, we may run out of resources (inodes,
4486890Seric **	whatever), so this tries to get around it.
4496890Seric */
4506890Seric 
4516890Seric FILE *
4526890Seric dfopen(filename, mode)
4536890Seric 	char *filename;
4546890Seric 	char *mode;
4556890Seric {
4566890Seric 	register int tries;
4576890Seric 	register FILE *fp;
4586890Seric 
4596890Seric 	for (tries = 0; tries < 10; tries++)
4606890Seric 	{
46125618Seric 		sleep((unsigned) (10 * tries));
4626890Seric 		errno = 0;
4636890Seric 		fp = fopen(filename, mode);
4649376Seric 		if (fp != NULL)
4656890Seric 			break;
4669376Seric 		if (errno != ENFILE && errno != EINTR)
4679376Seric 			break;
4686890Seric 	}
46956328Seric 	if (fp != NULL)
47056328Seric 	{
47156328Seric #ifdef FLOCK
47256328Seric 		int locktype;
47356328Seric 
47456328Seric 		/* lock the file to avoid accidental conflicts */
47556328Seric 		if (*mode == 'w' || *mode == 'a')
47656328Seric 			locktype = LOCK_EX;
47756328Seric 		else
47856328Seric 			locktype = LOCK_SH;
47956328Seric 		(void) flock(fileno(fp), locktype);
48056328Seric #endif
48156328Seric 		errno = 0;
48256328Seric 	}
4836890Seric 	return (fp);
4846890Seric }
4857124Seric /*
4867124Seric **  PUTLINE -- put a line like fputs obeying SMTP conventions
4877124Seric **
4887753Seric **	This routine always guarantees outputing a newline (or CRLF,
4897753Seric **	as appropriate) at the end of the string.
4907753Seric **
4917124Seric **	Parameters:
4927124Seric **		l -- line to put.
4937124Seric **		fp -- file to put it onto.
49410172Seric **		m -- the mailer used to control output.
4957124Seric **
4967124Seric **	Returns:
4977124Seric **		none
4987124Seric **
4997124Seric **	Side Effects:
5007124Seric **		output of l to fp.
5017124Seric */
5027124Seric 
50310172Seric putline(l, fp, m)
5047753Seric 	register char *l;
5057124Seric 	FILE *fp;
50610172Seric 	MAILER *m;
5077124Seric {
5087124Seric 	register char *p;
50947157Sbostic 	register char svchar;
5107124Seric 
51111275Seric 	/* strip out 0200 bits -- these can look like TELNET protocol */
51252106Seric 	if (bitnset(M_7BITS, m->m_flags))
51311275Seric 	{
51447157Sbostic 		for (p = l; svchar = *p; ++p)
51547157Sbostic 			if (svchar & 0200)
51647157Sbostic 				*p = svchar &~ 0200;
51711275Seric 	}
51811275Seric 
5197753Seric 	do
5207124Seric 	{
5217753Seric 		/* find the end of the line */
52256795Seric 		p = strchr(l, '\n');
5237753Seric 		if (p == NULL)
5247753Seric 			p = &l[strlen(l)];
5257124Seric 
5267753Seric 		/* check for line overflow */
52752106Seric 		while (m->m_linelimit > 0 && (p - l) > m->m_linelimit)
5287753Seric 		{
52952106Seric 			register char *q = &l[m->m_linelimit - 1];
5307124Seric 
5317753Seric 			svchar = *q;
5327753Seric 			*q = '\0';
53310685Seric 			if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
53423105Seric 				(void) putc('.', fp);
5357753Seric 			fputs(l, fp);
53623105Seric 			(void) putc('!', fp);
53710326Seric 			fputs(m->m_eol, fp);
5387753Seric 			*q = svchar;
5397753Seric 			l = q;
5407753Seric 		}
5417124Seric 
5427753Seric 		/* output last part */
54310685Seric 		if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
54423105Seric 			(void) putc('.', fp);
54547157Sbostic 		for ( ; l < p; ++l)
54647157Sbostic 			(void) putc(*l, fp);
54710326Seric 		fputs(m->m_eol, fp);
5487753Seric 		if (*l == '\n')
54947157Sbostic 			++l;
5507753Seric 	} while (l[0] != '\0');
5517124Seric }
5527676Seric /*
5537676Seric **  XUNLINK -- unlink a file, doing logging as appropriate.
5547676Seric **
5557676Seric **	Parameters:
5567676Seric **		f -- name of file to unlink.
5577676Seric **
5587676Seric **	Returns:
5597676Seric **		none.
5607676Seric **
5617676Seric **	Side Effects:
5627676Seric **		f is unlinked.
5637676Seric */
5647676Seric 
5657676Seric xunlink(f)
5667676Seric 	char *f;
5677676Seric {
5687676Seric 	register int i;
5697676Seric 
5707676Seric # ifdef LOG
5717676Seric 	if (LogLevel > 20)
5727812Seric 		syslog(LOG_DEBUG, "%s: unlink %s\n", CurEnv->e_id, f);
57356795Seric # endif /* LOG */
5747676Seric 
5757676Seric 	i = unlink(f);
5767676Seric # ifdef LOG
5777676Seric 	if (i < 0 && LogLevel > 21)
5787942Seric 		syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno);
57956795Seric # endif /* LOG */
5807676Seric }
5817685Seric /*
58214885Seric **  SFGETS -- "safe" fgets -- times out and ignores random interrupts.
5837685Seric **
5847685Seric **	Parameters:
5857685Seric **		buf -- place to put the input line.
5867685Seric **		siz -- size of buf.
5877685Seric **		fp -- file to read from.
5887685Seric **
5897685Seric **	Returns:
59015533Seric **		NULL on error (including timeout).  This will also leave
59115533Seric **			buf containing a null string.
5927685Seric **		buf otherwise.
5937685Seric **
5947685Seric **	Side Effects:
5957685Seric **		none.
5967685Seric */
5977685Seric 
59814885Seric static jmp_buf	CtxReadTimeout;
5997685Seric 
6007685Seric char *
6017685Seric sfgets(buf, siz, fp)
6027685Seric 	char *buf;
6037685Seric 	int siz;
6047685Seric 	FILE *fp;
6057685Seric {
6067942Seric 	register EVENT *ev = NULL;
6077685Seric 	register char *p;
60846928Sbostic 	static int readtimeout();
6097685Seric 
61014885Seric 	/* set the timeout */
6117942Seric 	if (ReadTimeout != 0)
61214885Seric 	{
61314885Seric 		if (setjmp(CtxReadTimeout) != 0)
61414885Seric 		{
61536233Skarels # ifdef LOG
61636230Skarels 			syslog(LOG_NOTICE,
61736230Skarels 			    "timeout waiting for input from %s\n",
61840998Sbostic 			    RealHostName? RealHostName: "local");
61936233Skarels # endif
62036230Skarels 			errno = 0;
62140964Sbostic 			usrerr("451 timeout waiting for input");
62219037Seric 			buf[0] = '\0';
62314885Seric 			return (NULL);
62414885Seric 		}
62516138Seric 		ev = setevent((time_t) ReadTimeout, readtimeout, 0);
62614885Seric 	}
62714885Seric 
62814885Seric 	/* try to read */
62915533Seric 	p = NULL;
63015533Seric 	while (p == NULL && !feof(fp) && !ferror(fp))
6317942Seric 	{
6327942Seric 		errno = 0;
6337942Seric 		p = fgets(buf, siz, fp);
63415533Seric 		if (errno == EINTR)
63515533Seric 			clearerr(fp);
63615533Seric 	}
63714885Seric 
63814885Seric 	/* clear the event if it has not sprung */
6397685Seric 	clrevent(ev);
64014885Seric 
64114885Seric 	/* clean up the books and exit */
6428055Seric 	LineNumber++;
64315533Seric 	if (p == NULL)
64416880Seric 	{
64515533Seric 		buf[0] = '\0';
64616880Seric 		return (NULL);
64716880Seric 	}
64852106Seric 	if (!EightBit)
64952106Seric 		for (p = buf; *p != '\0'; p++)
65052106Seric 			*p &= ~0200;
65116880Seric 	return (buf);
6527685Seric }
6537685Seric 
6547685Seric static
6557685Seric readtimeout()
6567685Seric {
65714885Seric 	longjmp(CtxReadTimeout, 1);
6587685Seric }
6597786Seric /*
6607786Seric **  FGETFOLDED -- like fgets, but know about folded lines.
6617786Seric **
6627786Seric **	Parameters:
6637786Seric **		buf -- place to put result.
6647786Seric **		n -- bytes available.
6657786Seric **		f -- file to read from.
6667786Seric **
6677786Seric **	Returns:
6687786Seric **		buf on success, NULL on error or EOF.
6697786Seric **
6707786Seric **	Side Effects:
6717786Seric **		buf gets lines from f, with continuation lines (lines
6727786Seric **		with leading white space) appended.  CRLF's are mapped
6737786Seric **		into single newlines.  Any trailing NL is stripped.
6747786Seric */
6757786Seric 
6767786Seric char *
6777786Seric fgetfolded(buf, n, f)
6787786Seric 	char *buf;
6797786Seric 	register int n;
6807786Seric 	FILE *f;
6817786Seric {
6827786Seric 	register char *p = buf;
6837786Seric 	register int i;
6847786Seric 
6857786Seric 	n--;
68617350Seric 	while ((i = getc(f)) != EOF)
6877786Seric 	{
68817350Seric 		if (i == '\r')
68917350Seric 		{
69017350Seric 			i = getc(f);
69117350Seric 			if (i != '\n')
69217350Seric 			{
69317350Seric 				if (i != EOF)
69423105Seric 					(void) ungetc(i, f);
69517350Seric 				i = '\r';
69617350Seric 			}
69717350Seric 		}
69817350Seric 		if (--n > 0)
69917350Seric 			*p++ = i;
70052647Seric 		else if (n == 0)
70152647Seric 			nmessage(Arpa_Info, "warning: line truncated");
70217350Seric 		if (i == '\n')
70317350Seric 		{
70417350Seric 			LineNumber++;
70517350Seric 			i = getc(f);
70617350Seric 			if (i != EOF)
70723105Seric 				(void) ungetc(i, f);
70817350Seric 			if (i != ' ' && i != '\t')
70952647Seric 				break;
71017350Seric 		}
7117786Seric 	}
71252647Seric 	if (p == buf)
71352647Seric 		return (NULL);
71452647Seric 	*--p = '\0';
71552647Seric 	return (buf);
7167786Seric }
7177860Seric /*
7187886Seric **  CURTIME -- return current time.
7197886Seric **
7207886Seric **	Parameters:
7217886Seric **		none.
7227886Seric **
7237886Seric **	Returns:
7247886Seric **		the current time.
7257886Seric **
7267886Seric **	Side Effects:
7277886Seric **		none.
7287886Seric */
7297886Seric 
7307886Seric time_t
7317886Seric curtime()
7327886Seric {
7337886Seric 	auto time_t t;
7347886Seric 
7357886Seric 	(void) time(&t);
7367886Seric 	return (t);
7377886Seric }
7388264Seric /*
7398264Seric **  ATOBOOL -- convert a string representation to boolean.
7408264Seric **
7418264Seric **	Defaults to "TRUE"
7428264Seric **
7438264Seric **	Parameters:
7448264Seric **		s -- string to convert.  Takes "tTyY" as true,
7458264Seric **			others as false.
7468264Seric **
7478264Seric **	Returns:
7488264Seric **		A boolean representation of the string.
7498264Seric **
7508264Seric **	Side Effects:
7518264Seric **		none.
7528264Seric */
7538264Seric 
7548264Seric bool
7558264Seric atobool(s)
7568264Seric 	register char *s;
7578264Seric {
75856795Seric 	if (*s == '\0' || strchr("tTyY", *s) != NULL)
7598264Seric 		return (TRUE);
7608264Seric 	return (FALSE);
7618264Seric }
7629048Seric /*
7639048Seric **  ATOOCT -- convert a string representation to octal.
7649048Seric **
7659048Seric **	Parameters:
7669048Seric **		s -- string to convert.
7679048Seric **
7689048Seric **	Returns:
7699048Seric **		An integer representing the string interpreted as an
7709048Seric **		octal number.
7719048Seric **
7729048Seric **	Side Effects:
7739048Seric **		none.
7749048Seric */
7759048Seric 
7769048Seric atooct(s)
7779048Seric 	register char *s;
7789048Seric {
7799048Seric 	register int i = 0;
7809048Seric 
7819048Seric 	while (*s >= '0' && *s <= '7')
7829048Seric 		i = (i << 3) | (*s++ - '0');
7839048Seric 	return (i);
7849048Seric }
7859376Seric /*
7869376Seric **  WAITFOR -- wait for a particular process id.
7879376Seric **
7889376Seric **	Parameters:
7899376Seric **		pid -- process id to wait for.
7909376Seric **
7919376Seric **	Returns:
7929376Seric **		status of pid.
7939376Seric **		-1 if pid never shows up.
7949376Seric **
7959376Seric **	Side Effects:
7969376Seric **		none.
7979376Seric */
7989376Seric 
7999376Seric waitfor(pid)
8009376Seric 	int pid;
8019376Seric {
8029376Seric 	auto int st;
8039376Seric 	int i;
8049376Seric 
8059376Seric 	do
8069376Seric 	{
8079376Seric 		errno = 0;
8089376Seric 		i = wait(&st);
8099376Seric 	} while ((i >= 0 || errno == EINTR) && i != pid);
8109376Seric 	if (i < 0)
8119376Seric 		st = -1;
8129376Seric 	return (st);
8139376Seric }
8149376Seric /*
81510685Seric **  BITINTERSECT -- tell if two bitmaps intersect
81610685Seric **
81710685Seric **	Parameters:
81810685Seric **		a, b -- the bitmaps in question
81910685Seric **
82010685Seric **	Returns:
82110685Seric **		TRUE if they have a non-null intersection
82210685Seric **		FALSE otherwise
82310685Seric **
82410685Seric **	Side Effects:
82510685Seric **		none.
82610685Seric */
82710685Seric 
82810685Seric bool
82910685Seric bitintersect(a, b)
83010685Seric 	BITMAP a;
83110685Seric 	BITMAP b;
83210685Seric {
83310685Seric 	int i;
83410685Seric 
83510685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
83610685Seric 		if ((a[i] & b[i]) != 0)
83710685Seric 			return (TRUE);
83810685Seric 	return (FALSE);
83910685Seric }
84010685Seric /*
84110685Seric **  BITZEROP -- tell if a bitmap is all zero
84210685Seric **
84310685Seric **	Parameters:
84410685Seric **		map -- the bit map to check
84510685Seric **
84610685Seric **	Returns:
84710685Seric **		TRUE if map is all zero.
84810685Seric **		FALSE if there are any bits set in map.
84910685Seric **
85010685Seric **	Side Effects:
85110685Seric **		none.
85210685Seric */
85310685Seric 
85410685Seric bool
85510685Seric bitzerop(map)
85610685Seric 	BITMAP map;
85710685Seric {
85810685Seric 	int i;
85910685Seric 
86010685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
86110685Seric 		if (map[i] != 0)
86210685Seric 			return (FALSE);
86310685Seric 	return (TRUE);
86410685Seric }
865