xref: /csrg-svn/usr.sbin/sendmail/src/util.c (revision 52637)
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*52637Seric static char sccsid[] = "@(#)util.c	5.24 (Berkeley) 02/21/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.
284101Seric **		qf -- if set, remove actual `` " '' characters
294101Seric **			as well as the quote bits.
30298Seric **
31298Seric **	Returns:
32298Seric **		none.
33298Seric **
34298Seric **	Side Effects:
35298Seric **		none.
36298Seric **
37298Seric **	Called By:
38298Seric **		deliver
39298Seric */
40298Seric 
414101Seric stripquotes(s, qf)
42298Seric 	char *s;
434101Seric 	bool qf;
44298Seric {
45298Seric 	register char *p;
46298Seric 	register char *q;
47298Seric 	register char c;
48298Seric 
494101Seric 	if (s == NULL)
504101Seric 		return;
514101Seric 
52298Seric 	for (p = q = s; (c = *p++) != '\0'; )
53298Seric 	{
544101Seric 		if (c != '"' || !qf)
55298Seric 			*q++ = c & 0177;
56298Seric 	}
57298Seric 	*q = '\0';
58298Seric }
59298Seric /*
609043Seric **  QSTRLEN -- give me the string length assuming 0200 bits add a char
619043Seric **
629043Seric **	Parameters:
639043Seric **		s -- the string to measure.
649043Seric **
659043Seric **	Reurns:
669043Seric **		The length of s, including space for backslash escapes.
679043Seric **
689043Seric **	Side Effects:
699043Seric **		none.
709043Seric */
719043Seric 
729043Seric qstrlen(s)
739043Seric 	register char *s;
749043Seric {
759043Seric 	register int l = 0;
769043Seric 	register char c;
779043Seric 
789043Seric 	while ((c = *s++) != '\0')
799043Seric 	{
809043Seric 		if (bitset(0200, c))
819043Seric 			l++;
829043Seric 		l++;
839043Seric 	}
849043Seric 	return (l);
859043Seric }
869043Seric /*
872900Seric **  CAPITALIZE -- return a copy of a string, properly capitalized.
882900Seric **
892900Seric **	Parameters:
902900Seric **		s -- the string to capitalize.
912900Seric **
922900Seric **	Returns:
932900Seric **		a pointer to a properly capitalized string.
942900Seric **
952900Seric **	Side Effects:
962900Seric **		none.
972900Seric */
982900Seric 
992900Seric char *
1002900Seric capitalize(s)
1012900Seric 	register char *s;
1022900Seric {
1032900Seric 	static char buf[50];
1042900Seric 	register char *p;
1052900Seric 
1062900Seric 	p = buf;
1072900Seric 
1082900Seric 	for (;;)
1092900Seric 	{
1102900Seric 		while (!isalpha(*s) && *s != '\0')
1112900Seric 			*p++ = *s++;
1122900Seric 		if (*s == '\0')
1132900Seric 			break;
11440999Sbostic 		*p++ = toupper(*s);
11540999Sbostic 		s++;
1162900Seric 		while (isalpha(*s))
1172900Seric 			*p++ = *s++;
1182900Seric 	}
1192900Seric 
1202900Seric 	*p = '\0';
1212900Seric 	return (buf);
1222900Seric }
1232900Seric /*
124298Seric **  XALLOC -- Allocate memory and bitch wildly on failure.
125298Seric **
126298Seric **	THIS IS A CLUDGE.  This should be made to give a proper
127298Seric **	error -- but after all, what can we do?
128298Seric **
129298Seric **	Parameters:
130298Seric **		sz -- size of area to allocate.
131298Seric **
132298Seric **	Returns:
133298Seric **		pointer to data region.
134298Seric **
135298Seric **	Side Effects:
136298Seric **		Memory is allocated.
137298Seric */
138298Seric 
139298Seric char *
140298Seric xalloc(sz)
1417007Seric 	register int sz;
142298Seric {
143298Seric 	register char *p;
14423121Seric 	extern char *malloc();
145298Seric 
14623121Seric 	p = malloc((unsigned) sz);
147298Seric 	if (p == NULL)
148298Seric 	{
149298Seric 		syserr("Out of memory!!");
15010685Seric 		abort();
15110685Seric 		/* exit(EX_UNAVAILABLE); */
152298Seric 	}
153298Seric 	return (p);
154298Seric }
155298Seric /*
1563151Seric **  COPYPLIST -- copy list of pointers.
1573151Seric **
1583151Seric **	This routine is the equivalent of newstr for lists of
1593151Seric **	pointers.
1603151Seric **
1613151Seric **	Parameters:
1623151Seric **		list -- list of pointers to copy.
1633151Seric **			Must be NULL terminated.
1643151Seric **		copycont -- if TRUE, copy the contents of the vector
1653151Seric **			(which must be a string) also.
1663151Seric **
1673151Seric **	Returns:
1683151Seric **		a copy of 'list'.
1693151Seric **
1703151Seric **	Side Effects:
1713151Seric **		none.
1723151Seric */
1733151Seric 
1743151Seric char **
1753151Seric copyplist(list, copycont)
1763151Seric 	char **list;
1773151Seric 	bool copycont;
1783151Seric {
1793151Seric 	register char **vp;
1803151Seric 	register char **newvp;
1813151Seric 
1823151Seric 	for (vp = list; *vp != NULL; vp++)
1833151Seric 		continue;
1843151Seric 
1853151Seric 	vp++;
1863151Seric 
18716897Seric 	newvp = (char **) xalloc((int) (vp - list) * sizeof *vp);
18816897Seric 	bcopy((char *) list, (char *) newvp, (int) (vp - list) * sizeof *vp);
1893151Seric 
1903151Seric 	if (copycont)
1913151Seric 	{
1923151Seric 		for (vp = newvp; *vp != NULL; vp++)
1933151Seric 			*vp = newstr(*vp);
1943151Seric 	}
1953151Seric 
1963151Seric 	return (newvp);
1973151Seric }
1983151Seric /*
1993151Seric **  PRINTAV -- print argument vector.
2003151Seric **
2013151Seric **	Parameters:
2023151Seric **		av -- argument vector.
2033151Seric **
2043151Seric **	Returns:
2053151Seric **		none.
2063151Seric **
2073151Seric **	Side Effects:
2083151Seric **		prints av.
2093151Seric */
2103151Seric 
2113151Seric printav(av)
2123151Seric 	register char **av;
2133151Seric {
2143151Seric 	while (*av != NULL)
2153151Seric 	{
2168063Seric 		if (tTd(0, 44))
2178063Seric 			printf("\n\t%08x=", *av);
2188063Seric 		else
21923105Seric 			(void) putchar(' ');
2203151Seric 		xputs(*av++);
2213151Seric 	}
22223105Seric 	(void) putchar('\n');
2233151Seric }
2243151Seric /*
2253151Seric **  LOWER -- turn letter into lower case.
2263151Seric **
2273151Seric **	Parameters:
2283151Seric **		c -- character to turn into lower case.
2293151Seric **
2303151Seric **	Returns:
2313151Seric **		c, in lower case.
2323151Seric **
2333151Seric **	Side Effects:
2343151Seric **		none.
2353151Seric */
2363151Seric 
2373151Seric char
2383151Seric lower(c)
2393151Seric 	register char c;
2403151Seric {
24133724Sbostic 	return(isascii(c) && isupper(c) ? tolower(c) : c);
2423151Seric }
2433151Seric /*
2443151Seric **  XPUTS -- put string doing control escapes.
2453151Seric **
2463151Seric **	Parameters:
2473151Seric **		s -- string to put.
2483151Seric **
2493151Seric **	Returns:
2503151Seric **		none.
2513151Seric **
2523151Seric **	Side Effects:
2533151Seric **		output to stdout
2543151Seric */
2553151Seric 
2563151Seric xputs(s)
2573151Seric 	register char *s;
2583151Seric {
2593151Seric 	register char c;
26051781Seric 	register struct metamac *mp;
26151781Seric 	extern struct metamac MetaMacros[];
2623151Seric 
2638055Seric 	if (s == NULL)
2648055Seric 	{
2658055Seric 		printf("<null>");
2668055Seric 		return;
2678055Seric 	}
26851781Seric 	c = *s;
26951781Seric 	if (c == MATCHREPL && isdigit(s[1]) && s[2] == '\0')
27051781Seric 	{
27151781Seric 		printf("$%c", s[1]);
27251781Seric 		return;
27351781Seric 	}
27451781Seric 	for (mp = MetaMacros; mp->metaname != NULL; mp++)
27551781Seric 	{
27651781Seric 		if (mp->metaval == c)
27751781Seric 		{
27851781Seric 			printf("$%c%s", mp->metaname, ++s);
27951781Seric 			return;
28051781Seric 		}
28151781Seric 	}
28223105Seric 	(void) putchar('"');
2833151Seric 	while ((c = *s++) != '\0')
2843151Seric 	{
2853151Seric 		if (!isascii(c))
2863151Seric 		{
28723105Seric 			(void) putchar('\\');
2883151Seric 			c &= 0177;
2893151Seric 		}
29010326Seric 		if (c < 040 || c >= 0177)
2913151Seric 		{
29252050Seric 			switch (c)
29352050Seric 			{
29452050Seric 			  case '\n':
29552050Seric 				c = 'n';
29652050Seric 				break;
29752050Seric 
29852050Seric 			  case '\r':
29952050Seric 				c = 'r';
30052050Seric 				break;
30152050Seric 
30252050Seric 			  case '\t':
30352050Seric 				c = 't';
30452050Seric 				break;
30552050Seric 
306*52637Seric 			  case '\001':
307*52637Seric 				(void) putchar('$');
308*52637Seric 				continue;
309*52637Seric 
31052050Seric 			  default:
31152050Seric 				(void) putchar('^');
31252050Seric 				(void) putchar(c ^ 0100);
31352050Seric 				continue;
31452050Seric 			}
31552050Seric 			(void) putchar('\\');
3163151Seric 		}
31723105Seric 		(void) putchar(c);
3183151Seric 	}
31923105Seric 	(void) putchar('"');
3204086Seric 	(void) fflush(stdout);
3213151Seric }
3223151Seric /*
3233151Seric **  MAKELOWER -- Translate a line into lower case
3243151Seric **
3253151Seric **	Parameters:
3263151Seric **		p -- the string to translate.  If NULL, return is
3273151Seric **			immediate.
3283151Seric **
3293151Seric **	Returns:
3303151Seric **		none.
3313151Seric **
3323151Seric **	Side Effects:
3333151Seric **		String pointed to by p is translated to lower case.
3343151Seric **
3353151Seric **	Called By:
3363151Seric **		parse
3373151Seric */
3383151Seric 
3393151Seric makelower(p)
3403151Seric 	register char *p;
3413151Seric {
3423151Seric 	register char c;
3433151Seric 
3443151Seric 	if (p == NULL)
3453151Seric 		return;
3463151Seric 	for (; (c = *p) != '\0'; p++)
3473151Seric 		if (isascii(c) && isupper(c))
34833724Sbostic 			*p = tolower(c);
3493151Seric }
3504059Seric /*
3515196Seric **  BUILDFNAME -- build full name from gecos style entry.
3524375Seric **
3535196Seric **	This routine interprets the strange entry that would appear
3545196Seric **	in the GECOS field of the password file.
3555196Seric **
3564375Seric **	Parameters:
3575196Seric **		p -- name to build.
3585196Seric **		login -- the login name of this user (for &).
3595196Seric **		buf -- place to put the result.
3604375Seric **
3614375Seric **	Returns:
3624375Seric **		none.
3634375Seric **
3644375Seric **	Side Effects:
3654375Seric **		none.
3664375Seric */
3674375Seric 
3685196Seric buildfname(p, login, buf)
3695196Seric 	register char *p;
3705196Seric 	char *login;
3714375Seric 	char *buf;
3724375Seric {
3734375Seric 	register char *bp = buf;
3744375Seric 
3754438Seric 	if (*p == '*')
3764438Seric 		p++;
3776278Seric 	while (*p != '\0' && *p != ',' && *p != ';' && *p != '%')
3784375Seric 	{
3794375Seric 		if (*p == '&')
3804375Seric 		{
3815196Seric 			(void) strcpy(bp, login);
3824375Seric 			*bp = toupper(*bp);
3834375Seric 			while (*bp != '\0')
3844375Seric 				bp++;
3854375Seric 			p++;
3864375Seric 		}
3874375Seric 		else
3884375Seric 			*bp++ = *p++;
3894375Seric 	}
3904375Seric 	*bp = '\0';
3914375Seric }
3924375Seric /*
3934538Seric **  SAFEFILE -- return true if a file exists and is safe for a user.
3944538Seric **
3954538Seric **	Parameters:
3964538Seric **		fn -- filename to check.
3974538Seric **		uid -- uid to compare against.
3984538Seric **		mode -- mode bits that must match.
3994538Seric **
4004538Seric **	Returns:
4014538Seric **		TRUE if fn exists, is owned by uid, and matches mode.
4024538Seric **		FALSE otherwise.
4034538Seric **
4044538Seric **	Side Effects:
4054538Seric **		none.
4064538Seric */
4074538Seric 
4084538Seric bool
4094538Seric safefile(fn, uid, mode)
4104538Seric 	char *fn;
4114538Seric 	int uid;
4124538Seric 	int mode;
4134538Seric {
4144538Seric 	struct stat stbuf;
4154538Seric 
4164538Seric 	if (stat(fn, &stbuf) >= 0 && stbuf.st_uid == uid &&
4174538Seric 	    (stbuf.st_mode & mode) == mode)
4184538Seric 		return (TRUE);
41911936Seric 	errno = 0;
4204538Seric 	return (FALSE);
4214538Seric }
4224538Seric /*
4234557Seric **  FIXCRLF -- fix <CR><LF> in line.
4244557Seric **
4254557Seric **	Looks for the <CR><LF> combination and turns it into the
4264557Seric **	UNIX canonical <NL> character.  It only takes one line,
4274557Seric **	i.e., it is assumed that the first <NL> found is the end
4284557Seric **	of the line.
4294557Seric **
4304557Seric **	Parameters:
4314557Seric **		line -- the line to fix.
4324557Seric **		stripnl -- if true, strip the newline also.
4334557Seric **
4344557Seric **	Returns:
4354557Seric **		none.
4364557Seric **
4374557Seric **	Side Effects:
4384557Seric **		line is changed in place.
4394557Seric */
4404557Seric 
4414557Seric fixcrlf(line, stripnl)
4424557Seric 	char *line;
4434557Seric 	bool stripnl;
4444557Seric {
4454557Seric 	register char *p;
4464557Seric 
4474557Seric 	p = index(line, '\n');
4484557Seric 	if (p == NULL)
4494557Seric 		return;
45036291Sbostic 	if (p > line && p[-1] == '\r')
4514557Seric 		p--;
4524557Seric 	if (!stripnl)
4534557Seric 		*p++ = '\n';
4544557Seric 	*p = '\0';
4554557Seric }
4564557Seric /*
4576890Seric **  DFOPEN -- determined file open
4586890Seric **
4596890Seric **	This routine has the semantics of fopen, except that it will
4606890Seric **	keep trying a few times to make this happen.  The idea is that
4616890Seric **	on very loaded systems, we may run out of resources (inodes,
4626890Seric **	whatever), so this tries to get around it.
4636890Seric */
4646890Seric 
4656890Seric FILE *
4666890Seric dfopen(filename, mode)
4676890Seric 	char *filename;
4686890Seric 	char *mode;
4696890Seric {
4706890Seric 	register int tries;
4716890Seric 	register FILE *fp;
4726890Seric 
4736890Seric 	for (tries = 0; tries < 10; tries++)
4746890Seric 	{
47525618Seric 		sleep((unsigned) (10 * tries));
4766890Seric 		errno = 0;
4776890Seric 		fp = fopen(filename, mode);
4789376Seric 		if (fp != NULL)
4796890Seric 			break;
4809376Seric 		if (errno != ENFILE && errno != EINTR)
4819376Seric 			break;
4826890Seric 	}
48311936Seric 	errno = 0;
4846890Seric 	return (fp);
4856890Seric }
4867124Seric /*
4877124Seric **  PUTLINE -- put a line like fputs obeying SMTP conventions
4887124Seric **
4897753Seric **	This routine always guarantees outputing a newline (or CRLF,
4907753Seric **	as appropriate) at the end of the string.
4917753Seric **
4927124Seric **	Parameters:
4937124Seric **		l -- line to put.
4947124Seric **		fp -- file to put it onto.
49510172Seric **		m -- the mailer used to control output.
4967124Seric **
4977124Seric **	Returns:
4987124Seric **		none
4997124Seric **
5007124Seric **	Side Effects:
5017124Seric **		output of l to fp.
5027124Seric */
5037124Seric 
50410172Seric putline(l, fp, m)
5057753Seric 	register char *l;
5067124Seric 	FILE *fp;
50710172Seric 	MAILER *m;
5087124Seric {
5097124Seric 	register char *p;
51047157Sbostic 	register char svchar;
5117124Seric 
51211275Seric 	/* strip out 0200 bits -- these can look like TELNET protocol */
51352106Seric 	if (bitnset(M_7BITS, m->m_flags))
51411275Seric 	{
51547157Sbostic 		for (p = l; svchar = *p; ++p)
51647157Sbostic 			if (svchar & 0200)
51747157Sbostic 				*p = svchar &~ 0200;
51811275Seric 	}
51911275Seric 
5207753Seric 	do
5217124Seric 	{
5227753Seric 		/* find the end of the line */
5237753Seric 		p = index(l, '\n');
5247753Seric 		if (p == NULL)
5257753Seric 			p = &l[strlen(l)];
5267124Seric 
5277753Seric 		/* check for line overflow */
52852106Seric 		while (m->m_linelimit > 0 && (p - l) > m->m_linelimit)
5297753Seric 		{
53052106Seric 			register char *q = &l[m->m_linelimit - 1];
5317124Seric 
5327753Seric 			svchar = *q;
5337753Seric 			*q = '\0';
53410685Seric 			if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
53523105Seric 				(void) putc('.', fp);
5367753Seric 			fputs(l, fp);
53723105Seric 			(void) putc('!', fp);
53810326Seric 			fputs(m->m_eol, fp);
5397753Seric 			*q = svchar;
5407753Seric 			l = q;
5417753Seric 		}
5427124Seric 
5437753Seric 		/* output last part */
54410685Seric 		if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
54523105Seric 			(void) putc('.', fp);
54647157Sbostic 		for ( ; l < p; ++l)
54747157Sbostic 			(void) putc(*l, fp);
54810326Seric 		fputs(m->m_eol, fp);
5497753Seric 		if (*l == '\n')
55047157Sbostic 			++l;
5517753Seric 	} while (l[0] != '\0');
5527124Seric }
5537676Seric /*
5547676Seric **  XUNLINK -- unlink a file, doing logging as appropriate.
5557676Seric **
5567676Seric **	Parameters:
5577676Seric **		f -- name of file to unlink.
5587676Seric **
5597676Seric **	Returns:
5607676Seric **		none.
5617676Seric **
5627676Seric **	Side Effects:
5637676Seric **		f is unlinked.
5647676Seric */
5657676Seric 
5667676Seric xunlink(f)
5677676Seric 	char *f;
5687676Seric {
5697676Seric 	register int i;
5707676Seric 
5717676Seric # ifdef LOG
5727676Seric 	if (LogLevel > 20)
5737812Seric 		syslog(LOG_DEBUG, "%s: unlink %s\n", CurEnv->e_id, f);
5747676Seric # endif LOG
5757676Seric 
5767676Seric 	i = unlink(f);
5777676Seric # ifdef LOG
5787676Seric 	if (i < 0 && LogLevel > 21)
5797942Seric 		syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno);
5807676Seric # endif LOG
5817676Seric }
5827685Seric /*
58314885Seric **  SFGETS -- "safe" fgets -- times out and ignores random interrupts.
5847685Seric **
5857685Seric **	Parameters:
5867685Seric **		buf -- place to put the input line.
5877685Seric **		siz -- size of buf.
5887685Seric **		fp -- file to read from.
5897685Seric **
5907685Seric **	Returns:
59115533Seric **		NULL on error (including timeout).  This will also leave
59215533Seric **			buf containing a null string.
5937685Seric **		buf otherwise.
5947685Seric **
5957685Seric **	Side Effects:
5967685Seric **		none.
5977685Seric */
5987685Seric 
59914885Seric static jmp_buf	CtxReadTimeout;
6007685Seric 
6017685Seric char *
6027685Seric sfgets(buf, siz, fp)
6037685Seric 	char *buf;
6047685Seric 	int siz;
6057685Seric 	FILE *fp;
6067685Seric {
6077942Seric 	register EVENT *ev = NULL;
6087685Seric 	register char *p;
60946928Sbostic 	static int readtimeout();
6107685Seric 
61114885Seric 	/* set the timeout */
6127942Seric 	if (ReadTimeout != 0)
61314885Seric 	{
61414885Seric 		if (setjmp(CtxReadTimeout) != 0)
61514885Seric 		{
61636233Skarels # ifdef LOG
61736230Skarels 			syslog(LOG_NOTICE,
61836230Skarels 			    "timeout waiting for input from %s\n",
61940998Sbostic 			    RealHostName? RealHostName: "local");
62036233Skarels # endif
62136230Skarels 			errno = 0;
62240964Sbostic 			usrerr("451 timeout waiting for input");
62319037Seric 			buf[0] = '\0';
62414885Seric 			return (NULL);
62514885Seric 		}
62616138Seric 		ev = setevent((time_t) ReadTimeout, readtimeout, 0);
62714885Seric 	}
62814885Seric 
62914885Seric 	/* try to read */
63015533Seric 	p = NULL;
63115533Seric 	while (p == NULL && !feof(fp) && !ferror(fp))
6327942Seric 	{
6337942Seric 		errno = 0;
6347942Seric 		p = fgets(buf, siz, fp);
63515533Seric 		if (errno == EINTR)
63615533Seric 			clearerr(fp);
63715533Seric 	}
63814885Seric 
63914885Seric 	/* clear the event if it has not sprung */
6407685Seric 	clrevent(ev);
64114885Seric 
64214885Seric 	/* clean up the books and exit */
6438055Seric 	LineNumber++;
64415533Seric 	if (p == NULL)
64516880Seric 	{
64615533Seric 		buf[0] = '\0';
64716880Seric 		return (NULL);
64816880Seric 	}
64952106Seric 	if (!EightBit)
65052106Seric 		for (p = buf; *p != '\0'; p++)
65152106Seric 			*p &= ~0200;
65216880Seric 	return (buf);
6537685Seric }
6547685Seric 
6557685Seric static
6567685Seric readtimeout()
6577685Seric {
65814885Seric 	longjmp(CtxReadTimeout, 1);
6597685Seric }
6607786Seric /*
6617786Seric **  FGETFOLDED -- like fgets, but know about folded lines.
6627786Seric **
6637786Seric **	Parameters:
6647786Seric **		buf -- place to put result.
6657786Seric **		n -- bytes available.
6667786Seric **		f -- file to read from.
6677786Seric **
6687786Seric **	Returns:
6697786Seric **		buf on success, NULL on error or EOF.
6707786Seric **
6717786Seric **	Side Effects:
6727786Seric **		buf gets lines from f, with continuation lines (lines
6737786Seric **		with leading white space) appended.  CRLF's are mapped
6747786Seric **		into single newlines.  Any trailing NL is stripped.
6757786Seric */
6767786Seric 
6777786Seric char *
6787786Seric fgetfolded(buf, n, f)
6797786Seric 	char *buf;
6807786Seric 	register int n;
6817786Seric 	FILE *f;
6827786Seric {
6837786Seric 	register char *p = buf;
6847786Seric 	register int i;
6857786Seric 
6867786Seric 	n--;
68717350Seric 	while ((i = getc(f)) != EOF)
6887786Seric 	{
68917350Seric 		if (i == '\r')
69017350Seric 		{
69117350Seric 			i = getc(f);
69217350Seric 			if (i != '\n')
69317350Seric 			{
69417350Seric 				if (i != EOF)
69523105Seric 					(void) ungetc(i, f);
69617350Seric 				i = '\r';
69717350Seric 			}
69817350Seric 		}
69917350Seric 		if (--n > 0)
70017350Seric 			*p++ = i;
70117350Seric 		if (i == '\n')
70217350Seric 		{
70317350Seric 			LineNumber++;
70417350Seric 			i = getc(f);
70517350Seric 			if (i != EOF)
70623105Seric 				(void) ungetc(i, f);
70717350Seric 			if (i != ' ' && i != '\t')
70817350Seric 			{
70917350Seric 				*--p = '\0';
71052106Seric 				if (!EightBit)
71152106Seric 				{
71252106Seric 					/* headers always have to be 7-bit */
71352106Seric 					for (p = buf; (i = *p) != '\0'; *p++)
71452106Seric 						if (bitset(0200, i))
71552106Seric 							*p = i & ~0200;
71652106Seric 				}
71717350Seric 				return (buf);
71817350Seric 			}
71917350Seric 		}
7207786Seric 	}
7217786Seric 	return (NULL);
7227786Seric }
7237860Seric /*
7247886Seric **  CURTIME -- return current time.
7257886Seric **
7267886Seric **	Parameters:
7277886Seric **		none.
7287886Seric **
7297886Seric **	Returns:
7307886Seric **		the current time.
7317886Seric **
7327886Seric **	Side Effects:
7337886Seric **		none.
7347886Seric */
7357886Seric 
7367886Seric time_t
7377886Seric curtime()
7387886Seric {
7397886Seric 	auto time_t t;
7407886Seric 
7417886Seric 	(void) time(&t);
7427886Seric 	return (t);
7437886Seric }
7448264Seric /*
7458264Seric **  ATOBOOL -- convert a string representation to boolean.
7468264Seric **
7478264Seric **	Defaults to "TRUE"
7488264Seric **
7498264Seric **	Parameters:
7508264Seric **		s -- string to convert.  Takes "tTyY" as true,
7518264Seric **			others as false.
7528264Seric **
7538264Seric **	Returns:
7548264Seric **		A boolean representation of the string.
7558264Seric **
7568264Seric **	Side Effects:
7578264Seric **		none.
7588264Seric */
7598264Seric 
7608264Seric bool
7618264Seric atobool(s)
7628264Seric 	register char *s;
7638264Seric {
7648264Seric 	if (*s == '\0' || index("tTyY", *s) != NULL)
7658264Seric 		return (TRUE);
7668264Seric 	return (FALSE);
7678264Seric }
7689048Seric /*
7699048Seric **  ATOOCT -- convert a string representation to octal.
7709048Seric **
7719048Seric **	Parameters:
7729048Seric **		s -- string to convert.
7739048Seric **
7749048Seric **	Returns:
7759048Seric **		An integer representing the string interpreted as an
7769048Seric **		octal number.
7779048Seric **
7789048Seric **	Side Effects:
7799048Seric **		none.
7809048Seric */
7819048Seric 
7829048Seric atooct(s)
7839048Seric 	register char *s;
7849048Seric {
7859048Seric 	register int i = 0;
7869048Seric 
7879048Seric 	while (*s >= '0' && *s <= '7')
7889048Seric 		i = (i << 3) | (*s++ - '0');
7899048Seric 	return (i);
7909048Seric }
7919376Seric /*
7929376Seric **  WAITFOR -- wait for a particular process id.
7939376Seric **
7949376Seric **	Parameters:
7959376Seric **		pid -- process id to wait for.
7969376Seric **
7979376Seric **	Returns:
7989376Seric **		status of pid.
7999376Seric **		-1 if pid never shows up.
8009376Seric **
8019376Seric **	Side Effects:
8029376Seric **		none.
8039376Seric */
8049376Seric 
8059376Seric waitfor(pid)
8069376Seric 	int pid;
8079376Seric {
8089376Seric 	auto int st;
8099376Seric 	int i;
8109376Seric 
8119376Seric 	do
8129376Seric 	{
8139376Seric 		errno = 0;
8149376Seric 		i = wait(&st);
8159376Seric 	} while ((i >= 0 || errno == EINTR) && i != pid);
8169376Seric 	if (i < 0)
8179376Seric 		st = -1;
8189376Seric 	return (st);
8199376Seric }
8209376Seric /*
82110685Seric **  BITINTERSECT -- tell if two bitmaps intersect
82210685Seric **
82310685Seric **	Parameters:
82410685Seric **		a, b -- the bitmaps in question
82510685Seric **
82610685Seric **	Returns:
82710685Seric **		TRUE if they have a non-null intersection
82810685Seric **		FALSE otherwise
82910685Seric **
83010685Seric **	Side Effects:
83110685Seric **		none.
83210685Seric */
83310685Seric 
83410685Seric bool
83510685Seric bitintersect(a, b)
83610685Seric 	BITMAP a;
83710685Seric 	BITMAP b;
83810685Seric {
83910685Seric 	int i;
84010685Seric 
84110685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
84210685Seric 		if ((a[i] & b[i]) != 0)
84310685Seric 			return (TRUE);
84410685Seric 	return (FALSE);
84510685Seric }
84610685Seric /*
84710685Seric **  BITZEROP -- tell if a bitmap is all zero
84810685Seric **
84910685Seric **	Parameters:
85010685Seric **		map -- the bit map to check
85110685Seric **
85210685Seric **	Returns:
85310685Seric **		TRUE if map is all zero.
85410685Seric **		FALSE if there are any bits set in map.
85510685Seric **
85610685Seric **	Side Effects:
85710685Seric **		none.
85810685Seric */
85910685Seric 
86010685Seric bool
86110685Seric bitzerop(map)
86210685Seric 	BITMAP map;
86310685Seric {
86410685Seric 	int i;
86510685Seric 
86610685Seric 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
86710685Seric 		if (map[i] != 0)
86810685Seric 			return (FALSE);
86910685Seric 	return (TRUE);
87010685Seric }
871