xref: /csrg-svn/usr.sbin/sendmail/src/util.c (revision 4059)
13151Seric # include <stdio.h>
2298Seric # include <sysexits.h>
3298Seric # include "useful.h"
42900Seric # include <ctype.h>
5298Seric 
6*4059Seric static char	SccsId[] = "@(#)util.c	3.5	08/08/81";
7409Seric 
8298Seric /*
9298Seric **  STRIPQUOTES -- Strip quotes & quote bits from a string.
10298Seric **
11298Seric **	Runs through a string and strips off unquoted quote
12298Seric **	characters and quote bits.  This is done in place.
13298Seric **
14298Seric **	Parameters:
15298Seric **		s -- the string to strip.
16298Seric **
17298Seric **	Returns:
18298Seric **		none.
19298Seric **
20298Seric **	Side Effects:
21298Seric **		none.
22298Seric **
23298Seric **	Called By:
24298Seric **		deliver
25298Seric */
26298Seric 
27298Seric stripquotes(s)
28298Seric 	char *s;
29298Seric {
30298Seric 	register char *p;
31298Seric 	register char *q;
32298Seric 	register char c;
33298Seric 
34298Seric 	for (p = q = s; (c = *p++) != '\0'; )
35298Seric 	{
36298Seric 		if (c != '"')
37298Seric 			*q++ = c & 0177;
38298Seric 	}
39298Seric 	*q = '\0';
40298Seric }
41298Seric /*
422900Seric **  CAPITALIZE -- return a copy of a string, properly capitalized.
432900Seric **
442900Seric **	Parameters:
452900Seric **		s -- the string to capitalize.
462900Seric **
472900Seric **	Returns:
482900Seric **		a pointer to a properly capitalized string.
492900Seric **
502900Seric **	Side Effects:
512900Seric **		none.
522900Seric */
532900Seric 
542900Seric char *
552900Seric capitalize(s)
562900Seric 	register char *s;
572900Seric {
582900Seric 	static char buf[50];
592900Seric 	register char *p;
602900Seric 
612900Seric 	p = buf;
622900Seric 
632900Seric 	for (;;)
642900Seric 	{
652900Seric 		while (!isalpha(*s) && *s != '\0')
662900Seric 			*p++ = *s++;
672900Seric 		if (*s == '\0')
682900Seric 			break;
692900Seric 		*p++ = toupper(*s++);
702900Seric 		while (isalpha(*s))
712900Seric 			*p++ = *s++;
722900Seric 	}
732900Seric 
742900Seric 	*p = '\0';
752900Seric 	return (buf);
762900Seric }
772900Seric /*
78298Seric **  XALLOC -- Allocate memory and bitch wildly on failure.
79298Seric **
80298Seric **	THIS IS A CLUDGE.  This should be made to give a proper
81298Seric **	error -- but after all, what can we do?
82298Seric **
83298Seric **	Parameters:
84298Seric **		sz -- size of area to allocate.
85298Seric **
86298Seric **	Returns:
87298Seric **		pointer to data region.
88298Seric **
89298Seric **	Side Effects:
90298Seric **		Memory is allocated.
91298Seric */
92298Seric 
93298Seric char *
94298Seric xalloc(sz)
95298Seric 	register unsigned int sz;
96298Seric {
97298Seric 	register char *p;
98298Seric 	extern char *malloc();
99298Seric 
100298Seric 	p = malloc(sz);
101298Seric 	if (p == NULL)
102298Seric 	{
103298Seric 		syserr("Out of memory!!");
1041598Seric 		exit(EX_UNAVAILABLE);
105298Seric 	}
106298Seric 	return (p);
107298Seric }
108298Seric /*
1092900Seric **  NEWSTR -- make copy of string.
1102900Seric **
1112900Seric **	Space is allocated for it using xalloc.
1122900Seric **
1132900Seric **	Parameters:
1142900Seric **		string to copy.
1152900Seric **
1162900Seric **	Returns:
1172900Seric **		pointer to new string.
1182900Seric **
1192900Seric **	Side Effects:
1202900Seric **		none.
1212900Seric */
1222900Seric 
1232900Seric char *
1242900Seric newstr(s)
1252900Seric 	register char *s;
1262900Seric {
1272900Seric 	register char *p;
1282992Seric 	extern char *strcpy();
1292900Seric 
1302992Seric 	p = xalloc((unsigned) (strlen(s) + 1));
1312900Seric 	strcpy(p, s);
1322900Seric 	return (p);
1332900Seric }
1343151Seric /*
1353151Seric **  COPYPLIST -- copy list of pointers.
1363151Seric **
1373151Seric **	This routine is the equivalent of newstr for lists of
1383151Seric **	pointers.
1393151Seric **
1403151Seric **	Parameters:
1413151Seric **		list -- list of pointers to copy.
1423151Seric **			Must be NULL terminated.
1433151Seric **		copycont -- if TRUE, copy the contents of the vector
1443151Seric **			(which must be a string) also.
1453151Seric **
1463151Seric **	Returns:
1473151Seric **		a copy of 'list'.
1483151Seric **
1493151Seric **	Side Effects:
1503151Seric **		none.
1513151Seric */
1523151Seric 
1533151Seric char **
1543151Seric copyplist(list, copycont)
1553151Seric 	char **list;
1563151Seric 	bool copycont;
1573151Seric {
1583151Seric 	register char **vp;
1593151Seric 	register char **newvp;
1603151Seric 	extern char *xalloc();
1613151Seric 
1623151Seric 	for (vp = list; *vp != NULL; vp++)
1633151Seric 		continue;
1643151Seric 
1653151Seric 	vp++;
1663151Seric 
1673151Seric 	newvp = (char **) xalloc((vp - list) * sizeof *vp);
1683151Seric 	bmove(list, newvp, (vp - list) * sizeof *vp);
1693151Seric 
1703151Seric 	if (copycont)
1713151Seric 	{
1723151Seric 		for (vp = newvp; *vp != NULL; vp++)
1733151Seric 			*vp = newstr(*vp);
1743151Seric 	}
1753151Seric 
1763151Seric 	return (newvp);
1773151Seric }
1783151Seric /*
1793151Seric **  PRINTAV -- print argument vector.
1803151Seric **
1813151Seric **	Parameters:
1823151Seric **		av -- argument vector.
1833151Seric **
1843151Seric **	Returns:
1853151Seric **		none.
1863151Seric **
1873151Seric **	Side Effects:
1883151Seric **		prints av.
1893151Seric */
1903151Seric 
1913151Seric # ifdef DEBUG
1923151Seric printav(av)
1933151Seric 	register char **av;
1943151Seric {
1953151Seric 	while (*av != NULL)
1963151Seric 	{
1973151Seric 		printf("\t%08x=", *av);
1983151Seric 		xputs(*av++);
1993151Seric 		putchar('\n');
2003151Seric 	}
2013151Seric }
2023151Seric # endif DEBUG
2033151Seric /*
2043151Seric **  LOWER -- turn letter into lower case.
2053151Seric **
2063151Seric **	Parameters:
2073151Seric **		c -- character to turn into lower case.
2083151Seric **
2093151Seric **	Returns:
2103151Seric **		c, in lower case.
2113151Seric **
2123151Seric **	Side Effects:
2133151Seric **		none.
2143151Seric */
2153151Seric 
2163151Seric char
2173151Seric lower(c)
2183151Seric 	register char c;
2193151Seric {
2203151Seric 	if (isascii(c) && isupper(c))
2213151Seric 		c = c - 'A' + 'a';
2223151Seric 	return (c);
2233151Seric }
2243151Seric /*
2253151Seric **  XPUTS -- put string doing control escapes.
2263151Seric **
2273151Seric **	Parameters:
2283151Seric **		s -- string to put.
2293151Seric **
2303151Seric **	Returns:
2313151Seric **		none.
2323151Seric **
2333151Seric **	Side Effects:
2343151Seric **		output to stdout
2353151Seric */
2363151Seric 
2373151Seric # ifdef DEBUG
2383151Seric xputs(s)
2393151Seric 	register char *s;
2403151Seric {
2413151Seric 	register char c;
2423151Seric 
2433151Seric 	while ((c = *s++) != '\0')
2443151Seric 	{
2453151Seric 		if (!isascii(c))
2463151Seric 		{
2473151Seric 			putchar('\\');
2483151Seric 			c &= 0177;
2493151Seric 		}
2503151Seric 		if (iscntrl(c))
2513151Seric 		{
2523151Seric 			putchar('^');
2533151Seric 			c |= 0100;
2543151Seric 		}
2553151Seric 		putchar(c);
2563151Seric 	}
2573151Seric 	fflush(stdout);
2583151Seric }
2593151Seric # endif DEBUG
2603151Seric /*
2613151Seric **  MAKELOWER -- Translate a line into lower case
2623151Seric **
2633151Seric **	Parameters:
2643151Seric **		p -- the string to translate.  If NULL, return is
2653151Seric **			immediate.
2663151Seric **
2673151Seric **	Returns:
2683151Seric **		none.
2693151Seric **
2703151Seric **	Side Effects:
2713151Seric **		String pointed to by p is translated to lower case.
2723151Seric **
2733151Seric **	Called By:
2743151Seric **		parse
2753151Seric */
2763151Seric 
2773151Seric makelower(p)
2783151Seric 	register char *p;
2793151Seric {
2803151Seric 	register char c;
2813151Seric 
2823151Seric 	if (p == NULL)
2833151Seric 		return;
2843151Seric 	for (; (c = *p) != '\0'; p++)
2853151Seric 		if (isascii(c) && isupper(c))
2863151Seric 			*p = c - 'A' + 'a';
2873151Seric }
288*4059Seric /*
289*4059Seric **  SAMEWORD -- return TRUE if the words are the same
290*4059Seric **
291*4059Seric **	Ignores case.
292*4059Seric **
293*4059Seric **	Parameters:
294*4059Seric **		a, b -- the words to compare.
295*4059Seric **
296*4059Seric **	Returns:
297*4059Seric **		TRUE if a & b match exactly (modulo case)
298*4059Seric **		FALSE otherwise.
299*4059Seric **
300*4059Seric **	Side Effects:
301*4059Seric **		none.
302*4059Seric */
303*4059Seric 
304*4059Seric bool
305*4059Seric sameword(a, b)
306*4059Seric 	register char *a, *b;
307*4059Seric {
308*4059Seric 	while (lower(*a) == lower(*b))
309*4059Seric 	{
310*4059Seric 		if (*a == '\0')
311*4059Seric 			return (TRUE);
312*4059Seric 		a++;
313*4059Seric 		b++;
314*4059Seric 	}
315*4059Seric 	return (FALSE);
316*4059Seric }
317