xref: /csrg-svn/usr.sbin/sendmail/src/util.c (revision 16880)
13151Seric # include <stdio.h>
24538Seric # include <sys/types.h>
34538Seric # include <sys/stat.h>
4298Seric # include <sysexits.h>
56890Seric # include <errno.h>
62900Seric # include <ctype.h>
76890Seric # include "sendmail.h"
8298Seric 
9*16880Seric SCCSID(@(#)util.c	4.6		08/11/84);
10409Seric 
11298Seric /*
12298Seric **  STRIPQUOTES -- Strip quotes & quote bits from a string.
13298Seric **
14298Seric **	Runs through a string and strips off unquoted quote
15298Seric **	characters and quote bits.  This is done in place.
16298Seric **
17298Seric **	Parameters:
18298Seric **		s -- the string to strip.
194101Seric **		qf -- if set, remove actual `` " '' characters
204101Seric **			as well as the quote bits.
21298Seric **
22298Seric **	Returns:
23298Seric **		none.
24298Seric **
25298Seric **	Side Effects:
26298Seric **		none.
27298Seric **
28298Seric **	Called By:
29298Seric **		deliver
30298Seric */
31298Seric 
324101Seric stripquotes(s, qf)
33298Seric 	char *s;
344101Seric 	bool qf;
35298Seric {
36298Seric 	register char *p;
37298Seric 	register char *q;
38298Seric 	register char c;
39298Seric 
404101Seric 	if (s == NULL)
414101Seric 		return;
424101Seric 
43298Seric 	for (p = q = s; (c = *p++) != '\0'; )
44298Seric 	{
454101Seric 		if (c != '"' || !qf)
46298Seric 			*q++ = c & 0177;
47298Seric 	}
48298Seric 	*q = '\0';
49298Seric }
50298Seric /*
519043Seric **  QSTRLEN -- give me the string length assuming 0200 bits add a char
529043Seric **
539043Seric **	Parameters:
549043Seric **		s -- the string to measure.
559043Seric **
569043Seric **	Reurns:
579043Seric **		The length of s, including space for backslash escapes.
589043Seric **
599043Seric **	Side Effects:
609043Seric **		none.
619043Seric */
629043Seric 
639043Seric qstrlen(s)
649043Seric 	register char *s;
659043Seric {
669043Seric 	register int l = 0;
679043Seric 	register char c;
689043Seric 
699043Seric 	while ((c = *s++) != '\0')
709043Seric 	{
719043Seric 		if (bitset(0200, c))
729043Seric 			l++;
739043Seric 		l++;
749043Seric 	}
759043Seric 	return (l);
769043Seric }
779043Seric /*
782900Seric **  CAPITALIZE -- return a copy of a string, properly capitalized.
792900Seric **
802900Seric **	Parameters:
812900Seric **		s -- the string to capitalize.
822900Seric **
832900Seric **	Returns:
842900Seric **		a pointer to a properly capitalized string.
852900Seric **
862900Seric **	Side Effects:
872900Seric **		none.
882900Seric */
892900Seric 
902900Seric char *
912900Seric capitalize(s)
922900Seric 	register char *s;
932900Seric {
942900Seric 	static char buf[50];
952900Seric 	register char *p;
962900Seric 
972900Seric 	p = buf;
982900Seric 
992900Seric 	for (;;)
1002900Seric 	{
1012900Seric 		while (!isalpha(*s) && *s != '\0')
1022900Seric 			*p++ = *s++;
1032900Seric 		if (*s == '\0')
1042900Seric 			break;
1052900Seric 		*p++ = toupper(*s++);
1062900Seric 		while (isalpha(*s))
1072900Seric 			*p++ = *s++;
1082900Seric 	}
1092900Seric 
1102900Seric 	*p = '\0';
1112900Seric 	return (buf);
1122900Seric }
1132900Seric /*
114298Seric **  XALLOC -- Allocate memory and bitch wildly on failure.
115298Seric **
116298Seric **	THIS IS A CLUDGE.  This should be made to give a proper
117298Seric **	error -- but after all, what can we do?
118298Seric **
119298Seric **	Parameters:
120298Seric **		sz -- size of area to allocate.
121298Seric **
122298Seric **	Returns:
123298Seric **		pointer to data region.
124298Seric **
125298Seric **	Side Effects:
126298Seric **		Memory is allocated.
127298Seric */
128298Seric 
129298Seric char *
130298Seric xalloc(sz)
1317007Seric 	register int sz;
132298Seric {
133298Seric 	register char *p;
134298Seric 
135298Seric 	p = malloc(sz);
136298Seric 	if (p == NULL)
137298Seric 	{
138298Seric 		syserr("Out of memory!!");
13910685Seric 		abort();
14010685Seric 		/* exit(EX_UNAVAILABLE); */
141298Seric 	}
142298Seric 	return (p);
143298Seric }
144298Seric /*
1453151Seric **  COPYPLIST -- copy list of pointers.
1463151Seric **
1473151Seric **	This routine is the equivalent of newstr for lists of
1483151Seric **	pointers.
1493151Seric **
1503151Seric **	Parameters:
1513151Seric **		list -- list of pointers to copy.
1523151Seric **			Must be NULL terminated.
1533151Seric **		copycont -- if TRUE, copy the contents of the vector
1543151Seric **			(which must be a string) also.
1553151Seric **
1563151Seric **	Returns:
1573151Seric **		a copy of 'list'.
1583151Seric **
1593151Seric **	Side Effects:
1603151Seric **		none.
1613151Seric */
1623151Seric 
1633151Seric char **
1643151Seric copyplist(list, copycont)
1653151Seric 	char **list;
1663151Seric 	bool copycont;
1673151Seric {
1683151Seric 	register char **vp;
1693151Seric 	register char **newvp;
1703151Seric 
1713151Seric 	for (vp = list; *vp != NULL; vp++)
1723151Seric 		continue;
1733151Seric 
1743151Seric 	vp++;
1753151Seric 
1767007Seric 	newvp = (char **) xalloc((vp - list) * sizeof *vp);
1774086Seric 	bmove((char *) list, (char *) newvp, (vp - list) * sizeof *vp);
1783151Seric 
1793151Seric 	if (copycont)
1803151Seric 	{
1813151Seric 		for (vp = newvp; *vp != NULL; vp++)
1823151Seric 			*vp = newstr(*vp);
1833151Seric 	}
1843151Seric 
1853151Seric 	return (newvp);
1863151Seric }
1873151Seric /*
1883151Seric **  PRINTAV -- print argument vector.
1893151Seric **
1903151Seric **	Parameters:
1913151Seric **		av -- argument vector.
1923151Seric **
1933151Seric **	Returns:
1943151Seric **		none.
1953151Seric **
1963151Seric **	Side Effects:
1973151Seric **		prints av.
1983151Seric */
1993151Seric 
2003151Seric # ifdef DEBUG
2013151Seric printav(av)
2023151Seric 	register char **av;
2033151Seric {
2043151Seric 	while (*av != NULL)
2053151Seric 	{
2068063Seric 		if (tTd(0, 44))
2078063Seric 			printf("\n\t%08x=", *av);
2088063Seric 		else
2098063Seric 			putchar(' ');
2103151Seric 		xputs(*av++);
2113151Seric 	}
2128063Seric 	putchar('\n');
2133151Seric }
2143151Seric # endif DEBUG
2153151Seric /*
2163151Seric **  LOWER -- turn letter into lower case.
2173151Seric **
2183151Seric **	Parameters:
2193151Seric **		c -- character to turn into lower case.
2203151Seric **
2213151Seric **	Returns:
2223151Seric **		c, in lower case.
2233151Seric **
2243151Seric **	Side Effects:
2253151Seric **		none.
2263151Seric */
2273151Seric 
2283151Seric char
2293151Seric lower(c)
2303151Seric 	register char c;
2313151Seric {
2323151Seric 	if (isascii(c) && isupper(c))
2333151Seric 		c = c - 'A' + 'a';
2343151Seric 	return (c);
2353151Seric }
2363151Seric /*
2373151Seric **  XPUTS -- put string doing control escapes.
2383151Seric **
2393151Seric **	Parameters:
2403151Seric **		s -- string to put.
2413151Seric **
2423151Seric **	Returns:
2433151Seric **		none.
2443151Seric **
2453151Seric **	Side Effects:
2463151Seric **		output to stdout
2473151Seric */
2483151Seric 
2493151Seric # ifdef DEBUG
2503151Seric xputs(s)
2513151Seric 	register char *s;
2523151Seric {
2533151Seric 	register char c;
2543151Seric 
2558055Seric 	if (s == NULL)
2568055Seric 	{
2578055Seric 		printf("<null>");
2588055Seric 		return;
2598055Seric 	}
2608063Seric 	putchar('"');
2613151Seric 	while ((c = *s++) != '\0')
2623151Seric 	{
2633151Seric 		if (!isascii(c))
2643151Seric 		{
2653151Seric 			putchar('\\');
2663151Seric 			c &= 0177;
2673151Seric 		}
26810326Seric 		if (c < 040 || c >= 0177)
2693151Seric 		{
2703151Seric 			putchar('^');
27110326Seric 			c ^= 0100;
2723151Seric 		}
2733151Seric 		putchar(c);
2743151Seric 	}
2758063Seric 	putchar('"');
2764086Seric 	(void) fflush(stdout);
2773151Seric }
2783151Seric # endif DEBUG
2793151Seric /*
2803151Seric **  MAKELOWER -- Translate a line into lower case
2813151Seric **
2823151Seric **	Parameters:
2833151Seric **		p -- the string to translate.  If NULL, return is
2843151Seric **			immediate.
2853151Seric **
2863151Seric **	Returns:
2873151Seric **		none.
2883151Seric **
2893151Seric **	Side Effects:
2903151Seric **		String pointed to by p is translated to lower case.
2913151Seric **
2923151Seric **	Called By:
2933151Seric **		parse
2943151Seric */
2953151Seric 
2963151Seric makelower(p)
2973151Seric 	register char *p;
2983151Seric {
2993151Seric 	register char c;
3003151Seric 
3013151Seric 	if (p == NULL)
3023151Seric 		return;
3033151Seric 	for (; (c = *p) != '\0'; p++)
3043151Seric 		if (isascii(c) && isupper(c))
3053151Seric 			*p = c - 'A' + 'a';
3063151Seric }
3074059Seric /*
3084059Seric **  SAMEWORD -- return TRUE if the words are the same
3094059Seric **
3104059Seric **	Ignores case.
3114059Seric **
3124059Seric **	Parameters:
3134059Seric **		a, b -- the words to compare.
3144059Seric **
3154059Seric **	Returns:
3164059Seric **		TRUE if a & b match exactly (modulo case)
3174059Seric **		FALSE otherwise.
3184059Seric **
3194059Seric **	Side Effects:
3204059Seric **		none.
3214059Seric */
3224059Seric 
3234059Seric bool
3244059Seric sameword(a, b)
3254059Seric 	register char *a, *b;
3264059Seric {
3274059Seric 	while (lower(*a) == lower(*b))
3284059Seric 	{
3294059Seric 		if (*a == '\0')
3304059Seric 			return (TRUE);
3314059Seric 		a++;
3324059Seric 		b++;
3334059Seric 	}
3344059Seric 	return (FALSE);
3354059Seric }
3364086Seric /*
3374101Seric **  CLEAR -- clear a block of memory
3384101Seric **
3394101Seric **	Parameters:
3404101Seric **		p -- location to clear.
3414101Seric **		l -- number of bytes to clear.
3424101Seric **
3434101Seric **	Returns:
3444101Seric **		none.
3454101Seric **
3464101Seric **	Side Effects:
3474101Seric **		none.
3484101Seric */
3494101Seric 
3504101Seric clear(p, l)
3514101Seric 	register char *p;
3524101Seric 	register int l;
3534101Seric {
3544101Seric 	while (l-- > 0)
3554101Seric 		*p++ = 0;
3564101Seric }
3574101Seric /*
3585196Seric **  BUILDFNAME -- build full name from gecos style entry.
3594375Seric **
3605196Seric **	This routine interprets the strange entry that would appear
3615196Seric **	in the GECOS field of the password file.
3625196Seric **
3634375Seric **	Parameters:
3645196Seric **		p -- name to build.
3655196Seric **		login -- the login name of this user (for &).
3665196Seric **		buf -- place to put the result.
3674375Seric **
3684375Seric **	Returns:
3694375Seric **		none.
3704375Seric **
3714375Seric **	Side Effects:
3724375Seric **		none.
3734375Seric */
3744375Seric 
3755196Seric buildfname(p, login, buf)
3765196Seric 	register char *p;
3775196Seric 	char *login;
3784375Seric 	char *buf;
3794375Seric {
3804375Seric 	register char *bp = buf;
3814375Seric 
3824438Seric 	if (*p == '*')
3834438Seric 		p++;
3846278Seric 	while (*p != '\0' && *p != ',' && *p != ';' && *p != '%')
3854375Seric 	{
3864375Seric 		if (*p == '&')
3874375Seric 		{
3885196Seric 			(void) strcpy(bp, login);
3894375Seric 			*bp = toupper(*bp);
3904375Seric 			while (*bp != '\0')
3914375Seric 				bp++;
3924375Seric 			p++;
3934375Seric 		}
3944375Seric 		else
3954375Seric 			*bp++ = *p++;
3964375Seric 	}
3974375Seric 	*bp = '\0';
3984375Seric }
3994375Seric /*
4004538Seric **  SAFEFILE -- return true if a file exists and is safe for a user.
4014538Seric **
4024538Seric **	Parameters:
4034538Seric **		fn -- filename to check.
4044538Seric **		uid -- uid to compare against.
4054538Seric **		mode -- mode bits that must match.
4064538Seric **
4074538Seric **	Returns:
4084538Seric **		TRUE if fn exists, is owned by uid, and matches mode.
4094538Seric **		FALSE otherwise.
4104538Seric **
4114538Seric **	Side Effects:
4124538Seric **		none.
4134538Seric */
4144538Seric 
4154538Seric bool
4164538Seric safefile(fn, uid, mode)
4174538Seric 	char *fn;
4184538Seric 	int uid;
4194538Seric 	int mode;
4204538Seric {
4214538Seric 	struct stat stbuf;
4224538Seric 
4234538Seric 	if (stat(fn, &stbuf) >= 0 && stbuf.st_uid == uid &&
4244538Seric 	    (stbuf.st_mode & mode) == mode)
4254538Seric 		return (TRUE);
42611936Seric 	errno = 0;
4274538Seric 	return (FALSE);
4284538Seric }
4294538Seric /*
4304557Seric **  FIXCRLF -- fix <CR><LF> in line.
4314557Seric **
4324557Seric **	Looks for the <CR><LF> combination and turns it into the
4334557Seric **	UNIX canonical <NL> character.  It only takes one line,
4344557Seric **	i.e., it is assumed that the first <NL> found is the end
4354557Seric **	of the line.
4364557Seric **
4374557Seric **	Parameters:
4384557Seric **		line -- the line to fix.
4394557Seric **		stripnl -- if true, strip the newline also.
4404557Seric **
4414557Seric **	Returns:
4424557Seric **		none.
4434557Seric **
4444557Seric **	Side Effects:
4454557Seric **		line is changed in place.
4464557Seric */
4474557Seric 
4484557Seric fixcrlf(line, stripnl)
4494557Seric 	char *line;
4504557Seric 	bool stripnl;
4514557Seric {
4524557Seric 	register char *p;
4534557Seric 
4544557Seric 	p = index(line, '\n');
4554557Seric 	if (p == NULL)
4564557Seric 		return;
4574794Seric 	if (p[-1] == '\r')
4584557Seric 		p--;
4594557Seric 	if (!stripnl)
4604557Seric 		*p++ = '\n';
4614557Seric 	*p = '\0';
4624557Seric }
4634557Seric /*
4644086Seric **  SYSLOG -- fake entry to fool lint
4654086Seric */
4664086Seric 
4674086Seric # ifdef LOG
4684086Seric # ifdef lint
4694086Seric 
4704086Seric /*VARARGS2*/
4714086Seric syslog(pri, fmt, args)
4724086Seric 	int pri;
4734086Seric 	char *fmt;
4744086Seric {
4754086Seric 	pri = *fmt;
4764086Seric 	args = pri;
4774086Seric 	pri = args;
4784086Seric }
4794086Seric 
4804086Seric # endif lint
4814086Seric # endif LOG
4826890Seric /*
4836890Seric **  DFOPEN -- determined file open
4846890Seric **
4856890Seric **	This routine has the semantics of fopen, except that it will
4866890Seric **	keep trying a few times to make this happen.  The idea is that
4876890Seric **	on very loaded systems, we may run out of resources (inodes,
4886890Seric **	whatever), so this tries to get around it.
4896890Seric */
4906890Seric 
4916890Seric FILE *
4926890Seric dfopen(filename, mode)
4936890Seric 	char *filename;
4946890Seric 	char *mode;
4956890Seric {
4966890Seric 	register int tries;
4976890Seric 	register FILE *fp;
4986890Seric 
4996890Seric 	for (tries = 0; tries < 10; tries++)
5006890Seric 	{
5016890Seric 		sleep(10 * tries);
5026890Seric 		errno = 0;
5036890Seric 		fp = fopen(filename, mode);
5049376Seric 		if (fp != NULL)
5056890Seric 			break;
5069376Seric 		if (errno != ENFILE && errno != EINTR)
5079376Seric 			break;
5086890Seric 	}
50911936Seric 	errno = 0;
5106890Seric 	return (fp);
5116890Seric }
5127124Seric /*
5137124Seric **  PUTLINE -- put a line like fputs obeying SMTP conventions
5147124Seric **
5157753Seric **	This routine always guarantees outputing a newline (or CRLF,
5167753Seric **	as appropriate) at the end of the string.
5177753Seric **
5187124Seric **	Parameters:
5197124Seric **		l -- line to put.
5207124Seric **		fp -- file to put it onto.
52110172Seric **		m -- the mailer used to control output.
5227124Seric **
5237124Seric **	Returns:
5247124Seric **		none
5257124Seric **
5267124Seric **	Side Effects:
5277124Seric **		output of l to fp.
5287124Seric */
5297124Seric 
5307753Seric # define SMTPLINELIM	990	/* maximum line length */
5317124Seric 
53210172Seric putline(l, fp, m)
5337753Seric 	register char *l;
5347124Seric 	FILE *fp;
53510172Seric 	MAILER *m;
5367124Seric {
5377124Seric 	register char *p;
5387753Seric 	char svchar;
5397124Seric 
54011275Seric 	/* strip out 0200 bits -- these can look like TELNET protocol */
54111275Seric 	if (bitnset(M_LIMITS, m->m_flags))
54211275Seric 	{
54311275Seric 		p = l;
54411275Seric 		while ((*p++ &= ~0200) != 0)
54511275Seric 			continue;
54611275Seric 	}
54711275Seric 
5487753Seric 	do
5497124Seric 	{
5507753Seric 		/* find the end of the line */
5517753Seric 		p = index(l, '\n');
5527753Seric 		if (p == NULL)
5537753Seric 			p = &l[strlen(l)];
5547124Seric 
5557753Seric 		/* check for line overflow */
55611275Seric 		while ((p - l) > SMTPLINELIM && bitnset(M_LIMITS, m->m_flags))
5577753Seric 		{
5587753Seric 			register char *q = &l[SMTPLINELIM - 1];
5597124Seric 
5607753Seric 			svchar = *q;
5617753Seric 			*q = '\0';
56210685Seric 			if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
56310172Seric 				fputc('.', fp);
5647753Seric 			fputs(l, fp);
56510066Seric 			fputc('!', fp);
56610326Seric 			fputs(m->m_eol, fp);
5677753Seric 			*q = svchar;
5687753Seric 			l = q;
5697753Seric 		}
5707124Seric 
5717753Seric 		/* output last part */
5727753Seric 		svchar = *p;
5737753Seric 		*p = '\0';
57410685Seric 		if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
57510172Seric 			fputc('.', fp);
5767124Seric 		fputs(l, fp);
57710326Seric 		fputs(m->m_eol, fp);
5787753Seric 		*p = svchar;
5797753Seric 		l = p;
5807753Seric 		if (*l == '\n')
5817753Seric 			l++;
5827753Seric 	} while (l[0] != '\0');
5837124Seric }
5847676Seric /*
5857676Seric **  XUNLINK -- unlink a file, doing logging as appropriate.
5867676Seric **
5877676Seric **	Parameters:
5887676Seric **		f -- name of file to unlink.
5897676Seric **
5907676Seric **	Returns:
5917676Seric **		none.
5927676Seric **
5937676Seric **	Side Effects:
5947676Seric **		f is unlinked.
5957676Seric */
5967676Seric 
5977676Seric xunlink(f)
5987676Seric 	char *f;
5997676Seric {
6007676Seric 	register int i;
6017676Seric 
6027676Seric # ifdef LOG
6037676Seric 	if (LogLevel > 20)
6047812Seric 		syslog(LOG_DEBUG, "%s: unlink %s\n", CurEnv->e_id, f);
6057676Seric # endif LOG
6067676Seric 
6077676Seric 	i = unlink(f);
6087676Seric # ifdef LOG
6097676Seric 	if (i < 0 && LogLevel > 21)
6107942Seric 		syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno);
6117676Seric # endif LOG
6127676Seric }
6137685Seric /*
61414885Seric **  SFGETS -- "safe" fgets -- times out and ignores random interrupts.
6157685Seric **
6167685Seric **	Parameters:
6177685Seric **		buf -- place to put the input line.
6187685Seric **		siz -- size of buf.
6197685Seric **		fp -- file to read from.
6207685Seric **
6217685Seric **	Returns:
62215533Seric **		NULL on error (including timeout).  This will also leave
62315533Seric **			buf containing a null string.
6247685Seric **		buf otherwise.
6257685Seric **
6267685Seric **	Side Effects:
6277685Seric **		none.
6287685Seric */
6297685Seric 
63014885Seric static jmp_buf	CtxReadTimeout;
6317685Seric 
63216138Seric #ifndef ETIMEDOUT
63316138Seric #define ETIMEDOUT	EINTR
63416138Seric #endif
63516138Seric 
6367685Seric char *
6377685Seric sfgets(buf, siz, fp)
6387685Seric 	char *buf;
6397685Seric 	int siz;
6407685Seric 	FILE *fp;
6417685Seric {
6427942Seric 	register EVENT *ev = NULL;
6437685Seric 	register char *p;
6447685Seric 	extern readtimeout();
6457685Seric 
64614885Seric 	/* set the timeout */
6477942Seric 	if (ReadTimeout != 0)
64814885Seric 	{
64914885Seric 		if (setjmp(CtxReadTimeout) != 0)
65014885Seric 		{
65116138Seric 			errno = ETIMEDOUT;
65214885Seric 			syserr("sfgets: timeout on read (mailer may be hung)");
65314885Seric 			return (NULL);
65414885Seric 		}
65516138Seric 		ev = setevent((time_t) ReadTimeout, readtimeout, 0);
65614885Seric 	}
65714885Seric 
65814885Seric 	/* try to read */
65915533Seric 	p = NULL;
66015533Seric 	while (p == NULL && !feof(fp) && !ferror(fp))
6617942Seric 	{
6627942Seric 		errno = 0;
6637942Seric 		p = fgets(buf, siz, fp);
66415533Seric 		if (errno == EINTR)
66515533Seric 			clearerr(fp);
66615533Seric 	}
66714885Seric 
66814885Seric 	/* clear the event if it has not sprung */
6697685Seric 	clrevent(ev);
67014885Seric 
67114885Seric 	/* clean up the books and exit */
6728055Seric 	LineNumber++;
67315533Seric 	if (p == NULL)
674*16880Seric 	{
67515533Seric 		buf[0] = '\0';
676*16880Seric 		return (NULL);
677*16880Seric 	}
678*16880Seric 	for (p = buf; *p != '\0'; p++)
679*16880Seric 		*p &= ~0200;
680*16880Seric 	return (buf);
6817685Seric }
6827685Seric 
6837685Seric static
6847685Seric readtimeout()
6857685Seric {
68614885Seric 	longjmp(CtxReadTimeout, 1);
6877685Seric }
6887786Seric /*
6897786Seric **  FGETFOLDED -- like fgets, but know about folded lines.
6907786Seric **
6917786Seric **	Parameters:
6927786Seric **		buf -- place to put result.
6937786Seric **		n -- bytes available.
6947786Seric **		f -- file to read from.
6957786Seric **
6967786Seric **	Returns:
6977786Seric **		buf on success, NULL on error or EOF.
6987786Seric **
6997786Seric **	Side Effects:
7007786Seric **		buf gets lines from f, with continuation lines (lines
7017786Seric **		with leading white space) appended.  CRLF's are mapped
7027786Seric **		into single newlines.  Any trailing NL is stripped.
7037786Seric */
7047786Seric 
7057786Seric char *
7067786Seric fgetfolded(buf, n, f)
7077786Seric 	char *buf;
7087786Seric 	register int n;
7097786Seric 	FILE *f;
7107786Seric {
7117786Seric 	register char *p = buf;
7127786Seric 	register int i;
7137786Seric 
7147786Seric 	n--;
71510030Seric 	while (fgets(p, n, f) != NULL)
7167786Seric 	{
71710030Seric 		LineNumber++;
7187786Seric 		fixcrlf(p, TRUE);
7197786Seric 		i = fgetc(f);
7207786Seric 		if (i != EOF)
7217786Seric 			ungetc(i, f);
7227786Seric 		if (i != ' ' && i != '\t')
7237786Seric 			return (buf);
7247786Seric 		i = strlen(p);
7257786Seric 		p += i;
7267786Seric 		*p++ = '\n';
7277786Seric 		n -= i + 1;
7287786Seric 	}
7297786Seric 	return (NULL);
7307786Seric }
7317860Seric /*
7327886Seric **  CURTIME -- return current time.
7337886Seric **
7347886Seric **	Parameters:
7357886Seric **		none.
7367886Seric **
7377886Seric **	Returns:
7387886Seric **		the current time.
7397886Seric **
7407886Seric **	Side Effects:
7417886Seric **		none.
7427886Seric */
7437886Seric 
7447886Seric time_t
7457886Seric curtime()
7467886Seric {
7477886Seric 	auto time_t t;
7487886Seric 
7497886Seric 	(void) time(&t);
7507886Seric 	return (t);
7517886Seric }
7528264Seric /*
7538264Seric **  ATOBOOL -- convert a string representation to boolean.
7548264Seric **
7558264Seric **	Defaults to "TRUE"
7568264Seric **
7578264Seric **	Parameters:
7588264Seric **		s -- string to convert.  Takes "tTyY" as true,
7598264Seric **			others as false.
7608264Seric **
7618264Seric **	Returns:
7628264Seric **		A boolean representation of the string.
7638264Seric **
7648264Seric **	Side Effects:
7658264Seric **		none.
7668264Seric */
7678264Seric 
7688264Seric bool
7698264Seric atobool(s)
7708264Seric 	register char *s;
7718264Seric {
7728264Seric 	if (*s == '\0' || index("tTyY", *s) != NULL)
7738264Seric 		return (TRUE);
7748264Seric 	return (FALSE);
7758264Seric }
7769048Seric /*
7779048Seric **  ATOOCT -- convert a string representation to octal.
7789048Seric **
7799048Seric **	Parameters:
7809048Seric **		s -- string to convert.
7819048Seric **
7829048Seric **	Returns:
7839048Seric **		An integer representing the string interpreted as an
7849048Seric **		octal number.
7859048Seric **
7869048Seric **	Side Effects:
7879048Seric **		none.
7889048Seric */
7899048Seric 
7909048Seric atooct(s)
7919048Seric 	register char *s;
7929048Seric {
7939048Seric 	register int i = 0;
7949048Seric 
7959048Seric 	while (*s >= '0' && *s <= '7')
7969048Seric 		i = (i << 3) | (*s++ - '0');
7979048Seric 	return (i);
7989048Seric }
7999376Seric /*
8009376Seric **  WAITFOR -- wait for a particular process id.
8019376Seric **
8029376Seric **	Parameters:
8039376Seric **		pid -- process id to wait for.
8049376Seric **
8059376Seric **	Returns:
8069376Seric **		status of pid.
8079376Seric **		-1 if pid never shows up.
8089376Seric **
8099376Seric **	Side Effects:
8109376Seric **		none.
8119376Seric */
8129376Seric 
8139376Seric waitfor(pid)
8149376Seric 	int pid;
8159376Seric {
8169376Seric 	auto int st;
8179376Seric 	int i;
8189376Seric 
8199376Seric 	do
8209376Seric 	{
8219376Seric 		errno = 0;
8229376Seric 		i = wait(&st);
8239376Seric 	} while ((i >= 0 || errno == EINTR) && i != pid);
8249376Seric 	if (i < 0)
8259376Seric 		st = -1;
8269376Seric 	return (st);
8279376Seric }
8289376Seric /*
82910685Seric **  BITINTERSECT -- tell if two bitmaps intersect
83010685Seric **
83110685Seric **	Parameters:
83210685Seric **		a, b -- the bitmaps in question
83310685Seric **
83410685Seric **	Returns:
83510685Seric **		TRUE if they have a non-null intersection
83610685Seric **		FALSE otherwise
83710685Seric **
83810685Seric **	Side Effects:
83910685Seric **		none.
84010685Seric */
84110685Seric 
84210685Seric bool
84310685Seric bitintersect(a, b)
84410685Seric 	BITMAP a;
84510685Seric 	BITMAP b;
84610685Seric {
84710685Seric 	int i;
84810685Seric 
84910685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
85010685Seric 		if ((a[i] & b[i]) != 0)
85110685Seric 			return (TRUE);
85210685Seric 	return (FALSE);
85310685Seric }
85410685Seric /*
85510685Seric **  BITZEROP -- tell if a bitmap is all zero
85610685Seric **
85710685Seric **	Parameters:
85810685Seric **		map -- the bit map to check
85910685Seric **
86010685Seric **	Returns:
86110685Seric **		TRUE if map is all zero.
86210685Seric **		FALSE if there are any bits set in map.
86310685Seric **
86410685Seric **	Side Effects:
86510685Seric **		none.
86610685Seric */
86710685Seric 
86810685Seric bool
86910685Seric bitzerop(map)
87010685Seric 	BITMAP map;
87110685Seric {
87210685Seric 	int i;
87310685Seric 
87410685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
87510685Seric 		if (map[i] != 0)
87610685Seric 			return (FALSE);
87710685Seric 	return (TRUE);
87810685Seric }
879