xref: /csrg-svn/usr.sbin/sendmail/src/util.c (revision 55193)
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*55193Seric static char sccsid[] = "@(#)util.c	5.28 (Berkeley) 07/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;
11823121Seric 	extern char *malloc();
119298Seric 
12023121Seric 	p = malloc((unsigned) sz);
121298Seric 	if (p == NULL)
122298Seric 	{
123298Seric 		syserr("Out of memory!!");
12410685Seric 		abort();
12510685Seric 		/* exit(EX_UNAVAILABLE); */
126298Seric 	}
127298Seric 	return (p);
128298Seric }
129298Seric /*
1303151Seric **  COPYPLIST -- copy list of pointers.
1313151Seric **
1323151Seric **	This routine is the equivalent of newstr for lists of
1333151Seric **	pointers.
1343151Seric **
1353151Seric **	Parameters:
1363151Seric **		list -- list of pointers to copy.
1373151Seric **			Must be NULL terminated.
1383151Seric **		copycont -- if TRUE, copy the contents of the vector
1393151Seric **			(which must be a string) also.
1403151Seric **
1413151Seric **	Returns:
1423151Seric **		a copy of 'list'.
1433151Seric **
1443151Seric **	Side Effects:
1453151Seric **		none.
1463151Seric */
1473151Seric 
1483151Seric char **
1493151Seric copyplist(list, copycont)
1503151Seric 	char **list;
1513151Seric 	bool copycont;
1523151Seric {
1533151Seric 	register char **vp;
1543151Seric 	register char **newvp;
1553151Seric 
1563151Seric 	for (vp = list; *vp != NULL; vp++)
1573151Seric 		continue;
1583151Seric 
1593151Seric 	vp++;
1603151Seric 
16116897Seric 	newvp = (char **) xalloc((int) (vp - list) * sizeof *vp);
16216897Seric 	bcopy((char *) list, (char *) newvp, (int) (vp - list) * sizeof *vp);
1633151Seric 
1643151Seric 	if (copycont)
1653151Seric 	{
1663151Seric 		for (vp = newvp; *vp != NULL; vp++)
1673151Seric 			*vp = newstr(*vp);
1683151Seric 	}
1693151Seric 
1703151Seric 	return (newvp);
1713151Seric }
1723151Seric /*
1733151Seric **  PRINTAV -- print argument vector.
1743151Seric **
1753151Seric **	Parameters:
1763151Seric **		av -- argument vector.
1773151Seric **
1783151Seric **	Returns:
1793151Seric **		none.
1803151Seric **
1813151Seric **	Side Effects:
1823151Seric **		prints av.
1833151Seric */
1843151Seric 
1853151Seric printav(av)
1863151Seric 	register char **av;
1873151Seric {
1883151Seric 	while (*av != NULL)
1893151Seric 	{
1908063Seric 		if (tTd(0, 44))
1918063Seric 			printf("\n\t%08x=", *av);
1928063Seric 		else
19323105Seric 			(void) putchar(' ');
1943151Seric 		xputs(*av++);
1953151Seric 	}
19623105Seric 	(void) putchar('\n');
1973151Seric }
1983151Seric /*
1993151Seric **  LOWER -- turn letter into lower case.
2003151Seric **
2013151Seric **	Parameters:
2023151Seric **		c -- character to turn into lower case.
2033151Seric **
2043151Seric **	Returns:
2053151Seric **		c, in lower case.
2063151Seric **
2073151Seric **	Side Effects:
2083151Seric **		none.
2093151Seric */
2103151Seric 
2113151Seric char
2123151Seric lower(c)
2133151Seric 	register char c;
2143151Seric {
21533724Sbostic 	return(isascii(c) && isupper(c) ? tolower(c) : c);
2163151Seric }
2173151Seric /*
2183151Seric **  XPUTS -- put string doing control escapes.
2193151Seric **
2203151Seric **	Parameters:
2213151Seric **		s -- string to put.
2223151Seric **
2233151Seric **	Returns:
2243151Seric **		none.
2253151Seric **
2263151Seric **	Side Effects:
2273151Seric **		output to stdout
2283151Seric */
2293151Seric 
2303151Seric xputs(s)
2313151Seric 	register char *s;
2323151Seric {
2333151Seric 	register char c;
23451781Seric 	register struct metamac *mp;
23551781Seric 	extern struct metamac MetaMacros[];
2363151Seric 
2378055Seric 	if (s == NULL)
2388055Seric 	{
2398055Seric 		printf("<null>");
2408055Seric 		return;
2418055Seric 	}
24251781Seric 	c = *s;
24351781Seric 	if (c == MATCHREPL && isdigit(s[1]) && s[2] == '\0')
24451781Seric 	{
24551781Seric 		printf("$%c", s[1]);
24651781Seric 		return;
24751781Seric 	}
24851781Seric 	for (mp = MetaMacros; mp->metaname != NULL; mp++)
24951781Seric 	{
25051781Seric 		if (mp->metaval == c)
25151781Seric 		{
25251781Seric 			printf("$%c%s", mp->metaname, ++s);
25351781Seric 			return;
25451781Seric 		}
25551781Seric 	}
25623105Seric 	(void) putchar('"');
2573151Seric 	while ((c = *s++) != '\0')
2583151Seric 	{
2593151Seric 		if (!isascii(c))
2603151Seric 		{
26123105Seric 			(void) putchar('\\');
2623151Seric 			c &= 0177;
2633151Seric 		}
26410326Seric 		if (c < 040 || c >= 0177)
2653151Seric 		{
26652050Seric 			switch (c)
26752050Seric 			{
26852050Seric 			  case '\n':
26952050Seric 				c = 'n';
27052050Seric 				break;
27152050Seric 
27252050Seric 			  case '\r':
27352050Seric 				c = 'r';
27452050Seric 				break;
27552050Seric 
27652050Seric 			  case '\t':
27752050Seric 				c = 't';
27852050Seric 				break;
27952050Seric 
28052637Seric 			  case '\001':
28152637Seric 				(void) putchar('$');
28252637Seric 				continue;
28352637Seric 
28452050Seric 			  default:
28552050Seric 				(void) putchar('^');
28652050Seric 				(void) putchar(c ^ 0100);
28752050Seric 				continue;
28852050Seric 			}
28952050Seric 			(void) putchar('\\');
2903151Seric 		}
29123105Seric 		(void) putchar(c);
2923151Seric 	}
29323105Seric 	(void) putchar('"');
2944086Seric 	(void) fflush(stdout);
2953151Seric }
2963151Seric /*
2973151Seric **  MAKELOWER -- Translate a line into lower case
2983151Seric **
2993151Seric **	Parameters:
3003151Seric **		p -- the string to translate.  If NULL, return is
3013151Seric **			immediate.
3023151Seric **
3033151Seric **	Returns:
3043151Seric **		none.
3053151Seric **
3063151Seric **	Side Effects:
3073151Seric **		String pointed to by p is translated to lower case.
3083151Seric **
3093151Seric **	Called By:
3103151Seric **		parse
3113151Seric */
3123151Seric 
3133151Seric makelower(p)
3143151Seric 	register char *p;
3153151Seric {
3163151Seric 	register char c;
3173151Seric 
3183151Seric 	if (p == NULL)
3193151Seric 		return;
3203151Seric 	for (; (c = *p) != '\0'; p++)
3213151Seric 		if (isascii(c) && isupper(c))
32233724Sbostic 			*p = tolower(c);
3233151Seric }
3244059Seric /*
3255196Seric **  BUILDFNAME -- build full name from gecos style entry.
3264375Seric **
3275196Seric **	This routine interprets the strange entry that would appear
3285196Seric **	in the GECOS field of the password file.
3295196Seric **
3304375Seric **	Parameters:
3315196Seric **		p -- name to build.
3325196Seric **		login -- the login name of this user (for &).
3335196Seric **		buf -- place to put the result.
3344375Seric **
3354375Seric **	Returns:
3364375Seric **		none.
3374375Seric **
3384375Seric **	Side Effects:
3394375Seric **		none.
3404375Seric */
3414375Seric 
34254984Seric buildfname(gecos, login, buf)
34354984Seric 	register char *gecos;
3445196Seric 	char *login;
3454375Seric 	char *buf;
3464375Seric {
34754984Seric 	register char *p;
3484375Seric 	register char *bp = buf;
34954984Seric 	int l;
35054984Seric 	bool quoteit;
3514375Seric 
35254984Seric 	if (*gecos == '*')
35354984Seric 		gecos++;
35454984Seric 
35554984Seric 	/* see if the full name needs to be quoted */
35654984Seric 	l = 0;
35754984Seric 	quoteit = FALSE;
35854984Seric 	for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
35954984Seric 	{
36054984Seric 		if (index("<>()'.", *p) != NULL)
36154984Seric 			quoteit = TRUE;
36254984Seric 		if (*p == '&')
36354984Seric 			l += strlen(login);
36454984Seric 		else
36554984Seric 			l++;
36654984Seric 	}
36754984Seric 	if (quoteit)
36854984Seric 		l += 2;
36954984Seric 
37054984Seric 	/* now fill in buf */
37154984Seric 	if (quoteit)
37254984Seric 		*bp++ = '"';
373*55193Seric 	for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
3744375Seric 	{
3754375Seric 		if (*p == '&')
3764375Seric 		{
3775196Seric 			(void) strcpy(bp, login);
3784375Seric 			*bp = toupper(*bp);
3794375Seric 			while (*bp != '\0')
3804375Seric 				bp++;
3814375Seric 		}
3824375Seric 		else
383*55193Seric 			*bp++ = *p;
3844375Seric 	}
38554984Seric 	if (quoteit)
38654984Seric 		*bp++ = '"';
3874375Seric 	*bp = '\0';
3884375Seric }
3894375Seric /*
3904538Seric **  SAFEFILE -- return true if a file exists and is safe for a user.
3914538Seric **
3924538Seric **	Parameters:
3934538Seric **		fn -- filename to check.
3944538Seric **		uid -- uid to compare against.
3954538Seric **		mode -- mode bits that must match.
3964538Seric **
3974538Seric **	Returns:
3984538Seric **		TRUE if fn exists, is owned by uid, and matches mode.
3994538Seric **		FALSE otherwise.
4004538Seric **
4014538Seric **	Side Effects:
4024538Seric **		none.
4034538Seric */
4044538Seric 
4054538Seric bool
4064538Seric safefile(fn, uid, mode)
4074538Seric 	char *fn;
4084538Seric 	int uid;
4094538Seric 	int mode;
4104538Seric {
4114538Seric 	struct stat stbuf;
4124538Seric 
4134538Seric 	if (stat(fn, &stbuf) >= 0 && stbuf.st_uid == uid &&
4144538Seric 	    (stbuf.st_mode & mode) == mode)
4154538Seric 		return (TRUE);
41611936Seric 	errno = 0;
4174538Seric 	return (FALSE);
4184538Seric }
4194538Seric /*
4204557Seric **  FIXCRLF -- fix <CR><LF> in line.
4214557Seric **
4224557Seric **	Looks for the <CR><LF> combination and turns it into the
4234557Seric **	UNIX canonical <NL> character.  It only takes one line,
4244557Seric **	i.e., it is assumed that the first <NL> found is the end
4254557Seric **	of the line.
4264557Seric **
4274557Seric **	Parameters:
4284557Seric **		line -- the line to fix.
4294557Seric **		stripnl -- if true, strip the newline also.
4304557Seric **
4314557Seric **	Returns:
4324557Seric **		none.
4334557Seric **
4344557Seric **	Side Effects:
4354557Seric **		line is changed in place.
4364557Seric */
4374557Seric 
4384557Seric fixcrlf(line, stripnl)
4394557Seric 	char *line;
4404557Seric 	bool stripnl;
4414557Seric {
4424557Seric 	register char *p;
4434557Seric 
4444557Seric 	p = index(line, '\n');
4454557Seric 	if (p == NULL)
4464557Seric 		return;
44736291Sbostic 	if (p > line && p[-1] == '\r')
4484557Seric 		p--;
4494557Seric 	if (!stripnl)
4504557Seric 		*p++ = '\n';
4514557Seric 	*p = '\0';
4524557Seric }
4534557Seric /*
4546890Seric **  DFOPEN -- determined file open
4556890Seric **
4566890Seric **	This routine has the semantics of fopen, except that it will
4576890Seric **	keep trying a few times to make this happen.  The idea is that
4586890Seric **	on very loaded systems, we may run out of resources (inodes,
4596890Seric **	whatever), so this tries to get around it.
4606890Seric */
4616890Seric 
4626890Seric FILE *
4636890Seric dfopen(filename, mode)
4646890Seric 	char *filename;
4656890Seric 	char *mode;
4666890Seric {
4676890Seric 	register int tries;
4686890Seric 	register FILE *fp;
4696890Seric 
4706890Seric 	for (tries = 0; tries < 10; tries++)
4716890Seric 	{
47225618Seric 		sleep((unsigned) (10 * tries));
4736890Seric 		errno = 0;
4746890Seric 		fp = fopen(filename, mode);
4759376Seric 		if (fp != NULL)
4766890Seric 			break;
4779376Seric 		if (errno != ENFILE && errno != EINTR)
4789376Seric 			break;
4796890Seric 	}
48011936Seric 	errno = 0;
4816890Seric 	return (fp);
4826890Seric }
4837124Seric /*
4847124Seric **  PUTLINE -- put a line like fputs obeying SMTP conventions
4857124Seric **
4867753Seric **	This routine always guarantees outputing a newline (or CRLF,
4877753Seric **	as appropriate) at the end of the string.
4887753Seric **
4897124Seric **	Parameters:
4907124Seric **		l -- line to put.
4917124Seric **		fp -- file to put it onto.
49210172Seric **		m -- the mailer used to control output.
4937124Seric **
4947124Seric **	Returns:
4957124Seric **		none
4967124Seric **
4977124Seric **	Side Effects:
4987124Seric **		output of l to fp.
4997124Seric */
5007124Seric 
50110172Seric putline(l, fp, m)
5027753Seric 	register char *l;
5037124Seric 	FILE *fp;
50410172Seric 	MAILER *m;
5057124Seric {
5067124Seric 	register char *p;
50747157Sbostic 	register char svchar;
5087124Seric 
50911275Seric 	/* strip out 0200 bits -- these can look like TELNET protocol */
51052106Seric 	if (bitnset(M_7BITS, m->m_flags))
51111275Seric 	{
51247157Sbostic 		for (p = l; svchar = *p; ++p)
51347157Sbostic 			if (svchar & 0200)
51447157Sbostic 				*p = svchar &~ 0200;
51511275Seric 	}
51611275Seric 
5177753Seric 	do
5187124Seric 	{
5197753Seric 		/* find the end of the line */
5207753Seric 		p = index(l, '\n');
5217753Seric 		if (p == NULL)
5227753Seric 			p = &l[strlen(l)];
5237124Seric 
5247753Seric 		/* check for line overflow */
52552106Seric 		while (m->m_linelimit > 0 && (p - l) > m->m_linelimit)
5267753Seric 		{
52752106Seric 			register char *q = &l[m->m_linelimit - 1];
5287124Seric 
5297753Seric 			svchar = *q;
5307753Seric 			*q = '\0';
53110685Seric 			if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
53223105Seric 				(void) putc('.', fp);
5337753Seric 			fputs(l, fp);
53423105Seric 			(void) putc('!', fp);
53510326Seric 			fputs(m->m_eol, fp);
5367753Seric 			*q = svchar;
5377753Seric 			l = q;
5387753Seric 		}
5397124Seric 
5407753Seric 		/* output last part */
54110685Seric 		if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
54223105Seric 			(void) putc('.', fp);
54347157Sbostic 		for ( ; l < p; ++l)
54447157Sbostic 			(void) putc(*l, fp);
54510326Seric 		fputs(m->m_eol, fp);
5467753Seric 		if (*l == '\n')
54747157Sbostic 			++l;
5487753Seric 	} while (l[0] != '\0');
5497124Seric }
5507676Seric /*
5517676Seric **  XUNLINK -- unlink a file, doing logging as appropriate.
5527676Seric **
5537676Seric **	Parameters:
5547676Seric **		f -- name of file to unlink.
5557676Seric **
5567676Seric **	Returns:
5577676Seric **		none.
5587676Seric **
5597676Seric **	Side Effects:
5607676Seric **		f is unlinked.
5617676Seric */
5627676Seric 
5637676Seric xunlink(f)
5647676Seric 	char *f;
5657676Seric {
5667676Seric 	register int i;
5677676Seric 
5687676Seric # ifdef LOG
5697676Seric 	if (LogLevel > 20)
5707812Seric 		syslog(LOG_DEBUG, "%s: unlink %s\n", CurEnv->e_id, f);
5717676Seric # endif LOG
5727676Seric 
5737676Seric 	i = unlink(f);
5747676Seric # ifdef LOG
5757676Seric 	if (i < 0 && LogLevel > 21)
5767942Seric 		syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno);
5777676Seric # endif LOG
5787676Seric }
5797685Seric /*
58014885Seric **  SFGETS -- "safe" fgets -- times out and ignores random interrupts.
5817685Seric **
5827685Seric **	Parameters:
5837685Seric **		buf -- place to put the input line.
5847685Seric **		siz -- size of buf.
5857685Seric **		fp -- file to read from.
5867685Seric **
5877685Seric **	Returns:
58815533Seric **		NULL on error (including timeout).  This will also leave
58915533Seric **			buf containing a null string.
5907685Seric **		buf otherwise.
5917685Seric **
5927685Seric **	Side Effects:
5937685Seric **		none.
5947685Seric */
5957685Seric 
59614885Seric static jmp_buf	CtxReadTimeout;
5977685Seric 
5987685Seric char *
5997685Seric sfgets(buf, siz, fp)
6007685Seric 	char *buf;
6017685Seric 	int siz;
6027685Seric 	FILE *fp;
6037685Seric {
6047942Seric 	register EVENT *ev = NULL;
6057685Seric 	register char *p;
60646928Sbostic 	static int readtimeout();
6077685Seric 
60814885Seric 	/* set the timeout */
6097942Seric 	if (ReadTimeout != 0)
61014885Seric 	{
61114885Seric 		if (setjmp(CtxReadTimeout) != 0)
61214885Seric 		{
61336233Skarels # ifdef LOG
61436230Skarels 			syslog(LOG_NOTICE,
61536230Skarels 			    "timeout waiting for input from %s\n",
61640998Sbostic 			    RealHostName? RealHostName: "local");
61736233Skarels # endif
61836230Skarels 			errno = 0;
61940964Sbostic 			usrerr("451 timeout waiting for input");
62019037Seric 			buf[0] = '\0';
62114885Seric 			return (NULL);
62214885Seric 		}
62316138Seric 		ev = setevent((time_t) ReadTimeout, readtimeout, 0);
62414885Seric 	}
62514885Seric 
62614885Seric 	/* try to read */
62715533Seric 	p = NULL;
62815533Seric 	while (p == NULL && !feof(fp) && !ferror(fp))
6297942Seric 	{
6307942Seric 		errno = 0;
6317942Seric 		p = fgets(buf, siz, fp);
63215533Seric 		if (errno == EINTR)
63315533Seric 			clearerr(fp);
63415533Seric 	}
63514885Seric 
63614885Seric 	/* clear the event if it has not sprung */
6377685Seric 	clrevent(ev);
63814885Seric 
63914885Seric 	/* clean up the books and exit */
6408055Seric 	LineNumber++;
64115533Seric 	if (p == NULL)
64216880Seric 	{
64315533Seric 		buf[0] = '\0';
64416880Seric 		return (NULL);
64516880Seric 	}
64652106Seric 	if (!EightBit)
64752106Seric 		for (p = buf; *p != '\0'; p++)
64852106Seric 			*p &= ~0200;
64916880Seric 	return (buf);
6507685Seric }
6517685Seric 
6527685Seric static
6537685Seric readtimeout()
6547685Seric {
65514885Seric 	longjmp(CtxReadTimeout, 1);
6567685Seric }
6577786Seric /*
6587786Seric **  FGETFOLDED -- like fgets, but know about folded lines.
6597786Seric **
6607786Seric **	Parameters:
6617786Seric **		buf -- place to put result.
6627786Seric **		n -- bytes available.
6637786Seric **		f -- file to read from.
6647786Seric **
6657786Seric **	Returns:
6667786Seric **		buf on success, NULL on error or EOF.
6677786Seric **
6687786Seric **	Side Effects:
6697786Seric **		buf gets lines from f, with continuation lines (lines
6707786Seric **		with leading white space) appended.  CRLF's are mapped
6717786Seric **		into single newlines.  Any trailing NL is stripped.
6727786Seric */
6737786Seric 
6747786Seric char *
6757786Seric fgetfolded(buf, n, f)
6767786Seric 	char *buf;
6777786Seric 	register int n;
6787786Seric 	FILE *f;
6797786Seric {
6807786Seric 	register char *p = buf;
6817786Seric 	register int i;
6827786Seric 
6837786Seric 	n--;
68417350Seric 	while ((i = getc(f)) != EOF)
6857786Seric 	{
68617350Seric 		if (i == '\r')
68717350Seric 		{
68817350Seric 			i = getc(f);
68917350Seric 			if (i != '\n')
69017350Seric 			{
69117350Seric 				if (i != EOF)
69223105Seric 					(void) ungetc(i, f);
69317350Seric 				i = '\r';
69417350Seric 			}
69517350Seric 		}
69617350Seric 		if (--n > 0)
69717350Seric 			*p++ = i;
69852647Seric 		else if (n == 0)
69952647Seric 			nmessage(Arpa_Info, "warning: line truncated");
70017350Seric 		if (i == '\n')
70117350Seric 		{
70217350Seric 			LineNumber++;
70317350Seric 			i = getc(f);
70417350Seric 			if (i != EOF)
70523105Seric 				(void) ungetc(i, f);
70617350Seric 			if (i != ' ' && i != '\t')
70752647Seric 				break;
70817350Seric 		}
7097786Seric 	}
71052647Seric 	if (p == buf)
71152647Seric 		return (NULL);
71252647Seric 	*--p = '\0';
71352647Seric 	return (buf);
7147786Seric }
7157860Seric /*
7167886Seric **  CURTIME -- return current time.
7177886Seric **
7187886Seric **	Parameters:
7197886Seric **		none.
7207886Seric **
7217886Seric **	Returns:
7227886Seric **		the current time.
7237886Seric **
7247886Seric **	Side Effects:
7257886Seric **		none.
7267886Seric */
7277886Seric 
7287886Seric time_t
7297886Seric curtime()
7307886Seric {
7317886Seric 	auto time_t t;
7327886Seric 
7337886Seric 	(void) time(&t);
7347886Seric 	return (t);
7357886Seric }
7368264Seric /*
7378264Seric **  ATOBOOL -- convert a string representation to boolean.
7388264Seric **
7398264Seric **	Defaults to "TRUE"
7408264Seric **
7418264Seric **	Parameters:
7428264Seric **		s -- string to convert.  Takes "tTyY" as true,
7438264Seric **			others as false.
7448264Seric **
7458264Seric **	Returns:
7468264Seric **		A boolean representation of the string.
7478264Seric **
7488264Seric **	Side Effects:
7498264Seric **		none.
7508264Seric */
7518264Seric 
7528264Seric bool
7538264Seric atobool(s)
7548264Seric 	register char *s;
7558264Seric {
7568264Seric 	if (*s == '\0' || index("tTyY", *s) != NULL)
7578264Seric 		return (TRUE);
7588264Seric 	return (FALSE);
7598264Seric }
7609048Seric /*
7619048Seric **  ATOOCT -- convert a string representation to octal.
7629048Seric **
7639048Seric **	Parameters:
7649048Seric **		s -- string to convert.
7659048Seric **
7669048Seric **	Returns:
7679048Seric **		An integer representing the string interpreted as an
7689048Seric **		octal number.
7699048Seric **
7709048Seric **	Side Effects:
7719048Seric **		none.
7729048Seric */
7739048Seric 
7749048Seric atooct(s)
7759048Seric 	register char *s;
7769048Seric {
7779048Seric 	register int i = 0;
7789048Seric 
7799048Seric 	while (*s >= '0' && *s <= '7')
7809048Seric 		i = (i << 3) | (*s++ - '0');
7819048Seric 	return (i);
7829048Seric }
7839376Seric /*
7849376Seric **  WAITFOR -- wait for a particular process id.
7859376Seric **
7869376Seric **	Parameters:
7879376Seric **		pid -- process id to wait for.
7889376Seric **
7899376Seric **	Returns:
7909376Seric **		status of pid.
7919376Seric **		-1 if pid never shows up.
7929376Seric **
7939376Seric **	Side Effects:
7949376Seric **		none.
7959376Seric */
7969376Seric 
7979376Seric waitfor(pid)
7989376Seric 	int pid;
7999376Seric {
8009376Seric 	auto int st;
8019376Seric 	int i;
8029376Seric 
8039376Seric 	do
8049376Seric 	{
8059376Seric 		errno = 0;
8069376Seric 		i = wait(&st);
8079376Seric 	} while ((i >= 0 || errno == EINTR) && i != pid);
8089376Seric 	if (i < 0)
8099376Seric 		st = -1;
8109376Seric 	return (st);
8119376Seric }
8129376Seric /*
81310685Seric **  BITINTERSECT -- tell if two bitmaps intersect
81410685Seric **
81510685Seric **	Parameters:
81610685Seric **		a, b -- the bitmaps in question
81710685Seric **
81810685Seric **	Returns:
81910685Seric **		TRUE if they have a non-null intersection
82010685Seric **		FALSE otherwise
82110685Seric **
82210685Seric **	Side Effects:
82310685Seric **		none.
82410685Seric */
82510685Seric 
82610685Seric bool
82710685Seric bitintersect(a, b)
82810685Seric 	BITMAP a;
82910685Seric 	BITMAP b;
83010685Seric {
83110685Seric 	int i;
83210685Seric 
83310685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
83410685Seric 		if ((a[i] & b[i]) != 0)
83510685Seric 			return (TRUE);
83610685Seric 	return (FALSE);
83710685Seric }
83810685Seric /*
83910685Seric **  BITZEROP -- tell if a bitmap is all zero
84010685Seric **
84110685Seric **	Parameters:
84210685Seric **		map -- the bit map to check
84310685Seric **
84410685Seric **	Returns:
84510685Seric **		TRUE if map is all zero.
84610685Seric **		FALSE if there are any bits set in map.
84710685Seric **
84810685Seric **	Side Effects:
84910685Seric **		none.
85010685Seric */
85110685Seric 
85210685Seric bool
85310685Seric bitzerop(map)
85410685Seric 	BITMAP map;
85510685Seric {
85610685Seric 	int i;
85710685Seric 
85810685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
85910685Seric 		if (map[i] != 0)
86010685Seric 			return (FALSE);
86110685Seric 	return (TRUE);
86210685Seric }
863