xref: /csrg-svn/usr.sbin/sendmail/src/util.c (revision 61707)
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*61707Seric static char sccsid[] = "@(#)util.c	6.21 (Berkeley) 06/05/93";
1133731Sbostic #endif /* not lint */
1222717Sdist 
1358332Seric # include "sendmail.h"
14298Seric # include <sysexits.h>
1557135Seric /*
16298Seric **  STRIPQUOTES -- Strip quotes & quote bits from a string.
17298Seric **
18298Seric **	Runs through a string and strips off unquoted quote
19298Seric **	characters and quote bits.  This is done in place.
20298Seric **
21298Seric **	Parameters:
22298Seric **		s -- the string to strip.
23298Seric **
24298Seric **	Returns:
25298Seric **		none.
26298Seric **
27298Seric **	Side Effects:
28298Seric **		none.
29298Seric **
30298Seric **	Called By:
31298Seric **		deliver
32298Seric */
33298Seric 
3454983Seric stripquotes(s)
35298Seric 	char *s;
36298Seric {
37298Seric 	register char *p;
38298Seric 	register char *q;
39298Seric 	register char c;
40298Seric 
414101Seric 	if (s == NULL)
424101Seric 		return;
434101Seric 
4454983Seric 	p = q = s;
4554983Seric 	do
46298Seric 	{
4754983Seric 		c = *p++;
4854983Seric 		if (c == '\\')
4954983Seric 			c = *p++;
5054983Seric 		else if (c == '"')
5154983Seric 			continue;
5254983Seric 		*q++ = c;
5354983Seric 	} while (c != '\0');
54298Seric }
55298Seric /*
56298Seric **  XALLOC -- Allocate memory and bitch wildly on failure.
57298Seric **
58298Seric **	THIS IS A CLUDGE.  This should be made to give a proper
59298Seric **	error -- but after all, what can we do?
60298Seric **
61298Seric **	Parameters:
62298Seric **		sz -- size of area to allocate.
63298Seric **
64298Seric **	Returns:
65298Seric **		pointer to data region.
66298Seric **
67298Seric **	Side Effects:
68298Seric **		Memory is allocated.
69298Seric */
70298Seric 
71298Seric char *
72298Seric xalloc(sz)
737007Seric 	register int sz;
74298Seric {
75298Seric 	register char *p;
76298Seric 
7723121Seric 	p = malloc((unsigned) sz);
78298Seric 	if (p == NULL)
79298Seric 	{
80298Seric 		syserr("Out of memory!!");
8110685Seric 		abort();
8210685Seric 		/* exit(EX_UNAVAILABLE); */
83298Seric 	}
84298Seric 	return (p);
85298Seric }
86298Seric /*
873151Seric **  COPYPLIST -- copy list of pointers.
883151Seric **
893151Seric **	This routine is the equivalent of newstr for lists of
903151Seric **	pointers.
913151Seric **
923151Seric **	Parameters:
933151Seric **		list -- list of pointers to copy.
943151Seric **			Must be NULL terminated.
953151Seric **		copycont -- if TRUE, copy the contents of the vector
963151Seric **			(which must be a string) also.
973151Seric **
983151Seric **	Returns:
993151Seric **		a copy of 'list'.
1003151Seric **
1013151Seric **	Side Effects:
1023151Seric **		none.
1033151Seric */
1043151Seric 
1053151Seric char **
1063151Seric copyplist(list, copycont)
1073151Seric 	char **list;
1083151Seric 	bool copycont;
1093151Seric {
1103151Seric 	register char **vp;
1113151Seric 	register char **newvp;
1123151Seric 
1133151Seric 	for (vp = list; *vp != NULL; vp++)
1143151Seric 		continue;
1153151Seric 
1163151Seric 	vp++;
1173151Seric 
11816897Seric 	newvp = (char **) xalloc((int) (vp - list) * sizeof *vp);
11916897Seric 	bcopy((char *) list, (char *) newvp, (int) (vp - list) * sizeof *vp);
1203151Seric 
1213151Seric 	if (copycont)
1223151Seric 	{
1233151Seric 		for (vp = newvp; *vp != NULL; vp++)
1243151Seric 			*vp = newstr(*vp);
1253151Seric 	}
1263151Seric 
1273151Seric 	return (newvp);
1283151Seric }
1293151Seric /*
13058170Seric **  COPYQUEUE -- copy address queue.
13158170Seric **
13258170Seric **	This routine is the equivalent of newstr for address queues
13358170Seric **	addresses marked with QDONTSEND aren't copied
13458170Seric **
13558170Seric **	Parameters:
13658170Seric **		addr -- list of address structures to copy.
13758170Seric **
13858170Seric **	Returns:
13958170Seric **		a copy of 'addr'.
14058170Seric **
14158170Seric **	Side Effects:
14258170Seric **		none.
14358170Seric */
14458170Seric 
14558170Seric ADDRESS *
14658170Seric copyqueue(addr)
14758170Seric 	ADDRESS *addr;
14858170Seric {
14958170Seric 	register ADDRESS *newaddr;
15058170Seric 	ADDRESS *ret;
15158170Seric 	register ADDRESS **tail = &ret;
15258170Seric 
15358170Seric 	while (addr != NULL)
15458170Seric 	{
15558170Seric 		if (!bitset(QDONTSEND, addr->q_flags))
15658170Seric 		{
15758170Seric 			newaddr = (ADDRESS *) xalloc(sizeof(ADDRESS));
15858170Seric 			STRUCTCOPY(*addr, *newaddr);
15958170Seric 			*tail = newaddr;
16058170Seric 			tail = &newaddr->q_next;
16158170Seric 		}
16258170Seric 		addr = addr->q_next;
16358170Seric 	}
16458170Seric 	*tail = NULL;
16558170Seric 
16658170Seric 	return ret;
16758170Seric }
16858170Seric /*
1693151Seric **  PRINTAV -- print argument vector.
1703151Seric **
1713151Seric **	Parameters:
1723151Seric **		av -- argument vector.
1733151Seric **
1743151Seric **	Returns:
1753151Seric **		none.
1763151Seric **
1773151Seric **	Side Effects:
1783151Seric **		prints av.
1793151Seric */
1803151Seric 
1813151Seric printav(av)
1823151Seric 	register char **av;
1833151Seric {
1843151Seric 	while (*av != NULL)
1853151Seric 	{
1868063Seric 		if (tTd(0, 44))
1878063Seric 			printf("\n\t%08x=", *av);
1888063Seric 		else
18923105Seric 			(void) putchar(' ');
1903151Seric 		xputs(*av++);
1913151Seric 	}
19223105Seric 	(void) putchar('\n');
1933151Seric }
1943151Seric /*
1953151Seric **  LOWER -- turn letter into lower case.
1963151Seric **
1973151Seric **	Parameters:
1983151Seric **		c -- character to turn into lower case.
1993151Seric **
2003151Seric **	Returns:
2013151Seric **		c, in lower case.
2023151Seric **
2033151Seric **	Side Effects:
2043151Seric **		none.
2053151Seric */
2063151Seric 
2073151Seric char
2083151Seric lower(c)
2093151Seric 	register char c;
2103151Seric {
21158050Seric 	return((isascii(c) && isupper(c)) ? tolower(c) : c);
2123151Seric }
2133151Seric /*
2143151Seric **  XPUTS -- put string doing control escapes.
2153151Seric **
2163151Seric **	Parameters:
2173151Seric **		s -- string to put.
2183151Seric **
2193151Seric **	Returns:
2203151Seric **		none.
2213151Seric **
2223151Seric **	Side Effects:
2233151Seric **		output to stdout
2243151Seric */
2253151Seric 
2263151Seric xputs(s)
2273151Seric 	register char *s;
2283151Seric {
22958050Seric 	register int c;
23051781Seric 	register struct metamac *mp;
23151781Seric 	extern struct metamac MetaMacros[];
2323151Seric 
2338055Seric 	if (s == NULL)
2348055Seric 	{
2358055Seric 		printf("<null>");
2368055Seric 		return;
2378055Seric 	}
23858050Seric 	while ((c = (*s++ & 0377)) != '\0')
2393151Seric 	{
2403151Seric 		if (!isascii(c))
2413151Seric 		{
24258050Seric 			if (c == MATCHREPL || c == MACROEXPAND)
24358050Seric 			{
24458050Seric 				putchar('$');
24558050Seric 				continue;
24658050Seric 			}
24758050Seric 			for (mp = MetaMacros; mp->metaname != '\0'; mp++)
24858050Seric 			{
24958050Seric 				if ((mp->metaval & 0377) == c)
25058050Seric 				{
25158050Seric 					printf("$%c", mp->metaname);
25258050Seric 					break;
25358050Seric 				}
25458050Seric 			}
25558050Seric 			if (mp->metaname != '\0')
25658050Seric 				continue;
25723105Seric 			(void) putchar('\\');
2583151Seric 			c &= 0177;
2593151Seric 		}
26057589Seric 		if (isprint(c))
2613151Seric 		{
26257589Seric 			putchar(c);
26357589Seric 			continue;
26457589Seric 		}
26552050Seric 
26657589Seric 		/* wasn't a meta-macro -- find another way to print it */
26757589Seric 		switch (c)
26857589Seric 		{
26957589Seric 		  case '\0':
27057589Seric 			continue;
27152050Seric 
27257589Seric 		  case '\n':
27357589Seric 			c = 'n';
27457589Seric 			break;
27552050Seric 
27657589Seric 		  case '\r':
27757589Seric 			c = 'r';
27857589Seric 			break;
27952637Seric 
28057589Seric 		  case '\t':
28157589Seric 			c = 't';
28257589Seric 			break;
28357589Seric 
28457589Seric 		  default:
28557589Seric 			(void) putchar('^');
28657589Seric 			(void) putchar(c ^ 0100);
28757589Seric 			continue;
2883151Seric 		}
2893151Seric 	}
2904086Seric 	(void) fflush(stdout);
2913151Seric }
2923151Seric /*
2933151Seric **  MAKELOWER -- Translate a line into lower case
2943151Seric **
2953151Seric **	Parameters:
2963151Seric **		p -- the string to translate.  If NULL, return is
2973151Seric **			immediate.
2983151Seric **
2993151Seric **	Returns:
3003151Seric **		none.
3013151Seric **
3023151Seric **	Side Effects:
3033151Seric **		String pointed to by p is translated to lower case.
3043151Seric **
3053151Seric **	Called By:
3063151Seric **		parse
3073151Seric */
3083151Seric 
3093151Seric makelower(p)
3103151Seric 	register char *p;
3113151Seric {
3123151Seric 	register char c;
3133151Seric 
3143151Seric 	if (p == NULL)
3153151Seric 		return;
3163151Seric 	for (; (c = *p) != '\0'; p++)
3173151Seric 		if (isascii(c) && isupper(c))
31833724Sbostic 			*p = tolower(c);
3193151Seric }
3204059Seric /*
3215196Seric **  BUILDFNAME -- build full name from gecos style entry.
3224375Seric **
3235196Seric **	This routine interprets the strange entry that would appear
3245196Seric **	in the GECOS field of the password file.
3255196Seric **
3264375Seric **	Parameters:
3275196Seric **		p -- name to build.
3285196Seric **		login -- the login name of this user (for &).
3295196Seric **		buf -- place to put the result.
3304375Seric **
3314375Seric **	Returns:
3324375Seric **		none.
3334375Seric **
3344375Seric **	Side Effects:
3354375Seric **		none.
3364375Seric */
3374375Seric 
33854984Seric buildfname(gecos, login, buf)
33954984Seric 	register char *gecos;
3405196Seric 	char *login;
3414375Seric 	char *buf;
3424375Seric {
34354984Seric 	register char *p;
3444375Seric 	register char *bp = buf;
34554984Seric 	int l;
3464375Seric 
34754984Seric 	if (*gecos == '*')
34854984Seric 		gecos++;
34954984Seric 
35057123Seric 	/* find length of final string */
35154984Seric 	l = 0;
35254984Seric 	for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
35354984Seric 	{
35454984Seric 		if (*p == '&')
35554984Seric 			l += strlen(login);
35654984Seric 		else
35754984Seric 			l++;
35854984Seric 	}
35954984Seric 
36054984Seric 	/* now fill in buf */
36155193Seric 	for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
3624375Seric 	{
3634375Seric 		if (*p == '&')
3644375Seric 		{
3655196Seric 			(void) strcpy(bp, login);
3664375Seric 			*bp = toupper(*bp);
3674375Seric 			while (*bp != '\0')
3684375Seric 				bp++;
3694375Seric 		}
3704375Seric 		else
37155193Seric 			*bp++ = *p;
3724375Seric 	}
3734375Seric 	*bp = '\0';
3744375Seric }
3754375Seric /*
3764538Seric **  SAFEFILE -- return true if a file exists and is safe for a user.
3774538Seric **
3784538Seric **	Parameters:
3794538Seric **		fn -- filename to check.
3804538Seric **		uid -- uid to compare against.
3814538Seric **		mode -- mode bits that must match.
3824538Seric **
3834538Seric **	Returns:
38458247Seric **		0 if fn exists, is owned by uid, and matches mode.
38558247Seric **		An errno otherwise.  The actual errno is cleared.
3864538Seric **
3874538Seric **	Side Effects:
3884538Seric **		none.
3894538Seric */
3904538Seric 
39158247Seric int
3924538Seric safefile(fn, uid, mode)
3934538Seric 	char *fn;
39455372Seric 	uid_t uid;
3954538Seric 	int mode;
3964538Seric {
3974538Seric 	struct stat stbuf;
3984538Seric 
39958247Seric 	if (stat(fn, &stbuf) < 0)
40058247Seric 	{
40158247Seric 		int ret = errno;
40258247Seric 
40358247Seric 		errno = 0;
40458247Seric 		return ret;
40558247Seric 	}
40658247Seric 	if (stbuf.st_uid == uid && (stbuf.st_mode & mode) == mode)
40758247Seric 		return 0;
40858247Seric 	return EPERM;
4094538Seric }
4104538Seric /*
4114557Seric **  FIXCRLF -- fix <CR><LF> in line.
4124557Seric **
4134557Seric **	Looks for the <CR><LF> combination and turns it into the
4144557Seric **	UNIX canonical <NL> character.  It only takes one line,
4154557Seric **	i.e., it is assumed that the first <NL> found is the end
4164557Seric **	of the line.
4174557Seric **
4184557Seric **	Parameters:
4194557Seric **		line -- the line to fix.
4204557Seric **		stripnl -- if true, strip the newline also.
4214557Seric **
4224557Seric **	Returns:
4234557Seric **		none.
4244557Seric **
4254557Seric **	Side Effects:
4264557Seric **		line is changed in place.
4274557Seric */
4284557Seric 
4294557Seric fixcrlf(line, stripnl)
4304557Seric 	char *line;
4314557Seric 	bool stripnl;
4324557Seric {
4334557Seric 	register char *p;
4344557Seric 
43556795Seric 	p = strchr(line, '\n');
4364557Seric 	if (p == NULL)
4374557Seric 		return;
43836291Sbostic 	if (p > line && p[-1] == '\r')
4394557Seric 		p--;
4404557Seric 	if (!stripnl)
4414557Seric 		*p++ = '\n';
4424557Seric 	*p = '\0';
4434557Seric }
4444557Seric /*
4456890Seric **  DFOPEN -- determined file open
4466890Seric **
4476890Seric **	This routine has the semantics of fopen, except that it will
4486890Seric **	keep trying a few times to make this happen.  The idea is that
4496890Seric **	on very loaded systems, we may run out of resources (inodes,
4506890Seric **	whatever), so this tries to get around it.
4516890Seric */
4526890Seric 
45359745Seric struct omodes
45459745Seric {
45559745Seric 	int	mask;
45659745Seric 	int	mode;
45759745Seric 	char	*farg;
45859745Seric } OpenModes[] =
45959745Seric {
46059745Seric 	O_ACCMODE,		O_RDONLY,		"r",
46159745Seric 	O_ACCMODE|O_APPEND,	O_WRONLY,		"w",
46259745Seric 	O_ACCMODE|O_APPEND,	O_WRONLY|O_APPEND,	"a",
46359745Seric 	O_TRUNC,		0,			"w+",
46459745Seric 	O_APPEND,		O_APPEND,		"a+",
46559745Seric 	0,			0,			"r+",
46659745Seric };
46759745Seric 
4686890Seric FILE *
46959745Seric dfopen(filename, omode, cmode)
4706890Seric 	char *filename;
47159745Seric 	int omode;
47259745Seric 	int cmode;
4736890Seric {
4746890Seric 	register int tries;
47559745Seric 	int fd;
47659745Seric 	register struct omodes *om;
47759431Seric 	struct stat st;
4786890Seric 
47959745Seric 	for (om = OpenModes; om->mask != 0; om++)
48059745Seric 		if ((omode & om->mask) == om->mode)
48159745Seric 			break;
48259745Seric 
4836890Seric 	for (tries = 0; tries < 10; tries++)
4846890Seric 	{
48525618Seric 		sleep((unsigned) (10 * tries));
4866890Seric 		errno = 0;
48759745Seric 		fd = open(filename, omode, cmode);
48859745Seric 		if (fd >= 0)
4896890Seric 			break;
4909376Seric 		if (errno != ENFILE && errno != EINTR)
4919376Seric 			break;
4926890Seric 	}
49359745Seric 	if (fd >= 0 && fstat(fd, &st) >= 0 && S_ISREG(st.st_mode))
49456328Seric 	{
49556328Seric 		int locktype;
49656328Seric 
49756328Seric 		/* lock the file to avoid accidental conflicts */
49859745Seric 		if ((omode & O_ACCMODE) != O_RDONLY)
49956328Seric 			locktype = LOCK_EX;
50056328Seric 		else
50156328Seric 			locktype = LOCK_SH;
50259745Seric 		(void) lockfile(fd, filename, locktype);
50356328Seric 		errno = 0;
50456328Seric 	}
50559745Seric 	return fdopen(fd, om->farg);
5066890Seric }
5077124Seric /*
5087124Seric **  PUTLINE -- put a line like fputs obeying SMTP conventions
5097124Seric **
5107753Seric **	This routine always guarantees outputing a newline (or CRLF,
5117753Seric **	as appropriate) at the end of the string.
5127753Seric **
5137124Seric **	Parameters:
5147124Seric **		l -- line to put.
5157124Seric **		fp -- file to put it onto.
51610172Seric **		m -- the mailer used to control output.
5177124Seric **
5187124Seric **	Returns:
5197124Seric **		none
5207124Seric **
5217124Seric **	Side Effects:
5227124Seric **		output of l to fp.
5237124Seric */
5247124Seric 
52510172Seric putline(l, fp, m)
5267753Seric 	register char *l;
5277124Seric 	FILE *fp;
52810172Seric 	MAILER *m;
5297124Seric {
5307124Seric 	register char *p;
53147157Sbostic 	register char svchar;
5327124Seric 
53311275Seric 	/* strip out 0200 bits -- these can look like TELNET protocol */
53452106Seric 	if (bitnset(M_7BITS, m->m_flags))
53511275Seric 	{
536*61707Seric 		for (p = l; (svchar = *p) != '\0'; ++p)
537*61707Seric 			if (bitset(0200, svchar))
53847157Sbostic 				*p = svchar &~ 0200;
53911275Seric 	}
54011275Seric 
5417753Seric 	do
5427124Seric 	{
5437753Seric 		/* find the end of the line */
54456795Seric 		p = strchr(l, '\n');
5457753Seric 		if (p == NULL)
5467753Seric 			p = &l[strlen(l)];
5477124Seric 
5487753Seric 		/* check for line overflow */
54952106Seric 		while (m->m_linelimit > 0 && (p - l) > m->m_linelimit)
5507753Seric 		{
55152106Seric 			register char *q = &l[m->m_linelimit - 1];
5527124Seric 
5537753Seric 			svchar = *q;
5547753Seric 			*q = '\0';
55510685Seric 			if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
55623105Seric 				(void) putc('.', fp);
5577753Seric 			fputs(l, fp);
55823105Seric 			(void) putc('!', fp);
55910326Seric 			fputs(m->m_eol, fp);
5607753Seric 			*q = svchar;
5617753Seric 			l = q;
5627753Seric 		}
5637124Seric 
5647753Seric 		/* output last part */
56510685Seric 		if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
56623105Seric 			(void) putc('.', fp);
56747157Sbostic 		for ( ; l < p; ++l)
56847157Sbostic 			(void) putc(*l, fp);
56910326Seric 		fputs(m->m_eol, fp);
5707753Seric 		if (*l == '\n')
57147157Sbostic 			++l;
5727753Seric 	} while (l[0] != '\0');
5737124Seric }
5747676Seric /*
5757676Seric **  XUNLINK -- unlink a file, doing logging as appropriate.
5767676Seric **
5777676Seric **	Parameters:
5787676Seric **		f -- name of file to unlink.
5797676Seric **
5807676Seric **	Returns:
5817676Seric **		none.
5827676Seric **
5837676Seric **	Side Effects:
5847676Seric **		f is unlinked.
5857676Seric */
5867676Seric 
5877676Seric xunlink(f)
5887676Seric 	char *f;
5897676Seric {
5907676Seric 	register int i;
5917676Seric 
5927676Seric # ifdef LOG
59358020Seric 	if (LogLevel > 98)
59458020Seric 		syslog(LOG_DEBUG, "%s: unlink %s", CurEnv->e_id, f);
59556795Seric # endif /* LOG */
5967676Seric 
5977676Seric 	i = unlink(f);
5987676Seric # ifdef LOG
59958020Seric 	if (i < 0 && LogLevel > 97)
6007942Seric 		syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno);
60156795Seric # endif /* LOG */
6027676Seric }
6037685Seric /*
60458680Seric **  XFCLOSE -- close a file, doing logging as appropriate.
60558680Seric **
60658680Seric **	Parameters:
60758680Seric **		fp -- file pointer for the file to close
60858680Seric **		a, b -- miscellaneous crud to print for debugging
60958680Seric **
61058680Seric **	Returns:
61158680Seric **		none.
61258680Seric **
61358680Seric **	Side Effects:
61458680Seric **		fp is closed.
61558680Seric */
61658680Seric 
61758680Seric xfclose(fp, a, b)
61858680Seric 	FILE *fp;
61958680Seric 	char *a, *b;
62058680Seric {
62158796Seric 	if (tTd(53, 99))
62258680Seric 		printf("xfclose(%x) %s %s\n", fp, a, b);
62358796Seric 	if (fclose(fp) < 0 && tTd(53, 99))
62458680Seric 		printf("xfclose FAILURE: %s\n", errstring(errno));
62558680Seric }
62658680Seric /*
62714885Seric **  SFGETS -- "safe" fgets -- times out and ignores random interrupts.
6287685Seric **
6297685Seric **	Parameters:
6307685Seric **		buf -- place to put the input line.
6317685Seric **		siz -- size of buf.
6327685Seric **		fp -- file to read from.
63357384Seric **		timeout -- the timeout before error occurs.
63461093Seric **		during -- what we are trying to read (for error messages).
6357685Seric **
6367685Seric **	Returns:
63715533Seric **		NULL on error (including timeout).  This will also leave
63815533Seric **			buf containing a null string.
6397685Seric **		buf otherwise.
6407685Seric **
6417685Seric **	Side Effects:
6427685Seric **		none.
6437685Seric */
6447685Seric 
64514885Seric static jmp_buf	CtxReadTimeout;
6467685Seric 
6477685Seric char *
64861093Seric sfgets(buf, siz, fp, timeout, during)
6497685Seric 	char *buf;
6507685Seric 	int siz;
6517685Seric 	FILE *fp;
65257384Seric 	time_t timeout;
65361093Seric 	char *during;
6547685Seric {
6557942Seric 	register EVENT *ev = NULL;
6567685Seric 	register char *p;
65746928Sbostic 	static int readtimeout();
6587685Seric 
65914885Seric 	/* set the timeout */
66057384Seric 	if (timeout != 0)
66114885Seric 	{
66214885Seric 		if (setjmp(CtxReadTimeout) != 0)
66314885Seric 		{
66436233Skarels # ifdef LOG
66536230Skarels 			syslog(LOG_NOTICE,
66661093Seric 			    "timeout waiting for input from %s during %s\n",
66761093Seric 			    CurHostName? CurHostName: "local", during);
66836233Skarels # endif
66936230Skarels 			errno = 0;
67061093Seric 			usrerr("451 timeout waiting for input during %s",
67161093Seric 				during);
67219037Seric 			buf[0] = '\0';
67314885Seric 			return (NULL);
67414885Seric 		}
67557384Seric 		ev = setevent(timeout, readtimeout, 0);
67614885Seric 	}
67714885Seric 
67814885Seric 	/* try to read */
67915533Seric 	p = NULL;
68015533Seric 	while (p == NULL && !feof(fp) && !ferror(fp))
6817942Seric 	{
6827942Seric 		errno = 0;
6837942Seric 		p = fgets(buf, siz, fp);
68415533Seric 		if (errno == EINTR)
68515533Seric 			clearerr(fp);
68615533Seric 	}
68714885Seric 
68814885Seric 	/* clear the event if it has not sprung */
6897685Seric 	clrevent(ev);
69014885Seric 
69114885Seric 	/* clean up the books and exit */
6928055Seric 	LineNumber++;
69315533Seric 	if (p == NULL)
69416880Seric 	{
69515533Seric 		buf[0] = '\0';
69616880Seric 		return (NULL);
69716880Seric 	}
69859709Seric 	if (SevenBit)
69952106Seric 		for (p = buf; *p != '\0'; p++)
70052106Seric 			*p &= ~0200;
70116880Seric 	return (buf);
7027685Seric }
7037685Seric 
7047685Seric static
7057685Seric readtimeout()
7067685Seric {
70714885Seric 	longjmp(CtxReadTimeout, 1);
7087685Seric }
7097786Seric /*
7107786Seric **  FGETFOLDED -- like fgets, but know about folded lines.
7117786Seric **
7127786Seric **	Parameters:
7137786Seric **		buf -- place to put result.
7147786Seric **		n -- bytes available.
7157786Seric **		f -- file to read from.
7167786Seric **
7177786Seric **	Returns:
71857135Seric **		input line(s) on success, NULL on error or EOF.
71957135Seric **		This will normally be buf -- unless the line is too
72057135Seric **			long, when it will be xalloc()ed.
7217786Seric **
7227786Seric **	Side Effects:
7237786Seric **		buf gets lines from f, with continuation lines (lines
7247786Seric **		with leading white space) appended.  CRLF's are mapped
7257786Seric **		into single newlines.  Any trailing NL is stripped.
7267786Seric */
7277786Seric 
7287786Seric char *
7297786Seric fgetfolded(buf, n, f)
7307786Seric 	char *buf;
7317786Seric 	register int n;
7327786Seric 	FILE *f;
7337786Seric {
7347786Seric 	register char *p = buf;
73557135Seric 	char *bp = buf;
7367786Seric 	register int i;
7377786Seric 
7387786Seric 	n--;
73917350Seric 	while ((i = getc(f)) != EOF)
7407786Seric 	{
74117350Seric 		if (i == '\r')
74217350Seric 		{
74317350Seric 			i = getc(f);
74417350Seric 			if (i != '\n')
74517350Seric 			{
74617350Seric 				if (i != EOF)
74723105Seric 					(void) ungetc(i, f);
74817350Seric 				i = '\r';
74917350Seric 			}
75017350Seric 		}
75157135Seric 		if (--n <= 0)
75257135Seric 		{
75357135Seric 			/* allocate new space */
75457135Seric 			char *nbp;
75557135Seric 			int nn;
75657135Seric 
75757135Seric 			nn = (p - bp);
75857232Seric 			if (nn < MEMCHUNKSIZE)
75957135Seric 				nn *= 2;
76057135Seric 			else
76157232Seric 				nn += MEMCHUNKSIZE;
76257135Seric 			nbp = xalloc(nn);
76357135Seric 			bcopy(bp, nbp, p - bp);
76457135Seric 			p = &nbp[p - bp];
76557135Seric 			if (bp != buf)
76657135Seric 				free(bp);
76757135Seric 			bp = nbp;
76857135Seric 			n = nn - (p - bp);
76957135Seric 		}
77057135Seric 		*p++ = i;
77117350Seric 		if (i == '\n')
77217350Seric 		{
77317350Seric 			LineNumber++;
77417350Seric 			i = getc(f);
77517350Seric 			if (i != EOF)
77623105Seric 				(void) ungetc(i, f);
77717350Seric 			if (i != ' ' && i != '\t')
77852647Seric 				break;
77917350Seric 		}
7807786Seric 	}
78157135Seric 	if (p == bp)
78252647Seric 		return (NULL);
78352647Seric 	*--p = '\0';
78457135Seric 	return (bp);
7857786Seric }
7867860Seric /*
7877886Seric **  CURTIME -- return current time.
7887886Seric **
7897886Seric **	Parameters:
7907886Seric **		none.
7917886Seric **
7927886Seric **	Returns:
7937886Seric **		the current time.
7947886Seric **
7957886Seric **	Side Effects:
7967886Seric **		none.
7977886Seric */
7987886Seric 
7997886Seric time_t
8007886Seric curtime()
8017886Seric {
8027886Seric 	auto time_t t;
8037886Seric 
8047886Seric 	(void) time(&t);
8057886Seric 	return (t);
8067886Seric }
8078264Seric /*
8088264Seric **  ATOBOOL -- convert a string representation to boolean.
8098264Seric **
8108264Seric **	Defaults to "TRUE"
8118264Seric **
8128264Seric **	Parameters:
8138264Seric **		s -- string to convert.  Takes "tTyY" as true,
8148264Seric **			others as false.
8158264Seric **
8168264Seric **	Returns:
8178264Seric **		A boolean representation of the string.
8188264Seric **
8198264Seric **	Side Effects:
8208264Seric **		none.
8218264Seric */
8228264Seric 
8238264Seric bool
8248264Seric atobool(s)
8258264Seric 	register char *s;
8268264Seric {
82756795Seric 	if (*s == '\0' || strchr("tTyY", *s) != NULL)
8288264Seric 		return (TRUE);
8298264Seric 	return (FALSE);
8308264Seric }
8319048Seric /*
8329048Seric **  ATOOCT -- convert a string representation to octal.
8339048Seric **
8349048Seric **	Parameters:
8359048Seric **		s -- string to convert.
8369048Seric **
8379048Seric **	Returns:
8389048Seric **		An integer representing the string interpreted as an
8399048Seric **		octal number.
8409048Seric **
8419048Seric **	Side Effects:
8429048Seric **		none.
8439048Seric */
8449048Seric 
8459048Seric atooct(s)
8469048Seric 	register char *s;
8479048Seric {
8489048Seric 	register int i = 0;
8499048Seric 
8509048Seric 	while (*s >= '0' && *s <= '7')
8519048Seric 		i = (i << 3) | (*s++ - '0');
8529048Seric 	return (i);
8539048Seric }
8549376Seric /*
8559376Seric **  WAITFOR -- wait for a particular process id.
8569376Seric **
8579376Seric **	Parameters:
8589376Seric **		pid -- process id to wait for.
8599376Seric **
8609376Seric **	Returns:
8619376Seric **		status of pid.
8629376Seric **		-1 if pid never shows up.
8639376Seric **
8649376Seric **	Side Effects:
8659376Seric **		none.
8669376Seric */
8679376Seric 
8689376Seric waitfor(pid)
8699376Seric 	int pid;
8709376Seric {
8719376Seric 	auto int st;
8729376Seric 	int i;
8739376Seric 
8749376Seric 	do
8759376Seric 	{
8769376Seric 		errno = 0;
8779376Seric 		i = wait(&st);
8789376Seric 	} while ((i >= 0 || errno == EINTR) && i != pid);
8799376Seric 	if (i < 0)
8809376Seric 		st = -1;
8819376Seric 	return (st);
8829376Seric }
8839376Seric /*
88410685Seric **  BITINTERSECT -- tell if two bitmaps intersect
88510685Seric **
88610685Seric **	Parameters:
88710685Seric **		a, b -- the bitmaps in question
88810685Seric **
88910685Seric **	Returns:
89010685Seric **		TRUE if they have a non-null intersection
89110685Seric **		FALSE otherwise
89210685Seric **
89310685Seric **	Side Effects:
89410685Seric **		none.
89510685Seric */
89610685Seric 
89710685Seric bool
89810685Seric bitintersect(a, b)
89910685Seric 	BITMAP a;
90010685Seric 	BITMAP b;
90110685Seric {
90210685Seric 	int i;
90310685Seric 
90410685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
90510685Seric 		if ((a[i] & b[i]) != 0)
90610685Seric 			return (TRUE);
90710685Seric 	return (FALSE);
90810685Seric }
90910685Seric /*
91010685Seric **  BITZEROP -- tell if a bitmap is all zero
91110685Seric **
91210685Seric **	Parameters:
91310685Seric **		map -- the bit map to check
91410685Seric **
91510685Seric **	Returns:
91610685Seric **		TRUE if map is all zero.
91710685Seric **		FALSE if there are any bits set in map.
91810685Seric **
91910685Seric **	Side Effects:
92010685Seric **		none.
92110685Seric */
92210685Seric 
92310685Seric bool
92410685Seric bitzerop(map)
92510685Seric 	BITMAP map;
92610685Seric {
92710685Seric 	int i;
92810685Seric 
92910685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
93010685Seric 		if (map[i] != 0)
93110685Seric 			return (FALSE);
93210685Seric 	return (TRUE);
93310685Seric }
93458247Seric /*
93558318Seric **  STRCONTAINEDIN -- tell if one string is contained in another
93658318Seric **
93758318Seric **	Parameters:
93858318Seric **		a -- possible substring.
93958318Seric **		b -- possible superstring.
94058318Seric **
94158318Seric **	Returns:
94258318Seric **		TRUE if a is contained in b.
94358318Seric **		FALSE otherwise.
94458318Seric */
94558318Seric 
94658318Seric bool
94758318Seric strcontainedin(a, b)
94858318Seric 	register char *a;
94958318Seric 	register char *b;
95058318Seric {
95158318Seric 	int l;
95258318Seric 
95358318Seric 	l = strlen(a);
95458318Seric 	for (;;)
95558318Seric 	{
95658318Seric 		b = strchr(b, a[0]);
95758318Seric 		if (b == NULL)
95858318Seric 			return FALSE;
95958318Seric 		if (strncmp(a, b, l) == 0)
96058318Seric 			return TRUE;
96158318Seric 		b++;
96258318Seric 	}
96358318Seric }
964