xref: /original-bsd/usr.sbin/sendmail/src/util.c (revision 3705696b)
1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #ifndef lint
10 static char sccsid[] = "@(#)util.c	8.5 (Berkeley) 07/19/93";
11 #endif /* not lint */
12 
13 # include "sendmail.h"
14 # include <sysexits.h>
15 /*
16 **  STRIPQUOTES -- Strip quotes & quote bits from a string.
17 **
18 **	Runs through a string and strips off unquoted quote
19 **	characters and quote bits.  This is done in place.
20 **
21 **	Parameters:
22 **		s -- the string to strip.
23 **
24 **	Returns:
25 **		none.
26 **
27 **	Side Effects:
28 **		none.
29 **
30 **	Called By:
31 **		deliver
32 */
33 
34 stripquotes(s)
35 	char *s;
36 {
37 	register char *p;
38 	register char *q;
39 	register char c;
40 
41 	if (s == NULL)
42 		return;
43 
44 	p = q = s;
45 	do
46 	{
47 		c = *p++;
48 		if (c == '\\')
49 			c = *p++;
50 		else if (c == '"')
51 			continue;
52 		*q++ = c;
53 	} while (c != '\0');
54 }
55 /*
56 **  XALLOC -- Allocate memory and bitch wildly on failure.
57 **
58 **	THIS IS A CLUDGE.  This should be made to give a proper
59 **	error -- but after all, what can we do?
60 **
61 **	Parameters:
62 **		sz -- size of area to allocate.
63 **
64 **	Returns:
65 **		pointer to data region.
66 **
67 **	Side Effects:
68 **		Memory is allocated.
69 */
70 
71 char *
72 xalloc(sz)
73 	register int sz;
74 {
75 	register char *p;
76 
77 	p = malloc((unsigned) sz);
78 	if (p == NULL)
79 	{
80 		syserr("Out of memory!!");
81 		abort();
82 		/* exit(EX_UNAVAILABLE); */
83 	}
84 	return (p);
85 }
86 /*
87 **  COPYPLIST -- copy list of pointers.
88 **
89 **	This routine is the equivalent of newstr for lists of
90 **	pointers.
91 **
92 **	Parameters:
93 **		list -- list of pointers to copy.
94 **			Must be NULL terminated.
95 **		copycont -- if TRUE, copy the contents of the vector
96 **			(which must be a string) also.
97 **
98 **	Returns:
99 **		a copy of 'list'.
100 **
101 **	Side Effects:
102 **		none.
103 */
104 
105 char **
106 copyplist(list, copycont)
107 	char **list;
108 	bool copycont;
109 {
110 	register char **vp;
111 	register char **newvp;
112 
113 	for (vp = list; *vp != NULL; vp++)
114 		continue;
115 
116 	vp++;
117 
118 	newvp = (char **) xalloc((int) (vp - list) * sizeof *vp);
119 	bcopy((char *) list, (char *) newvp, (int) (vp - list) * sizeof *vp);
120 
121 	if (copycont)
122 	{
123 		for (vp = newvp; *vp != NULL; vp++)
124 			*vp = newstr(*vp);
125 	}
126 
127 	return (newvp);
128 }
129 /*
130 **  COPYQUEUE -- copy address queue.
131 **
132 **	This routine is the equivalent of newstr for address queues
133 **	addresses marked with QDONTSEND aren't copied
134 **
135 **	Parameters:
136 **		addr -- list of address structures to copy.
137 **
138 **	Returns:
139 **		a copy of 'addr'.
140 **
141 **	Side Effects:
142 **		none.
143 */
144 
145 ADDRESS *
146 copyqueue(addr)
147 	ADDRESS *addr;
148 {
149 	register ADDRESS *newaddr;
150 	ADDRESS *ret;
151 	register ADDRESS **tail = &ret;
152 
153 	while (addr != NULL)
154 	{
155 		if (!bitset(QDONTSEND, addr->q_flags))
156 		{
157 			newaddr = (ADDRESS *) xalloc(sizeof(ADDRESS));
158 			STRUCTCOPY(*addr, *newaddr);
159 			*tail = newaddr;
160 			tail = &newaddr->q_next;
161 		}
162 		addr = addr->q_next;
163 	}
164 	*tail = NULL;
165 
166 	return ret;
167 }
168 /*
169 **  PRINTAV -- print argument vector.
170 **
171 **	Parameters:
172 **		av -- argument vector.
173 **
174 **	Returns:
175 **		none.
176 **
177 **	Side Effects:
178 **		prints av.
179 */
180 
181 printav(av)
182 	register char **av;
183 {
184 	while (*av != NULL)
185 	{
186 		if (tTd(0, 44))
187 			printf("\n\t%08x=", *av);
188 		else
189 			(void) putchar(' ');
190 		xputs(*av++);
191 	}
192 	(void) putchar('\n');
193 }
194 /*
195 **  LOWER -- turn letter into lower case.
196 **
197 **	Parameters:
198 **		c -- character to turn into lower case.
199 **
200 **	Returns:
201 **		c, in lower case.
202 **
203 **	Side Effects:
204 **		none.
205 */
206 
207 char
208 lower(c)
209 	register char c;
210 {
211 	return((isascii(c) && isupper(c)) ? tolower(c) : c);
212 }
213 /*
214 **  XPUTS -- put string doing control escapes.
215 **
216 **	Parameters:
217 **		s -- string to put.
218 **
219 **	Returns:
220 **		none.
221 **
222 **	Side Effects:
223 **		output to stdout
224 */
225 
226 xputs(s)
227 	register char *s;
228 {
229 	register int c;
230 	register struct metamac *mp;
231 	extern struct metamac MetaMacros[];
232 
233 	if (s == NULL)
234 	{
235 		printf("<null>");
236 		return;
237 	}
238 	while ((c = (*s++ & 0377)) != '\0')
239 	{
240 		if (!isascii(c))
241 		{
242 			if (c == MATCHREPL || c == MACROEXPAND)
243 			{
244 				putchar('$');
245 				continue;
246 			}
247 			for (mp = MetaMacros; mp->metaname != '\0'; mp++)
248 			{
249 				if ((mp->metaval & 0377) == c)
250 				{
251 					printf("$%c", mp->metaname);
252 					break;
253 				}
254 			}
255 			if (mp->metaname != '\0')
256 				continue;
257 			(void) putchar('\\');
258 			c &= 0177;
259 		}
260 		if (isprint(c))
261 		{
262 			putchar(c);
263 			continue;
264 		}
265 
266 		/* wasn't a meta-macro -- find another way to print it */
267 		switch (c)
268 		{
269 		  case '\0':
270 			continue;
271 
272 		  case '\n':
273 			c = 'n';
274 			break;
275 
276 		  case '\r':
277 			c = 'r';
278 			break;
279 
280 		  case '\t':
281 			c = 't';
282 			break;
283 
284 		  default:
285 			(void) putchar('^');
286 			(void) putchar(c ^ 0100);
287 			continue;
288 		}
289 	}
290 	(void) fflush(stdout);
291 }
292 /*
293 **  MAKELOWER -- Translate a line into lower case
294 **
295 **	Parameters:
296 **		p -- the string to translate.  If NULL, return is
297 **			immediate.
298 **
299 **	Returns:
300 **		none.
301 **
302 **	Side Effects:
303 **		String pointed to by p is translated to lower case.
304 **
305 **	Called By:
306 **		parse
307 */
308 
309 makelower(p)
310 	register char *p;
311 {
312 	register char c;
313 
314 	if (p == NULL)
315 		return;
316 	for (; (c = *p) != '\0'; p++)
317 		if (isascii(c) && isupper(c))
318 			*p = tolower(c);
319 }
320 /*
321 **  BUILDFNAME -- build full name from gecos style entry.
322 **
323 **	This routine interprets the strange entry that would appear
324 **	in the GECOS field of the password file.
325 **
326 **	Parameters:
327 **		p -- name to build.
328 **		login -- the login name of this user (for &).
329 **		buf -- place to put the result.
330 **
331 **	Returns:
332 **		none.
333 **
334 **	Side Effects:
335 **		none.
336 */
337 
338 buildfname(gecos, login, buf)
339 	register char *gecos;
340 	char *login;
341 	char *buf;
342 {
343 	register char *p;
344 	register char *bp = buf;
345 	int l;
346 
347 	if (*gecos == '*')
348 		gecos++;
349 
350 	/* find length of final string */
351 	l = 0;
352 	for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
353 	{
354 		if (*p == '&')
355 			l += strlen(login);
356 		else
357 			l++;
358 	}
359 
360 	/* now fill in buf */
361 	for (p = gecos; *p != '\0' && *p != ',' && *p != ';' && *p != '%'; p++)
362 	{
363 		if (*p == '&')
364 		{
365 			(void) strcpy(bp, login);
366 			*bp = toupper(*bp);
367 			while (*bp != '\0')
368 				bp++;
369 		}
370 		else
371 			*bp++ = *p;
372 	}
373 	*bp = '\0';
374 }
375 /*
376 **  SAFEFILE -- return true if a file exists and is safe for a user.
377 **
378 **	Parameters:
379 **		fn -- filename to check.
380 **		uid -- uid to compare against.
381 **		mustown -- to be safe, this uid must own the file.
382 **		mode -- mode bits that must match.
383 **
384 **	Returns:
385 **		0 if fn exists, is owned by uid, and matches mode.
386 **		An errno otherwise.  The actual errno is cleared.
387 **
388 **	Side Effects:
389 **		none.
390 */
391 
392 #ifndef S_IXOTH
393 # define S_IXOTH	(S_IEXEC >> 6)
394 #endif
395 
396 #ifndef S_IXUSR
397 # define S_IXUSR	(S_IEXEC)
398 #endif
399 
400 int
401 safefile(fn, uid, mustown, mode)
402 	char *fn;
403 	uid_t uid;
404 	bool mustown;
405 	int mode;
406 {
407 	register char *p;
408 	struct stat stbuf;
409 
410 	if (tTd(54, 4))
411 		printf("safefile(%s, %d, %d, %o): ", fn, uid, mustown, mode);
412 	errno = 0;
413 
414 	for (p = fn; (p = strchr(++p, '/')) != NULL; *p = '/')
415 	{
416 		*p = '\0';
417 		if (stat(fn, &stbuf) < 0 ||
418 		    !bitset(stbuf.st_uid == uid ? S_IXUSR : S_IXOTH,
419 			    stbuf.st_mode))
420 		{
421 			int ret = errno;
422 
423 			if (ret == 0)
424 				ret = EACCES;
425 			if (tTd(54, 4))
426 				printf("[dir %s] %s\n", fn, errstring(ret));
427 			*p = '/';
428 			return ret;
429 		}
430 	}
431 
432 	if (stat(fn, &stbuf) < 0)
433 	{
434 		int ret = errno;
435 
436 		if (tTd(54, 4))
437 			printf("%s\n", errstring(ret));
438 
439 		errno = 0;
440 		return ret;
441 	}
442 	if (stbuf.st_uid != uid || uid == 0 || !mustown)
443 		mode >>= 6;
444 	if (tTd(54, 4))
445 		printf("[uid %d, stat %o] ", stbuf.st_uid, stbuf.st_mode);
446 	if ((stbuf.st_uid == uid || uid == 0 || !mustown) &&
447 	    (stbuf.st_mode & mode) == mode)
448 	{
449 		if (tTd(54, 4))
450 			printf("OK\n");
451 		return 0;
452 	}
453 	if (tTd(54, 4))
454 		printf("EACCES\n");
455 	return EACCES;
456 }
457 /*
458 **  FIXCRLF -- fix <CR><LF> in line.
459 **
460 **	Looks for the <CR><LF> combination and turns it into the
461 **	UNIX canonical <NL> character.  It only takes one line,
462 **	i.e., it is assumed that the first <NL> found is the end
463 **	of the line.
464 **
465 **	Parameters:
466 **		line -- the line to fix.
467 **		stripnl -- if true, strip the newline also.
468 **
469 **	Returns:
470 **		none.
471 **
472 **	Side Effects:
473 **		line is changed in place.
474 */
475 
476 fixcrlf(line, stripnl)
477 	char *line;
478 	bool stripnl;
479 {
480 	register char *p;
481 
482 	p = strchr(line, '\n');
483 	if (p == NULL)
484 		return;
485 	if (p > line && p[-1] == '\r')
486 		p--;
487 	if (!stripnl)
488 		*p++ = '\n';
489 	*p = '\0';
490 }
491 /*
492 **  DFOPEN -- determined file open
493 **
494 **	This routine has the semantics of fopen, except that it will
495 **	keep trying a few times to make this happen.  The idea is that
496 **	on very loaded systems, we may run out of resources (inodes,
497 **	whatever), so this tries to get around it.
498 */
499 
500 #ifndef O_ACCMODE
501 # define O_ACCMODE	(O_RDONLY|O_WRONLY|O_RDWR)
502 #endif
503 
504 struct omodes
505 {
506 	int	mask;
507 	int	mode;
508 	char	*farg;
509 } OpenModes[] =
510 {
511 	O_ACCMODE,		O_RDONLY,		"r",
512 	O_ACCMODE|O_APPEND,	O_WRONLY,		"w",
513 	O_ACCMODE|O_APPEND,	O_WRONLY|O_APPEND,	"a",
514 	O_TRUNC,		0,			"w+",
515 	O_APPEND,		O_APPEND,		"a+",
516 	0,			0,			"r+",
517 };
518 
519 FILE *
520 dfopen(filename, omode, cmode)
521 	char *filename;
522 	int omode;
523 	int cmode;
524 {
525 	register int tries;
526 	int fd;
527 	register struct omodes *om;
528 	struct stat st;
529 
530 	for (om = OpenModes; om->mask != 0; om++)
531 		if ((omode & om->mask) == om->mode)
532 			break;
533 
534 	for (tries = 0; tries < 10; tries++)
535 	{
536 		sleep((unsigned) (10 * tries));
537 		errno = 0;
538 		fd = open(filename, omode, cmode);
539 		if (fd >= 0)
540 			break;
541 		if (errno != ENFILE && errno != EINTR)
542 			break;
543 	}
544 	if (fd >= 0 && fstat(fd, &st) >= 0 && S_ISREG(st.st_mode))
545 	{
546 		int locktype;
547 
548 		/* lock the file to avoid accidental conflicts */
549 		if ((omode & O_ACCMODE) != O_RDONLY)
550 			locktype = LOCK_EX;
551 		else
552 			locktype = LOCK_SH;
553 		(void) lockfile(fd, filename, locktype);
554 		errno = 0;
555 	}
556 	if (fd < 0)
557 		return NULL;
558 	else
559 		return fdopen(fd, om->farg);
560 }
561 /*
562 **  PUTLINE -- put a line like fputs obeying SMTP conventions
563 **
564 **	This routine always guarantees outputing a newline (or CRLF,
565 **	as appropriate) at the end of the string.
566 **
567 **	Parameters:
568 **		l -- line to put.
569 **		fp -- file to put it onto.
570 **		m -- the mailer used to control output.
571 **
572 **	Returns:
573 **		none
574 **
575 **	Side Effects:
576 **		output of l to fp.
577 */
578 
579 putline(l, fp, m)
580 	register char *l;
581 	FILE *fp;
582 	MAILER *m;
583 {
584 	register char *p;
585 	register char svchar;
586 
587 	/* strip out 0200 bits -- these can look like TELNET protocol */
588 	if (bitnset(M_7BITS, m->m_flags))
589 	{
590 		for (p = l; (svchar = *p) != '\0'; ++p)
591 			if (bitset(0200, svchar))
592 				*p = svchar &~ 0200;
593 	}
594 
595 	do
596 	{
597 		/* find the end of the line */
598 		p = strchr(l, '\n');
599 		if (p == NULL)
600 			p = &l[strlen(l)];
601 
602 		if (TrafficLogFile != NULL)
603 			fprintf(TrafficLogFile, "%05d >>> ", getpid());
604 
605 		/* check for line overflow */
606 		while (m->m_linelimit > 0 && (p - l) > m->m_linelimit)
607 		{
608 			register char *q = &l[m->m_linelimit - 1];
609 
610 			svchar = *q;
611 			*q = '\0';
612 			if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
613 			{
614 				(void) putc('.', fp);
615 				if (TrafficLogFile != NULL)
616 					(void) putc('.', TrafficLogFile);
617 			}
618 			fputs(l, fp);
619 			(void) putc('!', fp);
620 			fputs(m->m_eol, fp);
621 			if (TrafficLogFile != NULL)
622 				fprintf(TrafficLogFile, "%s!\n%05d >>> ",
623 					l, getpid());
624 			*q = svchar;
625 			l = q;
626 		}
627 
628 		/* output last part */
629 		if (l[0] == '.' && bitnset(M_XDOT, m->m_flags))
630 		{
631 			(void) putc('.', fp);
632 			if (TrafficLogFile != NULL)
633 				(void) putc('.', TrafficLogFile);
634 		}
635 		if (TrafficLogFile != NULL)
636 			fprintf(TrafficLogFile, "%.*s\n", p - l, l);
637 		for ( ; l < p; ++l)
638 			(void) putc(*l, fp);
639 		fputs(m->m_eol, fp);
640 		if (*l == '\n')
641 			++l;
642 	} while (l[0] != '\0');
643 }
644 /*
645 **  XUNLINK -- unlink a file, doing logging as appropriate.
646 **
647 **	Parameters:
648 **		f -- name of file to unlink.
649 **
650 **	Returns:
651 **		none.
652 **
653 **	Side Effects:
654 **		f is unlinked.
655 */
656 
657 xunlink(f)
658 	char *f;
659 {
660 	register int i;
661 
662 # ifdef LOG
663 	if (LogLevel > 98)
664 		syslog(LOG_DEBUG, "%s: unlink %s", CurEnv->e_id, f);
665 # endif /* LOG */
666 
667 	i = unlink(f);
668 # ifdef LOG
669 	if (i < 0 && LogLevel > 97)
670 		syslog(LOG_DEBUG, "%s: unlink-fail %d", f, errno);
671 # endif /* LOG */
672 }
673 /*
674 **  XFCLOSE -- close a file, doing logging as appropriate.
675 **
676 **	Parameters:
677 **		fp -- file pointer for the file to close
678 **		a, b -- miscellaneous crud to print for debugging
679 **
680 **	Returns:
681 **		none.
682 **
683 **	Side Effects:
684 **		fp is closed.
685 */
686 
687 xfclose(fp, a, b)
688 	FILE *fp;
689 	char *a, *b;
690 {
691 	if (tTd(53, 99))
692 		printf("xfclose(%x) %s %s\n", fp, a, b);
693 	if (fclose(fp) < 0 && tTd(53, 99))
694 		printf("xfclose FAILURE: %s\n", errstring(errno));
695 }
696 /*
697 **  SFGETS -- "safe" fgets -- times out and ignores random interrupts.
698 **
699 **	Parameters:
700 **		buf -- place to put the input line.
701 **		siz -- size of buf.
702 **		fp -- file to read from.
703 **		timeout -- the timeout before error occurs.
704 **		during -- what we are trying to read (for error messages).
705 **
706 **	Returns:
707 **		NULL on error (including timeout).  This will also leave
708 **			buf containing a null string.
709 **		buf otherwise.
710 **
711 **	Side Effects:
712 **		none.
713 */
714 
715 static jmp_buf	CtxReadTimeout;
716 static int	readtimeout();
717 
718 char *
719 sfgets(buf, siz, fp, timeout, during)
720 	char *buf;
721 	int siz;
722 	FILE *fp;
723 	time_t timeout;
724 	char *during;
725 {
726 	register EVENT *ev = NULL;
727 	register char *p;
728 
729 	/* set the timeout */
730 	if (timeout != 0)
731 	{
732 		if (setjmp(CtxReadTimeout) != 0)
733 		{
734 # ifdef LOG
735 			syslog(LOG_NOTICE,
736 			    "timeout waiting for input from %s during %s\n",
737 			    CurHostName? CurHostName: "local", during);
738 # endif
739 			errno = 0;
740 			usrerr("451 timeout waiting for input during %s",
741 				during);
742 			buf[0] = '\0';
743 #ifdef XDEBUG
744 			checkfd012(during);
745 #endif
746 			return (NULL);
747 		}
748 		ev = setevent(timeout, readtimeout, 0);
749 	}
750 
751 	/* try to read */
752 	p = NULL;
753 	while (p == NULL && !feof(fp) && !ferror(fp))
754 	{
755 		errno = 0;
756 		p = fgets(buf, siz, fp);
757 		if (errno == EINTR)
758 			clearerr(fp);
759 	}
760 
761 	/* clear the event if it has not sprung */
762 	clrevent(ev);
763 
764 	/* clean up the books and exit */
765 	LineNumber++;
766 	if (p == NULL)
767 	{
768 		buf[0] = '\0';
769 		if (TrafficLogFile != NULL)
770 			fprintf(TrafficLogFile, "%05d <<< [EOF]\n", getpid());
771 		return (NULL);
772 	}
773 	if (TrafficLogFile != NULL)
774 		fprintf(TrafficLogFile, "%05d <<< %s", getpid(), buf);
775 	if (SevenBit)
776 		for (p = buf; *p != '\0'; p++)
777 			*p &= ~0200;
778 	return (buf);
779 }
780 
781 static
782 readtimeout()
783 {
784 	longjmp(CtxReadTimeout, 1);
785 }
786 /*
787 **  FGETFOLDED -- like fgets, but know about folded lines.
788 **
789 **	Parameters:
790 **		buf -- place to put result.
791 **		n -- bytes available.
792 **		f -- file to read from.
793 **
794 **	Returns:
795 **		input line(s) on success, NULL on error or EOF.
796 **		This will normally be buf -- unless the line is too
797 **			long, when it will be xalloc()ed.
798 **
799 **	Side Effects:
800 **		buf gets lines from f, with continuation lines (lines
801 **		with leading white space) appended.  CRLF's are mapped
802 **		into single newlines.  Any trailing NL is stripped.
803 */
804 
805 char *
806 fgetfolded(buf, n, f)
807 	char *buf;
808 	register int n;
809 	FILE *f;
810 {
811 	register char *p = buf;
812 	char *bp = buf;
813 	register int i;
814 
815 	n--;
816 	while ((i = getc(f)) != EOF)
817 	{
818 		if (i == '\r')
819 		{
820 			i = getc(f);
821 			if (i != '\n')
822 			{
823 				if (i != EOF)
824 					(void) ungetc(i, f);
825 				i = '\r';
826 			}
827 		}
828 		if (--n <= 0)
829 		{
830 			/* allocate new space */
831 			char *nbp;
832 			int nn;
833 
834 			nn = (p - bp);
835 			if (nn < MEMCHUNKSIZE)
836 				nn *= 2;
837 			else
838 				nn += MEMCHUNKSIZE;
839 			nbp = xalloc(nn);
840 			bcopy(bp, nbp, p - bp);
841 			p = &nbp[p - bp];
842 			if (bp != buf)
843 				free(bp);
844 			bp = nbp;
845 			n = nn - (p - bp);
846 		}
847 		*p++ = i;
848 		if (i == '\n')
849 		{
850 			LineNumber++;
851 			i = getc(f);
852 			if (i != EOF)
853 				(void) ungetc(i, f);
854 			if (i != ' ' && i != '\t')
855 				break;
856 		}
857 	}
858 	if (p == bp)
859 		return (NULL);
860 	*--p = '\0';
861 	return (bp);
862 }
863 /*
864 **  CURTIME -- return current time.
865 **
866 **	Parameters:
867 **		none.
868 **
869 **	Returns:
870 **		the current time.
871 **
872 **	Side Effects:
873 **		none.
874 */
875 
876 time_t
877 curtime()
878 {
879 	auto time_t t;
880 
881 	(void) time(&t);
882 	return (t);
883 }
884 /*
885 **  ATOBOOL -- convert a string representation to boolean.
886 **
887 **	Defaults to "TRUE"
888 **
889 **	Parameters:
890 **		s -- string to convert.  Takes "tTyY" as true,
891 **			others as false.
892 **
893 **	Returns:
894 **		A boolean representation of the string.
895 **
896 **	Side Effects:
897 **		none.
898 */
899 
900 bool
901 atobool(s)
902 	register char *s;
903 {
904 	if (s == NULL || *s == '\0' || strchr("tTyY", *s) != NULL)
905 		return (TRUE);
906 	return (FALSE);
907 }
908 /*
909 **  ATOOCT -- convert a string representation to octal.
910 **
911 **	Parameters:
912 **		s -- string to convert.
913 **
914 **	Returns:
915 **		An integer representing the string interpreted as an
916 **		octal number.
917 **
918 **	Side Effects:
919 **		none.
920 */
921 
922 atooct(s)
923 	register char *s;
924 {
925 	register int i = 0;
926 
927 	while (*s >= '0' && *s <= '7')
928 		i = (i << 3) | (*s++ - '0');
929 	return (i);
930 }
931 /*
932 **  WAITFOR -- wait for a particular process id.
933 **
934 **	Parameters:
935 **		pid -- process id to wait for.
936 **
937 **	Returns:
938 **		status of pid.
939 **		-1 if pid never shows up.
940 **
941 **	Side Effects:
942 **		none.
943 */
944 
945 waitfor(pid)
946 	int pid;
947 {
948 	auto int st;
949 	int i;
950 
951 	do
952 	{
953 		errno = 0;
954 		i = wait(&st);
955 	} while ((i >= 0 || errno == EINTR) && i != pid);
956 	if (i < 0)
957 		st = -1;
958 	return (st);
959 }
960 /*
961 **  BITINTERSECT -- tell if two bitmaps intersect
962 **
963 **	Parameters:
964 **		a, b -- the bitmaps in question
965 **
966 **	Returns:
967 **		TRUE if they have a non-null intersection
968 **		FALSE otherwise
969 **
970 **	Side Effects:
971 **		none.
972 */
973 
974 bool
975 bitintersect(a, b)
976 	BITMAP a;
977 	BITMAP b;
978 {
979 	int i;
980 
981 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
982 		if ((a[i] & b[i]) != 0)
983 			return (TRUE);
984 	return (FALSE);
985 }
986 /*
987 **  BITZEROP -- tell if a bitmap is all zero
988 **
989 **	Parameters:
990 **		map -- the bit map to check
991 **
992 **	Returns:
993 **		TRUE if map is all zero.
994 **		FALSE if there are any bits set in map.
995 **
996 **	Side Effects:
997 **		none.
998 */
999 
1000 bool
1001 bitzerop(map)
1002 	BITMAP map;
1003 {
1004 	int i;
1005 
1006 	for (i = BITMAPBYTES / sizeof (int); --i >= 0; )
1007 		if (map[i] != 0)
1008 			return (FALSE);
1009 	return (TRUE);
1010 }
1011 /*
1012 **  STRCONTAINEDIN -- tell if one string is contained in another
1013 **
1014 **	Parameters:
1015 **		a -- possible substring.
1016 **		b -- possible superstring.
1017 **
1018 **	Returns:
1019 **		TRUE if a is contained in b.
1020 **		FALSE otherwise.
1021 */
1022 
1023 bool
1024 strcontainedin(a, b)
1025 	register char *a;
1026 	register char *b;
1027 {
1028 	int l;
1029 
1030 	l = strlen(a);
1031 	for (;;)
1032 	{
1033 		b = strchr(b, a[0]);
1034 		if (b == NULL)
1035 			return FALSE;
1036 		if (strncmp(a, b, l) == 0)
1037 			return TRUE;
1038 		b++;
1039 	}
1040 }
1041 /*
1042 **  CHECKFD012 -- check low numbered file descriptors
1043 **
1044 **	File descriptors 0, 1, and 2 should be open at all times.
1045 **	This routine verifies that, and fixes it if not true.
1046 **
1047 **	Parameters:
1048 **		where -- a tag printed if the assertion failed
1049 **
1050 **	Returns:
1051 **		none
1052 */
1053 
1054 checkfd012(where)
1055 	char *where;
1056 {
1057 #ifdef XDEBUG
1058 	register int i;
1059 	struct stat stbuf;
1060 
1061 	for (i = 0; i < 3; i++)
1062 	{
1063 		if (fstat(i, &stbuf) < 0)
1064 		{
1065 			/* oops.... */
1066 			int fd;
1067 
1068 			syserr("%s: fd %d not open", where, i);
1069 			fd = open("/dev/null", i == 0 ? O_RDONLY : O_WRONLY, 0666);
1070 			if (fd != i)
1071 			{
1072 				(void) dup2(fd, i);
1073 				(void) close(fd);
1074 			}
1075 		}
1076 	}
1077 #endif /* XDEBUG */
1078 }
1079